Skip to content

Commit

Permalink
Merge pull request grafana#664 from zoetrope/watch-datasource-configmap
Browse files Browse the repository at this point in the history
Watch ConfigMap to prevent datasources from not being imported.
  • Loading branch information
pb82 authored Feb 22, 2022
2 parents 97f526b + b146ab4 commit 3921869
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions controllers/grafanadatasource/datasource_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ import (
"github.com/grafana-operator/grafana-operator/v4/controllers/constants"
v1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/handler"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"

"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -234,7 +237,30 @@ func (r *GrafanaDatasourceReconciler) manageSuccess(datasources []grafanav1alpha

// SetupWithManager sets up the controller with the Manager.
func (r *GrafanaDatasourceReconciler) SetupWithManager(mgr ctrl.Manager) error {
cmHandler := func(o client.Object) []reconcile.Request {
if o.GetName() != constants.GrafanaDatasourcesConfigMapName {
return nil
}
ns := o.GetNamespace()
list := &grafanav1alpha1.GrafanaDataSourceList{}
opts := &client.ListOptions{
Namespace: ns,
}
err := r.Client.List(context.Background(), list, opts)
if err != nil {
return nil
}
requests := make([]reconcile.Request, len(list.Items))
for i, ds := range list.Items {
requests[i] = reconcile.Request{NamespacedName: types.NamespacedName{
Namespace: ns,
Name: ds.GetName(),
}}
}
return requests
}
return ctrl.NewControllerManagedBy(mgr).
For(&integreatlyorgv1alpha1.GrafanaDataSource{}).
Watches(&source.Kind{Type: &v1.ConfigMap{}}, handler.EnqueueRequestsFromMapFunc(cmHandler)).
Complete(r)
}

0 comments on commit 3921869

Please sign in to comment.