Skip to content

Commit

Permalink
restrict manager resource cache based on namespaces from environment
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 8e05abf
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
33 changes: 31 additions & 2 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,7 +97,12 @@ func main() {
TLSOpts: tlsOpts,
})

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
watchNamespace, err := getWatchNamespace()
if err != nil {
setupLog.Error(err, "manager requires namespace to be registered for caching resources")
os.Exit(1)
}
options := ctrl.Options{
Scheme: scheme,
Metrics: metricsserver.Options{
BindAddress: metricsAddr,
Expand All @@ -116,7 +124,17 @@ 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{watchNamespace: {}},
},
}
if strings.Contains(watchNamespace, ",") {
setupLog.Info("manager set up with multiple namespaces", "namespaces", watchNamespace)
options.Namespace = ""

Check failure on line 133 in cmd/main.go

View workflow job for this annotation

GitHub Actions / golangci-lint

options.Namespace undefined (type manager.Options has no field or method Namespace)

Check failure on line 133 in cmd/main.go

View workflow job for this annotation

GitHub Actions / govulncheck

options.Namespace undefined (type manager.Options has no field or method Namespace)
options.NewCache = cache.MultiNamespacedCacheBuilder(strings.Split(watchNamespace, ","))

Check failure on line 134 in cmd/main.go

View workflow job for this annotation

GitHub Actions / golangci-lint

undefined: cache.MultiNamespacedCacheBuilder (typecheck)

Check failure on line 134 in cmd/main.go

View workflow job for this annotation

GitHub Actions / govulncheck

undefined: cache.MultiNamespacedCacheBuilder
}

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), options)
if err != nil {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
Expand Down Expand Up @@ -160,3 +178,14 @@ 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
}
4 changes: 4 additions & 0 deletions config/manager/manager.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ spec:
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: WATCH_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
resources:
limits:
cpu: 500m
Expand Down
4 changes: 4 additions & 0 deletions deploy/all-in-one/install.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15494,6 +15494,10 @@ spec:
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: WATCH_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: CSI_SERVICE_ACCOUNT_PREFIX
value: ceph-csi-operator-
image: quay.io/cephcsi/ceph-csi-operator:latest
Expand Down
4 changes: 4 additions & 0 deletions deploy/multifile/operator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,10 @@ spec:
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: WATCH_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: CSI_SERVICE_ACCOUNT_PREFIX
value: ceph-csi-operator-
image: quay.io/cephcsi/ceph-csi-operator:latest
Expand Down

0 comments on commit 8e05abf

Please sign in to comment.