Skip to content

Commit

Permalink
expand CAPI_GROUP usage to cover node group size annotations
Browse files Browse the repository at this point in the history
This change updates the logic for the clusterapi autoscaler provider so
that the `CAPI_GROUP` environment variable will also affect the
annotations keys for minimum and maximum node group size. It also addes
unit tests and an update to the readme.
  • Loading branch information
elmiko committed Nov 8, 2021
1 parent 821d677 commit c726da7
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
4 changes: 4 additions & 0 deletions cluster-autoscaler/cloudprovider/clusterapi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 24 additions & 2 deletions cluster-autoscaler/cloudprovider/clusterapi/clusterapi_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package clusterapi

import (
"fmt"
"strconv"
"strings"

Expand All @@ -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"
)
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package clusterapi

import (
"fmt"
"os"
"reflect"
"strings"
"testing"
Expand Down Expand Up @@ -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)
}
}

0 comments on commit c726da7

Please sign in to comment.