Skip to content

Commit

Permalink
fix: enabling deleted grafana datasources to be recreated
Browse files Browse the repository at this point in the history
  • Loading branch information
Prashant Balachandran committed Dec 10, 2021
1 parent 94db768 commit cd13e13
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
10 changes: 8 additions & 2 deletions pkg/controllers/monitoring-stack/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,15 +318,21 @@ func stackComponentPatchers(ms *stack.MonitoringStack, instanceSelectorKey strin
func newGrafanaDataSource(ms *stack.MonitoringStack) *grafanav1alpha1.GrafanaDataSource {
datasourceName := fmt.Sprintf("ms-%s-%s", ms.Namespace, ms.Name)
prometheusURL := fmt.Sprintf("%s-prometheus.%s:9090", ms.Name, ms.Namespace)
annotations := map[string]string{
grafanaDatasourceOwnerName: ms.Name,
grafanaDatasourceOwnerNamespace: ms.Namespace,
}

return &grafanav1alpha1.GrafanaDataSource{
TypeMeta: metav1.TypeMeta{
// NOTE: uses a different naming convention for SchemeGroupVersion
APIVersion: grafanav1alpha1.GroupVersion.String(),
Kind: "GrafanaDataSource",
},
ObjectMeta: metav1.ObjectMeta{
Name: datasourceName,
Namespace: grafana_operator.Namespace,
Name: datasourceName,
Namespace: grafana_operator.Namespace,
Annotations: annotations,
},
Spec: grafanav1alpha1.GrafanaDataSourceSpec{
Name: datasourceName,
Expand Down
18 changes: 17 additions & 1 deletion pkg/controllers/monitoring-stack/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ import (
"strings"
"time"

grafanav1alpha1 "github.com/grafana-operator/grafana-operator/v4/api/integreatly/v1alpha1"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"

policyv1 "k8s.io/api/policy/v1"

"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
Expand All @@ -42,6 +47,11 @@ import (
monv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
)

const (
grafanaDatasourceOwnerName = "grafana.datasource.owner/name"
grafanaDatasourceOwnerNamespace = "grafana.datasource.owner/namespace"
)

type reconciler struct {
k8sClient client.Client
scheme *runtime.Scheme
Expand Down Expand Up @@ -100,7 +110,13 @@ func RegisterWithManager(mgr ctrl.Manager, opts Options) error {
Owns(&rbacv1.RoleBinding{}).WithEventFilter(p).
Owns(&monv1.ServiceMonitor{}).WithEventFilter(p).
Owns(&policyv1.PodDisruptionBudget{}).WithEventFilter(p).
Complete(r)
Watches(&source.Kind{Type: &grafanav1alpha1.GrafanaDataSource{}}, handler.EnqueueRequestsFromMapFunc(func(object client.Object) []reconcile.Request {
name := object.GetAnnotations()[grafanaDatasourceOwnerName]
namespace := object.GetAnnotations()[grafanaDatasourceOwnerNamespace]
namespaceName := types.NamespacedName{Name: name, Namespace: namespace}
return []reconcile.Request{{NamespacedName: namespaceName}}

})).Complete(r)
}

func (r *reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
Expand Down

0 comments on commit cd13e13

Please sign in to comment.