Skip to content

Commit

Permalink
feat: Support additional metadata for controller
Browse files Browse the repository at this point in the history
Signed-off-by: Guillaume Doussin <[email protected]>
  • Loading branch information
OpenGuidou committed Jan 25, 2024
1 parent 35c8050 commit 548cd15
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 12 deletions.
12 changes: 12 additions & 0 deletions api/v1alpha1/argorollouts_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ type RolloutManagerSpec struct {

// Version defines Argo Rollouts controller tag (optional)
Version string `json:"version,omitempty"`

// Metadata to apply to the controller resources
ControllerMetadata *ResourceMetadata `json:"controllerMetadata,omitempty"`
}

// ArgoRolloutsNodePlacementSpec is used to specify NodeSelector and Tolerations for Rollouts workloads
Expand Down Expand Up @@ -75,6 +78,15 @@ const (
PhaseFailure RolloutControllerPhase = "Failure"
)

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

Expand Down
34 changes: 34 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions config/crd/bases/argoproj.io_rolloutmanagers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@ spec:
spec:
description: RolloutManagerSpec defines the desired state of Argo Rollouts
properties:
controllerMetadata:
description: Metadata to apply to the controller 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:
Expand Down
26 changes: 19 additions & 7 deletions controllers/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,29 @@ func (r *RolloutManagerReconciler) reconcileRolloutsDeployment(ctx context.Conte
Namespace: cr.Namespace,
},
}
setRolloutsLabels(&desiredDeployment.ObjectMeta)
setRolloutsLabelsAndAnnotations(&desiredDeployment.ObjectMeta, cr)

labels := map[string]string{
DefaultRolloutsSelectorKey: DefaultArgoRolloutsResourceName,
}
annotations := map[string]string{}
if cr.Spec.ControllerMetadata != nil {
for k, v := range cr.Spec.ControllerMetadata.Labels {
labels[k] = v
}
for k, v := range cr.Spec.ControllerMetadata.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{
Expand Down Expand Up @@ -86,6 +96,7 @@ func (r *RolloutManagerReconciler) reconcileRolloutsDeployment(ctx context.Conte
actualPodSpec.ServiceAccountName != desiredPodSpec.ServiceAccountName ||
!reflect.DeepEqual(actualDeployment.Labels, desiredDeployment.Labels) ||
!reflect.DeepEqual(actualDeployment.Spec.Template.Labels, desiredDeployment.Spec.Template.Labels) ||
!reflect.DeepEqual(actualDeployment.Spec.Template.Annotations, desiredDeployment.Spec.Template.Annotations) ||
!reflect.DeepEqual(actualDeployment.Spec.Selector, desiredDeployment.Spec.Selector) ||
!reflect.DeepEqual(actualDeployment.Spec.Template.Spec.NodeSelector, desiredDeployment.Spec.Template.Spec.NodeSelector) ||
!reflect.DeepEqual(actualDeployment.Spec.Template.Spec.Tolerations, desiredDeployment.Spec.Template.Spec.Tolerations) ||
Expand All @@ -97,6 +108,7 @@ func (r *RolloutManagerReconciler) reconcileRolloutsDeployment(ctx context.Conte
actualDeployment.Spec.Template.Spec.ServiceAccountName = desiredPodSpec.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
Expand Down
8 changes: 4 additions & 4 deletions controllers/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (r *RolloutManagerReconciler) reconcileRolloutsServiceAccount(ctx context.C
Namespace: cr.Namespace,
},
}
setRolloutsLabels(&sa.ObjectMeta)
setRolloutsLabelsAndAnnotations(&sa.ObjectMeta, cr)

if err := fetchObject(ctx, r.Client, cr.Namespace, sa.Name, sa); err != nil {
if !apierrors.IsNotFound(err) {
Expand Down Expand Up @@ -56,7 +56,7 @@ func (r *RolloutManagerReconciler) reconcileRolloutsRole(ctx context.Context, cr
Namespace: cr.Namespace,
},
}
setRolloutsLabels(&role.ObjectMeta)
setRolloutsLabelsAndAnnotations(&role.ObjectMeta, cr)

if err := fetchObject(ctx, r.Client, cr.Namespace, role.Name, role); err != nil {
if !apierrors.IsNotFound(err) {
Expand Down Expand Up @@ -89,7 +89,7 @@ func (r *RolloutManagerReconciler) reconcileRolloutsRoleBinding(ctx context.Cont
Namespace: cr.Namespace,
},
}
setRolloutsLabels(&expectedRoleBinding.ObjectMeta)
setRolloutsLabelsAndAnnotations(&expectedRoleBinding.ObjectMeta, cr)

expectedRoleBinding.RoleRef = rbacv1.RoleRef{
APIGroup: rbacv1.GroupName,
Expand Down Expand Up @@ -244,7 +244,7 @@ func (r *RolloutManagerReconciler) reconcileRolloutsMetricsService(ctx context.C
Namespace: cr.Namespace,
},
}
setRolloutsLabels(&expectedSvc.ObjectMeta)
setRolloutsLabelsAndAnnotations(&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"
Expand Down
13 changes: 12 additions & 1 deletion controllers/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,28 @@ import (
"sort"
"strings"

rolloutsApi "github.com/argoproj-labs/argo-rollouts-manager/api/v1alpha1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)

func setRolloutsLabels(obj *metav1.ObjectMeta) {
func setRolloutsLabelsAndAnnotations(obj *metav1.ObjectMeta, cr *rolloutsApi.RolloutManager) {
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

if cr.Spec.ControllerMetadata != nil {
for k, v := range cr.Spec.ControllerMetadata.Labels {
obj.Labels[k] = v
}
for k, v := range cr.Spec.ControllerMetadata.Annotations {
obj.Annotations[k] = v
}
}
}

// fetchObject will retrieve the object with the given namespace and name using the Kubernetes API.
Expand Down
15 changes: 15 additions & 0 deletions examples/custom_metadata_rolloutmanager.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
apiVersion: argoproj.io/v1alpha1
kind: RolloutManager
metadata:
name: rollout-manager
labels:
example: withLabelsAndAnnotations
spec:
controllerMetadata:
labels:
exampleLabel: example-label
secondExampleLabel: second-example-label
annotations:
exampleAnnotation: example-annotation
secondExampleAnnotation: second-example-annotation
36 changes: 36 additions & 0 deletions tests/e2e/rollouts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,42 @@ var _ = Describe("RolloutManager tests", func() {
Expect(role.Rules).To(ConsistOf(controllers.GetPolicyRules()))

})

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{
ControllerMetadata: &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",
},
},
},
}

Expect(k8sClient.Create(ctx, &rolloutsManager)).To(Succeed())

Eventually(rolloutsManager, "60s", "1s").Should(rolloutManagerFixture.HavePhase(rolloutsmanagerv1alpha1.PhaseAvailable))

deployment := appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{Name: controllers.DefaultArgoRolloutsResourceName, Namespace: fixture.TestE2ENamespace},
}
Expect(k8sClient.Get(ctx, client.ObjectKeyFromObject(&deployment), &deployment)).To(Succeed())

Expect(deployment.Spec.Template.ObjectMeta.Labels).To(HaveKeyWithValue("foo-label", "bar-label"))
Expect(deployment.Spec.Template.ObjectMeta.Labels).To(HaveKeyWithValue("foo-label2", "bar-label2"))
Expect(deployment.Spec.Template.ObjectMeta.Annotations).To(HaveKeyWithValue("foo-annotation", "bar-annotation"))
Expect(deployment.Spec.Template.ObjectMeta.Annotations).To(HaveKeyWithValue("foo-annotation2", "bar-annotation2"))
})
})
})
})

0 comments on commit 548cd15

Please sign in to comment.