diff --git a/controllers/argorollouts_controller.go b/controllers/argorollouts_controller.go index b2eaf32..9c1cad5 100644 --- a/controllers/argorollouts_controller.go +++ b/controllers/argorollouts_controller.go @@ -47,6 +47,10 @@ type RolloutManagerReconciler struct { client.Client Scheme *runtime.Scheme OpenShiftRoutePluginLocation string + //ArgoRolloutsCustumLabel is used to set custom labels on Argo Rollouts resources + // This is used to set the label on the ConfigMap and Secrets created by the controller for now. + // The label is used to identify the resources created by the RolloutManager. + ArgoRolloutsCustomLabel map[string]string // NamespaceScopedArgoRolloutsController is used to configure scope of Argo Rollouts controller // If value is true then deploy namespace-scoped Argo Rollouts controller else cluster-scoped diff --git a/controllers/configmap.go b/controllers/configmap.go index 925826c..62c0d92 100644 --- a/controllers/configmap.go +++ b/controllers/configmap.go @@ -41,6 +41,9 @@ func (r *RolloutManagerReconciler) reconcileConfigMap(ctx context.Context, cr ro } setRolloutsLabelsAndAnnotationsToObject(&desiredConfigMap.ObjectMeta, cr) + if r.ArgoRolloutsCustomLabel != nil { + setCustomLabels(&desiredConfigMap.ObjectMeta, r.ArgoRolloutsCustomLabel) + } trafficRouterPluginsMap := map[string]pluginItem{ OpenShiftRolloutPluginName: { diff --git a/controllers/configmap_test.go b/controllers/configmap_test.go index 024be22..68602be 100644 --- a/controllers/configmap_test.go +++ b/controllers/configmap_test.go @@ -75,6 +75,37 @@ var _ = Describe("ConfigMap Test", func() { }) + It("verifies that the custom labels are added to the ConfigMap", func() { + expectedConfigMap := &corev1.ConfigMap{ + ObjectMeta: metav1.ObjectMeta{ + Name: DefaultRolloutsConfigMapName, + Labels: map[string]string{ + "custom1": "value", + "app.kubernetes.io/component": DefaultArgoRolloutsResourceName, + "app.kubernetes.io/name": DefaultArgoRolloutsResourceName, + "app.kubernetes.io/part-of": DefaultArgoRolloutsResourceName, + }, + }, + } + + By("Call reconcileConfigMap") + Expect(r.reconcileConfigMap(ctx, a)).To(Succeed()) + + By("Verify that the fetched ConfigMap matches the desired one") + + fetchedConfigMap := &corev1.ConfigMap{} + Expect(fetchObject(ctx, r.Client, a.Namespace, expectedConfigMap.Name, fetchedConfigMap)).To(Succeed()) + + Expect(fetchedConfigMap.Labels).To(Equal(expectedConfigMap.Labels)) + + By("Call reconcileConfigMap again") + Expect(r.reconcileConfigMap(ctx, a)).To(Succeed()) + + By("verifying that the data is still present") + Expect(fetchedConfigMap.Labels).To(Equal(expectedConfigMap.Labels)) + + }) + It("verifies traffic and metric plugin creation/modification and ensures OpenShiftRolloutPlugin existence", func() { By("Add a pod that matches the deployment's selector") addTestPodToFakeClient(r, a.Namespace, existingDeployment) diff --git a/controllers/resources.go b/controllers/resources.go index b6e21e1..78a7cb9 100644 --- a/controllers/resources.go +++ b/controllers/resources.go @@ -736,6 +736,9 @@ func (r *RolloutManagerReconciler) reconcileRolloutsSecrets(ctx context.Context, } setRolloutsLabelsAndAnnotationsToObject(&expectedSecret.ObjectMeta, cr) + if r.ArgoRolloutsCustomLabel != nil { + setCustomLabels(&expectedSecret.ObjectMeta, r.ArgoRolloutsCustomLabel) + } // If the Secret doesn't exist (or an unrelated error occurred).... liveSecret := &corev1.Secret{ObjectMeta: metav1.ObjectMeta{Name: expectedSecret.Name, Namespace: expectedSecret.Namespace}} diff --git a/controllers/resources_test.go b/controllers/resources_test.go index 89ecf54..da07202 100644 --- a/controllers/resources_test.go +++ b/controllers/resources_test.go @@ -538,6 +538,26 @@ var _ = Describe("Resource creation and cleanup tests", func() { Expect(secret.ObjectMeta.Annotations["keyannotation"]).To(Equal(a.Spec.AdditionalMetadata.Annotations["keyannotation"])) }) + It("Test for Custom Labels for secrets created by Rollouts Manager function", func() { + Expect(r.reconcileRolloutsSecrets(ctx, a)).To(Succeed()) + secret := &corev1.Secret{ + ObjectMeta: metav1.ObjectMeta{ + Name: DefaultRolloutsNotificationSecretName, + Namespace: a.Namespace, + Labels: map[string]string{ + "app.kubernetes.io/component": "argo-rollouts", + "app.kubernetes.io/name": "argo-rollouts", + "app.kubernetes.io/part-of": "argo-rollouts", + "custom1": "value", + "keylabel": "valuelabel", + }, + }, + } + fetchedSecret := &corev1.Secret{} + Expect(fetchObject(ctx, r.Client, a.Namespace, secret.Name, fetchedSecret)).To(Succeed()) + Expect(fetchedSecret.ObjectMeta.Labels).To(Equal(secret.ObjectMeta.Labels)) + }) + It("test for removeClusterScopedResourcesIfApplicable function", func() { By("creating default cluster-scoped ClusterRole/ClusterRoleBinding. These should be deleted by the call to removeClusterScopedResourcesIfApplicable") diff --git a/controllers/utils.go b/controllers/utils.go index 7ddcbbf..171aaad 100644 --- a/controllers/utils.go +++ b/controllers/utils.go @@ -66,6 +66,17 @@ func setRolloutsLabelsAndAnnotations(obj *metav1.ObjectMeta) { obj.Labels["app.kubernetes.io/component"] = DefaultArgoRolloutsResourceName } +func setCustomLabels(obj *metav1.ObjectMeta, customLabels map[string]string) { + if obj.Labels == nil { + obj.Labels = map[string]string{} + } + + for k, v := range customLabels { + obj.Labels[k] = v + } + +} + // fetchObject will retrieve the object with the given namespace and name using the Kubernetes API. // The result will be stored in the given object. func fetchObject(ctx context.Context, client client.Client, namespace string, name string, obj client.Object) error { diff --git a/controllers/utils_test.go b/controllers/utils_test.go index a8d9c98..905ffd3 100644 --- a/controllers/utils_test.go +++ b/controllers/utils_test.go @@ -792,8 +792,11 @@ func makeTestReconciler(obj ...client.Object) *RolloutManagerReconciler { cl := fake.NewClientBuilder().WithScheme(s).WithStatusSubresource(obj...).WithObjects(obj...).Build() return &RolloutManagerReconciler{ - Client: cl, - Scheme: s, + Client: cl, + Scheme: s, + ArgoRolloutsCustomLabel: map[string]string{ + "custom1": "value", + }, OpenShiftRoutePluginLocation: "file://non-empty-test-url", // Set a non-real, non-empty value for unit tests: override this to test a specific value } }