Skip to content

Commit

Permalink
fix: add common labels to created resources
Browse files Browse the repository at this point in the history
  • Loading branch information
theSuess committed Sep 5, 2024
1 parent 4255eb3 commit abb6838
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 20 deletions.
1 change: 1 addition & 0 deletions controllers/model/dashboard_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions controllers/model/grafana_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
},
}

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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 {
Expand Down
15 changes: 15 additions & 0 deletions controllers/model/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package model
import (
"crypto/rand"
"encoding/base64"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func generateRandomBytes(n int) []byte {
Expand Down Expand Up @@ -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)
}
1 change: 1 addition & 0 deletions controllers/reconcilers/grafana/admin_secret_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
Expand Down
1 change: 1 addition & 0 deletions controllers/reconcilers/grafana/config_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
1 change: 1 addition & 0 deletions controllers/reconcilers/grafana/deployment_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
2 changes: 2 additions & 0 deletions controllers/reconcilers/grafana/ingress_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down Expand Up @@ -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
Expand Down
28 changes: 8 additions & 20 deletions controllers/reconcilers/grafana/plugins_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -26,27 +26,15 @@ 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)

// 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
}
vars.Plugins = ""

// no plugins yet, assign plugins to empty string
vars.Plugins = ""
plugins := model.GetPluginsConfigMap(cr, scheme)
_, err := controllerutil.CreateOrUpdate(ctx, r.client, plugins, func() error {
model.SetCommonLabels(plugins)
return nil
})

return v1beta1.OperatorStageResultSuccess, nil
} else if err != nil {
if err != nil {
logger.Error(err, "error getting plugins config map", "name", plugins.Name, "namespace", plugins.Namespace)
return v1beta1.OperatorStageResultFailed, err
}
Expand Down
1 change: 1 addition & 0 deletions controllers/reconcilers/grafana/pvc_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit abb6838

Please sign in to comment.