From 4ba1527a6acca20d979a0a73a6dc47212bc1b281 Mon Sep 17 00:00:00 2001 From: Danil-Grigorev Date: Thu, 31 Oct 2024 19:17:34 +0100 Subject: [PATCH 1/8] Use namespace of the reference on Get Signed-off-by: Danil-Grigorev --- controllers/external/util.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/controllers/external/util.go b/controllers/external/util.go index 4efdb9c133f2..c064885518cd 100644 --- a/controllers/external/util.go +++ b/controllers/external/util.go @@ -31,7 +31,7 @@ import ( ) // Get uses the client and reference to get an external, unstructured object. -func Get(ctx context.Context, c client.Reader, ref *corev1.ObjectReference, namespace string) (*unstructured.Unstructured, error) { +func Get(ctx context.Context, c client.Reader, ref *corev1.ObjectReference, _ string) (*unstructured.Unstructured, error) { if ref == nil { return nil, errors.Errorf("cannot get object - object reference not set") } @@ -39,9 +39,9 @@ func Get(ctx context.Context, c client.Reader, ref *corev1.ObjectReference, name obj.SetAPIVersion(ref.APIVersion) obj.SetKind(ref.Kind) obj.SetName(ref.Name) - key := client.ObjectKey{Name: obj.GetName(), Namespace: namespace} - if err := c.Get(ctx, key, obj); err != nil { - return nil, errors.Wrapf(err, "failed to retrieve %s external object %q/%q", obj.GetKind(), key.Namespace, key.Name) + obj.SetNamespace(ref.Namespace) + if err := c.Get(ctx, client.ObjectKeyFromObject(obj), obj); err != nil { + return nil, errors.Wrapf(err, "failed to retrieve %s external object %q/%q", obj.GetKind(), ref.Namespace, ref.Name) } return obj, nil } From 25a4bd3a40204fbb4422772803f91db3c531e9fa Mon Sep 17 00:00:00 2001 From: Danil-Grigorev Date: Fri, 1 Nov 2024 10:31:43 +0100 Subject: [PATCH 2/8] Updating tests Signed-off-by: Danil-Grigorev --- .../machinepool_controller_phases_test.go | 15 +++++++++++++ .../machinepool_controller_test.go | 9 ++++++++ .../cluster/cluster_controller_phases_test.go | 5 +++++ .../machine_controller_noderef_test.go | 4 ++++ .../machine/machine_controller_phases_test.go | 10 +++++++++ .../machine/machine_controller_status_test.go | 21 +++++++++++++++++++ .../machine/machine_controller_test.go | 16 ++++++++++++++ .../machinedeployment_controller_test.go | 4 ++++ .../machinehealthcheck_controller.go | 1 + .../machinehealthcheck_controller_test.go | 4 ++++ .../machineset/machineset_controller_test.go | 2 ++ 11 files changed, 91 insertions(+) diff --git a/exp/internal/controllers/machinepool_controller_phases_test.go b/exp/internal/controllers/machinepool_controller_phases_test.go index 37a53341e3c8..8d329910913c 100644 --- a/exp/internal/controllers/machinepool_controller_phases_test.go +++ b/exp/internal/controllers/machinepool_controller_phases_test.go @@ -78,12 +78,14 @@ func TestReconcileMachinePoolPhases(t *testing.T) { APIVersion: builder.BootstrapGroupVersion.String(), Kind: builder.TestBootstrapConfigKind, Name: "bootstrap-config1", + Namespace: metav1.NamespaceDefault, }, }, InfrastructureRef: corev1.ObjectReference{ APIVersion: builder.InfrastructureGroupVersion.String(), Kind: builder.TestInfrastructureMachineTemplateKind, Name: "infra-config1", + Namespace: metav1.NamespaceDefault, }, }, }, @@ -808,6 +810,7 @@ func TestReconcileMachinePoolBootstrap(t *testing.T) { APIVersion: builder.BootstrapGroupVersion.String(), Kind: builder.TestBootstrapConfigKind, Name: "bootstrap-config1", + Namespace: metav1.NamespaceDefault, }, }, }, @@ -949,6 +952,7 @@ func TestReconcileMachinePoolBootstrap(t *testing.T) { APIVersion: builder.BootstrapGroupVersion.String(), Kind: builder.TestBootstrapConfigKind, Name: "bootstrap-config1", + Namespace: metav1.NamespaceDefault, }, DataSecretName: ptr.To("data"), }, @@ -1032,6 +1036,7 @@ func TestReconcileMachinePoolBootstrap(t *testing.T) { APIVersion: builder.BootstrapGroupVersion.String(), Kind: builder.TestBootstrapConfigKind, Name: "bootstrap-config1", + Namespace: metav1.NamespaceDefault, }, DataSecretName: ptr.To("data"), }, @@ -1107,12 +1112,14 @@ func TestReconcileMachinePoolInfrastructure(t *testing.T) { APIVersion: builder.BootstrapGroupVersion.String(), Kind: builder.TestBootstrapConfigKind, Name: "bootstrap-config1", + Namespace: metav1.NamespaceDefault, }, }, InfrastructureRef: corev1.ObjectReference{ APIVersion: builder.InfrastructureGroupVersion.String(), Kind: builder.TestInfrastructureMachineTemplateKind, Name: "infra-config1", + Namespace: metav1.NamespaceDefault, }, }, }, @@ -1186,12 +1193,14 @@ func TestReconcileMachinePoolInfrastructure(t *testing.T) { APIVersion: builder.BootstrapGroupVersion.String(), Kind: builder.TestBootstrapConfigKind, Name: "bootstrap-config1", + Namespace: metav1.NamespaceDefault, }, }, InfrastructureRef: corev1.ObjectReference{ APIVersion: builder.InfrastructureGroupVersion.String(), Kind: builder.TestInfrastructureMachineTemplateKind, Name: "infra-config1", + Namespace: metav1.NamespaceDefault, }, }, }, @@ -1245,12 +1254,14 @@ func TestReconcileMachinePoolInfrastructure(t *testing.T) { APIVersion: builder.BootstrapGroupVersion.String(), Kind: builder.TestBootstrapConfigKind, Name: "bootstrap-config1", + Namespace: metav1.NamespaceDefault, }, }, InfrastructureRef: corev1.ObjectReference{ APIVersion: builder.InfrastructureGroupVersion.String(), Kind: builder.TestInfrastructureMachineTemplateKind, Name: "infra-config1", + Namespace: metav1.NamespaceDefault, }, }, }, @@ -1755,12 +1766,14 @@ func TestReconcileMachinePoolScaleToFromZero(t *testing.T) { APIVersion: builder.BootstrapGroupVersion.String(), Kind: builder.TestBootstrapConfigKind, Name: "bootstrap-config1", + Namespace: ns.Name, }, }, InfrastructureRef: corev1.ObjectReference{ APIVersion: builder.InfrastructureGroupVersion.String(), Kind: builder.TestInfrastructureMachineTemplateKind, Name: "infra-config1", + Namespace: ns.Name, }, }, }, @@ -2181,12 +2194,14 @@ func getMachinePool(replicas int, mpName, clusterName, nsName string) expv1.Mach APIVersion: builder.BootstrapGroupVersion.String(), Kind: builder.GenericBootstrapConfigKind, Name: "bootstrap-config1", + Namespace: nsName, }, }, InfrastructureRef: corev1.ObjectReference{ APIVersion: builder.InfrastructureGroupVersion.String(), Kind: builder.GenericInfrastructureMachineKind, Name: "infra-config1", + Namespace: nsName, }, }, }, diff --git a/exp/internal/controllers/machinepool_controller_test.go b/exp/internal/controllers/machinepool_controller_test.go index 7f0897c6497a..d1c930630a86 100644 --- a/exp/internal/controllers/machinepool_controller_test.go +++ b/exp/internal/controllers/machinepool_controller_test.go @@ -359,6 +359,7 @@ func TestReconcileMachinePoolRequest(t *testing.T) { APIVersion: builder.InfrastructureGroupVersion.String(), Kind: builder.TestInfrastructureMachinePoolKind, Name: "infra-config1", + Namespace: metav1.NamespaceDefault, }, Bootstrap: clusterv1.Bootstrap{DataSecretName: ptr.To("data")}, }, @@ -406,6 +407,7 @@ func TestReconcileMachinePoolRequest(t *testing.T) { APIVersion: builder.InfrastructureGroupVersion.String(), Kind: builder.TestInfrastructureMachinePoolKind, Name: "infra-config1-already-deleted", // Use an InfrastructureMachinePool that doesn't exist, so reconcileDelete doesn't get stuck on deletion + Namespace: metav1.NamespaceDefault, }, Bootstrap: clusterv1.Bootstrap{DataSecretName: ptr.To("data")}, NodeDeletionTimeout: &metav1.Duration{Duration: 10 * time.Minute}, @@ -468,6 +470,7 @@ func TestReconcileMachinePoolRequest(t *testing.T) { APIVersion: builder.InfrastructureGroupVersion.String(), Kind: builder.TestInfrastructureMachinePoolKind, Name: "infra-config1-already-deleted", // Use an InfrastructureMachinePool that doesn't exist, so reconcileDelete doesn't get stuck on deletion + Namespace: metav1.NamespaceDefault, }, Bootstrap: clusterv1.Bootstrap{DataSecretName: ptr.To("data")}, NodeDeletionTimeout: &metav1.Duration{Duration: 10 * time.Minute}, @@ -530,6 +533,7 @@ func TestReconcileMachinePoolRequest(t *testing.T) { APIVersion: builder.InfrastructureGroupVersion.String(), Kind: builder.TestInfrastructureMachinePoolKind, Name: "infra-config1-already-deleted", // Use an InfrastructureMachinePool that doesn't exist, so reconcileDelete doesn't get stuck on deletion + Namespace: metav1.NamespaceDefault, }, Bootstrap: clusterv1.Bootstrap{DataSecretName: ptr.To("data")}, NodeDeletionTimeout: &metav1.Duration{Duration: 10 * time.Second}, // timeout passed @@ -764,12 +768,14 @@ func TestReconcileMachinePoolDeleteExternal(t *testing.T) { APIVersion: builder.InfrastructureGroupVersion.String(), Kind: builder.TestInfrastructureMachineTemplateKind, Name: "delete-infra", + Namespace: metav1.NamespaceDefault, }, Bootstrap: clusterv1.Bootstrap{ ConfigRef: &corev1.ObjectReference{ APIVersion: builder.BootstrapGroupVersion.String(), Kind: builder.TestBootstrapConfigKind, Name: "delete-bootstrap", + Namespace: metav1.NamespaceDefault, }, }, }, @@ -867,6 +873,7 @@ func TestRemoveMachinePoolFinalizerAfterDeleteReconcile(t *testing.T) { APIVersion: builder.InfrastructureGroupVersion.String(), Kind: builder.TestInfrastructureMachineTemplateKind, Name: "infra-config1", + Namespace: metav1.NamespaceDefault, }, Bootstrap: clusterv1.Bootstrap{DataSecretName: ptr.To("data")}, }, @@ -960,12 +967,14 @@ func TestMachinePoolConditions(t *testing.T) { APIVersion: builder.InfrastructureGroupVersion.String(), Kind: builder.TestInfrastructureMachineTemplateKind, Name: "infra1", + Namespace: metav1.NamespaceDefault, }, Bootstrap: clusterv1.Bootstrap{ ConfigRef: &corev1.ObjectReference{ APIVersion: builder.BootstrapGroupVersion.String(), Kind: builder.TestBootstrapConfigKind, Name: "bootstrap1", + Namespace: metav1.NamespaceDefault, }, }, }, diff --git a/internal/controllers/cluster/cluster_controller_phases_test.go b/internal/controllers/cluster/cluster_controller_phases_test.go index b5d96b92cd9a..2c53056446d1 100644 --- a/internal/controllers/cluster/cluster_controller_phases_test.go +++ b/internal/controllers/cluster/cluster_controller_phases_test.go @@ -59,6 +59,7 @@ func TestClusterReconcileInfrastructure(t *testing.T) { APIVersion: "infrastructure.cluster.x-k8s.io/v1beta1", Kind: "GenericInfrastructureMachine", Name: "test", + Namespace: "test-namespace", }, }, } @@ -75,6 +76,7 @@ func TestClusterReconcileInfrastructure(t *testing.T) { APIVersion: "infrastructure.cluster.x-k8s.io/v1beta1", Kind: "GenericInfrastructureMachine", Name: "test", + Namespace: "test-namespace", }, }, } @@ -305,6 +307,7 @@ func TestClusterReconcileControlPlane(t *testing.T) { APIVersion: "controlplane.cluster.x-k8s.io/v1beta1", Kind: "GenericControlPlane", Name: "test", + Namespace: "test-namespace", }, }, } @@ -321,6 +324,7 @@ func TestClusterReconcileControlPlane(t *testing.T) { APIVersion: "controlplane.cluster.x-k8s.io/v1beta1", Kind: "GenericControlPlane", Name: "test", + Namespace: "test-namespace", }, }, } @@ -836,6 +840,7 @@ func TestClusterReconcilePhases_reconcileFailureDomains(t *testing.T) { APIVersion: "infrastructure.cluster.x-k8s.io/v1beta1", Kind: "GenericInfrastructureCluster", Name: "test", + Namespace: "test-namespace", }, }, } diff --git a/internal/controllers/machine/machine_controller_noderef_test.go b/internal/controllers/machine/machine_controller_noderef_test.go index ca4f4ce7b92c..ef56f947ebc6 100644 --- a/internal/controllers/machine/machine_controller_noderef_test.go +++ b/internal/controllers/machine/machine_controller_noderef_test.go @@ -412,12 +412,14 @@ func TestNodeLabelSync(t *testing.T) { APIVersion: "bootstrap.cluster.x-k8s.io/v1beta1", Kind: "GenericBootstrapConfig", Name: "bootstrap-config1", + Namespace: metav1.NamespaceDefault, }, }, InfrastructureRef: corev1.ObjectReference{ APIVersion: "infrastructure.cluster.x-k8s.io/v1beta1", Kind: "GenericInfrastructureMachine", Name: "infra-config1", + Namespace: metav1.NamespaceDefault, }, }, } @@ -450,6 +452,8 @@ func TestNodeLabelSync(t *testing.T) { machine := defaultMachine.DeepCopy() machine.Namespace = ns.Name + machine.Spec.Bootstrap.ConfigRef.Namespace = ns.Name + machine.Spec.InfrastructureRef.Namespace = ns.Name machine.Spec.ProviderID = ptr.To(nodeProviderID) // Set Machine labels. diff --git a/internal/controllers/machine/machine_controller_phases_test.go b/internal/controllers/machine/machine_controller_phases_test.go index 57b9f74ccaeb..db7ca27ca87c 100644 --- a/internal/controllers/machine/machine_controller_phases_test.go +++ b/internal/controllers/machine/machine_controller_phases_test.go @@ -58,6 +58,7 @@ func TestReconcileBootstrap(t *testing.T) { APIVersion: "bootstrap.cluster.x-k8s.io/v1beta1", Kind: "GenericBootstrapConfig", Name: "bootstrap-config1", + Namespace: metav1.NamespaceDefault, }, }, }, @@ -219,6 +220,7 @@ func TestReconcileBootstrap(t *testing.T) { APIVersion: "bootstrap.cluster.x-k8s.io/v1beta1", Kind: "GenericBootstrapConfig", Name: "bootstrap-config1", + Namespace: metav1.NamespaceDefault, }, DataSecretName: ptr.To("secret-data"), }, @@ -263,6 +265,7 @@ func TestReconcileBootstrap(t *testing.T) { APIVersion: "bootstrap.cluster.x-k8s.io/v1beta1", Kind: "GenericBootstrapConfig", Name: "bootstrap-config1", + Namespace: metav1.NamespaceDefault, }, DataSecretName: ptr.To("secret-data"), }, @@ -342,6 +345,7 @@ func TestReconcileInfrastructure(t *testing.T) { APIVersion: "infrastructure.cluster.x-k8s.io/v1beta1", Kind: "GenericInfrastructureMachine", Name: "infra-config1", + Namespace: metav1.NamespaceDefault, }, }, } @@ -622,6 +626,7 @@ func TestReconcileInfrastructure(t *testing.T) { APIVersion: "infrastructure.cluster.x-k8s.io/v1beta1", Kind: "GenericInfrastructureMachine", Name: "infra-config1", + Namespace: metav1.NamespaceDefault, }, ProviderID: ptr.To("test://something"), FailureDomain: ptr.To("something"), @@ -683,6 +688,7 @@ func TestReconcileInfrastructure(t *testing.T) { APIVersion: "infrastructure.cluster.x-k8s.io/v1beta1", Kind: "GenericInfrastructureMachine", Name: "infra-config1", + Namespace: metav1.NamespaceDefault, }, ProviderID: ptr.To("test://something"), FailureDomain: ptr.To("something"), @@ -744,6 +750,7 @@ func TestReconcileInfrastructure(t *testing.T) { APIVersion: "infrastructure.cluster.x-k8s.io/v1beta1", Kind: "GenericInfrastructureMachine", Name: "infra-config1", + Namespace: metav1.NamespaceDefault, }, }, Status: clusterv1.MachineStatus{ @@ -772,6 +779,7 @@ func TestReconcileInfrastructure(t *testing.T) { APIVersion: "infrastructure.cluster.x-k8s.io/v1beta1", Kind: "GenericInfrastructureMachine", Name: "infra-config1", + Namespace: metav1.NamespaceDefault, }, }, Status: clusterv1.MachineStatus{ @@ -802,6 +810,7 @@ func TestReconcileInfrastructure(t *testing.T) { APIVersion: "infrastructure.cluster.x-k8s.io/v1beta1", Kind: "GenericInfrastructureMachine", Name: "infra-config1", + Namespace: metav1.NamespaceDefault, }, }, Status: clusterv1.MachineStatus{ @@ -828,6 +837,7 @@ func TestReconcileInfrastructure(t *testing.T) { APIVersion: "infrastructure.cluster.x-k8s.io/v1beta1", Kind: "GenericInfrastructureMachine", Name: "infra-config1", + Namespace: metav1.NamespaceDefault, }, }, Status: clusterv1.MachineStatus{ diff --git a/internal/controllers/machine/machine_controller_status_test.go b/internal/controllers/machine/machine_controller_status_test.go index 439077b016cf..edec3d4107d1 100644 --- a/internal/controllers/machine/machine_controller_status_test.go +++ b/internal/controllers/machine/machine_controller_status_test.go @@ -49,6 +49,7 @@ func TestSetBootstrapReadyCondition(t *testing.T) { APIVersion: "bootstrap.cluster.x-k8s.io/v1beta1", Kind: "GenericBootstrapConfig", Name: "bootstrap-config1", + Namespace: metav1.NamespaceDefault, }, }, }, @@ -297,6 +298,7 @@ func TestSetInfrastructureReadyCondition(t *testing.T) { APIVersion: "infrastructure.cluster.x-k8s.io/v1beta1", Kind: "GenericInfrastructureMachine", Name: "infra-machine1", + Namespace: metav1.NamespaceDefault, }, }, } @@ -646,6 +648,7 @@ func TestSetNodeHealthyAndReadyConditions(t *testing.T) { APIVersion: "infrastructure.cluster.x-k8s.io/v1beta1", Kind: "GenericInfrastructureMachine", Name: "infra-machine1", + Namespace: metav1.NamespaceDefault, }, }, } @@ -1757,12 +1760,14 @@ func TestReconcileMachinePhases(t *testing.T) { APIVersion: "bootstrap.cluster.x-k8s.io/v1beta1", Kind: "GenericBootstrapConfig", Name: "bootstrap-config1", + Namespace: metav1.NamespaceDefault, }, }, InfrastructureRef: corev1.ObjectReference{ APIVersion: "infrastructure.cluster.x-k8s.io/v1beta1", Kind: "GenericInfrastructureMachine", Name: "infra-config1", + Namespace: metav1.NamespaceDefault, }, }, } @@ -1811,6 +1816,8 @@ func TestReconcileMachinePhases(t *testing.T) { infraMachine.SetNamespace(ns.Name) machine := defaultMachine.DeepCopy() machine.Namespace = ns.Name + machine.Spec.InfrastructureRef.Namespace = ns.Name + machine.Spec.Bootstrap.ConfigRef.Namespace = ns.Name g.Expect(env.Create(ctx, cluster)).To(Succeed()) defaultKubeconfigSecret = kubeconfig.GenerateSecret(cluster, kubeconfig.FromEnvTestConfig(env.Config, cluster)) @@ -1863,6 +1870,8 @@ func TestReconcileMachinePhases(t *testing.T) { infraMachine.SetNamespace(ns.Name) machine := defaultMachine.DeepCopy() machine.Namespace = ns.Name + machine.Spec.InfrastructureRef.Namespace = ns.Name + machine.Spec.Bootstrap.ConfigRef.Namespace = ns.Name g.Expect(env.Create(ctx, cluster)).To(Succeed()) defaultKubeconfigSecret = kubeconfig.GenerateSecret(cluster, kubeconfig.FromEnvTestConfig(env.Config, cluster)) @@ -1906,6 +1915,8 @@ func TestReconcileMachinePhases(t *testing.T) { infraMachine.SetNamespace(ns.Name) machine := defaultMachine.DeepCopy() machine.Namespace = ns.Name + machine.Spec.InfrastructureRef.Namespace = ns.Name + machine.Spec.Bootstrap.ConfigRef.Namespace = ns.Name g.Expect(env.Create(ctx, cluster)).To(Succeed()) defaultKubeconfigSecret = kubeconfig.GenerateSecret(cluster, kubeconfig.FromEnvTestConfig(env.Config, cluster)) @@ -1966,6 +1977,8 @@ func TestReconcileMachinePhases(t *testing.T) { g.Expect(unstructured.SetNestedField(infraMachine.Object, "us-east-2a", "spec", "failureDomain")).To(Succeed()) machine := defaultMachine.DeepCopy() machine.Namespace = ns.Name + machine.Spec.InfrastructureRef.Namespace = ns.Name + machine.Spec.Bootstrap.ConfigRef.Namespace = ns.Name // Create Node. node := &corev1.Node{ @@ -2055,6 +2068,8 @@ func TestReconcileMachinePhases(t *testing.T) { g.Expect(unstructured.SetNestedField(infraMachine.Object, nodeProviderID, "spec", "providerID")).To(Succeed()) machine := defaultMachine.DeepCopy() machine.Namespace = ns.Name + machine.Spec.InfrastructureRef.Namespace = ns.Name + machine.Spec.Bootstrap.ConfigRef.Namespace = ns.Name // Create Node. node := &corev1.Node{ @@ -2133,6 +2148,8 @@ func TestReconcileMachinePhases(t *testing.T) { g.Expect(unstructured.SetNestedField(infraMachine.Object, nodeProviderID, "spec", "providerID")).To(Succeed()) machine := defaultMachine.DeepCopy() machine.Namespace = ns.Name + machine.Spec.InfrastructureRef.Namespace = ns.Name + machine.Spec.Bootstrap.ConfigRef.Namespace = ns.Name // Create Node. node := &corev1.Node{ @@ -2210,6 +2227,8 @@ func TestReconcileMachinePhases(t *testing.T) { g.Expect(unstructured.SetNestedField(infraMachine.Object, nodeProviderID, "spec", "providerID")).To(Succeed()) machine := defaultMachine.DeepCopy() machine.Namespace = ns.Name + machine.Spec.InfrastructureRef.Namespace = ns.Name + machine.Spec.Bootstrap.ConfigRef.Namespace = ns.Name // Set Machine ProviderID. machine.Spec.ProviderID = ptr.To(nodeProviderID) @@ -2272,6 +2291,8 @@ func TestReconcileMachinePhases(t *testing.T) { g.Expect(unstructured.SetNestedField(infraMachine.Object, nodeProviderID, "spec", "providerID")).To(Succeed()) machine := defaultMachine.DeepCopy() machine.Namespace = ns.Name + machine.Spec.InfrastructureRef.Namespace = ns.Name + machine.Spec.Bootstrap.ConfigRef.Namespace = ns.Name // Create Node. node := &corev1.Node{ diff --git a/internal/controllers/machine/machine_controller_test.go b/internal/controllers/machine/machine_controller_test.go index cefde3315aa9..0a0d37572030 100644 --- a/internal/controllers/machine/machine_controller_test.go +++ b/internal/controllers/machine/machine_controller_test.go @@ -156,12 +156,14 @@ func TestWatches(t *testing.T) { APIVersion: "infrastructure.cluster.x-k8s.io/v1beta1", Kind: "GenericInfrastructureMachine", Name: "infra-config1", + Namespace: ns.Name, }, Bootstrap: clusterv1.Bootstrap{ ConfigRef: &corev1.ObjectReference{ APIVersion: "bootstrap.cluster.x-k8s.io/v1beta1", Kind: "GenericBootstrapConfig", Name: "bootstrap-config-machinereconcile", + Namespace: ns.Name, }, }, }, @@ -292,12 +294,14 @@ func TestWatchesDelete(t *testing.T) { APIVersion: "infrastructure.cluster.x-k8s.io/v1beta1", Kind: "GenericInfrastructureMachine", Name: "infra-config1", + Namespace: ns.Name, }, Bootstrap: clusterv1.Bootstrap{ ConfigRef: &corev1.ObjectReference{ APIVersion: "bootstrap.cluster.x-k8s.io/v1beta1", Kind: "GenericBootstrapConfig", Name: "bootstrap-config-machinereconcile", + Namespace: ns.Name, }, }, }, @@ -442,12 +446,14 @@ func TestMachine_Reconcile(t *testing.T) { APIVersion: "infrastructure.cluster.x-k8s.io/v1beta1", Kind: "GenericInfrastructureMachine", Name: "infra-config1", + Namespace: ns.Name, }, Bootstrap: clusterv1.Bootstrap{ ConfigRef: &corev1.ObjectReference{ APIVersion: "bootstrap.cluster.x-k8s.io/v1beta1", Kind: "GenericBootstrapConfig", Name: "bootstrap-config-machinereconcile", + Namespace: ns.Name, }, }, }, @@ -861,6 +867,7 @@ func TestReconcileRequest(t *testing.T) { APIVersion: "infrastructure.cluster.x-k8s.io/v1beta1", Kind: "GenericInfrastructureMachine", Name: "infra-config1", + Namespace: metav1.NamespaceDefault, }, Bootstrap: clusterv1.Bootstrap{DataSecretName: ptr.To("data")}, }, @@ -890,6 +897,7 @@ func TestReconcileRequest(t *testing.T) { APIVersion: "infrastructure.cluster.x-k8s.io/v1beta1", Kind: "GenericInfrastructureMachine", Name: "infra-config1", + Namespace: metav1.NamespaceDefault, }, Bootstrap: clusterv1.Bootstrap{DataSecretName: ptr.To("data")}, }, @@ -923,6 +931,7 @@ func TestReconcileRequest(t *testing.T) { APIVersion: "infrastructure.cluster.x-k8s.io/v1beta1", Kind: "GenericInfrastructureMachine", Name: "infra-config1", + Namespace: metav1.NamespaceDefault, }, Bootstrap: clusterv1.Bootstrap{DataSecretName: ptr.To("data")}, }, @@ -1041,12 +1050,14 @@ func TestMachineConditions(t *testing.T) { APIVersion: "infrastructure.cluster.x-k8s.io/v1beta1", Kind: "GenericInfrastructureMachine", Name: "infra-config1", + Namespace: metav1.NamespaceDefault, }, Bootstrap: clusterv1.Bootstrap{ ConfigRef: &corev1.ObjectReference{ APIVersion: "bootstrap.cluster.x-k8s.io/v1beta1", Kind: "GenericBootstrapConfig", Name: "bootstrap-config1", + Namespace: metav1.NamespaceDefault, }, }, }, @@ -1289,6 +1300,7 @@ func TestRemoveMachineFinalizerAfterDeleteReconcile(t *testing.T) { APIVersion: "infrastructure.cluster.x-k8s.io/v1beta1", Kind: "GenericInfrastructureMachine", Name: "infra-config1", + Namespace: metav1.NamespaceDefault, }, Bootstrap: clusterv1.Bootstrap{DataSecretName: ptr.To("data")}, }, @@ -2921,12 +2933,14 @@ func TestNodeToMachine(t *testing.T) { APIVersion: "infrastructure.cluster.x-k8s.io/v1beta1", Kind: "GenericInfrastructureMachine", Name: "infra-config1", + Namespace: ns.Name, }, Bootstrap: clusterv1.Bootstrap{ ConfigRef: &corev1.ObjectReference{ APIVersion: "bootstrap.cluster.x-k8s.io/v1beta1", Kind: "GenericBootstrapConfig", Name: "bootstrap-config-machinereconcile", + Namespace: ns.Name, }, }, }, @@ -3098,6 +3112,7 @@ func TestNodeDeletion(t *testing.T) { APIVersion: "infrastructure.cluster.x-k8s.io/v1beta1", Kind: "GenericInfrastructureMachine", Name: "infra-config1", + Namespace: metav1.NamespaceDefault, }, Bootstrap: clusterv1.Bootstrap{DataSecretName: ptr.To("data")}, }, @@ -3294,6 +3309,7 @@ func TestNodeDeletionWithoutNodeRefFallback(t *testing.T) { APIVersion: "infrastructure.cluster.x-k8s.io/v1beta1", Kind: "GenericInfrastructureMachine", Name: "infra-config1", + Namespace: metav1.NamespaceDefault, }, Bootstrap: clusterv1.Bootstrap{DataSecretName: ptr.To("data")}, ProviderID: ptr.To("test://id-1"), diff --git a/internal/controllers/machinedeployment/machinedeployment_controller_test.go b/internal/controllers/machinedeployment/machinedeployment_controller_test.go index e18ff8ee4d67..0bcd742fedca 100644 --- a/internal/controllers/machinedeployment/machinedeployment_controller_test.go +++ b/internal/controllers/machinedeployment/machinedeployment_controller_test.go @@ -127,6 +127,7 @@ func TestMachineDeploymentReconciler(t *testing.T) { APIVersion: "infrastructure.cluster.x-k8s.io/v1beta1", Kind: "GenericInfrastructureMachineTemplate", Name: "md-template", + Namespace: namespace.Name, }, Bootstrap: clusterv1.Bootstrap{ DataSecretName: ptr.To("data-secret-name"), @@ -310,6 +311,7 @@ func TestMachineDeploymentReconciler(t *testing.T) { APIVersion: "infrastructure.cluster.x-k8s.io/v1beta1", Kind: "GenericInfrastructureMachineTemplate", Name: "md-template-2", + Namespace: namespace.Name, } modifyFunc = func(d *clusterv1.MachineDeployment) { d.Spec.Template.Spec.InfrastructureRef = infraTmpl2Ref } g.Expect(updateMachineDeployment(ctx, env, deployment, modifyFunc)).To(Succeed()) @@ -549,6 +551,7 @@ func TestMachineDeploymentReconciler_CleanUpManagedFieldsForSSAAdoption(t *testi APIVersion: "infrastructure.cluster.x-k8s.io/v1beta1", Kind: "GenericInfrastructureMachineTemplate", Name: "md-template", + Namespace: namespace.Name, }, Bootstrap: clusterv1.Bootstrap{ DataSecretName: ptr.To("data-secret-name"), @@ -619,6 +622,7 @@ func TestMachineDeploymentReconciler_CleanUpManagedFieldsForSSAAdoption(t *testi APIVersion: "infrastructure.cluster.x-k8s.io/v1beta1", Kind: "GenericInfrastructureMachineTemplate", Name: "md-template", + Namespace: testCluster.Namespace, }, Bootstrap: clusterv1.Bootstrap{ DataSecretName: ptr.To("data-secret-name"), diff --git a/internal/controllers/machinehealthcheck/machinehealthcheck_controller.go b/internal/controllers/machinehealthcheck/machinehealthcheck_controller.go index 968ad03484aa..8eb091f5a37c 100644 --- a/internal/controllers/machinehealthcheck/machinehealthcheck_controller.go +++ b/internal/controllers/machinehealthcheck/machinehealthcheck_controller.go @@ -730,6 +730,7 @@ func (r *Reconciler) getExternalRemediationRequest(ctx context.Context, m *clust APIVersion: m.Spec.RemediationTemplate.APIVersion, Kind: strings.TrimSuffix(m.Spec.RemediationTemplate.Kind, clusterv1.TemplateSuffix), Name: machineName, + Namespace: m.Namespace, } remediationReq, err := external.Get(ctx, r.Client, remediationRef, m.Namespace) if err != nil { diff --git a/internal/controllers/machinehealthcheck/machinehealthcheck_controller_test.go b/internal/controllers/machinehealthcheck/machinehealthcheck_controller_test.go index d7d1b449f4cc..754766767642 100644 --- a/internal/controllers/machinehealthcheck/machinehealthcheck_controller_test.go +++ b/internal/controllers/machinehealthcheck/machinehealthcheck_controller_test.go @@ -1328,6 +1328,7 @@ func TestMachineHealthCheck_Reconcile(t *testing.T) { APIVersion: "infrastructure.cluster.x-k8s.io/v1beta1", Kind: "GenericInfrastructureMachineTemplate", Name: infraTmpl.GetName(), + Namespace: mhc.Namespace, }, }, }, @@ -1536,6 +1537,7 @@ func TestMachineHealthCheck_Reconcile(t *testing.T) { APIVersion: builder.RemediationGroupVersion.String(), Kind: "GenericExternalRemediationTemplate", Name: infraRemediationTmpl.GetName(), + Namespace: cluster.Namespace, } mhc := newMachineHealthCheck(cluster.Namespace, cluster.Name) @@ -1690,6 +1692,7 @@ func TestMachineHealthCheck_Reconcile(t *testing.T) { APIVersion: builder.RemediationGroupVersion.String(), Kind: "GenericExternalRemediationTemplate", Name: infraRemediationTmpl.GetName(), + Namespace: ns.Name, } mhc := newMachineHealthCheck(cluster.Namespace, cluster.Name) @@ -2530,6 +2533,7 @@ func createMachinesWithNodes( APIVersion: infraMachine.GetAPIVersion(), Kind: infraMachine.GetKind(), Name: infraMachine.GetName(), + Namespace: infraMachine.GetNamespace(), } g.Expect(env.Create(ctx, machine)).To(Succeed()) fmt.Printf("machine created: %s\n", machine.GetName()) diff --git a/internal/controllers/machineset/machineset_controller_test.go b/internal/controllers/machineset/machineset_controller_test.go index 1e937ac0459e..cf7ccc39623d 100644 --- a/internal/controllers/machineset/machineset_controller_test.go +++ b/internal/controllers/machineset/machineset_controller_test.go @@ -106,12 +106,14 @@ func TestMachineSetReconciler(t *testing.T) { APIVersion: "bootstrap.cluster.x-k8s.io/v1beta1", Kind: "GenericBootstrapConfigTemplate", Name: "ms-template", + Namespace: namespace.Name, }, }, InfrastructureRef: corev1.ObjectReference{ APIVersion: "infrastructure.cluster.x-k8s.io/v1beta1", Kind: "GenericInfrastructureMachineTemplate", Name: "ms-template", + Namespace: namespace.Name, }, NodeDrainTimeout: duration10m, NodeDeletionTimeout: duration10m, From f5ecffe1a96a95538a688854503e1347bd4390ea Mon Sep 17 00:00:00 2001 From: Danil-Grigorev Date: Fri, 1 Nov 2024 11:07:19 +0100 Subject: [PATCH 3/8] Update references usage across the code Signed-off-by: Danil-Grigorev --- bootstrap/util/configowner.go | 2 +- cmd/clusterctl/client/tree/discovery.go | 16 ++++---- controllers/external/util.go | 6 +-- controllers/external/util_test.go | 4 +- .../kubeadm/internal/control_plane.go | 2 +- .../internal/controllers/controller_test.go | 2 +- .../kubeadm/internal/controllers/helpers.go | 2 +- .../internal/controllers/helpers_test.go | 4 +- .../controllers/machinepool_controller.go | 4 +- .../machinepool_controller_phases.go | 4 +- .../machinepool_controller_phases_test.go | 4 +- .../cluster/cluster_controller_phases.go | 2 +- .../clusterclass/clusterclass_controller.go | 6 +-- .../controllers/machine/machine_controller.go | 2 +- .../machine/machine_controller_phases.go | 2 +- .../machinedeployment_controller.go | 2 +- .../machinedeployment_controller_test.go | 2 +- .../machinehealthcheck_controller.go | 6 +-- .../machineset/machineset_controller.go | 6 +-- .../machineset/machineset_controller_test.go | 4 +- .../machineset/machineset_preflight.go | 2 +- internal/controllers/topology/cluster/util.go | 2 +- internal/webhooks/cluster.go | 2 +- test/e2e/clusterclass_changes.go | 24 +++++------ test/e2e/clusterclass_rollout.go | 40 +++++++++---------- test/e2e/clusterctl_upgrade.go | 2 +- 26 files changed, 77 insertions(+), 77 deletions(-) diff --git a/bootstrap/util/configowner.go b/bootstrap/util/configowner.go index 7ed7e2edbf24..39a411057f2b 100644 --- a/bootstrap/util/configowner.go +++ b/bootstrap/util/configowner.go @@ -176,7 +176,7 @@ func getConfigOwner(ctx context.Context, c client.Client, obj metav1.Object, get // GetOwnerByRef finds and returns the owner by looking at the object reference. func GetOwnerByRef(ctx context.Context, c client.Client, ref *corev1.ObjectReference) (*ConfigOwner, error) { - obj, err := external.Get(ctx, c, ref, ref.Namespace) + obj, err := external.Get(ctx, c, ref) if err != nil { return nil, err } diff --git a/cmd/clusterctl/client/tree/discovery.go b/cmd/clusterctl/client/tree/discovery.go index 09feb1960440..c86d3b573f69 100644 --- a/cmd/clusterctl/client/tree/discovery.go +++ b/cmd/clusterctl/client/tree/discovery.go @@ -84,7 +84,7 @@ func Discovery(ctx context.Context, c client.Client, namespace, name string, opt tree := NewObjectTree(cluster, options.toObjectTreeOptions()) // Adds cluster infra - clusterInfra, err := external.Get(ctx, c, cluster.Spec.InfrastructureRef, cluster.Namespace) + clusterInfra, err := external.Get(ctx, c, cluster.Spec.InfrastructureRef) if err != nil { return nil, errors.Wrap(err, "get InfraCluster reference from Cluster") } @@ -95,7 +95,7 @@ func Discovery(ctx context.Context, c client.Client, namespace, name string, opt } // Adds control plane - controlPlane, err := external.Get(ctx, c, cluster.Spec.ControlPlaneRef, cluster.Namespace) + controlPlane, err := external.Get(ctx, c, cluster.Spec.ControlPlaneRef) if err == nil { addControlPlane(cluster, controlPlane, tree, options) } @@ -112,13 +112,13 @@ func Discovery(ctx context.Context, c client.Client, namespace, name string, opt if visible { if (m.Spec.InfrastructureRef != corev1.ObjectReference{}) { - if machineInfra, err := external.Get(ctx, c, &m.Spec.InfrastructureRef, cluster.Namespace); err == nil { + if machineInfra, err := external.Get(ctx, c, &m.Spec.InfrastructureRef); err == nil { tree.Add(m, machineInfra, ObjectMetaName("MachineInfrastructure"), NoEcho(true)) } } if m.Spec.Bootstrap.ConfigRef != nil { - if machineBootstrap, err := external.Get(ctx, c, m.Spec.Bootstrap.ConfigRef, cluster.Namespace); err == nil { + if machineBootstrap, err := external.Get(ctx, c, m.Spec.Bootstrap.ConfigRef); err == nil { tree.Add(m, machineBootstrap, ObjectMetaName("BootstrapConfig"), NoEcho(true)) } } @@ -154,7 +154,7 @@ func Discovery(ctx context.Context, c client.Client, namespace, name string, opt if len(machinePoolList.Items) > 0 { // Add MachinePool objects tree.Add(cluster, workers) - addMachinePoolsToObjectTree(ctx, c, cluster.Namespace, workers, machinePoolList, machinesList, tree, addMachineFunc) + addMachinePoolsToObjectTree(ctx, c, workers, machinePoolList, machinesList, tree, addMachineFunc) } // Handles orphan machines. @@ -275,17 +275,17 @@ func addMachineDeploymentToObjectTree(ctx context.Context, c client.Client, clus return nil } -func addMachinePoolsToObjectTree(ctx context.Context, c client.Client, namespace string, workers *unstructured.Unstructured, machinePoolList *expv1.MachinePoolList, machinesList *clusterv1.MachineList, tree *ObjectTree, addMachineFunc func(parent client.Object, m *clusterv1.Machine)) { +func addMachinePoolsToObjectTree(ctx context.Context, c client.Client, workers *unstructured.Unstructured, machinePoolList *expv1.MachinePoolList, machinesList *clusterv1.MachineList, tree *ObjectTree, addMachineFunc func(parent client.Object, m *clusterv1.Machine)) { for i := range machinePoolList.Items { mp := &machinePoolList.Items[i] _, visible := tree.Add(workers, mp, GroupingObject(true)) if visible { - if machinePoolBootstrap, err := external.Get(ctx, c, mp.Spec.Template.Spec.Bootstrap.ConfigRef, namespace); err == nil { + if machinePoolBootstrap, err := external.Get(ctx, c, mp.Spec.Template.Spec.Bootstrap.ConfigRef); err == nil { tree.Add(mp, machinePoolBootstrap, ObjectMetaName("BootstrapConfig"), NoEcho(true)) } - if machinePoolInfra, err := external.Get(ctx, c, &mp.Spec.Template.Spec.InfrastructureRef, namespace); err == nil { + if machinePoolInfra, err := external.Get(ctx, c, &mp.Spec.Template.Spec.InfrastructureRef); err == nil { tree.Add(mp, machinePoolInfra, ObjectMetaName("MachinePoolInfrastructure"), NoEcho(true)) } } diff --git a/controllers/external/util.go b/controllers/external/util.go index c064885518cd..beca598d6347 100644 --- a/controllers/external/util.go +++ b/controllers/external/util.go @@ -31,7 +31,7 @@ import ( ) // Get uses the client and reference to get an external, unstructured object. -func Get(ctx context.Context, c client.Reader, ref *corev1.ObjectReference, _ string) (*unstructured.Unstructured, error) { +func Get(ctx context.Context, c client.Reader, ref *corev1.ObjectReference) (*unstructured.Unstructured, error) { if ref == nil { return nil, errors.Errorf("cannot get object - object reference not set") } @@ -41,7 +41,7 @@ func Get(ctx context.Context, c client.Reader, ref *corev1.ObjectReference, _ st obj.SetName(ref.Name) obj.SetNamespace(ref.Namespace) if err := c.Get(ctx, client.ObjectKeyFromObject(obj), obj); err != nil { - return nil, errors.Wrapf(err, "failed to retrieve %s external object %q/%q", obj.GetKind(), ref.Namespace, ref.Name) + return nil, errors.Wrapf(err, "failed to retrieve %s %s/%s", obj.GetKind(), ref.Namespace, ref.Name) } return obj, nil } @@ -92,7 +92,7 @@ type CreateFromTemplateInput struct { // CreateFromTemplate uses the client and the reference to create a new object from the template. func CreateFromTemplate(ctx context.Context, in *CreateFromTemplateInput) (*corev1.ObjectReference, error) { - from, err := Get(ctx, in.Client, in.TemplateRef, in.Namespace) + from, err := Get(ctx, in.Client, in.TemplateRef) if err != nil { return nil, err } diff --git a/controllers/external/util_test.go b/controllers/external/util_test.go index f10eefab3687..3ef41d354f07 100644 --- a/controllers/external/util_test.go +++ b/controllers/external/util_test.go @@ -63,7 +63,7 @@ func TestGetResourceFound(t *testing.T) { } fakeClient := fake.NewClientBuilder().WithObjects(testResource.DeepCopy()).Build() - got, err := Get(ctx, fakeClient, testResourceReference, metav1.NamespaceDefault) + got, err := Get(ctx, fakeClient, testResourceReference) g.Expect(err).ToNot(HaveOccurred()) g.Expect(got).To(BeComparableTo(testResource)) } @@ -79,7 +79,7 @@ func TestGetResourceNotFound(t *testing.T) { } fakeClient := fake.NewClientBuilder().Build() - _, err := Get(ctx, fakeClient, testResourceReference, metav1.NamespaceDefault) + _, err := Get(ctx, fakeClient, testResourceReference) g.Expect(err).To(HaveOccurred()) g.Expect(apierrors.IsNotFound(errors.Cause(err))).To(BeTrue()) } diff --git a/controlplane/kubeadm/internal/control_plane.go b/controlplane/kubeadm/internal/control_plane.go index 618bdceeca80..5887ab582e71 100644 --- a/controlplane/kubeadm/internal/control_plane.go +++ b/controlplane/kubeadm/internal/control_plane.go @@ -250,7 +250,7 @@ func (c *ControlPlane) UpToDateMachines() collections.Machines { func getInfraResources(ctx context.Context, cl client.Client, machines collections.Machines) (map[string]*unstructured.Unstructured, error) { result := map[string]*unstructured.Unstructured{} for _, m := range machines { - infraObj, err := external.Get(ctx, cl, &m.Spec.InfrastructureRef, m.Namespace) + infraObj, err := external.Get(ctx, cl, &m.Spec.InfrastructureRef) if err != nil { if apierrors.IsNotFound(errors.Cause(err)) { continue diff --git a/controlplane/kubeadm/internal/controllers/controller_test.go b/controlplane/kubeadm/internal/controllers/controller_test.go index c45509ce7886..53bbb9f3e71c 100644 --- a/controlplane/kubeadm/internal/controllers/controller_test.go +++ b/controlplane/kubeadm/internal/controllers/controller_test.go @@ -1399,7 +1399,7 @@ kubernetesVersion: metav1.16.1 machine := machineList.Items[0] g.Expect(machine.Name).To(HavePrefix(kcp.Name)) // Newly cloned infra objects should have the infraref annotation. - infraObj, err := external.Get(ctx, r.Client, &machine.Spec.InfrastructureRef, machine.Spec.InfrastructureRef.Namespace) + infraObj, err := external.Get(ctx, r.Client, &machine.Spec.InfrastructureRef) g.Expect(err).ToNot(HaveOccurred()) g.Expect(infraObj.GetAnnotations()).To(HaveKeyWithValue(clusterv1.TemplateClonedFromNameAnnotation, genericInfrastructureMachineTemplate.GetName())) g.Expect(infraObj.GetAnnotations()).To(HaveKeyWithValue(clusterv1.TemplateClonedFromGroupKindAnnotation, genericInfrastructureMachineTemplate.GroupVersionKind().GroupKind().String())) diff --git a/controlplane/kubeadm/internal/controllers/helpers.go b/controlplane/kubeadm/internal/controllers/helpers.go index f7917e4a9e38..646d1c32a80b 100644 --- a/controlplane/kubeadm/internal/controllers/helpers.go +++ b/controlplane/kubeadm/internal/controllers/helpers.go @@ -151,7 +151,7 @@ func (r *KubeadmControlPlaneReconciler) reconcileExternalReference(ctx context.C return err } - obj, err := external.Get(ctx, r.Client, ref, controlPlane.Cluster.Namespace) + obj, err := external.Get(ctx, r.Client, ref) if err != nil { if apierrors.IsNotFound(err) { controlPlane.InfraMachineTemplateIsNotFound = true diff --git a/controlplane/kubeadm/internal/controllers/helpers_test.go b/controlplane/kubeadm/internal/controllers/helpers_test.go index 311d202b7b8a..a36a65c5f97c 100644 --- a/controlplane/kubeadm/internal/controllers/helpers_test.go +++ b/controlplane/kubeadm/internal/controllers/helpers_test.go @@ -386,7 +386,7 @@ func TestCloneConfigsAndGenerateMachine(t *testing.T) { g.Expect(m.Name).NotTo(BeEmpty()) g.Expect(m.Name).To(HavePrefix(kcp.Name + namingTemplateKey)) - infraObj, err := external.Get(ctx, r.Client, &m.Spec.InfrastructureRef, m.Spec.InfrastructureRef.Namespace) + infraObj, err := external.Get(ctx, r.Client, &m.Spec.InfrastructureRef) g.Expect(err).ToNot(HaveOccurred()) g.Expect(infraObj.GetAnnotations()).To(HaveKeyWithValue(clusterv1.TemplateClonedFromNameAnnotation, genericInfrastructureMachineTemplate.GetName())) g.Expect(infraObj.GetAnnotations()).To(HaveKeyWithValue(clusterv1.TemplateClonedFromGroupKindAnnotation, genericInfrastructureMachineTemplate.GroupVersionKind().GroupKind().String())) @@ -469,7 +469,7 @@ func TestCloneConfigsAndGenerateMachineFail(t *testing.T) { Status: corev1.ConditionFalse, Severity: clusterv1.ConditionSeverityError, Reason: controlplanev1.InfrastructureTemplateCloningFailedReason, - Message: "failed to retrieve GenericMachineTemplate external object \"default\"/\"something_invalid\": genericmachinetemplates.generic.io \"something_invalid\" not found", + Message: "failed to retrieve GenericMachineTemplate default/something_invalid: genericmachinetemplates.generic.io \"something_invalid\" not found", })) } diff --git a/exp/internal/controllers/machinepool_controller.go b/exp/internal/controllers/machinepool_controller.go index 8a441e8a36aa..482e2b6a569f 100644 --- a/exp/internal/controllers/machinepool_controller.go +++ b/exp/internal/controllers/machinepool_controller.go @@ -341,10 +341,10 @@ func (r *MachinePoolReconciler) reconcileDeleteExternal(ctx context.Context, mac continue } - obj, err := external.Get(ctx, r.Client, ref, machinePool.Namespace) + obj, err := external.Get(ctx, r.Client, ref) if err != nil && !apierrors.IsNotFound(errors.Cause(err)) { return false, errors.Wrapf(err, "failed to get %s %q for MachinePool %q in namespace %q", - ref.GroupVersionKind(), ref.Name, machinePool.Name, machinePool.Namespace) + ref.GroupVersionKind(), ref.Name, machinePool.Name, ref.Namespace) } if obj != nil { objects = append(objects, obj) diff --git a/exp/internal/controllers/machinepool_controller_phases.go b/exp/internal/controllers/machinepool_controller_phases.go index 43533e4d4445..8cdc238499d8 100644 --- a/exp/internal/controllers/machinepool_controller_phases.go +++ b/exp/internal/controllers/machinepool_controller_phases.go @@ -113,11 +113,11 @@ func (r *MachinePoolReconciler) reconcileExternal(ctx context.Context, m *expv1. return external.ReconcileOutput{}, err } - obj, err := external.Get(ctx, r.Client, ref, m.Namespace) + obj, err := external.Get(ctx, r.Client, ref) if err != nil { if apierrors.IsNotFound(errors.Cause(err)) { return external.ReconcileOutput{}, errors.Wrapf(err, "could not find %v %q for MachinePool %q in namespace %q, requeuing", - ref.GroupVersionKind(), ref.Name, m.Name, m.Namespace) + ref.GroupVersionKind(), ref.Name, m.Name, ref.Namespace) } return external.ReconcileOutput{}, err } diff --git a/exp/internal/controllers/machinepool_controller_phases_test.go b/exp/internal/controllers/machinepool_controller_phases_test.go index 8d329910913c..d9c296416558 100644 --- a/exp/internal/controllers/machinepool_controller_phases_test.go +++ b/exp/internal/controllers/machinepool_controller_phases_test.go @@ -1448,7 +1448,7 @@ func TestReconcileMachinePoolMachines(t *testing.T) { g.Expect(machineList.Items).To(HaveLen(2)) for i := range machineList.Items { machine := &machineList.Items[i] - _, err := external.Get(ctx, r.Client, &machine.Spec.InfrastructureRef, machine.Namespace) + _, err := external.Get(ctx, r.Client, &machine.Spec.InfrastructureRef) g.Expect(err).ToNot(HaveOccurred()) } }) @@ -1519,7 +1519,7 @@ func TestReconcileMachinePoolMachines(t *testing.T) { g.Expect(machineList.Items).To(HaveLen(2)) for i := range machineList.Items { machine := &machineList.Items[i] - _, err := external.Get(ctx, r.Client, &machine.Spec.InfrastructureRef, machine.Namespace) + _, err := external.Get(ctx, r.Client, &machine.Spec.InfrastructureRef) g.Expect(err).ToNot(HaveOccurred()) } }) diff --git a/internal/controllers/cluster/cluster_controller_phases.go b/internal/controllers/cluster/cluster_controller_phases.go index f7e9fca144b7..11fa91eaab16 100644 --- a/internal/controllers/cluster/cluster_controller_phases.go +++ b/internal/controllers/cluster/cluster_controller_phases.go @@ -90,7 +90,7 @@ func (r *Reconciler) reconcileExternal(ctx context.Context, cluster *clusterv1.C return nil, err } - obj, err := external.Get(ctx, r.Client, ref, cluster.Namespace) + obj, err := external.Get(ctx, r.Client, ref) if err != nil { return nil, err } diff --git a/internal/controllers/clusterclass/clusterclass_controller.go b/internal/controllers/clusterclass/clusterclass_controller.go index 84c26875e630..274fc229d7a8 100644 --- a/internal/controllers/clusterclass/clusterclass_controller.go +++ b/internal/controllers/clusterclass/clusterclass_controller.go @@ -407,12 +407,12 @@ func refString(ref *corev1.ObjectReference) string { } func (r *Reconciler) reconcileExternal(ctx context.Context, clusterClass *clusterv1.ClusterClass, ref *corev1.ObjectReference) error { - obj, err := external.Get(ctx, r.Client, ref, clusterClass.Namespace) + obj, err := external.Get(ctx, r.Client, ref) if err != nil { if apierrors.IsNotFound(errors.Cause(err)) { - return errors.Wrapf(err, "could not find external object for the ClusterClass. refGroupVersionKind: %s, refName: %s", ref.GroupVersionKind(), ref.Name) + return errors.Wrapf(err, "Could not find external object for the ClusterClass. refGroupVersionKind: %s, refName: %s, refNamespace: %s", ref.GroupVersionKind(), ref.Name, ref.Namespace) } - return errors.Wrapf(err, "failed to get the external object for the ClusterClass. refGroupVersionKind: %s, refName: %s", ref.GroupVersionKind(), ref.Name) + return errors.Wrapf(err, "failed to get the external object for the ClusterClass. refGroupVersionKind: %s, refName: %s, refNamespace: %s", ref.GroupVersionKind(), ref.Name, ref.Namespace) } // Initialize the patch helper. diff --git a/internal/controllers/machine/machine_controller.go b/internal/controllers/machine/machine_controller.go index 00b419a26c0a..8654da74b8ec 100644 --- a/internal/controllers/machine/machine_controller.go +++ b/internal/controllers/machine/machine_controller.go @@ -733,7 +733,7 @@ func (r *Reconciler) isDeleteNodeAllowed(ctx context.Context, cluster *clusterv1 // controlPlaneRef is an optional field in the Cluster so skip the external // managed control plane check if it is nil if cluster.Spec.ControlPlaneRef != nil { - controlPlane, err := external.Get(ctx, r.Client, cluster.Spec.ControlPlaneRef, cluster.Spec.ControlPlaneRef.Namespace) + controlPlane, err := external.Get(ctx, r.Client, cluster.Spec.ControlPlaneRef) if apierrors.IsNotFound(err) { // If control plane object in the reference does not exist, log and skip check for // external managed control plane diff --git a/internal/controllers/machine/machine_controller_phases.go b/internal/controllers/machine/machine_controller_phases.go index f7e53d0ce232..8289c766d141 100644 --- a/internal/controllers/machine/machine_controller_phases.go +++ b/internal/controllers/machine/machine_controller_phases.go @@ -83,7 +83,7 @@ func (r *Reconciler) reconcileExternal(ctx context.Context, cluster *clusterv1.C func (r *Reconciler) ensureExternalOwnershipAndWatch(ctx context.Context, cluster *clusterv1.Cluster, m *clusterv1.Machine, ref *corev1.ObjectReference) (*unstructured.Unstructured, error) { log := ctrl.LoggerFrom(ctx) - obj, err := external.Get(ctx, r.Client, ref, m.Namespace) + obj, err := external.Get(ctx, r.Client, ref) if err != nil { return nil, err } diff --git a/internal/controllers/machinedeployment/machinedeployment_controller.go b/internal/controllers/machinedeployment/machinedeployment_controller.go index 0c8272508916..41184e4b69aa 100644 --- a/internal/controllers/machinedeployment/machinedeployment_controller.go +++ b/internal/controllers/machinedeployment/machinedeployment_controller.go @@ -513,7 +513,7 @@ func reconcileExternalTemplateReference(ctx context.Context, c client.Client, cl return errors.New(err.Error()) } - obj, err := external.Get(ctx, c, ref, cluster.Namespace) + obj, err := external.Get(ctx, c, ref) if err != nil { return err } diff --git a/internal/controllers/machinedeployment/machinedeployment_controller_test.go b/internal/controllers/machinedeployment/machinedeployment_controller_test.go index 0bcd742fedca..590d69bf5631 100644 --- a/internal/controllers/machinedeployment/machinedeployment_controller_test.go +++ b/internal/controllers/machinedeployment/machinedeployment_controller_test.go @@ -204,7 +204,7 @@ func TestMachineDeploymentReconciler(t *testing.T) { t.Log("Verifying the linked infrastructure template has a cluster owner reference") g.Eventually(func() bool { - obj, err := external.Get(ctx, env, &deployment.Spec.Template.Spec.InfrastructureRef, deployment.Namespace) + obj, err := external.Get(ctx, env, &deployment.Spec.Template.Spec.InfrastructureRef) if err != nil { return false } diff --git a/internal/controllers/machinehealthcheck/machinehealthcheck_controller.go b/internal/controllers/machinehealthcheck/machinehealthcheck_controller.go index 8eb091f5a37c..f67cacf997cc 100644 --- a/internal/controllers/machinehealthcheck/machinehealthcheck_controller.go +++ b/internal/controllers/machinehealthcheck/machinehealthcheck_controller.go @@ -441,7 +441,7 @@ func (r *Reconciler) patchUnhealthyTargets(ctx context.Context, logger logr.Logg UID: t.Machine.UID, } - from, err := external.Get(ctx, r.Client, m.Spec.RemediationTemplate, t.Machine.Namespace) + from, err := external.Get(ctx, r.Client, m.Spec.RemediationTemplate) if err != nil { conditions.MarkFalse(m, clusterv1.ExternalRemediationTemplateAvailableCondition, clusterv1.ExternalRemediationTemplateNotFoundReason, clusterv1.ConditionSeverityError, err.Error()) @@ -449,7 +449,7 @@ func (r *Reconciler) patchUnhealthyTargets(ctx context.Context, logger logr.Logg Type: clusterv1.MachineExternallyRemediatedV1Beta2Condition, Status: metav1.ConditionFalse, Reason: clusterv1.MachineExternallyRemediatedRemediationTemplateNotFoundV1Beta2Reason, - Message: fmt.Sprintf("Error retrieving remediation template %s %s", m.Spec.RemediationTemplate.Kind, klog.KRef(t.Machine.Namespace, m.Spec.RemediationTemplate.Name)), + Message: fmt.Sprintf("Error retrieving remediation template %s %s", m.Spec.RemediationTemplate.Kind, klog.KRef(m.Spec.RemediationTemplate.Namespace, m.Spec.RemediationTemplate.Name)), }) errList = append(errList, errors.Wrapf(err, "error retrieving remediation template %v %q for machine %q in namespace %q within cluster %q", m.Spec.RemediationTemplate.GroupVersionKind(), m.Spec.RemediationTemplate.Name, t.Machine.Name, t.Machine.Namespace, m.Spec.ClusterName)) return errList @@ -732,7 +732,7 @@ func (r *Reconciler) getExternalRemediationRequest(ctx context.Context, m *clust Name: machineName, Namespace: m.Namespace, } - remediationReq, err := external.Get(ctx, r.Client, remediationRef, m.Namespace) + remediationReq, err := external.Get(ctx, r.Client, remediationRef) if err != nil { return nil, errors.Wrapf(err, "failed to retrieve external remediation request object") } diff --git a/internal/controllers/machineset/machineset_controller.go b/internal/controllers/machineset/machineset_controller.go index 05a2b63544a6..960be34095e6 100644 --- a/internal/controllers/machineset/machineset_controller.go +++ b/internal/controllers/machineset/machineset_controller.go @@ -554,7 +554,7 @@ func (r *Reconciler) syncMachines(ctx context.Context, s *scope) (ctrl.Result, e } machines[i] = updatedMachine - infraMachine, err := external.Get(ctx, r.Client, &updatedMachine.Spec.InfrastructureRef, updatedMachine.Namespace) + infraMachine, err := external.Get(ctx, r.Client, &updatedMachine.Spec.InfrastructureRef) if err != nil { return ctrl.Result{}, errors.Wrapf(err, "failed to get InfrastructureMachine %s", klog.KRef(updatedMachine.Spec.InfrastructureRef.Namespace, updatedMachine.Spec.InfrastructureRef.Name)) @@ -576,7 +576,7 @@ func (r *Reconciler) syncMachines(ctx context.Context, s *scope) (ctrl.Result, e } if updatedMachine.Spec.Bootstrap.ConfigRef != nil { - bootstrapConfig, err := external.Get(ctx, r.Client, updatedMachine.Spec.Bootstrap.ConfigRef, updatedMachine.Namespace) + bootstrapConfig, err := external.Get(ctx, r.Client, updatedMachine.Spec.Bootstrap.ConfigRef) if err != nil { return ctrl.Result{}, errors.Wrapf(err, "failed to get BootstrapConfig %s", klog.KRef(updatedMachine.Spec.Bootstrap.ConfigRef.Namespace, updatedMachine.Spec.Bootstrap.ConfigRef.Name)) @@ -1516,7 +1516,7 @@ func (r *Reconciler) reconcileExternalTemplateReference(ctx context.Context, clu return false, err } - obj, err := external.Get(ctx, r.Client, ref, cluster.Namespace) + obj, err := external.Get(ctx, r.Client, ref) if err != nil { if apierrors.IsNotFound(err) { if !ms.DeletionTimestamp.IsZero() { diff --git a/internal/controllers/machineset/machineset_controller_test.go b/internal/controllers/machineset/machineset_controller_test.go index cf7ccc39623d..abb9f8247b6e 100644 --- a/internal/controllers/machineset/machineset_controller_test.go +++ b/internal/controllers/machineset/machineset_controller_test.go @@ -226,7 +226,7 @@ func TestMachineSetReconciler(t *testing.T) { t.Log("Verifying the linked bootstrap template has a cluster owner reference") g.Eventually(func() bool { - obj, err := external.Get(ctx, env, instance.Spec.Template.Spec.Bootstrap.ConfigRef, instance.Namespace) + obj, err := external.Get(ctx, env, instance.Spec.Template.Spec.Bootstrap.ConfigRef) if err != nil { return false } @@ -241,7 +241,7 @@ func TestMachineSetReconciler(t *testing.T) { t.Log("Verifying the linked infrastructure template has a cluster owner reference") g.Eventually(func() bool { - obj, err := external.Get(ctx, env, &instance.Spec.Template.Spec.InfrastructureRef, instance.Namespace) + obj, err := external.Get(ctx, env, &instance.Spec.Template.Spec.InfrastructureRef) if err != nil { return false } diff --git a/internal/controllers/machineset/machineset_preflight.go b/internal/controllers/machineset/machineset_preflight.go index 1f446eb96c81..4e3552f97650 100644 --- a/internal/controllers/machineset/machineset_preflight.go +++ b/internal/controllers/machineset/machineset_preflight.go @@ -66,7 +66,7 @@ func (r *Reconciler) runPreflightChecks(ctx context.Context, cluster *clusterv1. } // Get the control plane object. - controlPlane, err := external.Get(ctx, r.Client, cluster.Spec.ControlPlaneRef, cluster.Namespace) + controlPlane, err := external.Get(ctx, r.Client, cluster.Spec.ControlPlaneRef) if err != nil { return ctrl.Result{}, "", errors.Wrapf(err, "failed to perform %q: failed to perform preflight checks: failed to get ControlPlane %s", action, klog.KRef(cluster.Spec.ControlPlaneRef.Namespace, cluster.Spec.ControlPlaneRef.Name)) } diff --git a/internal/controllers/topology/cluster/util.go b/internal/controllers/topology/cluster/util.go index 3a2111713bea..643a4217fe66 100644 --- a/internal/controllers/topology/cluster/util.go +++ b/internal/controllers/topology/cluster/util.go @@ -33,7 +33,7 @@ func (r *Reconciler) getReference(ctx context.Context, ref *corev1.ObjectReferen return nil, errors.New("reference is not set") } - obj, err := external.Get(ctx, r.Client, ref, ref.Namespace) + obj, err := external.Get(ctx, r.Client, ref) if err != nil { return nil, errors.Wrapf(err, "failed to retrieve %s %s", ref.Kind, klog.KRef(ref.Namespace, ref.Name)) } diff --git a/internal/webhooks/cluster.go b/internal/webhooks/cluster.go index de51e7ab9d65..cc9b8974d19c 100644 --- a/internal/webhooks/cluster.go +++ b/internal/webhooks/cluster.go @@ -509,7 +509,7 @@ func (webhook *Cluster) validateTopologyVersion(ctx context.Context, fldPath *fi } func validateTopologyControlPlaneVersion(ctx context.Context, ctrlClient client.Reader, oldCluster *clusterv1.Cluster, oldVersion semver.Version) error { - cp, err := external.Get(ctx, ctrlClient, oldCluster.Spec.ControlPlaneRef, oldCluster.Namespace) + cp, err := external.Get(ctx, ctrlClient, oldCluster.Spec.ControlPlaneRef) if err != nil { return errors.Wrap(err, "failed to get ControlPlane object") } diff --git a/test/e2e/clusterclass_changes.go b/test/e2e/clusterclass_changes.go index f919a701ec92..454543f2a707 100644 --- a/test/e2e/clusterclass_changes.go +++ b/test/e2e/clusterclass_changes.go @@ -260,7 +260,7 @@ func modifyControlPlaneViaClusterClassAndWait(ctx context.Context, input modifyC // Get ControlPlaneTemplate object. controlPlaneTemplateRef := input.ClusterClass.Spec.ControlPlane.Ref - controlPlaneTemplate, err := external.Get(ctx, mgmtClient, controlPlaneTemplateRef, input.ClusterClass.Namespace) + controlPlaneTemplate, err := external.Get(ctx, mgmtClient, controlPlaneTemplateRef) Expect(err).ToNot(HaveOccurred()) // Create a new ControlPlaneTemplate object with a new name and ModifyControlPlaneFields set. @@ -287,7 +287,7 @@ func modifyControlPlaneViaClusterClassAndWait(ctx context.Context, input modifyC // Get the ControlPlane. controlPlaneRef := input.Cluster.Spec.ControlPlaneRef controlPlaneTopology := input.Cluster.Spec.Topology.ControlPlane - controlPlane, err := external.Get(ctx, mgmtClient, controlPlaneRef, input.ClusterClass.Namespace) + controlPlane, err := external.Get(ctx, mgmtClient, controlPlaneRef) g.Expect(err).ToNot(HaveOccurred()) // Verify that the fields from Cluster topology are set on the control plane. @@ -366,7 +366,7 @@ func modifyMachineDeploymentViaClusterClassAndWait(ctx context.Context, input mo // Retrieve BootstrapConfigTemplate object. bootstrapConfigTemplateRef = mdClass.Template.Bootstrap.Ref - bootstrapConfigTemplate, err := external.Get(ctx, mgmtClient, bootstrapConfigTemplateRef, input.Cluster.Namespace) + bootstrapConfigTemplate, err := external.Get(ctx, mgmtClient, bootstrapConfigTemplateRef) Expect(err).ToNot(HaveOccurred()) // Create a new BootstrapConfigTemplate object with a new name and ModifyBootstrapConfigTemplateFields set. newBootstrapConfigTemplate := bootstrapConfigTemplate.DeepCopy() @@ -383,7 +383,7 @@ func modifyMachineDeploymentViaClusterClassAndWait(ctx context.Context, input mo // Retrieve InfrastructureMachineTemplate object. infrastructureMachineTemplateRef := mdClass.Template.Infrastructure.Ref - infrastructureMachineTemplate, err := external.Get(ctx, mgmtClient, infrastructureMachineTemplateRef, input.Cluster.Namespace) + infrastructureMachineTemplate, err := external.Get(ctx, mgmtClient, infrastructureMachineTemplateRef) Expect(err).ToNot(HaveOccurred()) // Create a new InfrastructureMachineTemplate object with a new name and ModifyInfrastructureMachineTemplateFields set. newInfrastructureMachineTemplate := infrastructureMachineTemplate.DeepCopy() @@ -429,7 +429,7 @@ func modifyMachineDeploymentViaClusterClassAndWait(ctx context.Context, input mo if mdClass.Template.Bootstrap.Ref != nil { // Get the corresponding BootstrapConfigTemplate. bootstrapConfigTemplateRef := md.Spec.Template.Spec.Bootstrap.ConfigRef - bootstrapConfigTemplate, err := external.Get(ctx, mgmtClient, bootstrapConfigTemplateRef, input.Cluster.Namespace) + bootstrapConfigTemplate, err := external.Get(ctx, mgmtClient, bootstrapConfigTemplateRef) g.Expect(err).ToNot(HaveOccurred()) // Verify that ModifyBootstrapConfigTemplateFields have been set. @@ -443,7 +443,7 @@ func modifyMachineDeploymentViaClusterClassAndWait(ctx context.Context, input mo // Get the corresponding InfrastructureMachineTemplate. infrastructureMachineTemplateRef := md.Spec.Template.Spec.InfrastructureRef - infrastructureMachineTemplate, err := external.Get(ctx, mgmtClient, &infrastructureMachineTemplateRef, input.Cluster.Namespace) + infrastructureMachineTemplate, err := external.Get(ctx, mgmtClient, &infrastructureMachineTemplateRef) g.Expect(err).ToNot(HaveOccurred()) // Verify that ModifyInfrastructureMachineTemplateFields have been set. @@ -489,7 +489,7 @@ func modifyMachinePoolViaClusterClassAndWait(ctx context.Context, input modifyMa // Retrieve BootstrapConfigTemplate object. bootstrapConfigTemplateRef = mpClass.Template.Bootstrap.Ref - bootstrapConfigTemplate, err := external.Get(ctx, mgmtClient, bootstrapConfigTemplateRef, input.Cluster.Namespace) + bootstrapConfigTemplate, err := external.Get(ctx, mgmtClient, bootstrapConfigTemplateRef) Expect(err).ToNot(HaveOccurred()) // Create a new BootstrapConfigTemplate object with a new name and ModifyBootstrapConfigTemplateFields set. newBootstrapConfigTemplate := bootstrapConfigTemplate.DeepCopy() @@ -506,7 +506,7 @@ func modifyMachinePoolViaClusterClassAndWait(ctx context.Context, input modifyMa // Retrieve InfrastructureMachineTemplate object. infrastructureMachinePoolTemplateRef := mpClass.Template.Infrastructure.Ref - infrastructureMachinePoolTemplate, err := external.Get(ctx, mgmtClient, infrastructureMachinePoolTemplateRef, input.Cluster.Namespace) + infrastructureMachinePoolTemplate, err := external.Get(ctx, mgmtClient, infrastructureMachinePoolTemplateRef) Expect(err).ToNot(HaveOccurred()) // Create a new InfrastructureMachinePoolTemplate object with a new name and ModifyInfrastructureMachinePoolTemplateFields set. newInfrastructureMachinePoolTemplate := infrastructureMachinePoolTemplate.DeepCopy() @@ -552,7 +552,7 @@ func modifyMachinePoolViaClusterClassAndWait(ctx context.Context, input modifyMa if mpClass.Template.Bootstrap.Ref != nil { // Get the corresponding BootstrapConfig object. bootstrapConfigObjectRef := mp.Spec.Template.Spec.Bootstrap.ConfigRef - bootstrapConfigObject, err := external.Get(ctx, mgmtClient, bootstrapConfigObjectRef, input.Cluster.Namespace) + bootstrapConfigObject, err := external.Get(ctx, mgmtClient, bootstrapConfigObjectRef) g.Expect(err).ToNot(HaveOccurred()) // Verify that ModifyBootstrapConfigTemplateFields have been set and propagates to the BootstrapConfig. @@ -568,7 +568,7 @@ func modifyMachinePoolViaClusterClassAndWait(ctx context.Context, input modifyMa // Get the corresponding InfrastructureMachinePoolTemplate. infrastructureMachinePoolRef := mp.Spec.Template.Spec.InfrastructureRef - infrastructureMachinePool, err := external.Get(ctx, mgmtClient, &infrastructureMachinePoolRef, input.Cluster.Namespace) + infrastructureMachinePool, err := external.Get(ctx, mgmtClient, &infrastructureMachinePoolRef) g.Expect(err).ToNot(HaveOccurred()) // Verify that ModifyInfrastructureMachinePoolTemplateFields have been set. @@ -695,7 +695,7 @@ func rebaseClusterClassAndWait(ctx context.Context, input rebaseClusterClassAndW // Get the current ControlPlane, we will later verify that it has not changed. controlPlaneRef := input.Cluster.Spec.ControlPlaneRef - beforeControlPlane, err := external.Get(ctx, mgmtClient, controlPlaneRef, input.ClusterClass.Namespace) + beforeControlPlane, err := external.Get(ctx, mgmtClient, controlPlaneRef) Expect(err).ToNot(HaveOccurred()) // Rebase the Cluster to the new ClusterClass. @@ -741,7 +741,7 @@ func rebaseClusterClassAndWait(ctx context.Context, input rebaseClusterClassAndW // Verify that the ControlPlane has not been changed. // NOTE: MachineDeployments are rolled out before the ControlPlane. Thus, we know that the // ControlPlane would have been updated by now, if there have been any changes. - afterControlPlane, err := external.Get(ctx, mgmtClient, controlPlaneRef, input.ClusterClass.Namespace) + afterControlPlane, err := external.Get(ctx, mgmtClient, controlPlaneRef) Expect(err).ToNot(HaveOccurred()) Expect(afterControlPlane.GetGeneration()).To(Equal(beforeControlPlane.GetGeneration()), "ControlPlane generation should not be incremented during the rebase because ControlPlane should not be affected.") diff --git a/test/e2e/clusterclass_rollout.go b/test/e2e/clusterclass_rollout.go index 84a1233292b2..da8ea05788ab 100644 --- a/test/e2e/clusterclass_rollout.go +++ b/test/e2e/clusterclass_rollout.go @@ -1058,31 +1058,31 @@ func getClusterClassObjects(ctx context.Context, g Gomega, clusterProxy framewor } var err error - res.InfrastructureClusterTemplate, err = external.Get(ctx, mgmtClient, clusterClass.Spec.Infrastructure.Ref, clusterClass.Namespace) + res.InfrastructureClusterTemplate, err = external.Get(ctx, mgmtClient, clusterClass.Spec.Infrastructure.Ref) g.Expect(err).ToNot(HaveOccurred()) - res.ControlPlaneTemplate, err = external.Get(ctx, mgmtClient, clusterClass.Spec.ControlPlane.Ref, clusterClass.Namespace) + res.ControlPlaneTemplate, err = external.Get(ctx, mgmtClient, clusterClass.Spec.ControlPlane.Ref) g.Expect(err).ToNot(HaveOccurred()) - res.ControlPlaneInfrastructureMachineTemplate, err = external.Get(ctx, mgmtClient, clusterClass.Spec.ControlPlane.MachineInfrastructure.Ref, clusterClass.Namespace) + res.ControlPlaneInfrastructureMachineTemplate, err = external.Get(ctx, mgmtClient, clusterClass.Spec.ControlPlane.MachineInfrastructure.Ref) g.Expect(err).ToNot(HaveOccurred()) for _, mdClass := range clusterClass.Spec.Workers.MachineDeployments { - infrastructureMachineTemplate, err := external.Get(ctx, mgmtClient, mdClass.Template.Infrastructure.Ref, clusterClass.Namespace) + infrastructureMachineTemplate, err := external.Get(ctx, mgmtClient, mdClass.Template.Infrastructure.Ref) g.Expect(err).ToNot(HaveOccurred()) res.InfrastructureMachineTemplateByMachineDeploymentClass[mdClass.Class] = infrastructureMachineTemplate - bootstrapConfigTemplate, err := external.Get(ctx, mgmtClient, mdClass.Template.Bootstrap.Ref, clusterClass.Namespace) + bootstrapConfigTemplate, err := external.Get(ctx, mgmtClient, mdClass.Template.Bootstrap.Ref) g.Expect(err).ToNot(HaveOccurred()) res.BootstrapConfigTemplateByMachineDeploymentClass[mdClass.Class] = bootstrapConfigTemplate } for _, mpClass := range clusterClass.Spec.Workers.MachinePools { - infrastructureMachinePoolTemplate, err := external.Get(ctx, mgmtClient, mpClass.Template.Infrastructure.Ref, clusterClass.Namespace) + infrastructureMachinePoolTemplate, err := external.Get(ctx, mgmtClient, mpClass.Template.Infrastructure.Ref) g.Expect(err).ToNot(HaveOccurred()) res.InfrastructureMachinePoolTemplateByMachinePoolClass[mpClass.Class] = infrastructureMachinePoolTemplate - bootstrapConfigTemplate, err := external.Get(ctx, mgmtClient, mpClass.Template.Bootstrap.Ref, clusterClass.Namespace) + bootstrapConfigTemplate, err := external.Get(ctx, mgmtClient, mpClass.Template.Bootstrap.Ref) g.Expect(err).ToNot(HaveOccurred()) res.BootstrapConfigTemplateByMachinePoolClass[mpClass.Class] = bootstrapConfigTemplate } @@ -1133,15 +1133,15 @@ func getClusterObjects(ctx context.Context, g Gomega, clusterProxy framework.Clu var err error // InfrastructureCluster - res.InfrastructureCluster, err = external.Get(ctx, mgmtClient, cluster.Spec.InfrastructureRef, cluster.Namespace) + res.InfrastructureCluster, err = external.Get(ctx, mgmtClient, cluster.Spec.InfrastructureRef) g.Expect(err).ToNot(HaveOccurred()) // ControlPlane - res.ControlPlane, err = external.Get(ctx, mgmtClient, cluster.Spec.ControlPlaneRef, cluster.Namespace) + res.ControlPlane, err = external.Get(ctx, mgmtClient, cluster.Spec.ControlPlaneRef) g.Expect(err).ToNot(HaveOccurred()) controlPlaneInfrastructureMachineTemplateRef, err := contract.ControlPlane().MachineTemplate().InfrastructureRef().Get(res.ControlPlane) g.Expect(err).ToNot(HaveOccurred()) - res.ControlPlaneInfrastructureMachineTemplate, err = external.Get(ctx, mgmtClient, controlPlaneInfrastructureMachineTemplateRef, cluster.Namespace) + res.ControlPlaneInfrastructureMachineTemplate, err = external.Get(ctx, mgmtClient, controlPlaneInfrastructureMachineTemplateRef) g.Expect(err).ToNot(HaveOccurred()) controlPlaneMachineList := &clusterv1.MachineList{} g.Expect(mgmtClient.List(ctx, controlPlaneMachineList, client.InNamespace(cluster.Namespace), client.MatchingLabels{ @@ -1154,7 +1154,7 @@ func getClusterObjects(ctx context.Context, g Gomega, clusterProxy framework.Clu g.Expect(controlPlaneMachineList.Items).To(HaveLen(int(*replicas))) for _, machine := range controlPlaneMachineList.Items { res.ControlPlaneMachines = append(res.ControlPlaneMachines, &machine) - addMachineObjects(ctx, mgmtClient, workloadClient, g, res, cluster, &machine) + addMachineObjects(ctx, mgmtClient, workloadClient, g, res, &machine) } // MachineDeployments. @@ -1168,11 +1168,11 @@ func getClusterObjects(ctx context.Context, g Gomega, clusterProxy framework.Clu md := mdList.Items[0] res.MachineDeployments = append(res.MachineDeployments, &md) - bootstrapConfigTemplate, err := external.Get(ctx, mgmtClient, md.Spec.Template.Spec.Bootstrap.ConfigRef, cluster.Namespace) + bootstrapConfigTemplate, err := external.Get(ctx, mgmtClient, md.Spec.Template.Spec.Bootstrap.ConfigRef) g.Expect(err).ToNot(HaveOccurred()) res.BootstrapConfigTemplateByMachineDeployment[md.Name] = bootstrapConfigTemplate - infrastructureMachineTemplate, err := external.Get(ctx, mgmtClient, &md.Spec.Template.Spec.InfrastructureRef, cluster.Namespace) + infrastructureMachineTemplate, err := external.Get(ctx, mgmtClient, &md.Spec.Template.Spec.InfrastructureRef) g.Expect(err).ToNot(HaveOccurred()) res.InfrastructureMachineTemplateByMachineDeployment[md.Name] = infrastructureMachineTemplate @@ -1191,7 +1191,7 @@ func getClusterObjects(ctx context.Context, g Gomega, clusterProxy framework.Clu for _, machine := range machines { res.MachinesByMachineSet[machine.Labels[clusterv1.MachineSetNameLabel]] = append( res.MachinesByMachineSet[machine.Labels[clusterv1.MachineSetNameLabel]], &machine) - addMachineObjects(ctx, mgmtClient, workloadClient, g, res, cluster, &machine) + addMachineObjects(ctx, mgmtClient, workloadClient, g, res, &machine) } } @@ -1206,11 +1206,11 @@ func getClusterObjects(ctx context.Context, g Gomega, clusterProxy framework.Clu mp := mpList.Items[0] res.MachinePools = append(res.MachinePools, &mp) - bootstrapConfig, err := external.Get(ctx, mgmtClient, mp.Spec.Template.Spec.Bootstrap.ConfigRef, cluster.Namespace) + bootstrapConfig, err := external.Get(ctx, mgmtClient, mp.Spec.Template.Spec.Bootstrap.ConfigRef) g.Expect(err).ToNot(HaveOccurred()) res.BootstrapConfigByMachinePool[mp.Name] = bootstrapConfig - infrastructureMachinePool, err := external.Get(ctx, mgmtClient, &mp.Spec.Template.Spec.InfrastructureRef, cluster.Namespace) + infrastructureMachinePool, err := external.Get(ctx, mgmtClient, &mp.Spec.Template.Spec.InfrastructureRef) g.Expect(err).ToNot(HaveOccurred()) res.InfrastructureMachinePoolByMachinePool[mp.Name] = infrastructureMachinePool } @@ -1219,12 +1219,12 @@ func getClusterObjects(ctx context.Context, g Gomega, clusterProxy framework.Clu } // addMachineObjects adds objects related to the Machine (BootstrapConfig, InfraMachine, Node) to clusterObjects. -func addMachineObjects(ctx context.Context, mgmtClient, workloadClient client.Client, g Gomega, res clusterObjects, cluster *clusterv1.Cluster, machine *clusterv1.Machine) { - bootstrapConfig, err := external.Get(ctx, mgmtClient, machine.Spec.Bootstrap.ConfigRef, cluster.Namespace) +func addMachineObjects(ctx context.Context, mgmtClient, workloadClient client.Client, g Gomega, res clusterObjects, machine *clusterv1.Machine) { + bootstrapConfig, err := external.Get(ctx, mgmtClient, machine.Spec.Bootstrap.ConfigRef) g.Expect(err).ToNot(HaveOccurred()) res.BootstrapConfigByMachine[machine.Name] = bootstrapConfig - infrastructureMachine, err := external.Get(ctx, mgmtClient, &machine.Spec.InfrastructureRef, cluster.Namespace) + infrastructureMachine, err := external.Get(ctx, mgmtClient, &machine.Spec.InfrastructureRef) g.Expect(err).ToNot(HaveOccurred()) res.InfrastructureMachineByMachine[machine.Name] = infrastructureMachine @@ -1266,7 +1266,7 @@ func modifyControlPlaneViaClusterAndWait(ctx context.Context, input modifyContro // Get the ControlPlane. controlPlaneRef := input.Cluster.Spec.ControlPlaneRef controlPlaneTopology := input.Cluster.Spec.Topology.ControlPlane - controlPlane, err := external.Get(ctx, mgmtClient, controlPlaneRef, input.Cluster.Namespace) + controlPlane, err := external.Get(ctx, mgmtClient, controlPlaneRef) g.Expect(err).ToNot(HaveOccurred()) // Verify that the fields from Cluster topology are set on the control plane. diff --git a/test/e2e/clusterctl_upgrade.go b/test/e2e/clusterctl_upgrade.go index 7806eebfd36b..de179e9639f8 100644 --- a/test/e2e/clusterctl_upgrade.go +++ b/test/e2e/clusterctl_upgrade.go @@ -903,7 +903,7 @@ func calculateExpectedMachinePoolMachineCount(ctx context.Context, c client.Clie client.MatchingLabels{clusterv1.ClusterNameLabel: workloadClusterName}, ); err == nil { for _, mp := range machinePoolList.Items { - infraMachinePool, err := external.Get(ctx, c, &mp.Spec.Template.Spec.InfrastructureRef, workloadClusterNamespace) + infraMachinePool, err := external.Get(ctx, c, &mp.Spec.Template.Spec.InfrastructureRef) if err != nil { return 0, err } From f3693a8a6b40afcabec68103934e5048be8d9abb Mon Sep 17 00:00:00 2001 From: Danil-Grigorev Date: Tue, 5 Nov 2024 14:14:17 +0100 Subject: [PATCH 4/8] Ensure refrence namespace is populated in MD Signed-off-by: Danil-Grigorev --- internal/webhooks/machinedeployment.go | 9 +++++ internal/webhooks/machinedeployment_test.go | 40 +++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/internal/webhooks/machinedeployment.go b/internal/webhooks/machinedeployment.go index 80dae949086b..4e03f589830c 100644 --- a/internal/webhooks/machinedeployment.go +++ b/internal/webhooks/machinedeployment.go @@ -158,6 +158,15 @@ func (webhook *MachineDeployment) Default(ctx context.Context, obj runtime.Objec m.Spec.Template.Spec.Version = &normalizedVersion } + // Make sure the namespace of the referent is populated + if m.Spec.Template.Spec.Bootstrap.ConfigRef != nil && m.Spec.Template.Spec.Bootstrap.ConfigRef.Namespace == "" { + m.Spec.Template.Spec.Bootstrap.ConfigRef.Namespace = m.Namespace + } + + if m.Spec.Template.Spec.InfrastructureRef.Namespace == "" { + m.Spec.Template.Spec.InfrastructureRef.Namespace = m.Namespace + } + return nil } diff --git a/internal/webhooks/machinedeployment_test.go b/internal/webhooks/machinedeployment_test.go index d3def492651f..ebdcd3ad0ebe 100644 --- a/internal/webhooks/machinedeployment_test.go +++ b/internal/webhooks/machinedeployment_test.go @@ -23,6 +23,7 @@ import ( . "github.com/onsi/gomega" admissionv1 "k8s.io/api/admission/v1" + corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/util/intstr" @@ -85,6 +86,45 @@ func TestMachineDeploymentDefault(t *testing.T) { g.Expect(*md.Spec.Template.Spec.Version).To(Equal("v1.19.10")) } +func TestMachineDeploymentReferenceDefault(t *testing.T) { + g := NewWithT(t) + md := &clusterv1.MachineDeployment{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-md", + }, + Spec: clusterv1.MachineDeploymentSpec{ + ClusterName: "test-cluster", + Template: clusterv1.MachineTemplateSpec{ + Spec: clusterv1.MachineSpec{ + Version: ptr.To("1.19.10"), + Bootstrap: clusterv1.Bootstrap{ + ConfigRef: &corev1.ObjectReference{}, + }, + }, + }, + }, + } + + scheme := runtime.NewScheme() + g.Expect(clusterv1.AddToScheme(scheme)).To(Succeed()) + webhook := &MachineDeployment{ + decoder: admission.NewDecoder(scheme), + } + + reqCtx := admission.NewContextWithRequest(ctx, admission.Request{ + AdmissionRequest: admissionv1.AdmissionRequest{ + Operation: admissionv1.Create, + }, + }) + + t.Run("for MachineDeployment", util.CustomDefaultValidateTest(reqCtx, md, webhook)) + + g.Expect(webhook.Default(reqCtx, md)).To(Succeed()) + + g.Expect(md.Spec.Template.Spec.InfrastructureRef.Namespace).To(Equal(md.Namespace)) + g.Expect(md.Spec.Template.Spec.Bootstrap.ConfigRef.Namespace).To(Equal(md.Namespace)) +} + func TestCalculateMachineDeploymentReplicas(t *testing.T) { tests := []struct { name string From d9753b0e56204105c81c009b7cb5b6330cfee316 Mon Sep 17 00:00:00 2001 From: Danil-Grigorev Date: Tue, 5 Nov 2024 14:17:14 +0100 Subject: [PATCH 5/8] Use kref for logging Signed-off-by: Danil-Grigorev --- controllers/external/util.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/controllers/external/util.go b/controllers/external/util.go index beca598d6347..a601f31f4f96 100644 --- a/controllers/external/util.go +++ b/controllers/external/util.go @@ -25,6 +25,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apiserver/pkg/storage/names" + "k8s.io/klog/v2" "sigs.k8s.io/controller-runtime/pkg/client" clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" @@ -41,7 +42,7 @@ func Get(ctx context.Context, c client.Reader, ref *corev1.ObjectReference) (*un obj.SetName(ref.Name) obj.SetNamespace(ref.Namespace) if err := c.Get(ctx, client.ObjectKeyFromObject(obj), obj); err != nil { - return nil, errors.Wrapf(err, "failed to retrieve %s %s/%s", obj.GetKind(), ref.Namespace, ref.Name) + return nil, errors.Wrapf(err, "failed to retrieve %s %s", obj.GetKind(), klog.KRef(ref.Namespace, ref.Name)) } return obj, nil } @@ -54,7 +55,7 @@ func Delete(ctx context.Context, c client.Writer, ref *corev1.ObjectReference) e obj.SetName(ref.Name) obj.SetNamespace(ref.Namespace) if err := c.Delete(ctx, obj); err != nil { - return errors.Wrapf(err, "failed to delete %s external object %q/%q", obj.GetKind(), obj.GetNamespace(), obj.GetName()) + return errors.Wrapf(err, "failed to delete %s external object %s", obj.GetKind(), klog.KRef(ref.Namespace, ref.Name)) } return nil } From a3e3e9a0a5a76e951183a91260e8da4afa381de6 Mon Sep 17 00:00:00 2001 From: Danil-Grigorev Date: Tue, 5 Nov 2024 16:26:41 +0100 Subject: [PATCH 6/8] Ensure ref NS in MS is set, and ignored in hash Signed-off-by: Danil-Grigorev --- internal/controllers/machinedeployment/mdutil/util.go | 3 +++ internal/webhooks/machineset.go | 9 +++++++++ 2 files changed, 12 insertions(+) diff --git a/internal/controllers/machinedeployment/mdutil/util.go b/internal/controllers/machinedeployment/mdutil/util.go index bf0694859cdb..8451038bd0fe 100644 --- a/internal/controllers/machinedeployment/mdutil/util.go +++ b/internal/controllers/machinedeployment/mdutil/util.go @@ -438,9 +438,12 @@ func MachineTemplateDeepCopyRolloutFields(template *clusterv1.MachineTemplateSpe // Remove the version part from the references APIVersion field, // for more details see issue #2183 and #2140. + // Remove namespace part of the ref, as it always correllates with current MD namespace templateCopy.Spec.InfrastructureRef.APIVersion = templateCopy.Spec.InfrastructureRef.GroupVersionKind().Group + templateCopy.Spec.InfrastructureRef.Namespace = "" if templateCopy.Spec.Bootstrap.ConfigRef != nil { templateCopy.Spec.Bootstrap.ConfigRef.APIVersion = templateCopy.Spec.Bootstrap.ConfigRef.GroupVersionKind().Group + templateCopy.Spec.Bootstrap.ConfigRef.Namespace = "" } return templateCopy diff --git a/internal/webhooks/machineset.go b/internal/webhooks/machineset.go index fbdfeb0eee22..2624abdbe83b 100644 --- a/internal/webhooks/machineset.go +++ b/internal/webhooks/machineset.go @@ -125,6 +125,15 @@ func (webhook *MachineSet) Default(ctx context.Context, obj runtime.Object) erro m.Spec.Template.Spec.Version = &normalizedVersion } + // Make sure the namespace of the referent is populated + if m.Spec.Template.Spec.Bootstrap.ConfigRef != nil && m.Spec.Template.Spec.Bootstrap.ConfigRef.Namespace == "" { + m.Spec.Template.Spec.Bootstrap.ConfigRef.Namespace = m.Namespace + } + + if m.Spec.Template.Spec.InfrastructureRef.Namespace == "" { + m.Spec.Template.Spec.InfrastructureRef.Namespace = m.Namespace + } + return nil } From de51a2417d7dd090657ae63ccd736f744a981e98 Mon Sep 17 00:00:00 2001 From: Danil-Grigorev Date: Mon, 18 Nov 2024 15:50:40 +0100 Subject: [PATCH 7/8] Double-check and populate ns for MS and MD template Signed-off-by: Danil-Grigorev --- .../machinedeployment/machinedeployment_controller.go | 6 ++++++ internal/controllers/machineset/machineset_controller.go | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/internal/controllers/machinedeployment/machinedeployment_controller.go b/internal/controllers/machinedeployment/machinedeployment_controller.go index 41184e4b69aa..8b724b923f20 100644 --- a/internal/controllers/machinedeployment/machinedeployment_controller.go +++ b/internal/controllers/machinedeployment/machinedeployment_controller.go @@ -513,6 +513,12 @@ func reconcileExternalTemplateReference(ctx context.Context, c client.Client, cl return errors.New(err.Error()) } + // Ensure the ref namespace is populated for objects not yet defaulted by webhook + if ref.Namespace == "" { + ref = ref.DeepCopy() + ref.Namespace = cluster.Namespace + } + obj, err := external.Get(ctx, c, ref) if err != nil { return err diff --git a/internal/controllers/machineset/machineset_controller.go b/internal/controllers/machineset/machineset_controller.go index 960be34095e6..d4807fca138b 100644 --- a/internal/controllers/machineset/machineset_controller.go +++ b/internal/controllers/machineset/machineset_controller.go @@ -1516,6 +1516,12 @@ func (r *Reconciler) reconcileExternalTemplateReference(ctx context.Context, clu return false, err } + // Ensure the ref namespace is populated for objects not yet defaulted by webhook + if ref.Namespace == "" { + ref = ref.DeepCopy() + ref.Namespace = cluster.Namespace + } + obj, err := external.Get(ctx, r.Client, ref) if err != nil { if apierrors.IsNotFound(err) { From ca365167e2cc1cc301759307923c04765ecdb87d Mon Sep 17 00:00:00 2001 From: Danil-Grigorev Date: Mon, 18 Nov 2024 15:52:26 +0100 Subject: [PATCH 8/8] Review: log messages Signed-off-by: Danil-Grigorev --- controllers/external/util.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/controllers/external/util.go b/controllers/external/util.go index a601f31f4f96..61c94ad2d625 100644 --- a/controllers/external/util.go +++ b/controllers/external/util.go @@ -55,7 +55,7 @@ func Delete(ctx context.Context, c client.Writer, ref *corev1.ObjectReference) e obj.SetName(ref.Name) obj.SetNamespace(ref.Namespace) if err := c.Delete(ctx, obj); err != nil { - return errors.Wrapf(err, "failed to delete %s external object %s", obj.GetKind(), klog.KRef(ref.Namespace, ref.Name)) + return errors.Wrapf(err, "failed to delete %s %s", obj.GetKind(), klog.KRef(ref.Namespace, ref.Name)) } return nil }