Skip to content

Commit

Permalink
feat: Allow to restrict the CRs watched according to their labels
Browse files Browse the repository at this point in the history
Signed-off-by: Wilfried Roset <[email protected]>
  • Loading branch information
wilfriedroset committed Jan 20, 2025
1 parent cef6a54 commit 673b010
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
6 changes: 6 additions & 0 deletions deploy/helm/grafana-operator/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ spec:
{{ else }}
value: {{quote .Values.watchNamespaceSelector }}
{{- end }}
- name: WATCH_LABEL_SELECTORS
{{- if and .Values.watchLabelSelectors (eq .Values.watchLabelSelectors "") }}
value: ""
{{ else }}
value: {{quote .Values.watchLabelSelectors | toJson }}
{{- end }}
{{- with .Values.env }}
{{- toYaml . | nindent 12 }}
{{- end }}
Expand Down
6 changes: 6 additions & 0 deletions deploy/helm/grafana-operator/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ watchNamespaces: ""
# By default, the operator watches all namespaces. To make it watch only its own namespace, check out `namespaceScope` option instead.
watchNamespaceSelector: ""

# -- Sets the `WATCH_LABEL_SELECTORS` environment variable,
# if defines which CRs are watched according to thei labels.
# By default, the operator watches all CRs. To make it watch only a subset of CRs, set the value to a map of key/value
watchLabelSelectors: {}
# labelKey: labelValue

# -- Determines if the target cluster is OpenShift. Additional rbac permissions for routes will be added on OpenShift
isOpenShift: false

Expand Down
24 changes: 24 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package main

import (
"context"
"encoding/json"
"flag"
"os"
"os/signal"
Expand Down Expand Up @@ -48,6 +49,7 @@ import (
"github.com/grafana/grafana-operator/v5/controllers/autodetect"
"github.com/grafana/grafana-operator/v5/embeds"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/selection"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
Expand All @@ -65,6 +67,10 @@ const (
// eg: "environment: dev"
// If empty or undefined, the operator will run in cluster scope.
watchNamespaceEnvSelector = "WATCH_NAMESPACE_SELECTOR"
// watchLabelSelectorsEnvVar is the constnat for env variable WATCH_LABEL_SELECTORS which specifies the resources to watch according to their labels.
// eg: '{"labelKey": "labelValue"}'
// If empty of undefined, the operator will watch all CRs.
watchLabelSelectorsEnvVar = "WATCH_LABEL_SELECTORS"
)

var (
Expand Down Expand Up @@ -105,6 +111,7 @@ func main() {

watchNamespace, _ := os.LookupEnv(watchNamespaceEnvVar)
watchNamespaceSelector, _ := os.LookupEnv(watchNamespaceEnvSelector)
watchLabelSelectors, _ := os.LookupEnv(watchLabelSelectorsEnvVar)

controllerOptions := ctrl.Options{
Scheme: scheme,
Expand Down Expand Up @@ -183,6 +190,23 @@ func main() {
setupLog.Info("operator running in cluster scoped mode")
}

if watchLabelSelectors != "" {
labelSelectors := map[string]string{}
err := json.Unmarshal([]byte(watchLabelSelectors), &labelSelectors)
if err != nil {
setupLog.Error(err, "unable to Unmarshal labelSelectors")
os.Exit(1) //nolint
}
for k, v := range labelSelectors {
req, err := labels.NewRequirement(k, selection.Equals, []string{v}, nil)
if err != nil {
setupLog.Error(err, "unable to create labels selector")
os.Exit(1) //nolint
}
controllerOptions.Cache.DefaultLabelSelector.Add(*req)
}
}

ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGINT, syscall.SIGTERM, syscall.SIGPIPE)
defer stop()

Expand Down

0 comments on commit 673b010

Please sign in to comment.