-
Notifications
You must be signed in to change notification settings - Fork 402
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Allow to restrict the CRs watched according to their labels #1832
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
"os" | ||
"os/signal" | ||
"strings" | ||
|
@@ -65,6 +66,10 @@ | |
// eg: "environment: dev" | ||
// If empty or undefined, the operator will run in cluster scope. | ||
watchNamespaceEnvSelector = "WATCH_NAMESPACE_SELECTOR" | ||
// watchLabelSelectorsEnvVar is the constant for env variable WATCH_LABEL_SELECTORS which specifies the resources to watch according to their labels. | ||
// eg: 'partition in (customerA, customerB),environment!=qa' | ||
// If empty of undefined, the operator will watch all CRs. | ||
watchLabelSelectorsEnvVar = "WATCH_LABEL_SELECTORS" | ||
) | ||
|
||
var ( | ||
|
@@ -81,7 +86,7 @@ | |
//+kubebuilder:scaffold:scheme | ||
} | ||
|
||
func main() { | ||
Check failure on line 89 in main.go GitHub Actions / go-lint
|
||
var metricsAddr string | ||
var enableLeaderElection bool | ||
var probeAddr string | ||
|
@@ -105,6 +110,7 @@ | |
|
||
watchNamespace, _ := os.LookupEnv(watchNamespaceEnvVar) | ||
watchNamespaceSelector, _ := os.LookupEnv(watchNamespaceEnvSelector) | ||
watchLabelSelectors, _ := os.LookupEnv(watchLabelSelectorsEnvVar) | ||
|
||
controllerOptions := ctrl.Options{ | ||
Scheme: scheme, | ||
|
@@ -120,6 +126,18 @@ | |
PprofBindAddress: pprofAddr, | ||
} | ||
|
||
var labelSelectors labels.Selector | ||
var err error | ||
if watchLabelSelectors != "" { | ||
labelSelectors, err = labels.Parse(watchLabelSelectors) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: I like this quite a lot, and would prefer to swap the existing namespace selector parsing to this or the |
||
if err != nil { | ||
setupLog.Error(err, fmt.Sprintf("unable to parse %s", watchLabelSelectorsEnvVar)) | ||
os.Exit(1) //nolint | ||
} | ||
} else { | ||
labelSelectors = labels.Everything() // Match any labels | ||
} | ||
|
||
getNamespaceConfig := func(namespaces string) map[string]cache.Config { | ||
defaultNamespaces := map[string]cache.Config{} | ||
for _, v := range strings.Split(namespaces, ",") { | ||
|
@@ -128,7 +146,7 @@ | |
// this is the default behavior of the operator on v5, if you require finer grained control over this | ||
// please file an issue in the grafana-operator/grafana-operator GH project | ||
defaultNamespaces[v] = cache.Config{ | ||
LabelSelector: labels.Everything(), // Match any labels | ||
LabelSelector: labelSelectors, | ||
FieldSelector: fields.Everything(), // Match any fields | ||
Transform: nil, | ||
UnsafeDisableDeepCopy: nil, | ||
|
@@ -156,7 +174,7 @@ | |
// this is the default behavior of the operator on v5, if you require finer grained control over this | ||
// please file an issue in the grafana-operator/grafana-operator GH project | ||
defaultNamespaces[v.Name] = cache.Config{ | ||
LabelSelector: labels.Everything(), // Match any labels | ||
LabelSelector: labelSelectors, | ||
FieldSelector: fields.Everything(), // Match any fields | ||
Transform: nil, | ||
UnsafeDisableDeepCopy: nil, | ||
|
@@ -180,6 +198,7 @@ | |
|
||
case watchNamespace == "" && watchNamespaceSelector == "": | ||
// cluster scoped | ||
controllerOptions.Cache.DefaultLabelSelector = labelSelectors | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will unfortunately break the operator in several ways:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With #1818 most of this should be fine if we either
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only applying the watchLabels on Grafana CRDs will hide them due to the defaultLabelSelector unless overwritten with |
||
setupLog.Info("operator running in cluster scoped mode") | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This block triggers an error from the linter
I could move this block of code in a dedicated func outside of the main.
WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be honest, I think it's more readable when contained in the main function. If the refactor is easy enough then go for it, otherwise I'm good with ignoring the linter here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's keep it like that, I would have moved the code outside only to please the linter. I find the code readable enough.