diff --git a/cluster-autoscaler/cloudprovider/clusterapi/README.md b/cluster-autoscaler/cloudprovider/clusterapi/README.md index 3e434648b0e1..03c70ac54eb1 100644 --- a/cluster-autoscaler/cloudprovider/clusterapi/README.md +++ b/cluster-autoscaler/cloudprovider/clusterapi/README.md @@ -121,6 +121,10 @@ some situations, such as testing or prototyping, you may wish to change this group variable. For these situations you may use the environment variable `CAPI_GROUP` to change the group that the provider will use. +Please note that setting the `CAPI_GROUP` environment variable will also cause the +annotations for minimum and maximum size to change. For example, if `CAPI_GROUP=test.k8s.io` +then the minimum size annotation key will be `test.k8s.io/cluster-api-autoscaler-node-group-min-size`. + ## Sample manifest A sample manifest that will create a deployment running the autoscaler is diff --git a/cluster-autoscaler/cloudprovider/clusterapi/clusterapi_utils.go b/cluster-autoscaler/cloudprovider/clusterapi/clusterapi_utils.go index f564efc0c8f8..d1c5cbb32b94 100644 --- a/cluster-autoscaler/cloudprovider/clusterapi/clusterapi_utils.go +++ b/cluster-autoscaler/cloudprovider/clusterapi/clusterapi_utils.go @@ -17,6 +17,7 @@ limitations under the License. package clusterapi import ( + "fmt" "strconv" "strings" @@ -28,8 +29,6 @@ import ( const ( deprecatedNodeGroupMinSizeAnnotationKey = "cluster.k8s.io/cluster-api-autoscaler-node-group-min-size" deprecatedNodeGroupMaxSizeAnnotationKey = "cluster.k8s.io/cluster-api-autoscaler-node-group-max-size" - nodeGroupMinSizeAnnotationKey = "cluster.x-k8s.io/cluster-api-autoscaler-node-group-min-size" - 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" ) @@ -52,6 +51,13 @@ var ( // errInvalidMaxAnnotationValue is the error returned when a // machine set has a non-integral max annotation value. errInvalidMaxAnnotation = errors.New("invalid max annotation") + + // nodeGroupMinSizeAnnotationKey and nodeGroupMaxSizeAnnotationKey are the keys + // used in MachineSet and MachineDeployment annotations to specify the limits + // for the node group. Because the keys can be affected by the CAPI_GROUP env + // variable, they are initialized here. + nodeGroupMinSizeAnnotationKey = getNodeGroupMinSizeAnnotationKey() + nodeGroupMaxSizeAnnotationKey = getNodeGroupMaxSizeAnnotationKey() ) type normalizedProviderID string @@ -180,3 +186,19 @@ func clusterNameFromResource(r *unstructured.Unstructured) string { return "" } + +// getNodeGroupMinSizeAnnotationKey returns the key that is used for the +// node group minimum size annotation. This is needed because the user can +// change the defaulte group name by using the CAPI_GROUP environment variable. +func getNodeGroupMinSizeAnnotationKey() string { + key := fmt.Sprintf("%s/cluster-api-autoscaler-node-group-min-size", getCAPIGroup()) + return key +} + +// getNodeGroupMaxSizeAnnotationKey returns the key that is used for the +// node group maximum size annotation. This is needed because the user can +// change the defaulte group name by using the CAPI_GROUP environment variable. +func getNodeGroupMaxSizeAnnotationKey() string { + key := fmt.Sprintf("%s/cluster-api-autoscaler-node-group-max-size", getCAPIGroup()) + return key +} diff --git a/cluster-autoscaler/cloudprovider/clusterapi/clusterapi_utils_test.go b/cluster-autoscaler/cloudprovider/clusterapi/clusterapi_utils_test.go index 65ee11fe9ccb..e04a71f8ebc9 100644 --- a/cluster-autoscaler/cloudprovider/clusterapi/clusterapi_utils_test.go +++ b/cluster-autoscaler/cloudprovider/clusterapi/clusterapi_utils_test.go @@ -17,6 +17,8 @@ limitations under the License. package clusterapi import ( + "fmt" + "os" "reflect" "strings" "testing" @@ -689,4 +691,23 @@ func Test_clusterNameFromResource(t *testing.T) { } }) } + +} +func Test_getNodeGroupSizeAnnotationKey(t *testing.T) { + testgroup := "test.k8s.io" + if err := os.Setenv(CAPIGroupEnvVar, testgroup); err != nil { + t.Fatalf("unexpected error: %v", err) + } + + expected := fmt.Sprintf("%s/cluster-api-autoscaler-node-group-min-size", testgroup) + observed := getNodeGroupMinSizeAnnotationKey() + if expected != observed { + t.Errorf("min size annotations keys do not match: observed=%s, expected=%s", observed, expected) + } + + expected = fmt.Sprintf("%s/cluster-api-autoscaler-node-group-max-size", testgroup) + observed = getNodeGroupMaxSizeAnnotationKey() + if expected != observed { + t.Errorf("max size annotations keys do not match: observed=%s, expected=%s", observed, expected) + } }