From 749cbeb8c2a5f76dcfdf2f432af553d26c49862e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20S=C3=BC=C3=9F?= Date: Wed, 4 Sep 2024 15:26:17 +0200 Subject: [PATCH] fix: add common labels to created resources --- controllers/model/dashboard_resources.go | 1 + controllers/model/grafana_resources.go | 12 ++++++++ controllers/model/utils.go | 15 ++++++++++ .../grafana/admin_secret_reconciler.go | 1 + .../reconcilers/grafana/config_reconciler.go | 1 + .../grafana/deployment_reconciler.go | 1 + .../grafana/grafana_service_reconciler.go | 1 + .../reconcilers/grafana/ingress_reconciler.go | 2 ++ .../reconcilers/grafana/plugins_reconciler.go | 29 +++++-------------- .../reconcilers/grafana/pvc_reconciler.go | 1 + .../grafana/service_account_reconciler.go | 1 + 11 files changed, 44 insertions(+), 21 deletions(-) diff --git a/controllers/model/dashboard_resources.go b/controllers/model/dashboard_resources.go index 60a9c017b..8bc6a8d55 100644 --- a/controllers/model/dashboard_resources.go +++ b/controllers/model/dashboard_resources.go @@ -15,6 +15,7 @@ func GetPluginsConfigMap(cr *grafanav1beta1.Grafana, scheme *runtime.Scheme) *v1 ObjectMeta: metav1.ObjectMeta{ Name: fmt.Sprintf("%s-plugins", cr.Name), Namespace: cr.Namespace, + Labels: CommonLabels, }, } controllerutil.SetControllerReference(cr, config, scheme) //nolint:errcheck diff --git a/controllers/model/grafana_resources.go b/controllers/model/grafana_resources.go index a5e662591..2765ca011 100644 --- a/controllers/model/grafana_resources.go +++ b/controllers/model/grafana_resources.go @@ -13,11 +13,16 @@ import ( "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" ) +var CommonLabels = map[string]string{ + "app.kubernetes.io/managed-by": "grafana-operator", +} + func GetGrafanaConfigMap(cr *grafanav1beta1.Grafana, scheme *runtime.Scheme) *v1.ConfigMap { config := &v1.ConfigMap{ ObjectMeta: metav1.ObjectMeta{ Name: fmt.Sprintf("%s-ini", cr.Name), Namespace: cr.Namespace, + Labels: CommonLabels, }, } controllerutil.SetControllerReference(cr, config, scheme) //nolint:errcheck @@ -29,6 +34,7 @@ func GetGrafanaAdminSecret(cr *grafanav1beta1.Grafana, scheme *runtime.Scheme) * ObjectMeta: metav1.ObjectMeta{ Name: fmt.Sprintf("%s-admin-credentials", cr.Name), Namespace: cr.Namespace, + Labels: CommonLabels, }, } @@ -43,6 +49,7 @@ func GetGrafanaDataPVC(cr *grafanav1beta1.Grafana, scheme *runtime.Scheme) *v1.P ObjectMeta: metav1.ObjectMeta{ Name: fmt.Sprintf("%s-pvc", cr.Name), Namespace: cr.Namespace, + Labels: CommonLabels, }, } // using OwnerReference specifically here to allow admins to change storage variables without the operator complaining @@ -55,6 +62,7 @@ func GetGrafanaServiceAccount(cr *grafanav1beta1.Grafana, scheme *runtime.Scheme ObjectMeta: metav1.ObjectMeta{ Name: fmt.Sprintf("%s-sa", cr.Name), Namespace: cr.Namespace, + Labels: CommonLabels, }, } controllerutil.SetControllerReference(cr, sa, scheme) //nolint:errcheck @@ -66,6 +74,7 @@ func GetGrafanaService(cr *grafanav1beta1.Grafana, scheme *runtime.Scheme) *v1.S ObjectMeta: metav1.ObjectMeta{ Name: fmt.Sprintf("%s-service", cr.Name), Namespace: cr.Namespace, + Labels: CommonLabels, }, } controllerutil.SetControllerReference(cr, service, scheme) //nolint:errcheck @@ -77,6 +86,7 @@ func GetGrafanaIngress(cr *grafanav1beta1.Grafana, scheme *runtime.Scheme) *v12. ObjectMeta: metav1.ObjectMeta{ Name: fmt.Sprintf("%s-ingress", cr.Name), Namespace: cr.Namespace, + Labels: CommonLabels, }, } controllerutil.SetControllerReference(cr, ingress, scheme) //nolint:errcheck @@ -88,6 +98,7 @@ func GetGrafanaRoute(cr *grafanav1beta1.Grafana, scheme *runtime.Scheme) *routev ObjectMeta: metav1.ObjectMeta{ Name: fmt.Sprintf("%s-route", cr.Name), Namespace: cr.Namespace, + Labels: CommonLabels, }, } controllerutil.SetControllerReference(cr, route, scheme) //nolint:errcheck @@ -99,6 +110,7 @@ func GetGrafanaDeployment(cr *grafanav1beta1.Grafana, scheme *runtime.Scheme) *v ObjectMeta: metav1.ObjectMeta{ Name: fmt.Sprintf("%s-deployment", cr.Name), Namespace: cr.Namespace, + Labels: CommonLabels, }, } if scheme != nil { diff --git a/controllers/model/utils.go b/controllers/model/utils.go index 394384a57..a698628b8 100644 --- a/controllers/model/utils.go +++ b/controllers/model/utils.go @@ -3,6 +3,8 @@ package model import ( "crypto/rand" "encoding/base64" + + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) func generateRandomBytes(n int) []byte { @@ -33,3 +35,16 @@ func MergeAnnotations(requested map[string]string, existing map[string]string) m func BoolPtr(b bool) *bool { return &b } func IntPtr(b int64) *int64 { return &b } + +func SetCommonLabels(obj metav1.ObjectMetaAccessor) { + meta := obj.GetObjectMeta() + labels := meta.GetLabels() + if labels == nil { + labels = CommonLabels + } else { + for k, v := range CommonLabels { + labels[k] = v + } + } + meta.SetLabels(labels) +} diff --git a/controllers/reconcilers/grafana/admin_secret_reconciler.go b/controllers/reconcilers/grafana/admin_secret_reconciler.go index 1ddf4e271..d52b16aeb 100644 --- a/controllers/reconcilers/grafana/admin_secret_reconciler.go +++ b/controllers/reconcilers/grafana/admin_secret_reconciler.go @@ -27,6 +27,7 @@ func NewAdminSecretReconciler(client client.Client) reconcilers.OperatorGrafanaR func (r *AdminSecretReconciler) Reconcile(ctx context.Context, cr *v1beta1.Grafana, status *v1beta1.GrafanaStatus, vars *v1beta1.OperatorReconcileVars, scheme *runtime.Scheme) (v1beta1.OperatorStageStatus, error) { secret := model.GetGrafanaAdminSecret(cr, scheme) _, err := controllerutil.CreateOrUpdate(ctx, r.client, secret, func() error { + model.SetCommonLabels(secret) secret.Data = getData(cr, secret) return nil }) diff --git a/controllers/reconcilers/grafana/config_reconciler.go b/controllers/reconcilers/grafana/config_reconciler.go index c57ba561a..965772a42 100644 --- a/controllers/reconcilers/grafana/config_reconciler.go +++ b/controllers/reconcilers/grafana/config_reconciler.go @@ -31,6 +31,7 @@ func (r *ConfigReconciler) Reconcile(ctx context.Context, cr *v1beta1.Grafana, s configMap := model.GetGrafanaConfigMap(cr, scheme) _, err := controllerutil.CreateOrUpdate(ctx, r.client, configMap, func() error { + model.SetCommonLabels(configMap) if configMap.Data == nil { configMap.Data = make(map[string]string) } diff --git a/controllers/reconcilers/grafana/deployment_reconciler.go b/controllers/reconcilers/grafana/deployment_reconciler.go index a686c9945..8d72fafc1 100644 --- a/controllers/reconcilers/grafana/deployment_reconciler.go +++ b/controllers/reconcilers/grafana/deployment_reconciler.go @@ -52,6 +52,7 @@ func (r *DeploymentReconciler) Reconcile(ctx context.Context, cr *v1beta1.Grafan deployment := model.GetGrafanaDeployment(cr, scheme) _, err := controllerutil.CreateOrUpdate(ctx, r.client, deployment, func() error { + model.SetCommonLabels(deployment) deployment.Spec = getDeploymentSpec(cr, deployment.Name, scheme, vars, openshiftPlatform) err := v1beta1.Merge(deployment, cr.Spec.Deployment) return err diff --git a/controllers/reconcilers/grafana/grafana_service_reconciler.go b/controllers/reconcilers/grafana/grafana_service_reconciler.go index d6ee80070..92d119782 100644 --- a/controllers/reconcilers/grafana/grafana_service_reconciler.go +++ b/controllers/reconcilers/grafana/grafana_service_reconciler.go @@ -33,6 +33,7 @@ func (r *ServiceReconciler) Reconcile(ctx context.Context, cr *v1beta1.Grafana, service := model.GetGrafanaService(cr, scheme) _, err := controllerutil.CreateOrUpdate(ctx, r.client, service, func() error { + model.SetCommonLabels(service) service.Spec = v1.ServiceSpec{ Ports: getServicePorts(cr), Selector: map[string]string{ diff --git a/controllers/reconcilers/grafana/ingress_reconciler.go b/controllers/reconcilers/grafana/ingress_reconciler.go index 84190c5e0..25e37eb5a 100644 --- a/controllers/reconcilers/grafana/ingress_reconciler.go +++ b/controllers/reconcilers/grafana/ingress_reconciler.go @@ -52,6 +52,7 @@ func (r *IngressReconciler) reconcileIngress(ctx context.Context, cr *v1beta1.Gr ingress := model.GetGrafanaIngress(cr, scheme) _, err := controllerutil.CreateOrUpdate(ctx, r.client, ingress, func() error { + model.SetCommonLabels(ingress) ingress.Spec = getIngressSpec(cr, scheme) return v1beta1.Merge(ingress, cr.Spec.Ingress) }) @@ -85,6 +86,7 @@ func (r *IngressReconciler) reconcileRoute(ctx context.Context, cr *v1beta1.Graf route := model.GetGrafanaRoute(cr, scheme) _, err := controllerutil.CreateOrUpdate(ctx, r.client, route, func() error { + model.SetCommonLabels(route) route.Spec = getRouteSpec(cr, scheme) err := v1beta1.Merge(route, cr.Spec.Route) return err diff --git a/controllers/reconcilers/grafana/plugins_reconciler.go b/controllers/reconcilers/grafana/plugins_reconciler.go index 94da7249e..f2c98d9af 100644 --- a/controllers/reconcilers/grafana/plugins_reconciler.go +++ b/controllers/reconcilers/grafana/plugins_reconciler.go @@ -7,9 +7,9 @@ import ( "github.com/grafana/grafana-operator/v5/api/v1beta1" "github.com/grafana/grafana-operator/v5/controllers/model" "github.com/grafana/grafana-operator/v5/controllers/reconcilers" - "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/runtime" "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" "sigs.k8s.io/controller-runtime/pkg/log" ) @@ -26,27 +26,14 @@ func NewPluginsReconciler(client client.Client) reconcilers.OperatorGrafanaRecon func (r *PluginsReconciler) Reconcile(ctx context.Context, cr *v1beta1.Grafana, status *v1beta1.GrafanaStatus, vars *v1beta1.OperatorReconcileVars, scheme *runtime.Scheme) (v1beta1.OperatorStageStatus, error) { logger := log.FromContext(ctx).WithName("PluginsReconciler") - plugins := model.GetPluginsConfigMap(cr, scheme) - selector := client.ObjectKey{ - Namespace: plugins.Namespace, - Name: plugins.Name, - } - - err := r.client.Get(ctx, selector, plugins) + vars.Plugins = "" - // plugins config map not found, we need to create it - if err != nil && errors.IsNotFound(err) { - err = r.client.Create(ctx, plugins) - if err != nil { - logger.Error(err, "error creating plugins config map", "name", plugins.Name, "namespace", plugins.Namespace) - return v1beta1.OperatorStageResultFailed, err - } - - // no plugins yet, assign plugins to empty string - vars.Plugins = "" - - return v1beta1.OperatorStageResultSuccess, nil - } else if err != nil { + plugins := model.GetPluginsConfigMap(cr, scheme) + _, err := controllerutil.CreateOrUpdate(ctx, r.client, plugins, func() error { + model.SetCommonLabels(plugins) + return nil + }) + if err != nil { logger.Error(err, "error getting plugins config map", "name", plugins.Name, "namespace", plugins.Namespace) return v1beta1.OperatorStageResultFailed, err } diff --git a/controllers/reconcilers/grafana/pvc_reconciler.go b/controllers/reconcilers/grafana/pvc_reconciler.go index 24362362f..b18be8ed8 100644 --- a/controllers/reconcilers/grafana/pvc_reconciler.go +++ b/controllers/reconcilers/grafana/pvc_reconciler.go @@ -32,6 +32,7 @@ func (r *PvcReconciler) Reconcile(ctx context.Context, cr *v1beta1.Grafana, stat pvc := model.GetGrafanaDataPVC(cr, scheme) _, err := controllerutil.CreateOrUpdate(ctx, r.client, pvc, func() error { + model.SetCommonLabels(pvc) return v1beta1.Merge(pvc, cr.Spec.PersistentVolumeClaim) }) if err != nil { diff --git a/controllers/reconcilers/grafana/service_account_reconciler.go b/controllers/reconcilers/grafana/service_account_reconciler.go index 8389a7343..fc05d96a7 100644 --- a/controllers/reconcilers/grafana/service_account_reconciler.go +++ b/controllers/reconcilers/grafana/service_account_reconciler.go @@ -25,6 +25,7 @@ func (r *ServiceAccountReconciler) Reconcile(ctx context.Context, cr *v1beta1.Gr sa := model.GetGrafanaServiceAccount(cr, scheme) _, err := controllerutil.CreateOrUpdate(ctx, r.client, sa, func() error { + model.SetCommonLabels(sa) return v1beta1.Merge(sa, cr.Spec.ServiceAccount) }) if err != nil {