Skip to content
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

restrict manager resource cache based on namespaces from environment #186

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ package main
import (
"crypto/tls"
"flag"
"fmt"
"os"
"strings"

// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
// to ensure that exec-entrypoint and run can make use of them.
Expand All @@ -29,6 +31,7 @@ import (
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
Expand Down Expand Up @@ -94,6 +97,23 @@ func main() {
TLSOpts: tlsOpts,
})

defaultNamespaces := map[string]cache.Config{}
operatorNamespace, err := getOperatorNamespace()
if err != nil {
setupLog.Error(err, "manager requires namespace to be registered for controllers to reconcile")
os.Exit(1)
}
// ensure we always cache items from operator namespace
defaultNamespaces[operatorNamespace] = cache.Config{}

watchNamespace, err := getWatchNamespace()
if err != nil {
setupLog.Error(err, "manager will only watch for resources in the operator deployed namespace")
} else {
for _, namespace := range strings.Split(watchNamespace, ",") {
defaultNamespaces[namespace] = cache.Config{}
}
}
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
Metrics: metricsserver.Options{
Expand All @@ -116,6 +136,7 @@ func main() {
// if you are doing or is intended to do any operation such as perform cleanups
// after the manager stops then its usage might be unsafe.
// LeaderElectionReleaseOnCancel: true,
Cache: cache.Options{DefaultNamespaces: defaultNamespaces},
})
if err != nil {
setupLog.Error(err, "unable to start manager")
Expand Down Expand Up @@ -160,3 +181,25 @@ func main() {
os.Exit(1)
}
}

// getWatchNamespace returns the Namespace the operator should be watching for changes
func getWatchNamespace() (string, error) {
var watchNamespaceEnvVar = "WATCH_NAMESPACE"

ns := os.Getenv(watchNamespaceEnvVar)
if ns == "" {
return "", fmt.Errorf("%s must be set", watchNamespaceEnvVar)
}
return ns, nil
}

// getOperatorNamespace returns the Namespace the operator is running
func getOperatorNamespace() (string, error) {
var operatorNamespaceEnvVar = "OPERATOR_NAMESPACE"

ns := os.Getenv(operatorNamespaceEnvVar)
if ns == "" {
return "", fmt.Errorf("%s must be set", operatorNamespaceEnvVar)
}
return ns, nil
}
Loading