From a6c7e0822d90308cb043612543f848af164ec34b Mon Sep 17 00:00:00 2001 From: Guillaume Doussin Date: Fri, 15 Dec 2023 14:43:07 +0100 Subject: [PATCH 1/2] feat: Support additional metadata for controller Signed-off-by: Guillaume Doussin --- api/v1alpha1/argorollouts_types.go | 12 +++ api/v1alpha1/zz_generated.deepcopy.go | 34 ++++++++ .../bases/argoproj.io_rolloutmanagers.yaml | 14 ++++ controllers/configmap.go | 3 + controllers/deployment.go | 39 ++++++--- controllers/deployment_test.go | 2 +- controllers/reconcile.go | 10 +-- controllers/resources.go | 27 ++++--- controllers/resources_test.go | 22 ++--- controllers/utils.go | 29 ++++++- docs/crd_reference.md | 19 +++++ examples/custom_metadata_rolloutmanager.yaml | 15 ++++ .../cluster_scoped_rollouts_test.go | 12 ++- .../namespace_scoped_rollouts_test.go | 38 +++++++++ tests/e2e/rollouts_test.go | 62 ++++++++++++++ tests/e2e/test_utils.go | 81 +++++++++++++------ 16 files changed, 352 insertions(+), 67 deletions(-) create mode 100644 examples/custom_metadata_rolloutmanager.yaml diff --git a/api/v1alpha1/argorollouts_types.go b/api/v1alpha1/argorollouts_types.go index cfeba9c..985e6ee 100644 --- a/api/v1alpha1/argorollouts_types.go +++ b/api/v1alpha1/argorollouts_types.go @@ -43,6 +43,9 @@ type RolloutManagerSpec struct { // NamespaceScoped lets you specify if RolloutManager has to watch a namespace or the whole cluster NamespaceScoped bool `json:"namespaceScoped,omitempty"` + + // Metadata to apply to the generated resources + AdditionalMetadata *ResourceMetadata `json:"additionalMetadata,omitempty"` } // ArgoRolloutsNodePlacementSpec is used to specify NodeSelector and Tolerations for Rollouts workloads @@ -92,6 +95,15 @@ const ( RolloutManagerReasonInvalidScoped = "InvalidRolloutManagerScope" ) +type ResourceMetadata struct { + // Annotations to add to the resources during its creation. + // +optional + Annotations map[string]string `json:"annotations,omitempty"` + // Labels to add to the resources during its creation. + // +optional + Labels map[string]string `json:"labels,omitempty"` +} + //+kubebuilder:object:root=true //+kubebuilder:subresource:status diff --git a/api/v1alpha1/zz_generated.deepcopy.go b/api/v1alpha1/zz_generated.deepcopy.go index 6f5e116..ff3ee75 100644 --- a/api/v1alpha1/zz_generated.deepcopy.go +++ b/api/v1alpha1/zz_generated.deepcopy.go @@ -27,6 +27,35 @@ import ( runtime "k8s.io/apimachinery/pkg/runtime" ) +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *ResourceMetadata) DeepCopyInto(out *ResourceMetadata) { + *out = *in + if in.Annotations != nil { + in, out := &in.Annotations, &out.Annotations + *out = make(map[string]string, len(*in)) + for key, val := range *in { + (*out)[key] = val + } + } + if in.Labels != nil { + in, out := &in.Labels, &out.Labels + *out = make(map[string]string, len(*in)) + for key, val := range *in { + (*out)[key] = val + } + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ResourceMetadata. +func (in *ResourceMetadata) DeepCopy() *ResourceMetadata { + if in == nil { + return nil + } + out := new(ResourceMetadata) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *RolloutManager) DeepCopyInto(out *RolloutManager) { *out = *in @@ -106,6 +135,11 @@ func (in *RolloutManagerSpec) DeepCopyInto(out *RolloutManagerSpec) { *out = new(RolloutsNodePlacementSpec) (*in).DeepCopyInto(*out) } + if in.AdditionalMetadata != nil { + in, out := &in.AdditionalMetadata, &out.AdditionalMetadata + *out = new(ResourceMetadata) + (*in).DeepCopyInto(*out) + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new RolloutManagerSpec. diff --git a/config/crd/bases/argoproj.io_rolloutmanagers.yaml b/config/crd/bases/argoproj.io_rolloutmanagers.yaml index be7ee2c..ff9daf6 100644 --- a/config/crd/bases/argoproj.io_rolloutmanagers.yaml +++ b/config/crd/bases/argoproj.io_rolloutmanagers.yaml @@ -35,6 +35,20 @@ spec: spec: description: RolloutManagerSpec defines the desired state of Argo Rollouts properties: + additionalMetadata: + description: Metadata to apply to the generated resources + properties: + annotations: + additionalProperties: + type: string + description: Annotations to add to the resources during its creation. + type: object + labels: + additionalProperties: + type: string + description: Labels to add to the resources during its creation. + type: object + type: object env: description: Env lets you specify environment for Rollouts pods items: diff --git a/controllers/configmap.go b/controllers/configmap.go index 41ec103..c554454 100644 --- a/controllers/configmap.go +++ b/controllers/configmap.go @@ -30,6 +30,9 @@ func (r *RolloutManagerReconciler) reconcileConfigMap(ctx context.Context, cr ro }, }, } + + setRolloutsLabelsAndAnnotationsToObject(&desiredConfigMap.ObjectMeta, &cr) + trafficRouterPlugins := []pluginItem{ { Name: OpenShiftRolloutPluginName, diff --git a/controllers/deployment.go b/controllers/deployment.go index 20c4c89..5dd56f8 100644 --- a/controllers/deployment.go +++ b/controllers/deployment.go @@ -26,20 +26,30 @@ func generateDesiredRolloutsDeployment(cr rolloutsmanagerv1alpha1.RolloutManager Namespace: cr.Namespace, }, } + setRolloutsLabelsAndAnnotationsToObject(&desiredDeployment.ObjectMeta, &cr) - setRolloutsLabels(&desiredDeployment.ObjectMeta) + // Add labels and annotations as well to the pod template + labels := map[string]string{ + DefaultRolloutsSelectorKey: DefaultArgoRolloutsResourceName, + } + annotations := map[string]string{} + if cr.Spec.AdditionalMetadata != nil { + for k, v := range cr.Spec.AdditionalMetadata.Labels { + labels[k] = v + } + for k, v := range cr.Spec.AdditionalMetadata.Annotations { + annotations[k] = v + } + } desiredDeployment.Spec = appsv1.DeploymentSpec{ Selector: &metav1.LabelSelector{ - MatchLabels: map[string]string{ - DefaultRolloutsSelectorKey: DefaultArgoRolloutsResourceName, - }, + MatchLabels: labels, }, Template: corev1.PodTemplateSpec{ ObjectMeta: metav1.ObjectMeta{ - Labels: map[string]string{ - DefaultRolloutsSelectorKey: DefaultArgoRolloutsResourceName, - }, + Labels: labels, + Annotations: annotations, }, Spec: corev1.PodSpec{ NodeSelector: map[string]string{ @@ -123,6 +133,7 @@ func (r *RolloutManagerReconciler) reconcileRolloutsDeployment(ctx context.Conte actualDeployment.Spec.Template.Spec.ServiceAccountName = desiredDeployment.Spec.Template.Spec.ServiceAccountName actualDeployment.Labels = desiredDeployment.Labels actualDeployment.Spec.Template.Labels = desiredDeployment.Spec.Template.Labels + actualDeployment.Spec.Template.Annotations = desiredDeployment.Spec.Template.Annotations actualDeployment.Spec.Selector = desiredDeployment.Spec.Selector actualDeployment.Spec.Template.Spec.NodeSelector = desiredDeployment.Spec.Template.Spec.NodeSelector actualDeployment.Spec.Template.Spec.Tolerations = desiredDeployment.Spec.Template.Spec.Tolerations @@ -155,6 +166,10 @@ func identifyDeploymentDifference(x appsv1.Deployment, y appsv1.Deployment) stri return ".Spec.Template.Labels" } + if !reflect.DeepEqual(x.Spec.Template.Annotations, y.Spec.Template.Annotations) { + return ".Spec.Template.Annotations" + } + if !reflect.DeepEqual(x.Spec.Selector, y.Spec.Selector) { return ".Spec.Selector" } @@ -268,9 +283,10 @@ func normalizeDeployment(inputParam appsv1.Deployment) (appsv1.Deployment, error res := appsv1.Deployment{ ObjectMeta: metav1.ObjectMeta{ - Name: input.ObjectMeta.Name, - Namespace: input.ObjectMeta.Namespace, - Labels: input.ObjectMeta.Labels, + Name: input.ObjectMeta.Name, + Namespace: input.ObjectMeta.Namespace, + Labels: input.ObjectMeta.Labels, + Annotations: input.ObjectMeta.Annotations, }, } @@ -295,7 +311,8 @@ func normalizeDeployment(inputParam appsv1.Deployment) (appsv1.Deployment, error }, Template: corev1.PodTemplateSpec{ ObjectMeta: metav1.ObjectMeta{ - Labels: input.Spec.Template.Labels, + Labels: input.Spec.Template.Labels, + Annotations: input.Spec.Template.Annotations, }, Spec: corev1.PodSpec{ NodeSelector: input.Spec.Template.Spec.NodeSelector, diff --git a/controllers/deployment_test.go b/controllers/deployment_test.go index 9c8f19f..c654811 100644 --- a/controllers/deployment_test.go +++ b/controllers/deployment_test.go @@ -223,7 +223,7 @@ func deploymentCR(name string, namespace string, label string, volumeName string Namespace: namespace, }, } - setRolloutsLabels(&deploymentCR.ObjectMeta) + setRolloutsLabelsAndAnnotationsToObject(&deploymentCR.ObjectMeta, &rolloutManager) deploymentCR.Spec = appsv1.DeploymentSpec{ Selector: &metav1.LabelSelector{ MatchLabels: map[string]string{ diff --git a/controllers/reconcile.go b/controllers/reconcile.go index 6aacd98..21acd45 100644 --- a/controllers/reconcile.go +++ b/controllers/reconcile.go @@ -64,7 +64,7 @@ func (r *RolloutManagerReconciler) reconcileRolloutsManager(ctx context.Context, } } else { log.Info("reconciling Rollouts ClusterRoles") - clusterRole, err = r.reconcileRolloutsClusterRole(ctx) + clusterRole, err = r.reconcileRolloutsClusterRole(ctx, cr) if err != nil { log.Error(err, "failed to reconcile Rollout's ClusterRoles.") return wrapCondition(createCondition(err.Error())), err @@ -72,19 +72,19 @@ func (r *RolloutManagerReconciler) reconcileRolloutsManager(ctx context.Context, } log.Info("reconciling aggregate-to-admin ClusterRole") - if err := r.reconcileRolloutsAggregateToAdminClusterRole(ctx); err != nil { + if err := r.reconcileRolloutsAggregateToAdminClusterRole(ctx, cr); err != nil { log.Error(err, "failed to reconcile Rollout's aggregate-to-admin ClusterRoles.") return wrapCondition(createCondition(err.Error())), err } log.Info("reconciling aggregate-to-edit ClusterRole") - if err := r.reconcileRolloutsAggregateToEditClusterRole(ctx); err != nil { + if err := r.reconcileRolloutsAggregateToEditClusterRole(ctx, cr); err != nil { log.Error(err, "failed to reconcile Rollout's aggregate-to-edit ClusterRoles.") return wrapCondition(createCondition(err.Error())), err } log.Info("reconciling aggregate-to-view ClusterRole") - if err := r.reconcileRolloutsAggregateToViewClusterRole(ctx); err != nil { + if err := r.reconcileRolloutsAggregateToViewClusterRole(ctx, cr); err != nil { log.Error(err, "failed to reconcile Rollout's aggregate-to-view ClusterRoles.") return wrapCondition(createCondition(err.Error())), err } @@ -97,7 +97,7 @@ func (r *RolloutManagerReconciler) reconcileRolloutsManager(ctx context.Context, } } else { log.Info("reconciling Rollouts ClusterRoleBinding") - if err := r.reconcileRolloutsClusterRoleBinding(ctx, clusterRole, sa); err != nil { + if err := r.reconcileRolloutsClusterRoleBinding(ctx, clusterRole, sa, cr); err != nil { log.Error(err, "failed to reconcile Rollout's ClusterRoleBinding.") return wrapCondition(createCondition(err.Error())), err } diff --git a/controllers/resources.go b/controllers/resources.go index f11d91d..c935b7f 100644 --- a/controllers/resources.go +++ b/controllers/resources.go @@ -24,7 +24,7 @@ func (r *RolloutManagerReconciler) reconcileRolloutsServiceAccount(ctx context.C Namespace: cr.Namespace, }, } - setRolloutsLabels(&sa.ObjectMeta) + setRolloutsLabelsAndAnnotationsToObject(&sa.ObjectMeta, &cr) if err := fetchObject(ctx, r.Client, cr.Namespace, sa.Name, sa); err != nil { if !apierrors.IsNotFound(err) { @@ -56,7 +56,7 @@ func (r *RolloutManagerReconciler) reconcileRolloutsRole(ctx context.Context, cr Namespace: cr.Namespace, }, } - setRolloutsLabels(&role.ObjectMeta) + setRolloutsLabelsAndAnnotationsToObject(&role.ObjectMeta, &cr) if err := fetchObject(ctx, r.Client, cr.Namespace, role.Name, role); err != nil { if !apierrors.IsNotFound(err) { @@ -83,7 +83,7 @@ func (r *RolloutManagerReconciler) reconcileRolloutsRole(ctx context.Context, cr } // Reconciles Rollouts ClusterRole. -func (r *RolloutManagerReconciler) reconcileRolloutsClusterRole(ctx context.Context) (*rbacv1.ClusterRole, error) { +func (r *RolloutManagerReconciler) reconcileRolloutsClusterRole(ctx context.Context, cr rolloutsmanagerv1alpha1.RolloutManager) (*rbacv1.ClusterRole, error) { expectedPolicyRules := GetPolicyRules() @@ -92,7 +92,7 @@ func (r *RolloutManagerReconciler) reconcileRolloutsClusterRole(ctx context.Cont Name: DefaultArgoRolloutsResourceName, }, } - setRolloutsLabels(&clusterRole.ObjectMeta) + setRolloutsLabelsAndAnnotationsToObject(&clusterRole.ObjectMeta, &cr) if err := fetchObject(ctx, r.Client, "", clusterRole.Name, clusterRole); err != nil { if !apierrors.IsNotFound(err) { @@ -131,7 +131,7 @@ func (r *RolloutManagerReconciler) reconcileRolloutsRoleBinding(ctx context.Cont Namespace: cr.Namespace, }, } - setRolloutsLabels(&expectedRoleBinding.ObjectMeta) + setRolloutsLabelsAndAnnotationsToObject(&expectedRoleBinding.ObjectMeta, &cr) expectedRoleBinding.RoleRef = rbacv1.RoleRef{ APIGroup: rbacv1.GroupName, @@ -176,7 +176,7 @@ func (r *RolloutManagerReconciler) reconcileRolloutsRoleBinding(ctx context.Cont } // Reconcile Rollouts ClusterRoleBinding. -func (r *RolloutManagerReconciler) reconcileRolloutsClusterRoleBinding(ctx context.Context, clusterRole *rbacv1.ClusterRole, sa *corev1.ServiceAccount) error { +func (r *RolloutManagerReconciler) reconcileRolloutsClusterRoleBinding(ctx context.Context, clusterRole *rbacv1.ClusterRole, sa *corev1.ServiceAccount, cr rolloutsmanagerv1alpha1.RolloutManager) error { if clusterRole == nil { return fmt.Errorf("received ClusterRole is nil while reconciling ClusterRoleBinding") @@ -191,7 +191,7 @@ func (r *RolloutManagerReconciler) reconcileRolloutsClusterRoleBinding(ctx conte Name: DefaultArgoRolloutsResourceName, }, } - setRolloutsLabels(&expectedClusterRoleBinding.ObjectMeta) + setRolloutsLabelsAndAnnotationsToObject(&expectedClusterRoleBinding.ObjectMeta, &cr) expectedClusterRoleBinding.RoleRef = rbacv1.RoleRef{ APIGroup: rbacv1.GroupName, @@ -232,7 +232,7 @@ func (r *RolloutManagerReconciler) reconcileRolloutsClusterRoleBinding(ctx conte } // Reconciles aggregate-to-admin ClusterRole. -func (r *RolloutManagerReconciler) reconcileRolloutsAggregateToAdminClusterRole(ctx context.Context) error { +func (r *RolloutManagerReconciler) reconcileRolloutsAggregateToAdminClusterRole(ctx context.Context, cr rolloutsmanagerv1alpha1.RolloutManager) error { var aggregationType string = "aggregate-to-admin" name := fmt.Sprintf("%s-%s", DefaultArgoRolloutsResourceName, aggregationType) @@ -245,6 +245,7 @@ func (r *RolloutManagerReconciler) reconcileRolloutsAggregateToAdminClusterRole( }, } setRolloutsAggregatedClusterRoleLabels(&clusterRole.ObjectMeta, name, aggregationType) + setAdditionalRolloutsLabelsAndAnnotationsToObject(&clusterRole.ObjectMeta, &cr) if err := fetchObject(ctx, r.Client, "", clusterRole.Name, clusterRole); err != nil { if !apierrors.IsNotFound(err) { @@ -267,7 +268,7 @@ func (r *RolloutManagerReconciler) reconcileRolloutsAggregateToAdminClusterRole( } // Reconciles aggregate-to-edit ClusterRole. -func (r *RolloutManagerReconciler) reconcileRolloutsAggregateToEditClusterRole(ctx context.Context) error { +func (r *RolloutManagerReconciler) reconcileRolloutsAggregateToEditClusterRole(ctx context.Context, cr rolloutsmanagerv1alpha1.RolloutManager) error { var aggregationType string = "aggregate-to-edit" name := fmt.Sprintf("%s-%s", DefaultArgoRolloutsResourceName, aggregationType) @@ -280,6 +281,7 @@ func (r *RolloutManagerReconciler) reconcileRolloutsAggregateToEditClusterRole(c }, } setRolloutsAggregatedClusterRoleLabels(&clusterRole.ObjectMeta, name, aggregationType) + setAdditionalRolloutsLabelsAndAnnotationsToObject(&clusterRole.ObjectMeta, &cr) if err := fetchObject(ctx, r.Client, "", clusterRole.Name, clusterRole); err != nil { if !apierrors.IsNotFound(err) { @@ -302,7 +304,7 @@ func (r *RolloutManagerReconciler) reconcileRolloutsAggregateToEditClusterRole(c } // Reconciles aggregate-to-view ClusterRole. -func (r *RolloutManagerReconciler) reconcileRolloutsAggregateToViewClusterRole(ctx context.Context) error { +func (r *RolloutManagerReconciler) reconcileRolloutsAggregateToViewClusterRole(ctx context.Context, cr rolloutsmanagerv1alpha1.RolloutManager) error { var aggregationType string = "aggregate-to-view" name := fmt.Sprintf("%s-%s", DefaultArgoRolloutsResourceName, aggregationType) @@ -315,6 +317,7 @@ func (r *RolloutManagerReconciler) reconcileRolloutsAggregateToViewClusterRole(c }, } setRolloutsAggregatedClusterRoleLabels(&clusterRole.ObjectMeta, name, aggregationType) + setAdditionalRolloutsLabelsAndAnnotationsToObject(&clusterRole.ObjectMeta, &cr) if err := fetchObject(ctx, r.Client, "", clusterRole.Name, clusterRole); err != nil { if !apierrors.IsNotFound(err) { @@ -345,7 +348,7 @@ func (r *RolloutManagerReconciler) reconcileRolloutsMetricsService(ctx context.C Namespace: cr.Namespace, }, } - setRolloutsLabels(&expectedSvc.ObjectMeta) + setRolloutsLabelsAndAnnotationsToObject(&expectedSvc.ObjectMeta, &cr) // overwrite the annotations for Rollouts Metrics Service expectedSvc.ObjectMeta.Labels["app.kubernetes.io/name"] = DefaultArgoRolloutsMetricsServiceName expectedSvc.ObjectMeta.Labels["app.kubernetes.io/component"] = "server" @@ -398,6 +401,8 @@ func (r *RolloutManagerReconciler) reconcileRolloutsSecrets(ctx context.Context, Type: corev1.SecretTypeOpaque, } + setRolloutsLabelsAndAnnotationsToObject(&secret.ObjectMeta, &cr) + if err := fetchObject(ctx, r.Client, cr.Namespace, secret.Name, secret); err != nil { if !apierrors.IsNotFound(err) { return fmt.Errorf("failed to get the Secret %s: %w", secret.Name, err) diff --git a/controllers/resources_test.go b/controllers/resources_test.go index b3f5cfb..1786421 100644 --- a/controllers/resources_test.go +++ b/controllers/resources_test.go @@ -52,7 +52,7 @@ var _ = Describe("Resource creation and cleanup tests", func() { }) It("Test for reconcileRolloutsClusterRole function", func() { - clusterRole, err := r.reconcileRolloutsClusterRole(ctx) + clusterRole, err := r.reconcileRolloutsClusterRole(ctx, a) Expect(err).ToNot(HaveOccurred()) By("Modify Rules of Role.") @@ -60,7 +60,7 @@ var _ = Describe("Resource creation and cleanup tests", func() { Expect(r.Client.Update(ctx, clusterRole)).To(Succeed()) By("Reconciler should revert modifications.") - clusterRole, err = r.reconcileRolloutsClusterRole(ctx) + clusterRole, err = r.reconcileRolloutsClusterRole(ctx, a) Expect(err).ToNot(HaveOccurred()) Expect(clusterRole.Rules).To(Equal(GetPolicyRules())) }) @@ -95,10 +95,10 @@ var _ = Describe("Resource creation and cleanup tests", func() { It("Test for reconcileRolloutsClusterRoleBinding function", func() { sa, err := r.reconcileRolloutsServiceAccount(ctx, a) Expect(err).ToNot(HaveOccurred()) - clusterRole, err := r.reconcileRolloutsClusterRole(ctx) + clusterRole, err := r.reconcileRolloutsClusterRole(ctx, a) Expect(err).ToNot(HaveOccurred()) - Expect(r.reconcileRolloutsClusterRoleBinding(ctx, clusterRole, sa)).To(Succeed()) + Expect(r.reconcileRolloutsClusterRoleBinding(ctx, clusterRole, sa, a)).To(Succeed()) By("Modify Subject of ClusterRoleBinding.") crb := &rbacv1.ClusterRoleBinding{ @@ -113,13 +113,13 @@ var _ = Describe("Resource creation and cleanup tests", func() { Expect(r.Client.Update(ctx, crb)).To(Succeed()) By("Reconciler should revert modifications.") - Expect(r.reconcileRolloutsClusterRoleBinding(ctx, clusterRole, sa)).To(Succeed()) + Expect(r.reconcileRolloutsClusterRoleBinding(ctx, clusterRole, sa, a)).To(Succeed()) Expect(fetchObject(ctx, r.Client, "", crb.Name, crb)).To(Succeed()) Expect(crb.Subjects).To(Equal(subTemp)) }) It("Test for reconcileRolloutsAggregateToAdminClusterRole function", func() { - Expect(r.reconcileRolloutsAggregateToAdminClusterRole(ctx)).To(Succeed()) + Expect(r.reconcileRolloutsAggregateToAdminClusterRole(ctx, a)).To(Succeed()) By("Modify Rules of ClusterRole.") clusterRole := &rbacv1.ClusterRole{ @@ -132,13 +132,13 @@ var _ = Describe("Resource creation and cleanup tests", func() { Expect(r.Client.Update(ctx, clusterRole)).To(Succeed()) By("Reconciler should revert modifications.") - Expect(r.reconcileRolloutsAggregateToAdminClusterRole(ctx)).To(Succeed()) + Expect(r.reconcileRolloutsAggregateToAdminClusterRole(ctx, a)).To(Succeed()) Expect(fetchObject(ctx, r.Client, "", clusterRole.Name, clusterRole)).To(Succeed()) Expect(clusterRole.Rules).To(Equal(GetAggregateToAdminPolicyRules())) }) It("Test for reconcileRolloutsAggregateToEditClusterRole function", func() { - Expect(r.reconcileRolloutsAggregateToEditClusterRole(ctx)).To(Succeed()) + Expect(r.reconcileRolloutsAggregateToEditClusterRole(ctx, a)).To(Succeed()) By("Modify Rules of ClusterRole.") clusterRole := &rbacv1.ClusterRole{ @@ -151,13 +151,13 @@ var _ = Describe("Resource creation and cleanup tests", func() { Expect(r.Client.Update(ctx, clusterRole)).To(Succeed()) By("Reconciler should revert modifications.") - Expect(r.reconcileRolloutsAggregateToEditClusterRole(ctx)).To(Succeed()) + Expect(r.reconcileRolloutsAggregateToEditClusterRole(ctx, a)).To(Succeed()) Expect(fetchObject(ctx, r.Client, "", clusterRole.Name, clusterRole)).To(Succeed()) Expect(clusterRole.Rules).To(Equal(GetAggregateToEditPolicyRules())) }) It("Test for reconcileRolloutsAggregateToViewClusterRole function", func() { - Expect(r.reconcileRolloutsAggregateToViewClusterRole(ctx)).To(Succeed()) + Expect(r.reconcileRolloutsAggregateToViewClusterRole(ctx, a)).To(Succeed()) By("Modify Rules of ClusterRole.") clusterRole := &rbacv1.ClusterRole{ @@ -170,7 +170,7 @@ var _ = Describe("Resource creation and cleanup tests", func() { Expect(r.Client.Update(ctx, clusterRole)).To(Succeed()) By("Reconciler should revert modifications.") - Expect(r.reconcileRolloutsAggregateToViewClusterRole(ctx)).To(Succeed()) + Expect(r.reconcileRolloutsAggregateToViewClusterRole(ctx, a)).To(Succeed()) Expect(fetchObject(ctx, r.Client, "", clusterRole.Name, clusterRole)).To(Succeed()) Expect(clusterRole.Rules).To(Equal(GetAggregateToViewPolicyRules())) }) diff --git a/controllers/utils.go b/controllers/utils.go index 344dd60..165342b 100644 --- a/controllers/utils.go +++ b/controllers/utils.go @@ -30,8 +30,35 @@ type pluginItem struct { Sha256 string `json:"sha256" yaml:"sha256"` } -func setRolloutsLabels(obj *metav1.ObjectMeta) { +func setRolloutsLabelsAndAnnotationsToObject(obj *metav1.ObjectMeta, cr *rolloutsmanagerv1alpha1.RolloutManager) { + + setRolloutsLabelsAndAnnotations(obj) + + setAdditionalRolloutsLabelsAndAnnotationsToObject(obj, cr) +} + +func setAdditionalRolloutsLabelsAndAnnotationsToObject(obj *metav1.ObjectMeta, cr *rolloutsmanagerv1alpha1.RolloutManager) { + + if cr.Spec.AdditionalMetadata != nil { + if obj.Labels == nil { + obj.Labels = map[string]string{} + } + if obj.Annotations == nil { + obj.Annotations = map[string]string{} + } + for k, v := range cr.Spec.AdditionalMetadata.Labels { + obj.Labels[k] = v + } + for k, v := range cr.Spec.AdditionalMetadata.Annotations { + obj.Annotations[k] = v + } + } + +} + +func setRolloutsLabelsAndAnnotations(obj *metav1.ObjectMeta) { obj.Labels = map[string]string{} + obj.Annotations = map[string]string{} obj.Labels["app.kubernetes.io/name"] = DefaultArgoRolloutsResourceName obj.Labels["app.kubernetes.io/part-of"] = DefaultArgoRolloutsResourceName obj.Labels["app.kubernetes.io/component"] = DefaultArgoRolloutsResourceName diff --git a/docs/crd_reference.md b/docs/crd_reference.md index 8af4286..52dfb05 100644 --- a/docs/crd_reference.md +++ b/docs/crd_reference.md @@ -78,3 +78,22 @@ spec: ``` +### RolloutManager example with metadata for the resources generatd + +You can provide labels and annotation for all the resources generated (Argo Rollouts controller, ConfigMap, etc.). + +``` yaml +apiVersion: argoproj.io/v1alpha1 +kind: RolloutManager +metadata: + name: argo-rollout + labels: + example: with-metadata-example +spec: + additionalMetadata: + labels: + mylabel: "true" + annotations: + myannotation: "myvalue" +``` + diff --git a/examples/custom_metadata_rolloutmanager.yaml b/examples/custom_metadata_rolloutmanager.yaml new file mode 100644 index 0000000..f400f01 --- /dev/null +++ b/examples/custom_metadata_rolloutmanager.yaml @@ -0,0 +1,15 @@ +--- +apiVersion: argoproj.io/v1alpha1 +kind: RolloutManager +metadata: + name: rollout-manager + labels: + example: withLabelsAndAnnotations +spec: + additionalMetadata: + labels: + exampleLabel: example-label + secondExampleLabel: second-example-label + annotations: + exampleAnnotation: example-annotation + secondExampleAnnotation: second-example-annotation diff --git a/tests/e2e/cluster-scoped/cluster_scoped_rollouts_test.go b/tests/e2e/cluster-scoped/cluster_scoped_rollouts_test.go index 2cf3752..dc9b0c7 100644 --- a/tests/e2e/cluster-scoped/cluster_scoped_rollouts_test.go +++ b/tests/e2e/cluster-scoped/cluster_scoped_rollouts_test.go @@ -50,7 +50,17 @@ var _ = Describe("Cluster-scoped RolloutManager tests", func() { nsName := "test-ro-ns" By("Create cluster-scoped RolloutManager in default namespace.") - rolloutsManager, err := utils.CreateRolloutManager(ctx, k8sClient, "test-rollouts-manager-1", fixture.TestE2ENamespace, false) + metadata := &rmv1alpha1.ResourceMetadata{ + Annotations: map[string]string{ + "foo-annotation": "bar-annotation", + "foo-annotation2": "bar-annotation2", + }, + Labels: map[string]string{ + "foo-label": "bar-label", + "foo-label2": "bar-label2", + }, + } + rolloutsManager, err := utils.CreateRolloutManagerWithMetadata(ctx, k8sClient, "test-rollouts-manager-1", fixture.TestE2ENamespace, false, metadata) Expect(err).ToNot(HaveOccurred()) By("Verify that RolloutManager is successfully created.") diff --git a/tests/e2e/namespace-scoped/namespace_scoped_rollouts_test.go b/tests/e2e/namespace-scoped/namespace_scoped_rollouts_test.go index ee36560..e2fd7ea 100644 --- a/tests/e2e/namespace-scoped/namespace_scoped_rollouts_test.go +++ b/tests/e2e/namespace-scoped/namespace_scoped_rollouts_test.go @@ -305,5 +305,43 @@ var _ = Describe("Namespace-scoped RolloutManager tests", func() { BeTrue(), ) }) + + /* + In this test, we specify some additional labels and annotations to the Rollout Manager, and expect them to be set on all generated resources. + */ + It("Should create resources with additional metadata when provided", func() { + nsName := "test-rom-ns" + + By("Create a namespace for RolloutManager.") + Expect(utils.CreateNamespace(ctx, k8sClient, nsName)).To(Succeed()) + + By("Create namespace-scoped RolloutManager with additionalMetadata in same namespace.") + metadata := &rmv1alpha1.ResourceMetadata{ + Annotations: map[string]string{ + "foo-annotation": "bar-annotation", + "foo-annotation2": "bar-annotation2", + }, + Labels: map[string]string{ + "foo-label": "bar-label", + "foo-label2": "bar-label2", + }, + } + rolloutsManager, err := utils.CreateRolloutManagerWithMetadata(ctx, k8sClient, "test-rollouts-manager-1", nsName, true, metadata) + Expect(err).ToNot(HaveOccurred()) + + By("Verify that RolloutManager is successfully created.") + Eventually(rolloutsManager, "2m", "1s").Should(rmFixture.HavePhase(rmv1alpha1.PhaseAvailable)) + + By("Verify that Status.Condition is having success condition.") + Eventually(rolloutsManager, "2m", "1s").Should(rmFixture.HaveSuccessCondition()) + + By("Verify that expected resources are created.") + utils.ValidateArgoRolloutManagerResources(ctx, rolloutsManager, k8sClient, true) + + By("Verify argo Rollouts controller is able to reconcile CR.") + + By("Create and validate rollouts.") + utils.ValidateArgoRolloutsResources(ctx, k8sClient, nsName, 31000, 32000) + }) }) }) diff --git a/tests/e2e/rollouts_test.go b/tests/e2e/rollouts_test.go index 6472aff..7cfc6a7 100644 --- a/tests/e2e/rollouts_test.go +++ b/tests/e2e/rollouts_test.go @@ -239,5 +239,67 @@ var _ = Describe("RolloutManager tests", func() { }, "10s", "1s").Should(Equal(expectedVersion)) }) }) + + When("A RolloutManager specifies metadata", func() { + + It("should create the controller with the correct labels and annotations", func() { + + rolloutsManager := rolloutsmanagerv1alpha1.RolloutManager{ + ObjectMeta: metav1.ObjectMeta{ + Name: "basic-rollouts-manager-with-metadata", + Namespace: fixture.TestE2ENamespace, + }, + Spec: rolloutsmanagerv1alpha1.RolloutManagerSpec{ + AdditionalMetadata: &rolloutsmanagerv1alpha1.ResourceMetadata{ + Annotations: map[string]string{ + "foo-annotation": "bar-annotation", + "foo-annotation2": "bar-annotation2", + }, + Labels: map[string]string{ + "foo-label": "bar-label", + "foo-label2": "bar-label2", + }, + }, + NamespaceScoped: true, + }, + } + + Expect(k8sClient.Create(ctx, &rolloutsManager)).To(Succeed()) + + Eventually(rolloutsManager, "1m", "1s").Should(rolloutManagerFixture.HavePhase(rolloutsmanagerv1alpha1.PhaseAvailable)) + + deployment := appsv1.Deployment{ + ObjectMeta: metav1.ObjectMeta{Name: controllers.DefaultArgoRolloutsResourceName, Namespace: rolloutManager.Namespace}, + } + Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(&deployment), &deployment)).To(Succeed()) + + ExpectMetadataOnObjectMeta(&deployment.ObjectMeta, rolloutsManager.Spec.AdditionalMetadata) + ExpectMetadataOnObjectMeta(&deployment.Spec.Template.ObjectMeta, rolloutsManager.Spec.AdditionalMetadata) + + configMap := corev1.ConfigMap{ + ObjectMeta: metav1.ObjectMeta{Name: controllers.DefaultRolloutsConfigMapName, Namespace: rolloutManager.Namespace}, + } + Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(&configMap), &configMap)).To(Succeed()) + ExpectMetadataOnObjectMeta(&configMap.ObjectMeta, rolloutsManager.Spec.AdditionalMetadata) + + serviceAccount := corev1.ServiceAccount{ + ObjectMeta: metav1.ObjectMeta{Name: controllers.DefaultArgoRolloutsResourceName, Namespace: rolloutManager.Namespace}, + } + Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(&serviceAccount), &serviceAccount)).To(Succeed()) + ExpectMetadataOnObjectMeta(&serviceAccount.ObjectMeta, rolloutsManager.Spec.AdditionalMetadata) + + secret := corev1.Secret{ + ObjectMeta: metav1.ObjectMeta{Name: controllers.DefaultRolloutsNotificationSecretName, Namespace: rolloutManager.Namespace}, + } + Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(&secret), &secret)).To(Succeed()) + ExpectMetadataOnObjectMeta(&secret.ObjectMeta, rolloutsManager.Spec.AdditionalMetadata) + + service := corev1.Service{ + ObjectMeta: metav1.ObjectMeta{Name: controllers.DefaultArgoRolloutsMetricsServiceName, Namespace: rolloutManager.Namespace}, + } + Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(&service), &service)).To(Succeed()) + ExpectMetadataOnObjectMeta(&service.ObjectMeta, rolloutsManager.Spec.AdditionalMetadata) + }) + }) }) }) diff --git a/tests/e2e/test_utils.go b/tests/e2e/test_utils.go index 336fd2e..b7d465e 100644 --- a/tests/e2e/test_utils.go +++ b/tests/e2e/test_utils.go @@ -42,13 +42,21 @@ func CreateNamespace(ctx context.Context, k8sClient client.Client, name string) // Create RolloutManager CR func CreateRolloutManager(ctx context.Context, k8sClient client.Client, name, namespace string, namespaceScoped bool) (rmv1alpha1.RolloutManager, error) { + return CreateRolloutManagerWithMetadata(ctx, k8sClient, name, namespace, namespaceScoped, &rmv1alpha1.ResourceMetadata{}) +} + +// Create RolloutManager CR +func CreateRolloutManagerWithMetadata(ctx context.Context, k8sClient client.Client, name, namespace string, namespaceScoped bool, + additionalMetadata *rmv1alpha1.ResourceMetadata) (rmv1alpha1.RolloutManager, error) { + rolloutsManager := rmv1alpha1.RolloutManager{ ObjectMeta: metav1.ObjectMeta{ Name: name, Namespace: namespace, }, Spec: rmv1alpha1.RolloutManagerSpec{ - NamespaceScoped: namespaceScoped, + NamespaceScoped: namespaceScoped, + AdditionalMetadata: additionalMetadata, }, } return rolloutsManager, k8sClient.Create(ctx, &rolloutsManager) @@ -87,17 +95,17 @@ func ValidateArgoRolloutManagerResources(ctx context.Context, rolloutsManager rm validateArgoRolloutsRole(k8sClient, rolloutsManager) } else { By("Verify that argo-rollout ClusterRoles is created.") - validateArgoRolloutsClusterRole(k8sClient) + validateArgoRolloutsClusterRole(k8sClient, rolloutsManager) } By("Verify that aggregate-to-admin ClusterRole is created.") - validateAggregateToAdminClusterRole(k8sClient) + validateAggregateToAdminClusterRole(k8sClient, rolloutsManager) By("Verify that aggregate-to-edit ClusterRole is created.") - validateAggregateToEditClusterRole(k8sClient) + validateAggregateToEditClusterRole(k8sClient, rolloutsManager) By("Verify that aggregate-to-view ClusterRole is created.") - validateAggregateToViewClusterRole(k8sClient) + validateAggregateToViewClusterRole(k8sClient, rolloutsManager) if namespaceScoped { By("Verify that RoleBinding is created.") @@ -148,6 +156,18 @@ func ValidateArgoRolloutsResources(ctx context.Context, k8sClient client.Client, } +// Checks that the labels and annotations provided are present in the ObjectMeta +func ExpectMetadataOnObjectMeta(ObjectMeta *metav1.ObjectMeta, expectedMetadata *rmv1alpha1.ResourceMetadata) { + if expectedMetadata != nil { + for k, v := range expectedMetadata.Labels { + Expect(ObjectMeta.Labels).To(HaveKeyWithValue(k, v)) + } + for k, v := range expectedMetadata.Annotations { + Expect(ObjectMeta.Annotations).To(HaveKeyWithValue(k, v)) + } + } +} + func validateServiceAccount(k8sClient client.Client, rolloutsManager rmv1alpha1.RolloutManager) { sa := &corev1.ServiceAccount{ ObjectMeta: metav1.ObjectMeta{ @@ -157,8 +177,9 @@ func validateServiceAccount(k8sClient client.Client, rolloutsManager rmv1alpha1. } Eventually(sa, "10s", "1s").Should(k8s.ExistByName(k8sClient)) - By("Verify that ServiceAccount has correct labels.") + By("Verify that ServiceAccount has correct labels and annotations.") validateLabels(&sa.ObjectMeta) + ExpectMetadataOnObjectMeta(&sa.ObjectMeta, rolloutsManager.Spec.AdditionalMetadata) } func validateArgoRolloutsRole(k8sClient client.Client, rolloutsManager rmv1alpha1.RolloutManager) { @@ -170,14 +191,15 @@ func validateArgoRolloutsRole(k8sClient client.Client, rolloutsManager rmv1alpha } Eventually(role, "10s", "1s").Should(k8s.ExistByName(k8sClient)) - By("Verify that Role has correct labels.") + By("Verify that Role has correct labels and annotations.") validateLabels(&role.ObjectMeta) + ExpectMetadataOnObjectMeta(&role.ObjectMeta, rolloutsManager.Spec.AdditionalMetadata) By("Verify that Role has correct policy rules.") Expect(role.Rules).To(ConsistOf(controllers.GetPolicyRules())) } -func validateArgoRolloutsClusterRole(k8sClient client.Client) { +func validateArgoRolloutsClusterRole(k8sClient client.Client, rolloutsManager rmv1alpha1.RolloutManager) { clusterRole := &rbacv1.ClusterRole{ ObjectMeta: metav1.ObjectMeta{ Name: controllers.DefaultArgoRolloutsResourceName, @@ -185,14 +207,15 @@ func validateArgoRolloutsClusterRole(k8sClient client.Client) { } Eventually(clusterRole, "30s", "1s").Should(k8s.ExistByName(k8sClient)) - By("Verify that ClusterRole has correct labels.") + By("Verify that ClusterRole has correct labels and annotations.") validateLabels(&clusterRole.ObjectMeta) + ExpectMetadataOnObjectMeta(&clusterRole.ObjectMeta, rolloutsManager.Spec.AdditionalMetadata) By("Verify that ClusterRole has correct policy rules.") Expect(clusterRole.Rules).To(ConsistOf(controllers.GetPolicyRules())) } -func validateAggregateToAdminClusterRole(k8sClient client.Client) { +func validateAggregateToAdminClusterRole(k8sClient client.Client, rolloutsManager rmv1alpha1.RolloutManager) { aggregationType := "aggregate-to-admin" clusterRoleName := fmt.Sprintf("%s-%s", controllers.DefaultArgoRolloutsResourceName, aggregationType) @@ -203,14 +226,15 @@ func validateAggregateToAdminClusterRole(k8sClient client.Client) { } Eventually(clusterRole, "30s", "1s").Should(k8s.ExistByName(k8sClient)) - By("Verify that ClusterRole has correct labels.") + By("Verify that ClusterRole has correct labels and annotations.") validateAggregateLabels(&clusterRole.ObjectMeta, aggregationType) + ExpectMetadataOnObjectMeta(&clusterRole.ObjectMeta, rolloutsManager.Spec.AdditionalMetadata) By("Verify that ClusterRole has correct policy rules.") Expect(clusterRole.Rules).To(ConsistOf(controllers.GetAggregateToAdminPolicyRules())) } -func validateAggregateToEditClusterRole(k8sClient client.Client) { +func validateAggregateToEditClusterRole(k8sClient client.Client, rolloutsManager rmv1alpha1.RolloutManager) { aggregationType := "aggregate-to-edit" clusterRoleName := fmt.Sprintf("%s-%s", controllers.DefaultArgoRolloutsResourceName, aggregationType) @@ -221,14 +245,15 @@ func validateAggregateToEditClusterRole(k8sClient client.Client) { } Eventually(clusterRole, "30s", "1s").Should(k8s.ExistByName(k8sClient)) - By("Verify that ClusterRole has correct labels.") + By("Verify that ClusterRole has correct labels and annotations.") validateAggregateLabels(&clusterRole.ObjectMeta, aggregationType) + ExpectMetadataOnObjectMeta(&clusterRole.ObjectMeta, rolloutsManager.Spec.AdditionalMetadata) By("Verify that ClusterRole has correct policy rules.") Expect(clusterRole.Rules).To(ConsistOf(controllers.GetAggregateToEditPolicyRules())) } -func validateAggregateToViewClusterRole(k8sClient client.Client) { +func validateAggregateToViewClusterRole(k8sClient client.Client, rolloutsManager rmv1alpha1.RolloutManager) { aggregationType := "aggregate-to-view" clusterRoleName := fmt.Sprintf("%s-%s", controllers.DefaultArgoRolloutsResourceName, aggregationType) @@ -239,8 +264,9 @@ func validateAggregateToViewClusterRole(k8sClient client.Client) { } Eventually(clusterRole, "30s", "1s").Should(k8s.ExistByName(k8sClient)) - By("Verify that ClusterRole has correct labels.") + By("Verify that ClusterRole has correct labels and annotations.") validateAggregateLabels(&clusterRole.ObjectMeta, aggregationType) + ExpectMetadataOnObjectMeta(&clusterRole.ObjectMeta, rolloutsManager.Spec.AdditionalMetadata) By("Verify that ClusterRole has correct policy rules.") Expect(clusterRole.Rules).To(ConsistOf(controllers.GetAggregateToViewPolicyRules())) @@ -255,8 +281,9 @@ func validateRoleBinding(k8sClient client.Client, rolloutsManager rmv1alpha1.Rol } Eventually(binding, "30s", "1s").Should(k8s.ExistByName(k8sClient)) - By("Verify that RoleBinding has correct labels.") + By("Verify that RoleBinding has correct labels and annotations.") validateLabels(&binding.ObjectMeta) + ExpectMetadataOnObjectMeta(&binding.ObjectMeta, rolloutsManager.Spec.AdditionalMetadata) By("Verify that RoleBinding has correct RoleRef.") Expect(binding.RoleRef).To(Equal(rbacv1.RoleRef{ @@ -285,8 +312,9 @@ func validateClusterRoleBinding(k8sClient client.Client, rolloutsManager rmv1alp } Eventually(clusterRoleBinding, "30s", "1s").Should(k8s.ExistByName(k8sClient)) - By("Verify that ClusterRoleBinding has correct labels.") + By("Verify that ClusterRoleBinding has correct labels and annotations.") validateLabels(&clusterRoleBinding.ObjectMeta) + ExpectMetadataOnObjectMeta(&clusterRoleBinding.ObjectMeta, rolloutsManager.Spec.AdditionalMetadata) By("Verify that ClusterRoleBinding has correct RoleRef.") Expect(clusterRoleBinding.RoleRef).To(Equal(rbacv1.RoleRef{ @@ -316,10 +344,11 @@ func validateService(k8sClient client.Client, rolloutsManager rmv1alpha1.Rollout } Eventually(service, "10s", "1s").Should(k8s.ExistByName(k8sClient)) - By("Verify that Service has correct labels.") + By("Verify that Service has correct labels and annotations.") Expect(service.Labels["app.kubernetes.io/name"]).To(Equal(controllers.DefaultArgoRolloutsMetricsServiceName)) Expect(service.Labels["app.kubernetes.io/part-of"]).To(Equal(controllers.DefaultArgoRolloutsResourceName)) Expect(service.Labels["app.kubernetes.io/component"]).To(Equal("server")) + ExpectMetadataOnObjectMeta(&service.ObjectMeta, rolloutsManager.Spec.AdditionalMetadata) By("Verify that ClusterRoleBinding has correct Ports.") Expect(service.Spec.Ports).To(Equal([]corev1.ServicePort{ @@ -348,6 +377,9 @@ func validateSecret(k8sClient client.Client, rolloutsManager rmv1alpha1.RolloutM By("Verify that Secret has correct Type.") Expect(secret.Type).To(Equal(corev1.SecretTypeOpaque)) + + By("Verify that Secret has correct labels and annotations.") + ExpectMetadataOnObjectMeta(&secret.ObjectMeta, rolloutsManager.Spec.AdditionalMetadata) } func validateDeployment(ctx context.Context, k8sClient client.Client, rolloutsManager rmv1alpha1.RolloutManager) { @@ -367,17 +399,16 @@ func validateDeployment(ctx context.Context, k8sClient client.Client, rolloutsMa return depl.Status.ReadyReplicas == 1 }, "3m", "1s").Should(BeTrue()) - By("Verify that Deployment has correct labels.") + By("Verify that Deployment has correct labels and annotations.") validateLabels(&depl.ObjectMeta) + ExpectMetadataOnObjectMeta(&depl.ObjectMeta, rolloutsManager.Spec.AdditionalMetadata) By("Verify that Deployment has correct Selector.") - Expect(depl.Spec.Selector).To(Equal(&metav1.LabelSelector{ - MatchLabels: map[string]string{ - controllers.DefaultRolloutsSelectorKey: controllers.DefaultArgoRolloutsResourceName, - }})) + Expect(depl.Spec.Selector.MatchLabels).To(HaveKeyWithValue(controllers.DefaultRolloutsSelectorKey, controllers.DefaultArgoRolloutsResourceName)) By("Verify that Deployment Template has correct Template.") - Expect(depl.Spec.Template.Labels).To(Equal(map[string]string{controllers.DefaultRolloutsSelectorKey: controllers.DefaultArgoRolloutsResourceName})) + Expect(depl.Spec.Template.Labels).To(HaveKeyWithValue(controllers.DefaultRolloutsSelectorKey, controllers.DefaultArgoRolloutsResourceName)) + ExpectMetadataOnObjectMeta(&depl.Spec.Template.ObjectMeta, rolloutsManager.Spec.AdditionalMetadata) By("Verify that Deployment Template has correct NodeSelector.") Expect(depl.Spec.Template.Spec.NodeSelector).To(Equal(map[string]string{"kubernetes.io/os": "linux"})) @@ -394,7 +425,6 @@ func validateDeployment(ctx context.Context, k8sClient client.Client, rolloutsMa func validateLabels(object *metav1.ObjectMeta) { GinkgoHelper() - Expect(len(object.Labels)).To(Equal(3)) Expect(object.Labels["app.kubernetes.io/name"]).To(Equal(controllers.DefaultArgoRolloutsResourceName)) Expect(object.Labels["app.kubernetes.io/part-of"]).To(Equal(controllers.DefaultArgoRolloutsResourceName)) Expect(object.Labels["app.kubernetes.io/component"]).To(Equal(controllers.DefaultArgoRolloutsResourceName)) @@ -402,7 +432,6 @@ func validateLabels(object *metav1.ObjectMeta) { func validateAggregateLabels(object *metav1.ObjectMeta, aggregationType string) { GinkgoHelper() - Expect(len(object.Labels)).To(Equal(4)) Expect(object.Labels["app.kubernetes.io/name"]).To(Equal(object.Name)) Expect(object.Labels["app.kubernetes.io/part-of"]).To(Equal(controllers.DefaultArgoRolloutsResourceName)) Expect(object.Labels["app.kubernetes.io/component"]).To(Equal("aggregate-cluster-role")) From cef50914a2d1fcdc4d94d96d6d3da42de5cb17a1 Mon Sep 17 00:00:00 2001 From: Jonathan West Date: Wed, 22 May 2024 06:37:18 -0400 Subject: [PATCH 2/2] Unexport expectMetadataOnObjectMeta E2E test function Signed-off-by: Jonathan West --- tests/e2e/rollouts_test.go | 12 ++++++------ tests/e2e/test_utils.go | 26 +++++++++++++------------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/tests/e2e/rollouts_test.go b/tests/e2e/rollouts_test.go index 7cfc6a7..ee06041 100644 --- a/tests/e2e/rollouts_test.go +++ b/tests/e2e/rollouts_test.go @@ -273,32 +273,32 @@ var _ = Describe("RolloutManager tests", func() { } Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(&deployment), &deployment)).To(Succeed()) - ExpectMetadataOnObjectMeta(&deployment.ObjectMeta, rolloutsManager.Spec.AdditionalMetadata) - ExpectMetadataOnObjectMeta(&deployment.Spec.Template.ObjectMeta, rolloutsManager.Spec.AdditionalMetadata) + expectMetadataOnObjectMeta(&deployment.ObjectMeta, rolloutsManager.Spec.AdditionalMetadata) + expectMetadataOnObjectMeta(&deployment.Spec.Template.ObjectMeta, rolloutsManager.Spec.AdditionalMetadata) configMap := corev1.ConfigMap{ ObjectMeta: metav1.ObjectMeta{Name: controllers.DefaultRolloutsConfigMapName, Namespace: rolloutManager.Namespace}, } Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(&configMap), &configMap)).To(Succeed()) - ExpectMetadataOnObjectMeta(&configMap.ObjectMeta, rolloutsManager.Spec.AdditionalMetadata) + expectMetadataOnObjectMeta(&configMap.ObjectMeta, rolloutsManager.Spec.AdditionalMetadata) serviceAccount := corev1.ServiceAccount{ ObjectMeta: metav1.ObjectMeta{Name: controllers.DefaultArgoRolloutsResourceName, Namespace: rolloutManager.Namespace}, } Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(&serviceAccount), &serviceAccount)).To(Succeed()) - ExpectMetadataOnObjectMeta(&serviceAccount.ObjectMeta, rolloutsManager.Spec.AdditionalMetadata) + expectMetadataOnObjectMeta(&serviceAccount.ObjectMeta, rolloutsManager.Spec.AdditionalMetadata) secret := corev1.Secret{ ObjectMeta: metav1.ObjectMeta{Name: controllers.DefaultRolloutsNotificationSecretName, Namespace: rolloutManager.Namespace}, } Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(&secret), &secret)).To(Succeed()) - ExpectMetadataOnObjectMeta(&secret.ObjectMeta, rolloutsManager.Spec.AdditionalMetadata) + expectMetadataOnObjectMeta(&secret.ObjectMeta, rolloutsManager.Spec.AdditionalMetadata) service := corev1.Service{ ObjectMeta: metav1.ObjectMeta{Name: controllers.DefaultArgoRolloutsMetricsServiceName, Namespace: rolloutManager.Namespace}, } Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(&service), &service)).To(Succeed()) - ExpectMetadataOnObjectMeta(&service.ObjectMeta, rolloutsManager.Spec.AdditionalMetadata) + expectMetadataOnObjectMeta(&service.ObjectMeta, rolloutsManager.Spec.AdditionalMetadata) }) }) }) diff --git a/tests/e2e/test_utils.go b/tests/e2e/test_utils.go index b7d465e..ab951ee 100644 --- a/tests/e2e/test_utils.go +++ b/tests/e2e/test_utils.go @@ -157,7 +157,7 @@ func ValidateArgoRolloutsResources(ctx context.Context, k8sClient client.Client, } // Checks that the labels and annotations provided are present in the ObjectMeta -func ExpectMetadataOnObjectMeta(ObjectMeta *metav1.ObjectMeta, expectedMetadata *rmv1alpha1.ResourceMetadata) { +func expectMetadataOnObjectMeta(ObjectMeta *metav1.ObjectMeta, expectedMetadata *rmv1alpha1.ResourceMetadata) { if expectedMetadata != nil { for k, v := range expectedMetadata.Labels { Expect(ObjectMeta.Labels).To(HaveKeyWithValue(k, v)) @@ -179,7 +179,7 @@ func validateServiceAccount(k8sClient client.Client, rolloutsManager rmv1alpha1. By("Verify that ServiceAccount has correct labels and annotations.") validateLabels(&sa.ObjectMeta) - ExpectMetadataOnObjectMeta(&sa.ObjectMeta, rolloutsManager.Spec.AdditionalMetadata) + expectMetadataOnObjectMeta(&sa.ObjectMeta, rolloutsManager.Spec.AdditionalMetadata) } func validateArgoRolloutsRole(k8sClient client.Client, rolloutsManager rmv1alpha1.RolloutManager) { @@ -193,7 +193,7 @@ func validateArgoRolloutsRole(k8sClient client.Client, rolloutsManager rmv1alpha By("Verify that Role has correct labels and annotations.") validateLabels(&role.ObjectMeta) - ExpectMetadataOnObjectMeta(&role.ObjectMeta, rolloutsManager.Spec.AdditionalMetadata) + expectMetadataOnObjectMeta(&role.ObjectMeta, rolloutsManager.Spec.AdditionalMetadata) By("Verify that Role has correct policy rules.") Expect(role.Rules).To(ConsistOf(controllers.GetPolicyRules())) @@ -209,7 +209,7 @@ func validateArgoRolloutsClusterRole(k8sClient client.Client, rolloutsManager rm By("Verify that ClusterRole has correct labels and annotations.") validateLabels(&clusterRole.ObjectMeta) - ExpectMetadataOnObjectMeta(&clusterRole.ObjectMeta, rolloutsManager.Spec.AdditionalMetadata) + expectMetadataOnObjectMeta(&clusterRole.ObjectMeta, rolloutsManager.Spec.AdditionalMetadata) By("Verify that ClusterRole has correct policy rules.") Expect(clusterRole.Rules).To(ConsistOf(controllers.GetPolicyRules())) @@ -228,7 +228,7 @@ func validateAggregateToAdminClusterRole(k8sClient client.Client, rolloutsManage By("Verify that ClusterRole has correct labels and annotations.") validateAggregateLabels(&clusterRole.ObjectMeta, aggregationType) - ExpectMetadataOnObjectMeta(&clusterRole.ObjectMeta, rolloutsManager.Spec.AdditionalMetadata) + expectMetadataOnObjectMeta(&clusterRole.ObjectMeta, rolloutsManager.Spec.AdditionalMetadata) By("Verify that ClusterRole has correct policy rules.") Expect(clusterRole.Rules).To(ConsistOf(controllers.GetAggregateToAdminPolicyRules())) @@ -247,7 +247,7 @@ func validateAggregateToEditClusterRole(k8sClient client.Client, rolloutsManager By("Verify that ClusterRole has correct labels and annotations.") validateAggregateLabels(&clusterRole.ObjectMeta, aggregationType) - ExpectMetadataOnObjectMeta(&clusterRole.ObjectMeta, rolloutsManager.Spec.AdditionalMetadata) + expectMetadataOnObjectMeta(&clusterRole.ObjectMeta, rolloutsManager.Spec.AdditionalMetadata) By("Verify that ClusterRole has correct policy rules.") Expect(clusterRole.Rules).To(ConsistOf(controllers.GetAggregateToEditPolicyRules())) @@ -266,7 +266,7 @@ func validateAggregateToViewClusterRole(k8sClient client.Client, rolloutsManager By("Verify that ClusterRole has correct labels and annotations.") validateAggregateLabels(&clusterRole.ObjectMeta, aggregationType) - ExpectMetadataOnObjectMeta(&clusterRole.ObjectMeta, rolloutsManager.Spec.AdditionalMetadata) + expectMetadataOnObjectMeta(&clusterRole.ObjectMeta, rolloutsManager.Spec.AdditionalMetadata) By("Verify that ClusterRole has correct policy rules.") Expect(clusterRole.Rules).To(ConsistOf(controllers.GetAggregateToViewPolicyRules())) @@ -283,7 +283,7 @@ func validateRoleBinding(k8sClient client.Client, rolloutsManager rmv1alpha1.Rol By("Verify that RoleBinding has correct labels and annotations.") validateLabels(&binding.ObjectMeta) - ExpectMetadataOnObjectMeta(&binding.ObjectMeta, rolloutsManager.Spec.AdditionalMetadata) + expectMetadataOnObjectMeta(&binding.ObjectMeta, rolloutsManager.Spec.AdditionalMetadata) By("Verify that RoleBinding has correct RoleRef.") Expect(binding.RoleRef).To(Equal(rbacv1.RoleRef{ @@ -314,7 +314,7 @@ func validateClusterRoleBinding(k8sClient client.Client, rolloutsManager rmv1alp By("Verify that ClusterRoleBinding has correct labels and annotations.") validateLabels(&clusterRoleBinding.ObjectMeta) - ExpectMetadataOnObjectMeta(&clusterRoleBinding.ObjectMeta, rolloutsManager.Spec.AdditionalMetadata) + expectMetadataOnObjectMeta(&clusterRoleBinding.ObjectMeta, rolloutsManager.Spec.AdditionalMetadata) By("Verify that ClusterRoleBinding has correct RoleRef.") Expect(clusterRoleBinding.RoleRef).To(Equal(rbacv1.RoleRef{ @@ -348,7 +348,7 @@ func validateService(k8sClient client.Client, rolloutsManager rmv1alpha1.Rollout Expect(service.Labels["app.kubernetes.io/name"]).To(Equal(controllers.DefaultArgoRolloutsMetricsServiceName)) Expect(service.Labels["app.kubernetes.io/part-of"]).To(Equal(controllers.DefaultArgoRolloutsResourceName)) Expect(service.Labels["app.kubernetes.io/component"]).To(Equal("server")) - ExpectMetadataOnObjectMeta(&service.ObjectMeta, rolloutsManager.Spec.AdditionalMetadata) + expectMetadataOnObjectMeta(&service.ObjectMeta, rolloutsManager.Spec.AdditionalMetadata) By("Verify that ClusterRoleBinding has correct Ports.") Expect(service.Spec.Ports).To(Equal([]corev1.ServicePort{ @@ -379,7 +379,7 @@ func validateSecret(k8sClient client.Client, rolloutsManager rmv1alpha1.RolloutM Expect(secret.Type).To(Equal(corev1.SecretTypeOpaque)) By("Verify that Secret has correct labels and annotations.") - ExpectMetadataOnObjectMeta(&secret.ObjectMeta, rolloutsManager.Spec.AdditionalMetadata) + expectMetadataOnObjectMeta(&secret.ObjectMeta, rolloutsManager.Spec.AdditionalMetadata) } func validateDeployment(ctx context.Context, k8sClient client.Client, rolloutsManager rmv1alpha1.RolloutManager) { @@ -401,14 +401,14 @@ func validateDeployment(ctx context.Context, k8sClient client.Client, rolloutsMa By("Verify that Deployment has correct labels and annotations.") validateLabels(&depl.ObjectMeta) - ExpectMetadataOnObjectMeta(&depl.ObjectMeta, rolloutsManager.Spec.AdditionalMetadata) + expectMetadataOnObjectMeta(&depl.ObjectMeta, rolloutsManager.Spec.AdditionalMetadata) By("Verify that Deployment has correct Selector.") Expect(depl.Spec.Selector.MatchLabels).To(HaveKeyWithValue(controllers.DefaultRolloutsSelectorKey, controllers.DefaultArgoRolloutsResourceName)) By("Verify that Deployment Template has correct Template.") Expect(depl.Spec.Template.Labels).To(HaveKeyWithValue(controllers.DefaultRolloutsSelectorKey, controllers.DefaultArgoRolloutsResourceName)) - ExpectMetadataOnObjectMeta(&depl.Spec.Template.ObjectMeta, rolloutsManager.Spec.AdditionalMetadata) + expectMetadataOnObjectMeta(&depl.Spec.Template.ObjectMeta, rolloutsManager.Spec.AdditionalMetadata) By("Verify that Deployment Template has correct NodeSelector.") Expect(depl.Spec.Template.Spec.NodeSelector).To(Equal(map[string]string{"kubernetes.io/os": "linux"}))