diff --git a/controlplane/kubeadm/controllers/controller_test.go b/controlplane/kubeadm/controllers/controller_test.go index 194121eefab4..7dddf5854422 100644 --- a/controlplane/kubeadm/controllers/controller_test.go +++ b/controlplane/kubeadm/controllers/controller_test.go @@ -59,11 +59,11 @@ func TestClusterToKubeadmControlPlane(t *testing.T) { g := NewWithT(t) fakeClient := newFakeClient() - cluster := newCluster(&types.NamespacedName{Name: "foo", Namespace: "test"}) + cluster := newCluster(&types.NamespacedName{Name: "foo", Namespace: metav1.NamespaceDefault}) cluster.Spec = clusterv1.ClusterSpec{ ControlPlaneRef: &corev1.ObjectReference{ Kind: "KubeadmControlPlane", - Namespace: "test", + Namespace: metav1.NamespaceDefault, Name: "kcp-foo", APIVersion: controlplanev1.GroupVersion.String(), }, @@ -90,7 +90,7 @@ func TestClusterToKubeadmControlPlaneNoControlPlane(t *testing.T) { g := NewWithT(t) fakeClient := newFakeClient() - cluster := newCluster(&types.NamespacedName{Name: "foo", Namespace: "test"}) + cluster := newCluster(&types.NamespacedName{Name: "foo", Namespace: metav1.NamespaceDefault}) r := &KubeadmControlPlaneReconciler{ Client: fakeClient, @@ -105,11 +105,11 @@ func TestClusterToKubeadmControlPlaneOtherControlPlane(t *testing.T) { g := NewWithT(t) fakeClient := newFakeClient() - cluster := newCluster(&types.NamespacedName{Name: "foo", Namespace: "test"}) + cluster := newCluster(&types.NamespacedName{Name: "foo", Namespace: metav1.NamespaceDefault}) cluster.Spec = clusterv1.ClusterSpec{ ControlPlaneRef: &corev1.ObjectReference{ Kind: "OtherControlPlane", - Namespace: "test", + Namespace: metav1.NamespaceDefault, Name: "other-foo", APIVersion: controlplanev1.GroupVersion.String(), }, @@ -126,9 +126,16 @@ func TestClusterToKubeadmControlPlaneOtherControlPlane(t *testing.T) { func TestReconcileReturnErrorWhenOwnerClusterIsMissing(t *testing.T) { g := NewWithT(t) - cluster, kcp, _ := createClusterWithControlPlane() + + ns, err := env.CreateNamespace(ctx, "test-reconcile-return-error") + g.Expect(err).ToNot(HaveOccurred()) + + cluster, kcp, _ := createClusterWithControlPlane(ns.Name) g.Expect(env.Create(ctx, cluster)).To(Succeed()) g.Expect(env.Create(ctx, kcp)).To(Succeed()) + defer func(do ...client.Object) { + g.Expect(env.Cleanup(ctx, do...)).To(Succeed()) + }(kcp, ns) r := &KubeadmControlPlaneReconciler{ Client: env, @@ -158,9 +165,15 @@ func TestReconcileUpdateObservedGeneration(t *testing.T) { managementCluster: &internal.Management{Client: env.Client, Tracker: nil}, } - cluster, kcp, _ := createClusterWithControlPlane() + ns, err := env.CreateNamespace(ctx, "test-reconcile-upd-og") + g.Expect(err).ToNot(HaveOccurred()) + + cluster, kcp, _ := createClusterWithControlPlane(ns.Name) g.Expect(env.Create(ctx, cluster)).To(Succeed()) g.Expect(env.Create(ctx, kcp)).To(Succeed()) + defer func(do ...client.Object) { + g.Expect(env.Cleanup(ctx, do...)).To(Succeed()) + }(cluster, kcp, ns) // read kcp.Generation after create errGettingObject := env.Get(ctx, util.ObjectKey(kcp), kcp) @@ -208,7 +221,7 @@ func TestReconcileNoClusterOwnerRef(t *testing.T) { kcp := &controlplanev1.KubeadmControlPlane{ ObjectMeta: metav1.ObjectMeta{ - Namespace: "test", + Namespace: metav1.NamespaceDefault, Name: "foo", }, Spec: controlplanev1.KubeadmControlPlaneSpec{ @@ -218,7 +231,7 @@ func TestReconcileNoClusterOwnerRef(t *testing.T) { Kind: "UnknownInfraMachine", APIVersion: "test/v1alpha1", Name: "foo", - Namespace: "test", + Namespace: metav1.NamespaceDefault, }, }, }, @@ -237,7 +250,7 @@ func TestReconcileNoClusterOwnerRef(t *testing.T) { g.Expect(result).To(Equal(ctrl.Result{})) machineList := &clusterv1.MachineList{} - g.Expect(fakeClient.List(ctx, machineList, client.InNamespace("test"))).To(Succeed()) + g.Expect(fakeClient.List(ctx, machineList, client.InNamespace(metav1.NamespaceDefault))).To(Succeed()) g.Expect(machineList.Items).To(BeEmpty()) } @@ -246,7 +259,7 @@ func TestReconcileNoKCP(t *testing.T) { kcp := &controlplanev1.KubeadmControlPlane{ ObjectMeta: metav1.ObjectMeta{ - Namespace: "test", + Namespace: metav1.NamespaceDefault, Name: "foo", }, Spec: controlplanev1.KubeadmControlPlaneSpec{ @@ -256,7 +269,7 @@ func TestReconcileNoKCP(t *testing.T) { Kind: "UnknownInfraMachine", APIVersion: "test/v1alpha1", Name: "foo", - Namespace: "test", + Namespace: metav1.NamespaceDefault, }, }, }, @@ -277,7 +290,7 @@ func TestReconcileNoCluster(t *testing.T) { kcp := &controlplanev1.KubeadmControlPlane{ ObjectMeta: metav1.ObjectMeta{ - Namespace: "test", + Namespace: metav1.NamespaceDefault, Name: "foo", OwnerReferences: []metav1.OwnerReference{ { @@ -294,7 +307,7 @@ func TestReconcileNoCluster(t *testing.T) { Kind: "UnknownInfraMachine", APIVersion: "test/v1alpha1", Name: "foo", - Namespace: "test", + Namespace: metav1.NamespaceDefault, }, }, }, @@ -312,21 +325,21 @@ func TestReconcileNoCluster(t *testing.T) { g.Expect(err).To(HaveOccurred()) machineList := &clusterv1.MachineList{} - g.Expect(fakeClient.List(ctx, machineList, client.InNamespace("test"))).To(Succeed()) + g.Expect(fakeClient.List(ctx, machineList, client.InNamespace(metav1.NamespaceDefault))).To(Succeed()) g.Expect(machineList.Items).To(BeEmpty()) } func TestReconcilePaused(t *testing.T) { g := NewWithT(t) - clusterName, clusterNamespace := "foo", "test" + clusterName := "foo" // Test: cluster is paused and kcp is not - cluster := newCluster(&types.NamespacedName{Namespace: clusterNamespace, Name: clusterName}) + cluster := newCluster(&types.NamespacedName{Namespace: metav1.NamespaceDefault, Name: clusterName}) cluster.Spec.Paused = true kcp := &controlplanev1.KubeadmControlPlane{ ObjectMeta: metav1.ObjectMeta{ - Namespace: clusterNamespace, + Namespace: metav1.NamespaceDefault, Name: clusterName, OwnerReferences: []metav1.OwnerReference{ { @@ -343,7 +356,7 @@ func TestReconcilePaused(t *testing.T) { Kind: "UnknownInfraMachine", APIVersion: "test/v1alpha1", Name: "foo", - Namespace: "test", + Namespace: metav1.NamespaceDefault, }, }, }, @@ -360,7 +373,7 @@ func TestReconcilePaused(t *testing.T) { g.Expect(err).NotTo(HaveOccurred()) machineList := &clusterv1.MachineList{} - g.Expect(fakeClient.List(ctx, machineList, client.InNamespace(clusterNamespace))).To(Succeed()) + g.Expect(fakeClient.List(ctx, machineList, client.InNamespace(metav1.NamespaceDefault))).To(Succeed()) g.Expect(machineList.Items).To(BeEmpty()) // Test: kcp is paused and cluster is not @@ -374,7 +387,7 @@ func TestReconcilePaused(t *testing.T) { func TestReconcileClusterNoEndpoints(t *testing.T) { g := NewWithT(t) - cluster := newCluster(&types.NamespacedName{Name: "foo", Namespace: "test"}) + cluster := newCluster(&types.NamespacedName{Name: "foo", Namespace: metav1.NamespaceDefault}) cluster.Status = clusterv1.ClusterStatus{InfrastructureReady: true} kcp := &controlplanev1.KubeadmControlPlane{ @@ -396,7 +409,7 @@ func TestReconcileClusterNoEndpoints(t *testing.T) { Kind: "UnknownInfraMachine", APIVersion: "test/v1alpha1", Name: "foo", - Namespace: "test", + Namespace: metav1.NamespaceDefault, }, }, }, @@ -432,11 +445,11 @@ func TestReconcileClusterNoEndpoints(t *testing.T) { g.Expect(kcp.Status.Selector).NotTo(BeEmpty()) - _, err = secret.GetFromNamespacedName(ctx, fakeClient, client.ObjectKey{Namespace: "test", Name: "foo"}, secret.ClusterCA) + _, err = secret.GetFromNamespacedName(ctx, fakeClient, client.ObjectKey{Namespace: metav1.NamespaceDefault, Name: "foo"}, secret.ClusterCA) g.Expect(err).NotTo(HaveOccurred()) machineList := &clusterv1.MachineList{} - g.Expect(fakeClient.List(ctx, machineList, client.InNamespace("test"))).To(Succeed()) + g.Expect(fakeClient.List(ctx, machineList, client.InNamespace(metav1.NamespaceDefault))).To(Succeed()) g.Expect(machineList.Items).To(BeEmpty()) } @@ -445,7 +458,7 @@ func TestKubeadmControlPlaneReconciler_adoption(t *testing.T) { t.Run("adopts existing Machines", func(t *testing.T) { g := NewWithT(t) - cluster, kcp, tmpl := createClusterWithControlPlane() + cluster, kcp, tmpl := createClusterWithControlPlane(metav1.NamespaceDefault) cluster.Spec.ControlPlaneEndpoint.Host = "bar" cluster.Spec.ControlPlaneEndpoint.Port = 6443 kcp.Spec.Version = version @@ -508,7 +521,7 @@ func TestKubeadmControlPlaneReconciler_adoption(t *testing.T) { t.Run("adopts v1alpha2 cluster secrets", func(t *testing.T) { g := NewWithT(t) - cluster, kcp, tmpl := createClusterWithControlPlane() + cluster, kcp, tmpl := createClusterWithControlPlane(metav1.NamespaceDefault) cluster.Spec.ControlPlaneEndpoint.Host = "bar" cluster.Spec.ControlPlaneEndpoint.Port = 6443 kcp.Spec.Version = version @@ -613,7 +626,7 @@ func TestKubeadmControlPlaneReconciler_adoption(t *testing.T) { // 4. The update to our cache for our deletion timestamp arrives g := NewWithT(t) - cluster, kcp, tmpl := createClusterWithControlPlane() + cluster, kcp, tmpl := createClusterWithControlPlane(metav1.NamespaceDefault) cluster.Spec.ControlPlaneEndpoint.Host = "nodomain.example.com1" cluster.Spec.ControlPlaneEndpoint.Port = 6443 kcp.Spec.Version = version @@ -678,7 +691,7 @@ func TestKubeadmControlPlaneReconciler_adoption(t *testing.T) { t.Run("refuses to adopt Machines that are more than one version old", func(t *testing.T) { g := NewWithT(t) - cluster, kcp, tmpl := createClusterWithControlPlane() + cluster, kcp, tmpl := createClusterWithControlPlane(metav1.NamespaceDefault) cluster.Spec.ControlPlaneEndpoint.Host = "nodomain.example.com2" cluster.Spec.ControlPlaneEndpoint.Port = 6443 kcp.Spec.Version = "v1.17.0" @@ -731,7 +744,7 @@ func TestKubeadmControlPlaneReconciler_adoption(t *testing.T) { func TestReconcileInitializeControlPlane(t *testing.T) { g := NewWithT(t) - cluster := newCluster(&types.NamespacedName{Name: "foo", Namespace: "test"}) + cluster := newCluster(&types.NamespacedName{Name: "foo", Namespace: metav1.NamespaceDefault}) cluster.Spec = clusterv1.ClusterSpec{ ControlPlaneEndpoint: clusterv1.APIEndpoint{ Host: "test.local", @@ -889,7 +902,7 @@ kubernetesVersion: metav1.16.1`, g.Expect(kcp.Status.Replicas).To(BeEquivalentTo(1)) g.Expect(conditions.IsFalse(kcp, controlplanev1.AvailableCondition)).To(BeTrue()) - s, err := secret.GetFromNamespacedName(ctx, fakeClient, client.ObjectKey{Namespace: "test", Name: "foo"}, secret.ClusterCA) + s, err := secret.GetFromNamespacedName(ctx, fakeClient, client.ObjectKey{Namespace: metav1.NamespaceDefault, Name: "foo"}, secret.ClusterCA) g.Expect(err).NotTo(HaveOccurred()) g.Expect(s).NotTo(BeNil()) g.Expect(s.Data).NotTo(BeEmpty()) @@ -900,7 +913,7 @@ kubernetesVersion: metav1.16.1`, g.Expect(k).NotTo(BeEmpty()) machineList := &clusterv1.MachineList{} - g.Expect(fakeClient.List(ctx, machineList, client.InNamespace("test"))).To(Succeed()) + g.Expect(fakeClient.List(ctx, machineList, client.InNamespace(metav1.NamespaceDefault))).To(Succeed()) g.Expect(machineList.Items).To(HaveLen(1)) machine := machineList.Items[0] @@ -915,7 +928,7 @@ kubernetesVersion: metav1.16.1`, func TestKubeadmControlPlaneReconciler_updateCoreDNS(t *testing.T) { // TODO: (wfernandes) This test could use some refactor love. - cluster := newCluster(&types.NamespacedName{Name: "foo", Namespace: "default"}) + cluster := newCluster(&types.NamespacedName{Name: "foo", Namespace: metav1.NamespaceDefault}) kcp := &controlplanev1.KubeadmControlPlane{ ObjectMeta: metav1.ObjectMeta{ Namespace: cluster.Namespace, @@ -1191,7 +1204,7 @@ func TestKubeadmControlPlaneReconciler_reconcileDelete(t *testing.T) { t.Run("removes all control plane Machines", func(t *testing.T) { g := NewWithT(t) - cluster, kcp, _ := createClusterWithControlPlane() + cluster, kcp, _ := createClusterWithControlPlane(metav1.NamespaceDefault) controllerutil.AddFinalizer(kcp, controlplanev1.KubeadmControlPlaneFinalizer) initObjs := []client.Object{cluster.DeepCopy(), kcp.DeepCopy()} @@ -1214,7 +1227,7 @@ func TestKubeadmControlPlaneReconciler_reconcileDelete(t *testing.T) { result, err := r.reconcileDelete(ctx, cluster, kcp) g.Expect(result).To(Equal(ctrl.Result{RequeueAfter: deleteRequeueAfter})) - g.Expect(err).To(BeNil()) + g.Expect(err).ToNot(HaveOccurred()) g.Expect(kcp.Finalizers).To(ContainElement(controlplanev1.KubeadmControlPlaneFinalizer)) controlPlaneMachines := clusterv1.MachineList{} @@ -1230,7 +1243,7 @@ func TestKubeadmControlPlaneReconciler_reconcileDelete(t *testing.T) { t.Run("does not remove any control plane Machines if other Machines exist", func(t *testing.T) { g := NewWithT(t) - cluster, kcp, _ := createClusterWithControlPlane() + cluster, kcp, _ := createClusterWithControlPlane(metav1.NamespaceDefault) controllerutil.AddFinalizer(kcp, controlplanev1.KubeadmControlPlaneFinalizer) workerMachine := &clusterv1.Machine{ @@ -1263,7 +1276,7 @@ func TestKubeadmControlPlaneReconciler_reconcileDelete(t *testing.T) { result, err := r.reconcileDelete(ctx, cluster, kcp) g.Expect(result).To(Equal(ctrl.Result{RequeueAfter: deleteRequeueAfter})) - g.Expect(err).To(BeNil()) + g.Expect(err).ToNot(HaveOccurred()) g.Expect(kcp.Finalizers).To(ContainElement(controlplanev1.KubeadmControlPlaneFinalizer)) @@ -1279,7 +1292,7 @@ func TestKubeadmControlPlaneReconciler_reconcileDelete(t *testing.T) { _ = feature.MutableGates.Set("MachinePool=true") g := NewWithT(t) - cluster, kcp, _ := createClusterWithControlPlane() + cluster, kcp, _ := createClusterWithControlPlane(metav1.NamespaceDefault) controllerutil.AddFinalizer(kcp, controlplanev1.KubeadmControlPlaneFinalizer) workerMachinePool := &expv1.MachinePool{ @@ -1312,7 +1325,7 @@ func TestKubeadmControlPlaneReconciler_reconcileDelete(t *testing.T) { result, err := r.reconcileDelete(ctx, cluster, kcp) g.Expect(result).To(Equal(ctrl.Result{RequeueAfter: deleteRequeueAfter})) - g.Expect(err).To(BeNil()) + g.Expect(err).ToNot(HaveOccurred()) g.Expect(kcp.Finalizers).To(ContainElement(controlplanev1.KubeadmControlPlaneFinalizer)) @@ -1327,7 +1340,7 @@ func TestKubeadmControlPlaneReconciler_reconcileDelete(t *testing.T) { t.Run("removes the finalizer if no control plane Machines exist", func(t *testing.T) { g := NewWithT(t) - cluster, kcp, _ := createClusterWithControlPlane() + cluster, kcp, _ := createClusterWithControlPlane(metav1.NamespaceDefault) controllerutil.AddFinalizer(kcp, controlplanev1.KubeadmControlPlaneFinalizer) fakeClient := newFakeClient(cluster.DeepCopy(), kcp.DeepCopy()) @@ -1379,10 +1392,9 @@ func (c *fakeClient) Create(ctx context.Context, obj client.Object, opts ...clie return c.Client.Create(ctx, obj, opts...) } -func createClusterWithControlPlane() (*clusterv1.Cluster, *controlplanev1.KubeadmControlPlane, *unstructured.Unstructured) { +func createClusterWithControlPlane(namespace string) (*clusterv1.Cluster, *controlplanev1.KubeadmControlPlane, *unstructured.Unstructured) { kcpName := fmt.Sprintf("kcp-foo-%s", util.RandomString(6)) - namespace := "default" cluster := newCluster(&types.NamespacedName{Name: kcpName, Namespace: namespace}) cluster.Spec = clusterv1.ClusterSpec{ ControlPlaneRef: &corev1.ObjectReference{ diff --git a/controlplane/kubeadm/controllers/helpers_test.go b/controlplane/kubeadm/controllers/helpers_test.go index 3db90837b0fb..9a97a67e0e91 100644 --- a/controlplane/kubeadm/controllers/helpers_test.go +++ b/controlplane/kubeadm/controllers/helpers_test.go @@ -48,7 +48,7 @@ func TestReconcileKubeconfigEmptyAPIEndpoints(t *testing.T) { }, ObjectMeta: metav1.ObjectMeta{ Name: "foo", - Namespace: "test", + Namespace: metav1.NamespaceDefault, }, Spec: clusterv1.ClusterSpec{ ControlPlaneEndpoint: clusterv1.APIEndpoint{}, @@ -62,13 +62,13 @@ func TestReconcileKubeconfigEmptyAPIEndpoints(t *testing.T) { }, ObjectMeta: metav1.ObjectMeta{ Name: "foo", - Namespace: "test", + Namespace: metav1.NamespaceDefault, }, Spec: controlplanev1.KubeadmControlPlaneSpec{ Version: "v1.16.6", }, } - clusterName := client.ObjectKey{Namespace: "test", Name: "foo"} + clusterName := client.ObjectKey{Namespace: metav1.NamespaceDefault, Name: "foo"} fakeClient := newFakeClient(kcp.DeepCopy()) r := &KubeadmControlPlaneReconciler{ @@ -82,7 +82,7 @@ func TestReconcileKubeconfigEmptyAPIEndpoints(t *testing.T) { kubeconfigSecret := &corev1.Secret{} secretName := client.ObjectKey{ - Namespace: "test", + Namespace: metav1.NamespaceDefault, Name: secret.Name(clusterName.Name, secret.Kubeconfig), } g.Expect(r.Client.Get(ctx, secretName, kubeconfigSecret)).To(MatchError(ContainSubstring("not found"))) @@ -98,7 +98,7 @@ func TestReconcileKubeconfigMissingCACertificate(t *testing.T) { }, ObjectMeta: metav1.ObjectMeta{ Name: "foo", - Namespace: "test", + Namespace: metav1.NamespaceDefault, }, Spec: clusterv1.ClusterSpec{ ControlPlaneEndpoint: clusterv1.APIEndpoint{Host: "test.local", Port: 8443}, @@ -112,7 +112,7 @@ func TestReconcileKubeconfigMissingCACertificate(t *testing.T) { }, ObjectMeta: metav1.ObjectMeta{ Name: "foo", - Namespace: "test", + Namespace: metav1.NamespaceDefault, }, Spec: controlplanev1.KubeadmControlPlaneSpec{ Version: "v1.16.6", @@ -131,7 +131,7 @@ func TestReconcileKubeconfigMissingCACertificate(t *testing.T) { kubeconfigSecret := &corev1.Secret{} secretName := client.ObjectKey{ - Namespace: "test", + Namespace: metav1.NamespaceDefault, Name: secret.Name(cluster.Name, secret.Kubeconfig), } g.Expect(r.Client.Get(ctx, secretName, kubeconfigSecret)).To(MatchError(ContainSubstring("not found"))) @@ -147,7 +147,7 @@ func TestReconcileKubeconfigSecretAdoptsV1alpha2Secrets(t *testing.T) { }, ObjectMeta: metav1.ObjectMeta{ Name: "foo", - Namespace: "test", + Namespace: metav1.NamespaceDefault, }, Spec: clusterv1.ClusterSpec{ ControlPlaneEndpoint: clusterv1.APIEndpoint{Host: "test.local", Port: 8443}, @@ -161,7 +161,7 @@ func TestReconcileKubeconfigSecretAdoptsV1alpha2Secrets(t *testing.T) { }, ObjectMeta: metav1.ObjectMeta{ Name: "foo", - Namespace: "test", + Namespace: metav1.NamespaceDefault, }, Spec: controlplanev1.KubeadmControlPlaneSpec{ Version: "v1.16.6", @@ -169,7 +169,7 @@ func TestReconcileKubeconfigSecretAdoptsV1alpha2Secrets(t *testing.T) { } existingKubeconfigSecret := kubeconfig.GenerateSecretWithOwner( - client.ObjectKey{Name: "foo", Namespace: "test"}, + client.ObjectKey{Name: "foo", Namespace: metav1.NamespaceDefault}, []byte{}, metav1.OwnerReference{ APIVersion: clusterv1.GroupVersion.String(), @@ -191,7 +191,7 @@ func TestReconcileKubeconfigSecretAdoptsV1alpha2Secrets(t *testing.T) { kubeconfigSecret := &corev1.Secret{} secretName := client.ObjectKey{ - Namespace: "test", + Namespace: metav1.NamespaceDefault, Name: secret.Name(cluster.Name, secret.Kubeconfig), } g.Expect(r.Client.Get(ctx, secretName, kubeconfigSecret)).To(Succeed()) @@ -216,7 +216,7 @@ func TestReconcileKubeconfigSecretDoesNotAdoptsUserSecrets(t *testing.T) { }, ObjectMeta: metav1.ObjectMeta{ Name: "foo", - Namespace: "test", + Namespace: metav1.NamespaceDefault, }, Spec: clusterv1.ClusterSpec{ ControlPlaneEndpoint: clusterv1.APIEndpoint{Host: "test.local", Port: 8443}, @@ -230,7 +230,7 @@ func TestReconcileKubeconfigSecretDoesNotAdoptsUserSecrets(t *testing.T) { }, ObjectMeta: metav1.ObjectMeta{ Name: "foo", - Namespace: "test", + Namespace: metav1.NamespaceDefault, }, Spec: controlplanev1.KubeadmControlPlaneSpec{ Version: "v1.16.6", @@ -238,7 +238,7 @@ func TestReconcileKubeconfigSecretDoesNotAdoptsUserSecrets(t *testing.T) { } existingKubeconfigSecret := kubeconfig.GenerateSecretWithOwner( - client.ObjectKey{Name: "foo", Namespace: "test"}, + client.ObjectKey{Name: "foo", Namespace: metav1.NamespaceDefault}, []byte{}, metav1.OwnerReference{}, // user defined secrets are not owned by the cluster. ) @@ -255,7 +255,7 @@ func TestReconcileKubeconfigSecretDoesNotAdoptsUserSecrets(t *testing.T) { kubeconfigSecret := &corev1.Secret{} secretName := client.ObjectKey{ - Namespace: "test", + Namespace: metav1.NamespaceDefault, Name: secret.Name(cluster.Name, secret.Kubeconfig), } g.Expect(r.Client.Get(ctx, secretName, kubeconfigSecret)).To(Succeed()) @@ -274,7 +274,7 @@ func TestKubeadmControlPlaneReconciler_reconcileKubeconfig(t *testing.T) { }, ObjectMeta: metav1.ObjectMeta{ Name: "foo", - Namespace: "test", + Namespace: metav1.NamespaceDefault, }, Spec: clusterv1.ClusterSpec{ ControlPlaneEndpoint: clusterv1.APIEndpoint{Host: "test.local", Port: 8443}, @@ -288,7 +288,7 @@ func TestKubeadmControlPlaneReconciler_reconcileKubeconfig(t *testing.T) { }, ObjectMeta: metav1.ObjectMeta{ Name: "foo", - Namespace: "test", + Namespace: metav1.NamespaceDefault, }, Spec: controlplanev1.KubeadmControlPlaneSpec{ Version: "v1.16.6", @@ -299,7 +299,7 @@ func TestKubeadmControlPlaneReconciler_reconcileKubeconfig(t *testing.T) { g.Expect(clusterCerts.Generate()).To(Succeed()) caCert := clusterCerts.GetByPurpose(secret.ClusterCA) existingCACertSecret := caCert.AsSecret( - client.ObjectKey{Namespace: "test", Name: "foo"}, + client.ObjectKey{Namespace: metav1.NamespaceDefault, Name: "foo"}, *metav1.NewControllerRef(kcp, controlplanev1.GroupVersion.WithKind("KubeadmControlPlane")), ) @@ -314,7 +314,7 @@ func TestKubeadmControlPlaneReconciler_reconcileKubeconfig(t *testing.T) { kubeconfigSecret := &corev1.Secret{} secretName := client.ObjectKey{ - Namespace: "test", + Namespace: metav1.NamespaceDefault, Name: secret.Name(cluster.Name, secret.Kubeconfig), } g.Expect(r.Client.Get(ctx, secretName, kubeconfigSecret)).To(Succeed()) @@ -329,7 +329,7 @@ func TestCloneConfigsAndGenerateMachine(t *testing.T) { cluster := &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ Name: "foo", - Namespace: "test", + Namespace: metav1.NamespaceDefault, }, } @@ -413,7 +413,7 @@ func TestCloneConfigsAndGenerateMachineFail(t *testing.T) { cluster := &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ Name: "foo", - Namespace: "test", + Namespace: metav1.NamespaceDefault, }, } @@ -472,7 +472,7 @@ func TestCloneConfigsAndGenerateMachineFail(t *testing.T) { Status: corev1.ConditionFalse, Severity: clusterv1.ConditionSeverityError, Reason: controlplanev1.InfrastructureTemplateCloningFailedReason, - Message: "failed to retrieve GenericMachineTemplate external object \"test\"/\"something_invalid\": genericmachinetemplates.generic.io \"something_invalid\" not found", + Message: "failed to retrieve GenericMachineTemplate external object \"default\"/\"something_invalid\": genericmachinetemplates.generic.io \"something_invalid\" not found", })) } @@ -483,7 +483,7 @@ func TestKubeadmControlPlaneReconciler_generateMachine(t *testing.T) { cluster := &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ Name: "testCluster", - Namespace: "test", + Namespace: metav1.NamespaceDefault, }, } @@ -542,7 +542,7 @@ func TestKubeadmControlPlaneReconciler_generateKubeadmConfig(t *testing.T) { cluster := &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ Name: "testCluster", - Namespace: "test", + Namespace: metav1.NamespaceDefault, }, } diff --git a/controlplane/kubeadm/controllers/remediation_test.go b/controlplane/kubeadm/controllers/remediation_test.go index 6f92006411d6..89b0cfa3e228 100644 --- a/controlplane/kubeadm/controllers/remediation_test.go +++ b/controlplane/kubeadm/controllers/remediation_test.go @@ -48,6 +48,9 @@ func TestReconcileUnhealthyMachines(t *testing.T) { } ns, err := env.CreateNamespace(ctx, "ns1") g.Expect(err).ToNot(HaveOccurred()) + defer func() { + g.Expect(env.Cleanup(ctx, ns)).To(Succeed()) + }() t.Run("Remediation does not happen if there are no unhealthy machines", func(t *testing.T) { g := NewWithT(t) @@ -361,8 +364,6 @@ func TestReconcileUnhealthyMachines(t *testing.T) { g.Expect(env.Cleanup(ctx, m1, m2, m3, m4)).To(Succeed()) }) - - g.Expect(env.Cleanup(ctx, ns)).To(Succeed()) } func TestCanSafelyRemoveEtcdMember(t *testing.T) { @@ -371,6 +372,9 @@ func TestCanSafelyRemoveEtcdMember(t *testing.T) { ns, err := env.CreateNamespace(ctx, "ns1") g.Expect(err).ToNot(HaveOccurred()) + defer func() { + g.Expect(env.Cleanup(ctx, ns)).To(Succeed()) + }() t.Run("Can't safely remediate 1 machine CP", func(t *testing.T) { g := NewWithT(t) @@ -734,7 +738,6 @@ func TestCanSafelyRemoveEtcdMember(t *testing.T) { g.Expect(env.Cleanup(ctx, m1, m2, m3, m4, m5, m6, m7)).To(Succeed()) }) - g.Expect(env.Cleanup(ctx, ns)).To(Succeed()) } func nodes(machines collections.Machines) []string { diff --git a/controlplane/kubeadm/controllers/scale_test.go b/controlplane/kubeadm/controllers/scale_test.go index 939cbcb763d7..a31ab69ef9b7 100644 --- a/controlplane/kubeadm/controllers/scale_test.go +++ b/controlplane/kubeadm/controllers/scale_test.go @@ -40,7 +40,7 @@ import ( func TestKubeadmControlPlaneReconciler_initializeControlPlane(t *testing.T) { g := NewWithT(t) - cluster, kcp, genericMachineTemplate := createClusterWithControlPlane() + cluster, kcp, genericMachineTemplate := createClusterWithControlPlane(metav1.NamespaceDefault) fakeClient := newFakeClient(cluster.DeepCopy(), kcp.DeepCopy(), genericMachineTemplate.DeepCopy()) @@ -87,7 +87,7 @@ func TestKubeadmControlPlaneReconciler_scaleUpControlPlane(t *testing.T) { t.Run("creates a control plane Machine if preflight checks pass", func(t *testing.T) { g := NewWithT(t) - cluster, kcp, genericMachineTemplate := createClusterWithControlPlane() + cluster, kcp, genericMachineTemplate := createClusterWithControlPlane(metav1.NamespaceDefault) setKCPHealthy(kcp) initObjs := []client.Object{cluster.DeepCopy(), kcp.DeepCopy(), genericMachineTemplate.DeepCopy()} @@ -126,7 +126,7 @@ func TestKubeadmControlPlaneReconciler_scaleUpControlPlane(t *testing.T) { g.Expect(controlPlaneMachines.Items).To(HaveLen(3)) }) t.Run("does not create a control plane Machine if preflight checks fail", func(t *testing.T) { - cluster, kcp, genericMachineTemplate := createClusterWithControlPlane() + cluster, kcp, genericMachineTemplate := createClusterWithControlPlane(metav1.NamespaceDefault) initObjs := []client.Object{cluster.DeepCopy(), kcp.DeepCopy(), genericMachineTemplate.DeepCopy()} cluster.Spec.ControlPlaneEndpoint.Host = "nodomain.example.com" cluster.Spec.ControlPlaneEndpoint.Port = 6443 diff --git a/controlplane/kubeadm/controllers/status_test.go b/controlplane/kubeadm/controllers/status_test.go index 96737ad011a5..cab59cccd8c3 100644 --- a/controlplane/kubeadm/controllers/status_test.go +++ b/controlplane/kubeadm/controllers/status_test.go @@ -41,7 +41,7 @@ func TestKubeadmControlPlaneReconciler_updateStatusNoMachines(t *testing.T) { cluster := &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ Name: "foo", - Namespace: "test", + Namespace: metav1.NamespaceDefault, }, } @@ -97,7 +97,7 @@ func TestKubeadmControlPlaneReconciler_updateStatusAllMachinesNotReady(t *testin cluster := &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ Name: "foo", - Namespace: "test", + Namespace: metav1.NamespaceDefault, }, } @@ -161,7 +161,7 @@ func TestKubeadmControlPlaneReconciler_updateStatusAllMachinesReady(t *testing.T cluster := &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ - Namespace: "test", + Namespace: metav1.NamespaceDefault, Name: "foo", }, } @@ -235,7 +235,7 @@ func TestKubeadmControlPlaneReconciler_updateStatusMachinesReadyMixed(t *testing cluster := &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ Name: "foo", - Namespace: "test", + Namespace: metav1.NamespaceDefault, }, } @@ -307,7 +307,7 @@ func TestKubeadmControlPlaneReconciler_machinesCreatedIsIsTrueEvenWhenTheNodesAr cluster := &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ Name: "foo", - Namespace: "test", + Namespace: metav1.NamespaceDefault, }, } diff --git a/controlplane/kubeadm/controllers/upgrade_test.go b/controlplane/kubeadm/controllers/upgrade_test.go index 3210261ff39e..6979b438bfe1 100644 --- a/controlplane/kubeadm/controllers/upgrade_test.go +++ b/controlplane/kubeadm/controllers/upgrade_test.go @@ -42,7 +42,7 @@ const Host string = "nodomain.example.com" func TestKubeadmControlPlaneReconciler_RolloutStrategy_ScaleUp(t *testing.T) { g := NewWithT(t) - cluster, kcp, genericMachineTemplate := createClusterWithControlPlane() + cluster, kcp, genericMachineTemplate := createClusterWithControlPlane(metav1.NamespaceDefault) cluster.Spec.ControlPlaneEndpoint.Host = Host cluster.Spec.ControlPlaneEndpoint.Port = 6443 kcp.Spec.KubeadmConfigSpec.ClusterConfiguration = nil @@ -131,7 +131,7 @@ func TestKubeadmControlPlaneReconciler_RolloutStrategy_ScaleDown(t *testing.T) { version := "v1.17.3" g := NewWithT(t) - cluster, kcp, tmpl := createClusterWithControlPlane() + cluster, kcp, tmpl := createClusterWithControlPlane(metav1.NamespaceDefault) cluster.Spec.ControlPlaneEndpoint.Host = "nodomain.example.com1" cluster.Spec.ControlPlaneEndpoint.Port = 6443 kcp.Spec.Replicas = pointer.Int32Ptr(3) @@ -218,7 +218,7 @@ func machine(name string, opts ...machineOpt) *clusterv1.Machine { m := &clusterv1.Machine{ ObjectMeta: metav1.ObjectMeta{ Name: name, - Namespace: "default", + Namespace: metav1.NamespaceDefault, }, } for _, opt := range opts { diff --git a/controlplane/kubeadm/internal/cluster_test.go b/controlplane/kubeadm/internal/cluster_test.go index b6a0e9d37987..3aeeadc0c779 100644 --- a/controlplane/kubeadm/internal/cluster_test.go +++ b/controlplane/kubeadm/internal/cluster_test.go @@ -54,7 +54,7 @@ func TestGetMachinesForCluster(t *testing.T) { }} cluster := &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ - Namespace: "my-namespace", + Namespace: metav1.NamespaceDefault, Name: "my-cluster", }, } @@ -253,7 +253,7 @@ func machineListForTestGetMachinesForCluster() *clusterv1.MachineList { TypeMeta: metav1.TypeMeta{}, ObjectMeta: metav1.ObjectMeta{ Name: name, - Namespace: "my-namespace", + Namespace: metav1.NamespaceDefault, Labels: map[string]string{ clusterv1.ClusterLabelName: "my-cluster", },