Skip to content

Commit 5478995

Browse files
authored
MGMT-2212: propagate annotations and labels from OpenshiftAssistedControlplane to its machine's OpenshiftAssistedConfig (#458)
Signed-off-by: Riccardo Piccoli <[email protected]>
1 parent 7301392 commit 5478995

File tree

2 files changed

+122
-6
lines changed

2 files changed

+122
-6
lines changed

controlplane/internal/controller/openshiftassistedcontrolplane_controller.go

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -779,18 +779,41 @@ func (r *OpenshiftAssistedControlPlaneReconciler) computeInfraRef(ctx context.Co
779779
return infraRef, nil
780780
}
781781

782-
func (r *OpenshiftAssistedControlPlaneReconciler) generateOpenshiftAssistedConfig(acp *controlplanev1alpha2.OpenshiftAssistedControlPlane, clusterName string, name string) *bootstrapv1alpha1.OpenshiftAssistedConfig {
782+
func (r *OpenshiftAssistedControlPlaneReconciler) generateOpenshiftAssistedConfig(oacp *controlplanev1alpha2.OpenshiftAssistedControlPlane, clusterName string, name string) *bootstrapv1alpha1.OpenshiftAssistedConfig {
783+
labels := util.ControlPlaneMachineLabelsForCluster(oacp, clusterName)
784+
785+
// Merge in labels from the OpenshiftAssistedControlPlane itself
786+
// This allows users to set labels on the control plane that will be propagated to the configs
787+
for k, v := range oacp.Labels {
788+
if _, exists := labels[k]; !exists {
789+
labels[k] = v
790+
}
791+
}
792+
793+
annotations := make(map[string]string)
794+
for k, v := range oacp.Spec.MachineTemplate.ObjectMeta.Annotations {
795+
annotations[k] = v
796+
}
797+
798+
// Merge in annotations from the OpenshiftAssistedControlPlane itself
799+
// This allows propagation of discovery-ignition-override and other annotations
800+
for k, v := range oacp.Annotations {
801+
if _, exists := annotations[k]; !exists {
802+
annotations[k] = v
803+
}
804+
}
805+
783806
bootstrapConfig := &bootstrapv1alpha1.OpenshiftAssistedConfig{
784807
ObjectMeta: metav1.ObjectMeta{
785808
Name: name,
786-
Namespace: acp.Namespace,
787-
Labels: util.ControlPlaneMachineLabelsForCluster(acp, clusterName),
788-
Annotations: acp.Spec.MachineTemplate.ObjectMeta.Annotations,
809+
Namespace: oacp.Namespace,
810+
Labels: labels,
811+
Annotations: annotations,
789812
},
790-
Spec: *acp.Spec.OpenshiftAssistedConfigSpec.DeepCopy(),
813+
Spec: *oacp.Spec.OpenshiftAssistedConfigSpec.DeepCopy(),
791814
}
792815

793-
_ = controllerutil.SetOwnerReference(acp, bootstrapConfig, r.Scheme)
816+
_ = controllerutil.SetOwnerReference(oacp, bootstrapConfig, r.Scheme)
794817
return bootstrapConfig
795818
}
796819

controlplane/internal/controller/openshiftassistedcontrolplane_controller_test.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import (
4242
fakeclient "sigs.k8s.io/controller-runtime/pkg/client/fake"
4343
"sigs.k8s.io/controller-runtime/pkg/reconcile"
4444

45+
bootstrapv1alpha1 "github.com/openshift-assisted/cluster-api-provider-openshift-assisted/bootstrap/api/v1alpha1"
4546
controlplanev1alpha2 "github.com/openshift-assisted/cluster-api-provider-openshift-assisted/controlplane/api/v1alpha2"
4647
testutils "github.com/openshift-assisted/cluster-api-provider-openshift-assisted/test/utils"
4748
corev1 "k8s.io/api/core/v1"
@@ -871,6 +872,98 @@ var _ = Describe("Scale operations and machine updates", func() {
871872
Expect(oacp.Status.UpdatedReplicas).To(Equal(int32(0)))
872873
})
873874
})
875+
876+
Context("Label propagation", func() {
877+
BeforeEach(func() {
878+
oacp.Spec.Replicas = 1
879+
// Add custom labels to the OpenshiftAssistedControlPlane
880+
oacp.Labels = map[string]string{
881+
"custom-label": "custom-value",
882+
"discovery-ignition": "enabled",
883+
"test-label": "test-value",
884+
}
885+
Expect(k8sClient.Create(ctx, oacp)).To(Succeed())
886+
})
887+
888+
It("should copy labels from OpenshiftAssistedControlPlane to OpenshiftAssistedConfig", func() {
889+
// Reconcile to create machines and bootstrap configs
890+
result, err := controllerReconciler.Reconcile(ctx, reconcile.Request{NamespacedName: typeNamespacedName})
891+
Expect(err).NotTo(HaveOccurred())
892+
Expect(result).To(Equal(ctrlruntime.Result{}))
893+
894+
// Get the created bootstrap configs
895+
bootstrapConfigList := &bootstrapv1alpha1.OpenshiftAssistedConfigList{}
896+
Expect(k8sClient.List(ctx, bootstrapConfigList, client.InNamespace(namespace))).To(Succeed())
897+
Expect(bootstrapConfigList.Items).To(HaveLen(1))
898+
899+
bootstrapConfig := &bootstrapConfigList.Items[0]
900+
901+
// Verify that custom labels from oacp are present in the bootstrap config
902+
Expect(bootstrapConfig.Labels).To(HaveKeyWithValue("custom-label", "custom-value"))
903+
Expect(bootstrapConfig.Labels).To(HaveKeyWithValue("discovery-ignition", "enabled"))
904+
Expect(bootstrapConfig.Labels).To(HaveKeyWithValue("test-label", "test-value"))
905+
906+
// Verify that standard control plane labels are also present
907+
Expect(bootstrapConfig.Labels).To(HaveKeyWithValue(clusterv1.ClusterNameLabel, clusterName))
908+
Expect(bootstrapConfig.Labels).To(HaveKey(clusterv1.MachineControlPlaneLabel))
909+
Expect(bootstrapConfig.Labels).To(HaveKey(clusterv1.MachineControlPlaneNameLabel))
910+
})
911+
912+
It("should not override standard control plane labels with custom labels", func() {
913+
// Update oacp with labels that would conflict with standard labels
914+
Expect(k8sClient.Get(ctx, typeNamespacedName, oacp)).To(Succeed())
915+
oacp.Labels[clusterv1.ClusterNameLabel] = "wrong-cluster-name"
916+
Expect(k8sClient.Update(ctx, oacp)).To(Succeed())
917+
918+
// Reconcile to create machines and bootstrap configs
919+
result, err := controllerReconciler.Reconcile(ctx, reconcile.Request{NamespacedName: typeNamespacedName})
920+
Expect(err).NotTo(HaveOccurred())
921+
Expect(result).To(Equal(ctrlruntime.Result{}))
922+
923+
// Get the created bootstrap configs
924+
bootstrapConfigList := &bootstrapv1alpha1.OpenshiftAssistedConfigList{}
925+
Expect(k8sClient.List(ctx, bootstrapConfigList, client.InNamespace(namespace))).To(Succeed())
926+
Expect(bootstrapConfigList.Items).To(HaveLen(1))
927+
928+
bootstrapConfig := &bootstrapConfigList.Items[0]
929+
930+
// Verify that the standard cluster name label is NOT overridden by the custom label
931+
Expect(bootstrapConfig.Labels).To(HaveKeyWithValue(clusterv1.ClusterNameLabel, clusterName))
932+
Expect(bootstrapConfig.Labels).NotTo(HaveKeyWithValue(clusterv1.ClusterNameLabel, "wrong-cluster-name"))
933+
})
934+
})
935+
936+
Context("Annotation propagation for discovery ignition", func() {
937+
BeforeEach(func() {
938+
oacp.Spec.Replicas = 1
939+
})
940+
941+
It("should propagate discovery-ignition-override annotation from OpenshiftAssistedControlPlane to OpenshiftAssistedConfig", func() {
942+
// Add discovery-ignition-override annotation to the OpenshiftAssistedControlPlane
943+
discoveryIgnitionOverride := `{"ignition":{"version":"3.2.0"},"storage":{"files":[{"path":"/etc/test","contents":{"source":"data:,test"}}]}}`
944+
oacp.Annotations = map[string]string{
945+
bootstrapv1alpha1.DiscoveryIgnitionOverrideAnnotation: discoveryIgnitionOverride,
946+
"custom-annotation": "custom-value",
947+
}
948+
Expect(k8sClient.Create(ctx, oacp)).To(Succeed())
949+
950+
// Reconcile to create machines and bootstrap configs
951+
result, err := controllerReconciler.Reconcile(ctx, reconcile.Request{NamespacedName: typeNamespacedName})
952+
Expect(err).NotTo(HaveOccurred())
953+
Expect(result).To(Equal(ctrlruntime.Result{}))
954+
955+
// Get the created bootstrap configs
956+
bootstrapConfigList := &bootstrapv1alpha1.OpenshiftAssistedConfigList{}
957+
Expect(k8sClient.List(ctx, bootstrapConfigList, client.InNamespace(namespace))).To(Succeed())
958+
Expect(bootstrapConfigList.Items).To(HaveLen(1))
959+
960+
bootstrapConfig := &bootstrapConfigList.Items[0]
961+
962+
// Verify that annotations from oacp are present in the bootstrap config
963+
Expect(bootstrapConfig.Annotations).To(HaveKeyWithValue(bootstrapv1alpha1.DiscoveryIgnitionOverrideAnnotation, discoveryIgnitionOverride))
964+
Expect(bootstrapConfig.Annotations).To(HaveKeyWithValue("custom-annotation", "custom-value"))
965+
})
966+
})
874967
})
875968

876969
// Create dummy machine template

0 commit comments

Comments
 (0)