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 May 21, 2024
1 parent 68ce050 commit e143a94
Show file tree
Hide file tree
Showing 16 changed files with 323 additions and 58 deletions.
12 changes: 12 additions & 0 deletions api/v1alpha1/argorollouts_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

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:
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:
Expand Down
3 changes: 3 additions & 0 deletions controllers/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ func (r *RolloutManagerReconciler) reconcileConfigMap(ctx context.Context, cr ro
},
},
}

setRolloutsLabelsAndAnnotationsToObject(&desiredConfigMap.ObjectMeta, &cr)

trafficRouterPlugins := []pluginItem{
{
Name: OpenShiftRolloutPluginName,
Expand Down
29 changes: 22 additions & 7 deletions controllers/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"
}
Expand Down
2 changes: 1 addition & 1 deletion controllers/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
10 changes: 5 additions & 5 deletions controllers/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,27 +64,27 @@ 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
}
}

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
}
Expand All @@ -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
}
Expand Down
27 changes: 16 additions & 11 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)
setRolloutsLabelsAndAnnotationsToObject(&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)
setRolloutsLabelsAndAnnotationsToObject(&role.ObjectMeta, &cr)

if err := fetchObject(ctx, r.Client, cr.Namespace, role.Name, role); err != nil {
if !apierrors.IsNotFound(err) {
Expand All @@ -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()

Expand All @@ -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) {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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")
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -244,6 +244,7 @@ func (r *RolloutManagerReconciler) reconcileRolloutsAggregateToAdminClusterRole(
Name: name,
},
}
setRolloutsLabelsAndAnnotationsToObject(&clusterRole.ObjectMeta, &cr)
setRolloutsAggregatedClusterRoleLabels(&clusterRole.ObjectMeta, name, aggregationType)

if err := fetchObject(ctx, r.Client, "", clusterRole.Name, clusterRole); err != nil {
Expand All @@ -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)
Expand All @@ -279,6 +280,7 @@ func (r *RolloutManagerReconciler) reconcileRolloutsAggregateToEditClusterRole(c
Name: name,
},
}
setRolloutsLabelsAndAnnotationsToObject(&clusterRole.ObjectMeta, &cr)
setRolloutsAggregatedClusterRoleLabels(&clusterRole.ObjectMeta, name, aggregationType)

if err := fetchObject(ctx, r.Client, "", clusterRole.Name, clusterRole); err != nil {
Expand All @@ -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)
Expand All @@ -314,6 +316,7 @@ func (r *RolloutManagerReconciler) reconcileRolloutsAggregateToViewClusterRole(c
Name: name,
},
}
setRolloutsLabelsAndAnnotationsToObject(&clusterRole.ObjectMeta, &cr)
setRolloutsAggregatedClusterRoleLabels(&clusterRole.ObjectMeta, name, aggregationType)

if err := fetchObject(ctx, r.Client, "", clusterRole.Name, clusterRole); err != nil {
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
Loading

0 comments on commit e143a94

Please sign in to comment.