Skip to content

Commit 4dd21c3

Browse files
committed
compare autodiscover nodepools with previous cycle and log the updates
1 parent 14652be commit 4dd21c3

File tree

2 files changed

+104
-5
lines changed

2 files changed

+104
-5
lines changed

cluster-autoscaler/cloudprovider/oci/nodepools/oci_manager.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,11 +412,35 @@ func (m *ociManagerImpl) TaintToPreventFurtherSchedulingOnRestart(nodes []*apiv1
412412
func (m *ociManagerImpl) forceRefresh() error {
413413
// auto discover node groups
414414
if m.nodeGroups != nil {
415-
// empty previous nodepool map to do an auto discovery
415+
// create a copy of m.staticNodePools to use it in comparison
416+
staticNodePoolsCopy := make(map[string]NodePool)
417+
for k, v := range m.staticNodePools {
418+
staticNodePoolsCopy[k] = v
419+
}
420+
421+
// empty previous nodepool map to do a fresh auto discovery
416422
m.staticNodePools = make(map[string]NodePool)
423+
424+
// run auto-discovery
417425
for _, nodeGroup := range m.nodeGroups {
418426
autoDiscoverNodeGroups(m, m.okeClient, nodeGroup)
419427
}
428+
429+
// compare the new and previous nodepool list to log the updates
430+
for nodepoolId, nodepool := range m.staticNodePools {
431+
if _, ok := staticNodePoolsCopy[nodepoolId]; !ok {
432+
klog.Infof("New nodepool discovered. [id: %s ,minSize: %d, maxSize:%d]", nodepool.Id(), nodepool.MinSize(), nodepool.MaxSize())
433+
} else if staticNodePoolsCopy[nodepoolId].MinSize() != nodepool.MinSize() || staticNodePoolsCopy[nodepoolId].MaxSize() != nodepool.MaxSize() {
434+
klog.Infof("Nodepool min/max sizes are updated. [id: %s ,minSize: %d, maxSize:%d]", nodepool.Id(), nodepool.MinSize(), nodepool.MaxSize())
435+
}
436+
}
437+
438+
// log if there are nodepools removed from the list
439+
for k := range staticNodePoolsCopy {
440+
if _, ok := m.staticNodePools[k]; !ok {
441+
klog.Infof("Previously auto-discovered nodepool removed from the managed nodepool list. nodepoolid: %s", k)
442+
}
443+
}
420444
}
421445
// rebuild nodepool cache
422446
err := m.nodePoolCache.rebuild(m.staticNodePools, maxGetNodepoolRetries)

cluster-autoscaler/cloudprovider/oci/nodepools/oci_manager_test.go

Lines changed: 79 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package nodepools
66

77
import (
88
"context"
9+
"fmt"
910
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider/oci/nodepools/consts"
1011
"net/http"
1112
"reflect"
@@ -20,6 +21,10 @@ import (
2021
oke "k8s.io/autoscaler/cluster-autoscaler/cloudprovider/oci/vendor-internal/github.com/oracle/oci-go-sdk/v65/containerengine"
2122
)
2223

24+
const (
25+
autoDiscoveryCompartment = "ocid1.compartment.oc1.test-region.test"
26+
)
27+
2328
func TestNodePoolFromArgs(t *testing.T) {
2429
value := `1:5:ocid`
2530
nodePool, err := nodePoolFromArg(value)
@@ -321,8 +326,15 @@ func TestBuildGenericLabels(t *testing.T) {
321326

322327
type mockOKEClient struct{}
323328

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
326338
}
327339
func (c mockOKEClient) UpdateNodePool(context.Context, oke.UpdateNodePoolRequest) (oke.UpdateNodePoolResponse, error) {
328340
return oke.UpdateNodePoolResponse{}, nil
@@ -336,7 +348,39 @@ func (c mockOKEClient) DeleteNode(context.Context, oke.DeleteNodeRequest) (oke.D
336348
}, nil
337349
}
338350

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+
340384
return oke.ListNodePoolsResponse{}, nil
341385
}
342386

@@ -393,8 +437,39 @@ func TestRemoveInstance(t *testing.T) {
393437
}
394438
}
395439

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+
396471
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)
398473
nodeGroupAutoDiscovery, err := nodeGroupFromArg(nodeGroupArg)
399474
if err != nil {
400475
t.Errorf("Error: #{err}")

0 commit comments

Comments
 (0)