Skip to content

Commit 06e5f6a

Browse files
committed
Update group identifier to use for Cluster API annotations
- Also add backwards compatibility for the previously used deprecated annotations
1 parent 150dbde commit 06e5f6a

File tree

8 files changed

+88
-31
lines changed

8 files changed

+88
-31
lines changed

cluster-autoscaler/cloudprovider/clusterapi/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,12 @@ resources depending on the type of cluster-api mechanism that you are using.
101101

102102
There are two annotations that control how a cluster resource should be scaled:
103103

104-
* `cluster.k8s.io/cluster-api-autoscaler-node-group-min-size` - This specifies
104+
* `cluster.x-k8s.io/cluster-api-autoscaler-node-group-min-size` - This specifies
105105
the minimum number of nodes for the associated resource group. The autoscaler
106106
will not scale the group below this number. Please note that currently the
107107
cluster-api provider will not scale down to zero nodes.
108108

109-
* `cluster.k8s.io/cluster-api-autoscaler-node-group-max-size` - This specifies
109+
* `cluster.x-k8s.io/cluster-api-autoscaler-node-group-max-size` - This specifies
110110
the maximum number of nodes for the associated resource group. The autoscaler
111111
will not scale the group above this number.
112112

cluster-autoscaler/cloudprovider/clusterapi/clusterapi_controller.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,12 @@ func (c *machineController) findMachineByProviderID(providerID normalizedProvide
258258
if node == nil {
259259
return nil, nil
260260
}
261-
return c.findMachine(node.Annotations[machineAnnotationKey])
261+
262+
machineID, ok := node.Annotations[machineAnnotationKey]
263+
if !ok {
264+
machineID = node.Annotations[deprecatedMachineAnnotationKey]
265+
}
266+
return c.findMachine(machineID)
262267
}
263268

264269
func isFailedMachineProviderID(providerID normalizedProviderID) bool {

cluster-autoscaler/cloudprovider/clusterapi/clusterapi_controller_test.go

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -514,24 +514,32 @@ func deleteTestConfigs(t *testing.T, controller *machineController, testConfigs
514514

515515
func TestControllerFindMachine(t *testing.T) {
516516
type testCase struct {
517-
description string
518-
name string
519-
namespace string
520-
lookupSucceeds bool
517+
description string
518+
name string
519+
namespace string
520+
useDeprecatedAnnotation bool
521+
lookupSucceeds bool
521522
}
522523

523524
var testCases = []testCase{{
524-
description: "lookup fails",
525-
lookupSucceeds: false,
526-
name: "machine-does-not-exist",
527-
namespace: "namespace-does-not-exist",
525+
description: "lookup fails",
526+
lookupSucceeds: false,
527+
useDeprecatedAnnotation: false,
528+
name: "machine-does-not-exist",
529+
namespace: "namespace-does-not-exist",
528530
}, {
529-
description: "lookup fails in valid namespace",
530-
lookupSucceeds: false,
531-
name: "machine-does-not-exist-in-existing-namespace",
531+
description: "lookup fails in valid namespace",
532+
lookupSucceeds: false,
533+
useDeprecatedAnnotation: false,
534+
name: "machine-does-not-exist-in-existing-namespace",
532535
}, {
533-
description: "lookup succeeds",
534-
lookupSucceeds: true,
536+
description: "lookup succeeds",
537+
lookupSucceeds: true,
538+
useDeprecatedAnnotation: false,
539+
}, {
540+
description: "lookup succeeds with deprecated annotation",
541+
lookupSucceeds: true,
542+
useDeprecatedAnnotation: true,
535543
}}
536544

537545
test := func(t *testing.T, tc testCase, testConfig *testConfig) {
@@ -569,6 +577,19 @@ func TestControllerFindMachine(t *testing.T) {
569577
if tc.namespace == "" {
570578
tc.namespace = testConfig.machines[0].GetNamespace()
571579
}
580+
if tc.useDeprecatedAnnotation {
581+
for i := range testConfig.machines {
582+
n := testConfig.nodes[i]
583+
annotations := n.GetAnnotations()
584+
val, ok := annotations[machineAnnotationKey]
585+
if !ok {
586+
t.Fatal("node did not contain machineAnnotationKey")
587+
}
588+
delete(annotations, machineAnnotationKey)
589+
annotations[deprecatedMachineAnnotationKey] = val
590+
n.SetAnnotations(annotations)
591+
}
592+
}
572593
test(t, tc, testConfig)
573594
})
574595
}

cluster-autoscaler/cloudprovider/clusterapi/clusterapi_nodegroup.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,13 @@ import (
2727
)
2828

2929
const (
30-
machineDeleteAnnotationKey = "cluster.k8s.io/delete-machine"
31-
machineAnnotationKey = "cluster.k8s.io/machine"
32-
debugFormat = "%s (min: %d, max: %d, replicas: %d)"
30+
// deprecatedMachineDeleteAnnotationKey should not be removed until minimum cluster-api support is v1alpha3
31+
deprecatedMachineDeleteAnnotationKey = "cluster.k8s.io/delete-machine"
32+
// TODO: determine what currently relies on deprecatedMachineAnnotationKey to determine when it can be removed
33+
deprecatedMachineAnnotationKey = "cluster.k8s.io/machine"
34+
machineDeleteAnnotationKey = "cluster.x-k8s.io/delete-machine"
35+
machineAnnotationKey = "cluster.x-k8s.io/machine"
36+
debugFormat = "%s (min: %d, max: %d, replicas: %d)"
3337
)
3438

3539
type nodegroup struct {

cluster-autoscaler/cloudprovider/clusterapi/clusterapi_nodegroup_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,9 @@ func TestNodeGroupDeleteNodes(t *testing.T) {
649649
if _, found := machine.GetAnnotations()[machineDeleteAnnotationKey]; !found {
650650
t.Errorf("expected annotation %q on machine %s", machineDeleteAnnotationKey, machine.GetName())
651651
}
652+
if _, found := machine.GetAnnotations()[deprecatedMachineDeleteAnnotationKey]; !found {
653+
t.Errorf("expected annotation %q on machine %s", deprecatedMachineDeleteAnnotationKey, machine.GetName())
654+
}
652655
}
653656

654657
gvr, err := ng.scalableResource.GroupVersionResource()

cluster-autoscaler/cloudprovider/clusterapi/clusterapi_unstructured.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,15 +129,12 @@ func (r unstructuredScalableResource) UnmarkMachineForDeletion(machine *unstruct
129129
}
130130

131131
annotations := u.GetAnnotations()
132-
if _, ok := annotations[machineDeleteAnnotationKey]; ok {
133-
delete(annotations, machineDeleteAnnotationKey)
134-
u.SetAnnotations(annotations)
135-
_, updateErr := r.controller.managementClient.Resource(r.controller.machineResource).Namespace(u.GetNamespace()).Update(context.TODO(), u, metav1.UpdateOptions{})
136-
137-
return updateErr
138-
}
132+
delete(annotations, machineDeleteAnnotationKey)
133+
delete(annotations, deprecatedMachineDeleteAnnotationKey)
134+
u.SetAnnotations(annotations)
135+
_, updateErr := r.controller.managementClient.Resource(r.controller.machineResource).Namespace(u.GetNamespace()).Update(context.TODO(), u, metav1.UpdateOptions{})
139136

140-
return nil
137+
return updateErr
141138
}
142139

143140
func (r unstructuredScalableResource) MarkMachineForDeletion(machine *unstructured.Unstructured) error {
@@ -154,6 +151,7 @@ func (r unstructuredScalableResource) MarkMachineForDeletion(machine *unstructur
154151
}
155152

156153
annotations[machineDeleteAnnotationKey] = time.Now().String()
154+
annotations[deprecatedMachineDeleteAnnotationKey] = time.Now().String()
157155
u.SetAnnotations(annotations)
158156

159157
_, updateErr := r.controller.managementClient.Resource(r.controller.machineResource).Namespace(u.GetNamespace()).Update(context.TODO(), u, metav1.UpdateOptions{})

cluster-autoscaler/cloudprovider/clusterapi/clusterapi_utils.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@ import (
2626
)
2727

2828
const (
29-
nodeGroupMinSizeAnnotationKey = "cluster.k8s.io/cluster-api-autoscaler-node-group-min-size"
30-
nodeGroupMaxSizeAnnotationKey = "cluster.k8s.io/cluster-api-autoscaler-node-group-max-size"
31-
clusterNameLabel = "cluster.x-k8s.io/cluster-name"
32-
deprecatedClusterNameLabel = "cluster.k8s.io/cluster-name"
29+
deprecatedNodeGroupMinSizeAnnotationKey = "cluster.k8s.io/cluster-api-autoscaler-node-group-min-size"
30+
deprecatedNodeGroupMaxSizeAnnotationKey = "cluster.k8s.io/cluster-api-autoscaler-node-group-max-size"
31+
nodeGroupMinSizeAnnotationKey = "cluster.x-k8s.io/cluster-api-autoscaler-node-group-min-size"
32+
nodeGroupMaxSizeAnnotationKey = "cluster.x-k8s.io/cluster-api-autoscaler-node-group-max-size"
33+
clusterNameLabel = "cluster.x-k8s.io/cluster-name"
34+
deprecatedClusterNameLabel = "cluster.k8s.io/cluster-name"
3335
)
3436

3537
var (
@@ -60,6 +62,9 @@ type normalizedProviderID string
6062
// value is not of type int.
6163
func minSize(annotations map[string]string) (int, error) {
6264
val, found := annotations[nodeGroupMinSizeAnnotationKey]
65+
if !found {
66+
val, found = annotations[deprecatedNodeGroupMinSizeAnnotationKey]
67+
}
6368
if !found {
6469
return 0, errMissingMinAnnotation
6570
}
@@ -76,6 +81,9 @@ func minSize(annotations map[string]string) (int, error) {
7681
// value is not of type int.
7782
func maxSize(annotations map[string]string) (int, error) {
7883
val, found := annotations[nodeGroupMaxSizeAnnotationKey]
84+
if !found {
85+
val, found = annotations[deprecatedNodeGroupMaxSizeAnnotationKey]
86+
}
7987
if !found {
8088
return 0, errMissingMaxAnnotation
8189
}

cluster-autoscaler/cloudprovider/clusterapi/clusterapi_utils_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,24 @@ func TestUtilParseScalingBounds(t *testing.T) {
112112
},
113113
min: 0,
114114
max: 1,
115+
}, {
116+
description: "deprecated min/max annotations still work, result is min 0, max 1",
117+
annotations: map[string]string{
118+
deprecatedNodeGroupMinSizeAnnotationKey: "0",
119+
deprecatedNodeGroupMaxSizeAnnotationKey: "1",
120+
},
121+
min: 0,
122+
max: 1,
123+
}, {
124+
description: "deprecated min/max annotations do not take precedence over non-deprecated annotations, result is min 1, max 2",
125+
annotations: map[string]string{
126+
deprecatedNodeGroupMinSizeAnnotationKey: "0",
127+
deprecatedNodeGroupMaxSizeAnnotationKey: "1",
128+
nodeGroupMinSizeAnnotationKey: "1",
129+
nodeGroupMaxSizeAnnotationKey: "2",
130+
},
131+
min: 1,
132+
max: 2,
115133
}} {
116134
t.Run(tc.description, func(t *testing.T) {
117135
machineSet := unstructured.Unstructured{

0 commit comments

Comments
 (0)