@@ -6,6 +6,7 @@ package nodepools
6
6
7
7
import (
8
8
"context"
9
+ "fmt"
9
10
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider/oci/nodepools/consts"
10
11
"net/http"
11
12
"reflect"
@@ -20,6 +21,10 @@ import (
20
21
oke "k8s.io/autoscaler/cluster-autoscaler/cloudprovider/oci/vendor-internal/github.com/oracle/oci-go-sdk/v65/containerengine"
21
22
)
22
23
24
+ const (
25
+ autoDiscoveryCompartment = "ocid1.compartment.oc1.test-region.test"
26
+ )
27
+
23
28
func TestNodePoolFromArgs (t * testing.T ) {
24
29
value := `1:5:ocid`
25
30
nodePool , err := nodePoolFromArg (value )
@@ -321,8 +326,15 @@ func TestBuildGenericLabels(t *testing.T) {
321
326
322
327
type mockOKEClient struct {}
323
328
324
- func (c mockOKEClient ) GetNodePool (context.Context , oke.GetNodePoolRequest ) (oke.GetNodePoolResponse , error ) {
325
- return oke.GetNodePoolResponse {}, nil
329
+ func (c mockOKEClient ) GetNodePool (ctx context.Context , req oke.GetNodePoolRequest ) (oke.GetNodePoolResponse , error ) {
330
+ return oke.GetNodePoolResponse {
331
+ NodePool : oke.NodePool {
332
+ Id : req .NodePoolId ,
333
+ NodeConfigDetails : & oke.NodePoolNodeConfigDetails {
334
+ Size : common .Int (1 ),
335
+ },
336
+ },
337
+ }, nil
326
338
}
327
339
func (c mockOKEClient ) UpdateNodePool (context.Context , oke.UpdateNodePoolRequest ) (oke.UpdateNodePoolResponse , error ) {
328
340
return oke.UpdateNodePoolResponse {}, nil
@@ -336,7 +348,39 @@ func (c mockOKEClient) DeleteNode(context.Context, oke.DeleteNodeRequest) (oke.D
336
348
}, nil
337
349
}
338
350
339
- func (c mockOKEClient ) ListNodePools (context.Context , oke.ListNodePoolsRequest ) (oke.ListNodePoolsResponse , error ) {
351
+ func (c mockOKEClient ) ListNodePools (ctx context.Context , req oke.ListNodePoolsRequest ) (oke.ListNodePoolsResponse , error ) {
352
+ // below test data added for auto-discovery tests
353
+ if req .CompartmentId != nil && * req .CompartmentId == autoDiscoveryCompartment {
354
+ freeformTags1 := map [string ]string {
355
+ "ca-managed" : "true" ,
356
+ }
357
+ freeformTags2 := map [string ]string {
358
+ "ca-managed" : "true" ,
359
+ "minSize" : "4" ,
360
+ "maxSize" : "10" ,
361
+ }
362
+ definedTags := map [string ]map [string ]interface {}{
363
+ "namespace" : {
364
+ "foo" : "bar" ,
365
+ },
366
+ }
367
+ resp := oke.ListNodePoolsResponse {
368
+ Items : []oke.NodePoolSummary {
369
+ {
370
+ Id : common .String ("node-pool-1" ),
371
+ FreeformTags : freeformTags1 ,
372
+ DefinedTags : definedTags ,
373
+ },
374
+ {
375
+ Id : common .String ("node-pool-2" ),
376
+ FreeformTags : freeformTags2 ,
377
+ DefinedTags : definedTags ,
378
+ },
379
+ },
380
+ }
381
+ return resp , nil
382
+ }
383
+
340
384
return oke.ListNodePoolsResponse {}, nil
341
385
}
342
386
@@ -393,8 +437,39 @@ func TestRemoveInstance(t *testing.T) {
393
437
}
394
438
}
395
439
440
+ func TestNodeGroupAutoDiscovery (t * testing.T ) {
441
+ var nodeGroupArg = fmt .Sprintf ("clusterId:ocid1.cluster.oc1.test-region.test,compartmentId:%s,nodepoolTags:ca-managed=true&namespace.foo=bar,min:1,max:5" , autoDiscoveryCompartment )
442
+ nodeGroup , err := nodeGroupFromArg (nodeGroupArg )
443
+ if err != nil {
444
+ t .Errorf ("Error: #{err}" )
445
+ }
446
+ nodePoolCache := newNodePoolCache (nil )
447
+ nodePoolCache .okeClient = mockOKEClient {}
448
+
449
+ cloudConfig , err := ocicommon .CreateCloudConfig ("" , common .DefaultConfigProvider (), "" )
450
+
451
+ manager := & ociManagerImpl {
452
+ nodePoolCache : nodePoolCache ,
453
+ nodeGroups : []nodeGroupAutoDiscovery {* nodeGroup },
454
+ okeClient : mockOKEClient {},
455
+ cfg : cloudConfig ,
456
+ staticNodePools : map [string ]NodePool {},
457
+ }
458
+ // test data to use as initial nodepools
459
+ nodepool2 := & nodePool {
460
+ id : "node-pool-2" , minSize : 1 , maxSize : 5 ,
461
+ }
462
+ manager .staticNodePools [nodepool2 .id ] = nodepool2
463
+ nodepool3 := & nodePool {
464
+ id : "node-pool-3" , minSize : 2 , maxSize : 5 ,
465
+ }
466
+ manager .staticNodePools [nodepool3 .id ] = nodepool3
467
+
468
+ manager .forceRefresh ()
469
+ }
470
+
396
471
func TestNodeGroupFromArg (t * testing.T ) {
397
- var nodeGroupArg = "clusterId:ocid1.cluster.oc1.test-region.test,compartmentId:ocid1.compartment.oc1.test-region.test ,nodepoolTags:ca-managed=true&namespace.foo=bar,min:1,max:5"
472
+ var nodeGroupArg = fmt . Sprintf ( "clusterId:ocid1.cluster.oc1.test-region.test,compartmentId:%s ,nodepoolTags:ca-managed=true&namespace.foo=bar,min:1,max:5" , autoDiscoveryCompartment )
398
473
nodeGroupAutoDiscovery , err := nodeGroupFromArg (nodeGroupArg )
399
474
if err != nil {
400
475
t .Errorf ("Error: #{err}" )
0 commit comments