Skip to content

Commit

Permalink
restrict manager resource cache to only operator namespace
Browse files Browse the repository at this point in the history
Signed-off-by: Leela Venkaiah G <[email protected]>
  • Loading branch information
leelavg committed Dec 11, 2024
1 parent be6a10a commit 50bc611
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package main
import (
"crypto/tls"
"flag"
"fmt"
"os"

// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
Expand All @@ -29,6 +30,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 +96,11 @@ func main() {
TLSOpts: tlsOpts,
})

operatorNamespace, err := getOperatorNamespace()
if err != nil {
setupLog.Error(err, "manager requires namespace to be registered for caching resources")
os.Exit(1)
}
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
Metrics: metricsserver.Options{
Expand All @@ -116,6 +123,9 @@ 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: map[string]cache.Config{operatorNamespace: {}},
},
})
if err != nil {
setupLog.Error(err, "unable to start manager")
Expand Down Expand Up @@ -160,3 +170,14 @@ func main() {
os.Exit(1)
}
}

// getOperatorNamespace returns the Namespace the operator should be watching for changes
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
}

0 comments on commit 50bc611

Please sign in to comment.