Update group identifier to use for Cluster API annotations

- Also add backwards compatibility for the previously used deprecated annotations
This commit is contained in:
Jason DeTiberus 2020-09-21 10:42:46 -04:00 committed by Ben Moss
parent 09accf65d1
commit 1e0fe7c85d
No known key found for this signature in database
GPG Key ID: A19E72F2857FCB7C
8 changed files with 88 additions and 31 deletions

View File

@ -101,12 +101,12 @@ resources depending on the type of cluster-api mechanism that you are using.
There are two annotations that control how a cluster resource should be scaled: There are two annotations that control how a cluster resource should be scaled:
* `cluster.k8s.io/cluster-api-autoscaler-node-group-min-size` - This specifies * `cluster.x-k8s.io/cluster-api-autoscaler-node-group-min-size` - This specifies
the minimum number of nodes for the associated resource group. The autoscaler the minimum number of nodes for the associated resource group. The autoscaler
will not scale the group below this number. Please note that currently the will not scale the group below this number. Please note that currently the
cluster-api provider will not scale down to zero nodes. cluster-api provider will not scale down to zero nodes.
* `cluster.k8s.io/cluster-api-autoscaler-node-group-max-size` - This specifies * `cluster.x-k8s.io/cluster-api-autoscaler-node-group-max-size` - This specifies
the maximum number of nodes for the associated resource group. The autoscaler the maximum number of nodes for the associated resource group. The autoscaler
will not scale the group above this number. will not scale the group above this number.

View File

@ -258,7 +258,12 @@ func (c *machineController) findMachineByProviderID(providerID normalizedProvide
if node == nil { if node == nil {
return nil, nil return nil, nil
} }
return c.findMachine(node.Annotations[machineAnnotationKey])
machineID, ok := node.Annotations[machineAnnotationKey]
if !ok {
machineID = node.Annotations[deprecatedMachineAnnotationKey]
}
return c.findMachine(machineID)
} }
func isFailedMachineProviderID(providerID normalizedProviderID) bool { func isFailedMachineProviderID(providerID normalizedProviderID) bool {

View File

@ -486,24 +486,32 @@ func deleteTestConfigs(t *testing.T, controller *machineController, testConfigs
func TestControllerFindMachine(t *testing.T) { func TestControllerFindMachine(t *testing.T) {
type testCase struct { type testCase struct {
description string description string
name string name string
namespace string namespace string
lookupSucceeds bool useDeprecatedAnnotation bool
lookupSucceeds bool
} }
var testCases = []testCase{{ var testCases = []testCase{{
description: "lookup fails", description: "lookup fails",
lookupSucceeds: false, lookupSucceeds: false,
name: "machine-does-not-exist", useDeprecatedAnnotation: false,
namespace: "namespace-does-not-exist", name: "machine-does-not-exist",
namespace: "namespace-does-not-exist",
}, { }, {
description: "lookup fails in valid namespace", description: "lookup fails in valid namespace",
lookupSucceeds: false, lookupSucceeds: false,
name: "machine-does-not-exist-in-existing-namespace", useDeprecatedAnnotation: false,
name: "machine-does-not-exist-in-existing-namespace",
}, { }, {
description: "lookup succeeds", description: "lookup succeeds",
lookupSucceeds: true, lookupSucceeds: true,
useDeprecatedAnnotation: false,
}, {
description: "lookup succeeds with deprecated annotation",
lookupSucceeds: true,
useDeprecatedAnnotation: true,
}} }}
test := func(t *testing.T, tc testCase, testConfig *testConfig) { test := func(t *testing.T, tc testCase, testConfig *testConfig) {
@ -541,6 +549,19 @@ func TestControllerFindMachine(t *testing.T) {
if tc.namespace == "" { if tc.namespace == "" {
tc.namespace = testConfig.machines[0].GetNamespace() tc.namespace = testConfig.machines[0].GetNamespace()
} }
if tc.useDeprecatedAnnotation {
for i := range testConfig.machines {
n := testConfig.nodes[i]
annotations := n.GetAnnotations()
val, ok := annotations[machineAnnotationKey]
if !ok {
t.Fatal("node did not contain machineAnnotationKey")
}
delete(annotations, machineAnnotationKey)
annotations[deprecatedMachineAnnotationKey] = val
n.SetAnnotations(annotations)
}
}
test(t, tc, testConfig) test(t, tc, testConfig)
}) })
} }

View File

@ -27,9 +27,13 @@ import (
) )
const ( const (
machineDeleteAnnotationKey = "cluster.k8s.io/delete-machine" // deprecatedMachineDeleteAnnotationKey should not be removed until minimum cluster-api support is v1alpha3
machineAnnotationKey = "cluster.k8s.io/machine" deprecatedMachineDeleteAnnotationKey = "cluster.k8s.io/delete-machine"
debugFormat = "%s (min: %d, max: %d, replicas: %d)" // TODO: determine what currently relies on deprecatedMachineAnnotationKey to determine when it can be removed
deprecatedMachineAnnotationKey = "cluster.k8s.io/machine"
machineDeleteAnnotationKey = "cluster.x-k8s.io/delete-machine"
machineAnnotationKey = "cluster.x-k8s.io/machine"
debugFormat = "%s (min: %d, max: %d, replicas: %d)"
) )
type nodegroup struct { type nodegroup struct {

View File

@ -649,6 +649,9 @@ func TestNodeGroupDeleteNodes(t *testing.T) {
if _, found := machine.GetAnnotations()[machineDeleteAnnotationKey]; !found { if _, found := machine.GetAnnotations()[machineDeleteAnnotationKey]; !found {
t.Errorf("expected annotation %q on machine %s", machineDeleteAnnotationKey, machine.GetName()) t.Errorf("expected annotation %q on machine %s", machineDeleteAnnotationKey, machine.GetName())
} }
if _, found := machine.GetAnnotations()[deprecatedMachineDeleteAnnotationKey]; !found {
t.Errorf("expected annotation %q on machine %s", deprecatedMachineDeleteAnnotationKey, machine.GetName())
}
} }
gvr, err := ng.scalableResource.GroupVersionResource() gvr, err := ng.scalableResource.GroupVersionResource()

View File

@ -129,15 +129,12 @@ func (r unstructuredScalableResource) UnmarkMachineForDeletion(machine *unstruct
} }
annotations := u.GetAnnotations() annotations := u.GetAnnotations()
if _, ok := annotations[machineDeleteAnnotationKey]; ok { delete(annotations, machineDeleteAnnotationKey)
delete(annotations, machineDeleteAnnotationKey) delete(annotations, deprecatedMachineDeleteAnnotationKey)
u.SetAnnotations(annotations) u.SetAnnotations(annotations)
_, updateErr := r.controller.managementClient.Resource(r.controller.machineResource).Namespace(u.GetNamespace()).Update(context.TODO(), u, metav1.UpdateOptions{}) _, updateErr := r.controller.managementClient.Resource(r.controller.machineResource).Namespace(u.GetNamespace()).Update(context.TODO(), u, metav1.UpdateOptions{})
return updateErr return updateErr
}
return nil
} }
func (r unstructuredScalableResource) MarkMachineForDeletion(machine *unstructured.Unstructured) error { func (r unstructuredScalableResource) MarkMachineForDeletion(machine *unstructured.Unstructured) error {
@ -154,6 +151,7 @@ func (r unstructuredScalableResource) MarkMachineForDeletion(machine *unstructur
} }
annotations[machineDeleteAnnotationKey] = time.Now().String() annotations[machineDeleteAnnotationKey] = time.Now().String()
annotations[deprecatedMachineDeleteAnnotationKey] = time.Now().String()
u.SetAnnotations(annotations) u.SetAnnotations(annotations)
_, updateErr := r.controller.managementClient.Resource(r.controller.machineResource).Namespace(u.GetNamespace()).Update(context.TODO(), u, metav1.UpdateOptions{}) _, updateErr := r.controller.managementClient.Resource(r.controller.machineResource).Namespace(u.GetNamespace()).Update(context.TODO(), u, metav1.UpdateOptions{})

View File

@ -26,10 +26,12 @@ import (
) )
const ( const (
nodeGroupMinSizeAnnotationKey = "cluster.k8s.io/cluster-api-autoscaler-node-group-min-size" deprecatedNodeGroupMinSizeAnnotationKey = "cluster.k8s.io/cluster-api-autoscaler-node-group-min-size"
nodeGroupMaxSizeAnnotationKey = "cluster.k8s.io/cluster-api-autoscaler-node-group-max-size" deprecatedNodeGroupMaxSizeAnnotationKey = "cluster.k8s.io/cluster-api-autoscaler-node-group-max-size"
clusterNameLabel = "cluster.x-k8s.io/cluster-name" nodeGroupMinSizeAnnotationKey = "cluster.x-k8s.io/cluster-api-autoscaler-node-group-min-size"
deprecatedClusterNameLabel = "cluster.k8s.io/cluster-name" nodeGroupMaxSizeAnnotationKey = "cluster.x-k8s.io/cluster-api-autoscaler-node-group-max-size"
clusterNameLabel = "cluster.x-k8s.io/cluster-name"
deprecatedClusterNameLabel = "cluster.k8s.io/cluster-name"
) )
var ( var (
@ -60,6 +62,9 @@ type normalizedProviderID string
// value is not of type int. // value is not of type int.
func minSize(annotations map[string]string) (int, error) { func minSize(annotations map[string]string) (int, error) {
val, found := annotations[nodeGroupMinSizeAnnotationKey] val, found := annotations[nodeGroupMinSizeAnnotationKey]
if !found {
val, found = annotations[deprecatedNodeGroupMinSizeAnnotationKey]
}
if !found { if !found {
return 0, errMissingMinAnnotation return 0, errMissingMinAnnotation
} }
@ -76,6 +81,9 @@ func minSize(annotations map[string]string) (int, error) {
// value is not of type int. // value is not of type int.
func maxSize(annotations map[string]string) (int, error) { func maxSize(annotations map[string]string) (int, error) {
val, found := annotations[nodeGroupMaxSizeAnnotationKey] val, found := annotations[nodeGroupMaxSizeAnnotationKey]
if !found {
val, found = annotations[deprecatedNodeGroupMaxSizeAnnotationKey]
}
if !found { if !found {
return 0, errMissingMaxAnnotation return 0, errMissingMaxAnnotation
} }

View File

@ -112,6 +112,24 @@ func TestUtilParseScalingBounds(t *testing.T) {
}, },
min: 0, min: 0,
max: 1, max: 1,
}, {
description: "deprecated min/max annotations still work, result is min 0, max 1",
annotations: map[string]string{
deprecatedNodeGroupMinSizeAnnotationKey: "0",
deprecatedNodeGroupMaxSizeAnnotationKey: "1",
},
min: 0,
max: 1,
}, {
description: "deprecated min/max annotations do not take precedence over non-deprecated annotations, result is min 1, max 2",
annotations: map[string]string{
deprecatedNodeGroupMinSizeAnnotationKey: "0",
deprecatedNodeGroupMaxSizeAnnotationKey: "1",
nodeGroupMinSizeAnnotationKey: "1",
nodeGroupMaxSizeAnnotationKey: "2",
},
min: 1,
max: 2,
}} { }} {
t.Run(tc.description, func(t *testing.T) { t.Run(tc.description, func(t *testing.T) {
machineSet := unstructured.Unstructured{ machineSet := unstructured.Unstructured{