Skip to content

Commit 9a9bb49

Browse files
committed
feat: Limit caching of ConfigMaps and Secrets
feat: Toggle caching of ConfigMaps and Secrets with CommonLabels
1 parent e94237b commit 9a9bb49

File tree

4 files changed

+30
-6
lines changed

4 files changed

+30
-6
lines changed

deploy/helm/grafana-operator/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,5 +104,6 @@ It's easier to just manage this configuration outside of the operator.
104104
| serviceMonitor.targetLabels | list | `[]` | Set of labels to transfer from the Kubernetes Service onto the target |
105105
| serviceMonitor.telemetryPath | string | `"/metrics"` | Set path to metrics path |
106106
| tolerations | list | `[]` | pod tolerations |
107+
| watchLabeledReferencesOnly | bool | `false` | Sets the `WATCH_LABELED_REFERENCES_ONLY` environment variable, it enables the caching of ConfigMaps and Secrets labeled with `"app.kubernetes.io/managed-by": "grafana-operator"` to reduce requests to the api. By default, ConfigMaps and Secrets are not cached to reduce the memory usage of the operator in large clusters. WARNING This will hide unlabeled ConfigMaps and Secrets from the Operator |
107108
| watchNamespaceSelector | string | `""` | Sets the `WATCH_NAMESPACE_SELECTOR` environment variable, it defines which namespaces the operator should be listening for based on a namespace label (e.g. `"environment: dev"`). By default, the operator watches all namespaces. To make it watch only its own namespace, check out `namespaceScope` option instead. |
108109
| watchNamespaces | string | `""` | Sets the `WATCH_NAMESPACE` environment variable, it defines which namespaces the operator should be listening for (e.g. `"grafana, foo"`). By default, the operator watches all namespaces. To make it watch only its own namespace, check out `namespaceScope` option instead. |

deploy/helm/grafana-operator/templates/deployment.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ spec:
5151
{{ else }}
5252
value: {{quote .Values.watchNamespaceSelector }}
5353
{{- end }}
54+
- name: WATCH_LABELED_REFERENCES_ONLY
55+
{{- if .Values.watchLabeledReferencesOnly }}
56+
value: "true"
57+
{{ else }}
58+
value: ""
59+
{{- end }}
5460
{{- with .Values.env }}
5561
{{- toYaml . | nindent 12 }}
5662
{{- end }}

deploy/helm/grafana-operator/values.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ watchNamespaces: ""
1515
# By default, the operator watches all namespaces. To make it watch only its own namespace, check out `namespaceScope` option instead.
1616
watchNamespaceSelector: ""
1717

18+
# -- Sets the `WATCH_LABELED_REFERENCES_ONLY` environment variable,
19+
# it enables the caching of ConfigMaps and Secrets labeled with `"app.kubernetes.io/managed-by": "grafana-operator"` to reduce requests to the api.
20+
# By default, ConfigMaps and Secrets are not cached to reduce the memory usage of the operator in large clusters.
21+
# WARNING This will hide unlabeled ConfigMaps and Secrets from the Operator
22+
watchLabeledReferencesOnly: false
23+
1824
# -- Determines if the target cluster is OpenShift. Additional rbac permissions for routes will be added on OpenShift
1925
isOpenShift: false
2026

main.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ const (
6767
// eg: "environment: dev"
6868
// If empty or undefined, the operator will run in cluster scope.
6969
watchNamespaceEnvSelector = "WATCH_NAMESPACE_SELECTOR"
70+
// Enable caching of ConfigMaps and Secrets to reduce API read requests
71+
// If empty or undefined, the operator will disable caching
72+
// This will hide all referenced ConfigMaps and Secrets not labeled with: app.kubernetes.io/managed-by: grafana-operator
73+
watchLabeledReferencesOnlyEnvVar = "WATCH_LABELED_REFERENCES_ONLY"
7074
)
7175

7276
var (
@@ -105,8 +109,10 @@ func main() {
105109

106110
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
107111

112+
// Detect environment variables
108113
watchNamespace, _ := os.LookupEnv(watchNamespaceEnvVar)
109114
watchNamespaceSelector, _ := os.LookupEnv(watchNamespaceEnvSelector)
115+
_, watchLabeledReferencesOnly := os.LookupEnv(watchLabeledReferencesOnlyEnvVar)
110116

111117
// Platform detection
112118
restConfig := ctrl.GetConfigOrDie()
@@ -131,23 +137,28 @@ func main() {
131137
LeaderElectionID: "f75f3bba.integreatly.org",
132138
PprofBindAddress: pprofAddr,
133139
// Limit caching to reduce heap usage with CommonLabels as selector
134-
// ConfigMap and Secret are omitted here to prevent interference with Get and List in reconcilers, see TODO below
135140
Cache: cache.Options{ByObject: map[client.Object]cache.ByObject{
136141
&v1.Deployment{}: cacheLabels,
137142
&corev1.Service{}: cacheLabels,
138143
&corev1.ServiceAccount{}: cacheLabels,
139144
&networkingv1.Ingress{}: cacheLabels,
140145
&corev1.PersistentVolumeClaim{}: cacheLabels,
146+
&corev1.ConfigMap{}: cacheLabels, // Matching just labeled ConfigMaps and Secrets greatly reduces cache size
147+
&corev1.Secret{}: cacheLabels, // Omitting labels or supporting custom labels would require changes in Grafana Reconciler
141148
}},
142149
}
143150
if isOpenShift {
144151
controllerOptions.Cache.ByObject[&routev1.Route{}] = cacheLabels
145152
}
146-
147-
// TODO Add a config option to limit ConfigMaps and Secrets in Cache
148-
// Likely similar to how namespace scope is handled
149-
// controllerOptions.Cache.ByObject[&corev1.ConfigMap{}] = cacheLabels
150-
// controllerOptions.Cache.ByObject[&corev1.Secret{}] = cacheLabels
153+
// Disable ConfigMap and Secret cache lookups per default
154+
// all reads will hit the api
155+
if !watchLabeledReferencesOnly {
156+
controllerOptions.Client = client.Options{
157+
Cache: &client.CacheOptions{
158+
DisableFor: []client.Object{&corev1.ConfigMap{}, &corev1.Secret{}},
159+
},
160+
}
161+
}
151162

152163
// Determine Operator scope
153164
switch {

0 commit comments

Comments
 (0)