Skip to content

Commit bf86702

Browse files
authored
Merge pull request #8491 from gvnc/oci-auto-discovery-enhancement
read min and max values from nodepool tags for oci autodiscovery
2 parents 54e1faf + e77e29c commit bf86702

File tree

2 files changed

+128
-8
lines changed

2 files changed

+128
-8
lines changed

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

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ const (
4040
nodepoolTags = "nodepoolTags"
4141
min = "min"
4242
max = "max"
43+
minSize = "minSize"
44+
maxSize = "maxSize"
4345
)
4446

4547
var (
@@ -90,6 +92,9 @@ func CreateNodePoolManager(cloudConfigPath string, nodeGroupAutoDiscoveryList []
9092
var err error
9193
var configProvider common.ConfigurationProvider
9294

95+
// enable SDK to look up the IMDS endpoint to figure out the right realmDomain
96+
common.EnableInstanceMetadataServiceLookup()
97+
9398
if os.Getenv(ipconsts.OciUseWorkloadIdentityEnvVar) == "true" {
9499
klog.Info("using workload identity provider")
95100
configProvider, err = auth.OkeWorkloadIdentityConfigurationProvider()
@@ -214,21 +219,34 @@ func autoDiscoverNodeGroups(m *ociManagerImpl, okeClient okeClient, nodeGroup no
214219
if validateNodepoolTags(nodeGroup.tags, nodePoolSummary.FreeformTags, nodePoolSummary.DefinedTags) {
215220
nodepool := &nodePool{}
216221
nodepool.id = *nodePoolSummary.Id
217-
nodepool.minSize = nodeGroup.minSize
218-
nodepool.maxSize = nodeGroup.maxSize
222+
// set minSize-maxSize from nodepool free form tags, or else use nodeGroupAutoDiscovery configuration
223+
nodepool.minSize = getIntFromMap(nodePoolSummary.FreeformTags, minSize, nodeGroup.minSize)
224+
nodepool.maxSize = getIntFromMap(nodePoolSummary.FreeformTags, maxSize, nodeGroup.maxSize)
219225

220226
nodepool.manager = nodeGroup.manager
221227
nodepool.kubeClient = nodeGroup.kubeClient
222228

223229
m.staticNodePools[nodepool.id] = nodepool
224-
klog.V(5).Infof("auto discovered nodepool in compartment : %s , nodepoolid: %s", nodeGroup.compartmentId, nodepool.id)
230+
klog.V(4).Infof("auto discovered nodepool in compartment : %s , nodepoolid: %s ,minSize: %d, maxSize:%d", nodeGroup.compartmentId, nodepool.id, nodepool.minSize, nodepool.maxSize)
225231
} else {
226232
klog.Warningf("nodepool ignored as the tags do not satisfy the requirement : %s , %v, %v", *nodePoolSummary.Id, nodePoolSummary.FreeformTags, nodePoolSummary.DefinedTags)
227233
}
228234
}
229235
return true, nil
230236
}
231237

238+
func getIntFromMap(m map[string]string, key string, defaultValue int) int {
239+
value, ok := m[key]
240+
if !ok {
241+
return defaultValue
242+
}
243+
i, err := strconv.Atoi(value)
244+
if err != nil {
245+
return defaultValue
246+
}
247+
return i
248+
}
249+
232250
func validateNodepoolTags(nodeGroupTags map[string]string, freeFormTags map[string]string, definedTags map[string]map[string]interface{}) bool {
233251
if nodeGroupTags != nil {
234252
for tagKey, tagValue := range nodeGroupTags {
@@ -394,11 +412,35 @@ func (m *ociManagerImpl) TaintToPreventFurtherSchedulingOnRestart(nodes []*apiv1
394412
func (m *ociManagerImpl) forceRefresh() error {
395413
// auto discover node groups
396414
if m.nodeGroups != nil {
397-
// 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
398422
m.staticNodePools = make(map[string]NodePool)
423+
424+
// run auto-discovery
399425
for _, nodeGroup := range m.nodeGroups {
400426
autoDiscoverNodeGroups(m, m.okeClient, nodeGroup)
401427
}
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+
}
402444
}
403445
// rebuild nodepool cache
404446
err := m.nodePoolCache.rebuild(m.staticNodePools, maxGetNodepoolRetries)

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

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

77
import (
88
"context"
9+
"fmt"
910
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider/oci/nodepools/consts"
1011
"net/http"
1112
"reflect"
1213
"testing"
14+
"time"
1315

1416
apiv1 "k8s.io/api/core/v1"
1517
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider"
@@ -20,6 +22,10 @@ import (
2022
oke "k8s.io/autoscaler/cluster-autoscaler/cloudprovider/oci/vendor-internal/github.com/oracle/oci-go-sdk/v65/containerengine"
2123
)
2224

25+
const (
26+
autoDiscoveryCompartment = "ocid1.compartment.oc1.test-region.test"
27+
)
28+
2329
func TestNodePoolFromArgs(t *testing.T) {
2430
value := `1:5:ocid`
2531
nodePool, err := nodePoolFromArg(value)
@@ -321,8 +327,15 @@ func TestBuildGenericLabels(t *testing.T) {
321327

322328
type mockOKEClient struct{}
323329

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
326339
}
327340
func (c mockOKEClient) UpdateNodePool(context.Context, oke.UpdateNodePoolRequest) (oke.UpdateNodePoolResponse, error) {
328341
return oke.UpdateNodePoolResponse{}, nil
@@ -336,7 +349,39 @@ func (c mockOKEClient) DeleteNode(context.Context, oke.DeleteNodeRequest) (oke.D
336349
}, nil
337350
}
338351

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+
340385
return oke.ListNodePoolsResponse{}, nil
341386
}
342387

@@ -393,8 +438,41 @@ func TestRemoveInstance(t *testing.T) {
393438
}
394439
}
395440

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+
396474
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)
398476
nodeGroupAutoDiscovery, err := nodeGroupFromArg(nodeGroupArg)
399477
if err != nil {
400478
t.Errorf("Error: #{err}")

0 commit comments

Comments
 (0)