diff --git a/controlplane/kubeadm/controllers/helpers.go b/controlplane/kubeadm/controllers/helpers.go index e284754e6dc3..12e1b72d40ec 100644 --- a/controlplane/kubeadm/controllers/helpers.go +++ b/controlplane/kubeadm/controllers/helpers.go @@ -271,7 +271,7 @@ func (r *KubeadmControlPlaneReconciler) generateMachine(ctx context.Context, kcp Name: names.SimpleNameGenerator.GenerateName(kcp.Name + "-"), Namespace: kcp.Namespace, Labels: internal.ControlPlaneMachineLabelsForCluster(kcp, cluster.Name), - Annotations: kcp.Spec.MachineTemplate.ObjectMeta.Annotations, + Annotations: map[string]string{}, OwnerReferences: []metav1.OwnerReference{ *metav1.NewControllerRef(kcp, controlplanev1.GroupVersion.WithKind("KubeadmControlPlane")), }, @@ -294,8 +294,11 @@ func (r *KubeadmControlPlaneReconciler) generateMachine(ctx context.Context, kcp if err != nil { return errors.Wrap(err, "failed to marshal cluster configuration") } - if machine.Annotations == nil { - machine.Annotations = map[string]string{} + + // Add the annotations from the MachineTemplate. + // Note: we intentionally don't use the map directly to ensure we don't modify the map in KCP. + for k, v := range kcp.Spec.MachineTemplate.ObjectMeta.Annotations { + machine.Annotations[k] = v } machine.Annotations[controlplanev1.KubeadmClusterConfigurationAnnotation] = string(clusterConfig) diff --git a/controlplane/kubeadm/controllers/helpers_test.go b/controlplane/kubeadm/controllers/helpers_test.go index b872d633dae6..2d02abc27827 100644 --- a/controlplane/kubeadm/controllers/helpers_test.go +++ b/controlplane/kubeadm/controllers/helpers_test.go @@ -486,6 +486,14 @@ func TestKubeadmControlPlaneReconciler_generateMachine(t *testing.T) { }, } + kcpMachineTemplateObjectMeta := clusterv1.ObjectMeta{ + Labels: map[string]string{ + "machineTemplateLabel": "machineTemplateLabelValue", + }, + Annotations: map[string]string{ + "machineTemplateAnnotation": "machineTemplateAnnotationValue", + }, + } kcp := &controlplanev1.KubeadmControlPlane{ ObjectMeta: metav1.ObjectMeta{ Name: "testControlPlane", @@ -493,6 +501,9 @@ func TestKubeadmControlPlaneReconciler_generateMachine(t *testing.T) { }, Spec: controlplanev1.KubeadmControlPlaneSpec{ Version: "v1.16.6", + MachineTemplate: controlplanev1.KubeadmControlPlaneMachineTemplate{ + ObjectMeta: kcpMachineTemplateObjectMeta, + }, }, } @@ -532,6 +543,19 @@ func TestKubeadmControlPlaneReconciler_generateMachine(t *testing.T) { g.Expect(machine.OwnerReferences).To(HaveLen(1)) g.Expect(machine.OwnerReferences).To(ContainElement(*metav1.NewControllerRef(kcp, controlplanev1.GroupVersion.WithKind("KubeadmControlPlane")))) g.Expect(machine.Spec).To(Equal(expectedMachineSpec)) + + // Verify that the machineTemplate.ObjectMeta has been propagated to the Machine. + for k, v := range kcpMachineTemplateObjectMeta.Labels { + g.Expect(machine.Labels[k]).To(Equal(v)) + } + for k, v := range kcpMachineTemplateObjectMeta.Annotations { + g.Expect(machine.Annotations[k]).To(Equal(v)) + } + + // Verify that machineTemplate.ObjectMeta in KCP has not been modified. + g.Expect(kcp.Spec.MachineTemplate.ObjectMeta.Labels).NotTo(HaveKey(clusterv1.ClusterLabelName)) + g.Expect(kcp.Spec.MachineTemplate.ObjectMeta.Labels).NotTo(HaveKey(clusterv1.MachineControlPlaneLabelName)) + g.Expect(kcp.Spec.MachineTemplate.ObjectMeta.Annotations).NotTo(HaveKey(controlplanev1.KubeadmClusterConfigurationAnnotation)) } func TestKubeadmControlPlaneReconciler_generateKubeadmConfig(t *testing.T) { diff --git a/controlplane/kubeadm/internal/cluster_labels.go b/controlplane/kubeadm/internal/cluster_labels.go index 4adffc6b948e..bb6be3a7472e 100644 --- a/controlplane/kubeadm/internal/cluster_labels.go +++ b/controlplane/kubeadm/internal/cluster_labels.go @@ -23,9 +23,12 @@ import ( // ControlPlaneMachineLabelsForCluster returns a set of labels to add to a control plane machine for this specific cluster. func ControlPlaneMachineLabelsForCluster(kcp *controlplanev1.KubeadmControlPlane, clusterName string) map[string]string { - labels := kcp.Spec.MachineTemplate.ObjectMeta.Labels - if labels == nil { - labels = map[string]string{} + labels := map[string]string{} + + // Add the labels from the MachineTemplate. + // Note: we intentionally don't use the map directly to ensure we don't modify the map in KCP. + for k, v := range kcp.Spec.MachineTemplate.ObjectMeta.Labels { + labels[k] = v } // Always force these labels over the ones coming from the spec.