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 JoelSpeed committed May 31, 2022
1 parent f2c779b commit 5c3ac70
Show file tree
Hide file tree
Showing 3 changed files with 56 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 @@ -1455,8 +1481,7 @@ func TestGetAPIGroupPreferredVersion(t *testing.T) {
{
description: "find version for default API group",
APIGroup: defaultCAPIGroup,
preferredVersion: "v1alpha3",
envVar: "",
preferredVersion: "v1beta1",
error: false,
},
{
Expand Down Expand Up @@ -1489,7 +1514,7 @@ func TestGetAPIGroupPreferredVersion(t *testing.T) {
GroupVersion: fmt.Sprintf("%s/v1beta1", customCAPIGroup),
},
{
GroupVersion: fmt.Sprintf("%s/v1alpha3", defaultCAPIGroup),
GroupVersion: fmt.Sprintf("%s/v1beta1", defaultCAPIGroup),
},
{
GroupVersion: fmt.Sprintf("%s/%s", customCAPIGroup, customVersion),
Expand Down Expand Up @@ -1534,14 +1559,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 @@ -1558,7 +1583,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 @@ -317,10 +317,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 @@ -394,6 +395,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 @@ -589,6 +589,11 @@ func Test_clusterNameFromResource(t *testing.T) {
}

func Test_getKeyHelpers(t *testing.T) {
// unset the environment variable to ensure we get the default from the code
if err := os.Setenv(CAPIGroupEnvVar, ""); err != nil {
t.Fatalf("unexpected error: %v", err)
}

for _, tc := range []struct {
name string
expected string
Expand Down

0 comments on commit 5c3ac70

Please sign in to comment.