Skip to content

Commit

Permalink
UPSTREAM: <carry>: Set default annotations, and cluster name label to…
Browse files Browse the repository at this point in the history
… use machine.openshift.io

This change updates the default annotations and cluster name label to
use the openshift specific values.

Also updates the unit tests to incorporate the changed api group and
adds the capi group environment variable awareness to the tests.

Co-authored-by: Michael McCune <[email protected]>
  • Loading branch information
2 people authored and cloud-team-rebase-bot committed Dec 13, 2021
1 parent a614513 commit ab63621
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ const customCAPIGroup = "custom.x-k8s.io"
const fifteenSecondDuration = time.Second * 15

func mustCreateTestController(t *testing.T, testConfigs ...*testConfig) (*machineController, testControllerShutdownFunc) {
// we need to set the environment variable for the CAPI Group to represent the OpenShift specific value
// UNLESS, the specific test has already set it, in which case we should ignore.
if _, found := os.LookupEnv(CAPIGroupEnvVar); !found {
if err := os.Setenv(CAPIGroupEnvVar, "machine.openshift.io"); err != nil {
t.Fatalf("failed to set CAPI_GROUP environment variable: %v", err)
}
}

t.Helper()

nodeObjects := make([]runtime.Object, 0)
Expand All @@ -97,21 +105,38 @@ func mustCreateTestController(t *testing.T, testConfigs ...*testConfig) (*machin
dynamicClientset := fakedynamic.NewSimpleDynamicClientWithCustomListKinds(
runtime.NewScheme(),
map[schema.GroupVersionResource]string{
{Group: "cluster.x-k8s.io", Version: "v1alpha3", Resource: "machinedeployments"}: "kindList",
{Group: "cluster.x-k8s.io", Version: "v1alpha3", Resource: "machines"}: "kindList",
{Group: "cluster.x-k8s.io", Version: "v1alpha3", Resource: "machinesets"}: "kindList",
{Group: "cluster.x-k8s.io", Version: "v1beta1", Resource: "machinedeployments"}: "kindList",
{Group: "cluster.x-k8s.io", Version: "v1beta1", Resource: "machines"}: "kindList",
{Group: "cluster.x-k8s.io", Version: "v1beta1", Resource: "machinesets"}: "kindList",
{Group: "custom.x-k8s.io", Version: "v1beta1", Resource: "machinedeployments"}: "kindList",
{Group: "custom.x-k8s.io", Version: "v1beta1", Resource: "machines"}: "kindList",
{Group: "custom.x-k8s.io", Version: "v1beta1", Resource: "machinesets"}: "kindList",
{Group: "machine.openshift.io", Version: "v1beta1", Resource: "machinedeployments"}: "kindList",
{Group: "machine.openshift.io", Version: "v1beta1", Resource: "machines"}: "kindList",
{Group: "machine.openshift.io", Version: "v1beta1", Resource: "machinesets"}: "kindList",
{Group: "cluster.x-k8s.io", Version: "v1alpha3", Resource: "machinedeployments"}: "kindList",
{Group: "cluster.x-k8s.io", Version: "v1alpha3", Resource: "machines"}: "kindList",
{Group: "cluster.x-k8s.io", Version: "v1alpha3", Resource: "machinesets"}: "kindList",
{Group: "cluster.x-k8s.io", Version: "v1beta1", Resource: "machinedeployments"}: "kindList",
{Group: "cluster.x-k8s.io", Version: "v1beta1", Resource: "machines"}: "kindList",
{Group: "cluster.x-k8s.io", Version: "v1beta1", Resource: "machinesets"}: "kindList",
{Group: "custom.x-k8s.io", Version: "v1beta1", Resource: "machinedeployments"}: "kindList",
{Group: "custom.x-k8s.io", Version: "v1beta1", Resource: "machines"}: "kindList",
{Group: "custom.x-k8s.io", Version: "v1beta1", Resource: "machinesets"}: "kindList",
},
machineObjects...,
)
discoveryClient := &fakediscovery.FakeDiscovery{
Fake: &clientgotesting.Fake{
Resources: []*metav1.APIResourceList{
{
GroupVersion: fmt.Sprintf("%s/v1beta1", "machine.openshift.io"),
APIResources: []metav1.APIResource{
{
Name: resourceNameMachineDeployment,
},
{
Name: resourceNameMachineSet,
},
{
Name: resourceNameMachine,
},
},
},
{
GroupVersion: fmt.Sprintf("%s/v1beta1", customCAPIGroup),
APIResources: []metav1.APIResource{
Expand All @@ -127,7 +152,7 @@ func mustCreateTestController(t *testing.T, testConfigs ...*testConfig) (*machin
},
},
{
GroupVersion: fmt.Sprintf("%s/v1alpha3", defaultCAPIGroup),
GroupVersion: fmt.Sprintf("%s/v1beta1", defaultCAPIGroup),
APIResources: []metav1.APIResource{
{
Name: resourceNameMachineDeployment,
Expand Down Expand Up @@ -166,7 +191,7 @@ func mustCreateTestController(t *testing.T, testConfigs ...*testConfig) (*machin

gvr := schema.GroupVersionResource{
Group: action.GetResource().Group,
Version: "v1alpha3",
Version: "v1beta1",
Resource: resource,
}

Expand Down Expand Up @@ -307,7 +332,7 @@ func createTestConfigs(specs ...testSpec) []*testConfig {
config.machineSet = &unstructured.Unstructured{
Object: map[string]interface{}{
"kind": machineSetKind,
"apiVersion": "cluster.x-k8s.io/v1alpha3",
"apiVersion": "machine.openshift.io/v1beta1",
"metadata": map[string]interface{}{
"name": spec.machineSetName,
"namespace": spec.namespace,
Expand Down Expand Up @@ -336,7 +361,7 @@ func createTestConfigs(specs ...testSpec) []*testConfig {
config.machineDeployment = &unstructured.Unstructured{
Object: map[string]interface{}{
"kind": machineDeploymentKind,
"apiVersion": "cluster.x-k8s.io/v1alpha3",
"apiVersion": "machine.openshift.io/v1beta1",
"metadata": map[string]interface{}{
"name": spec.machineDeploymentName,
"namespace": spec.namespace,
Expand Down Expand Up @@ -403,7 +428,7 @@ func makeLinkedNodeAndMachine(i int, namespace, clusterName string, owner metav1
machine := &unstructured.Unstructured{
Object: map[string]interface{}{
"kind": machineKind,
"apiVersion": "cluster.x-k8s.io/v1alpha3",
"apiVersion": "machine.openshift.io/v1beta1",
"metadata": map[string]interface{}{
"name": fmt.Sprintf("%s-%s-machine-%d", namespace, owner.Name, i),
"namespace": namespace,
Expand Down Expand Up @@ -586,6 +611,7 @@ func TestControllerFindMachine(t *testing.T) {
nodeGroupMinSizeAnnotationKey: "1",
nodeGroupMaxSizeAnnotationKey: "10",
})

if tc.name == "" {
tc.name = testConfig.machines[0].GetName()
}
Expand Down Expand Up @@ -1433,7 +1459,7 @@ func TestGetAPIGroupPreferredVersion(t *testing.T) {
{
description: "find version for default API group",
APIGroup: defaultCAPIGroup,
preferredVersion: "v1alpha3",
preferredVersion: "v1beta1",
error: false,
},
{
Expand All @@ -1457,7 +1483,7 @@ func TestGetAPIGroupPreferredVersion(t *testing.T) {
GroupVersion: fmt.Sprintf("%s/v1beta1", customCAPIGroup),
},
{
GroupVersion: fmt.Sprintf("%s/v1alpha3", defaultCAPIGroup),
GroupVersion: fmt.Sprintf("%s/v1beta1", defaultCAPIGroup),
},
},
},
Expand Down Expand Up @@ -1486,14 +1512,14 @@ func TestGroupVersionHasResource(t *testing.T) {
{
description: "true when it finds resource",
resourceName: resourceNameMachineDeployment,
APIGroup: fmt.Sprintf("%s/v1alpha3", defaultCAPIGroup),
APIGroup: fmt.Sprintf("%s/v1beta1", defaultCAPIGroup),
expected: true,
error: false,
},
{
description: "false when it does not find resource",
resourceName: "resourceDoesNotExist",
APIGroup: fmt.Sprintf("%s/v1alpha3", defaultCAPIGroup),
APIGroup: fmt.Sprintf("%s/v1beta1", defaultCAPIGroup),
expected: false,
error: false,
},
Expand All @@ -1510,7 +1536,7 @@ func TestGroupVersionHasResource(t *testing.T) {
Fake: &clientgotesting.Fake{
Resources: []*metav1.APIResourceList{
{
GroupVersion: fmt.Sprintf("%s/v1alpha3", defaultCAPIGroup),
GroupVersion: fmt.Sprintf("%s/v1beta1", defaultCAPIGroup),
APIResources: []metav1.APIResource{
{
Name: resourceNameMachineDeployment,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ import (
)

const (
debugFormat = "%s (min: %d, max: %d, replicas: %d)"
// deprecatedMachineDeleteAnnotationKey should not be removed until minimum cluster-api support is v1alpha3
deprecatedMachineDeleteAnnotationKey = "cluster.k8s.io/delete-machine"
// TODO: determine what currently relies on deprecatedMachineAnnotationKey to determine when it can be removed
deprecatedMachineAnnotationKey = "cluster.k8s.io/machine"
machineDeleteAnnotationKey = "machine.openshift.io/cluster-api-delete-machine"
machineAnnotationKey = "machine.openshift.io/machine"
debugFormat = "%s (min: %d, max: %d, replicas: %d)"
)

type nodegroup struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,10 +315,11 @@ func TestNodeGroupIncreaseSizeErrors(t *testing.T) {

func TestNodeGroupIncreaseSize(t *testing.T) {
type testCase struct {
description string
delta int
initial int32
expected int32
description string
delta int
initial int32
initialStatus int32
expected int32
}

test := func(t *testing.T, tc *testCase, testConfig *testConfig) {
Expand Down Expand Up @@ -392,6 +393,7 @@ func TestNodeGroupDecreaseTargetSize(t *testing.T) {
description string
delta int
initial int32
initialStatus int32
targetSizeIncrement int32
expected int32
expectedError bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ 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 = "machine.openshift.io/cluster-api-autoscaler-node-group-min-size"
nodeGroupMaxSizeAnnotationKey = "machine.openshift.io/cluster-api-autoscaler-node-group-max-size"
clusterNameLabel = "machine.openshift.io/cluster-name"
deprecatedClusterNameLabel = "cluster.k8s.io/cluster-name"
)

Expand Down

0 comments on commit ab63621

Please sign in to comment.