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