Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 KCP: stop reconciling ObjectMeta into the KCP machine template #5187

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions controlplane/kubeadm/controllers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
},
Expand All @@ -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)

Expand Down
24 changes: 24 additions & 0 deletions controlplane/kubeadm/controllers/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,13 +486,24 @@ 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",
Namespace: cluster.Namespace,
},
Spec: controlplanev1.KubeadmControlPlaneSpec{
Version: "v1.16.6",
MachineTemplate: controlplanev1.KubeadmControlPlaneMachineTemplate{
ObjectMeta: kcpMachineTemplateObjectMeta,
},
},
}

Expand Down Expand Up @@ -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) {
Expand Down
9 changes: 6 additions & 3 deletions controlplane/kubeadm/internal/cluster_labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down