Skip to content

Commit 711b490

Browse files
authored
Merge pull request #186 from leelavg/oom-fix
restrict manager resource cache based on namespaces from environment
2 parents bdd5baa + ada2433 commit 711b490

File tree

6 files changed

+60
-2
lines changed

6 files changed

+60
-2
lines changed

Makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ IMAGE_NAME ?= ceph-csi-operator
77
# Allow customization of the name prefix and/or namespace
88
NAME_PREFIX ?= ceph-csi-operator-
99
NAMESPACE ?= $(NAME_PREFIX)system
10+
# A comma separated list of namespaces for operator to cache objects from
11+
WATCH_NAMESPACE ?= ""
1012

1113
IMG ?= $(IMAGE_REGISTRY)/$(REGISTRY_NAMESPACE)/$(IMAGE_NAME):$(IMAGE_TAG)
1214

@@ -47,6 +49,11 @@ patches:
4749
value:
4850
name: CSI_SERVICE_ACCOUNT_PREFIX
4951
value: $(NAME_PREFIX)
52+
- op: add
53+
path: /spec/template/spec/containers/1/env/-
54+
value:
55+
name: WATCH_NAMESPACE
56+
value: $(WATCH_NAMESPACE)
5057
target:
5158
kind: Deployment
5259
name: controller-manager

cmd/main.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ package main
1919
import (
2020
"crypto/tls"
2121
"flag"
22+
"fmt"
2223
"os"
24+
"strings"
2325

2426
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
2527
// to ensure that exec-entrypoint and run can make use of them.
@@ -29,13 +31,15 @@ import (
2931
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
3032
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
3133
ctrl "sigs.k8s.io/controller-runtime"
34+
"sigs.k8s.io/controller-runtime/pkg/cache"
3235
"sigs.k8s.io/controller-runtime/pkg/healthz"
3336
"sigs.k8s.io/controller-runtime/pkg/log/zap"
3437
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
3538
"sigs.k8s.io/controller-runtime/pkg/webhook"
3639

3740
csiv1alpha1 "github.com/ceph/ceph-csi-operator/api/v1alpha1"
3841
"github.com/ceph/ceph-csi-operator/internal/controller"
42+
"github.com/ceph/ceph-csi-operator/internal/utils"
3943
//+kubebuilder:scaffold:imports
4044
)
4145

@@ -94,6 +98,23 @@ func main() {
9498
TLSOpts: tlsOpts,
9599
})
96100

101+
defaultNamespaces := map[string]cache.Config{}
102+
operatorNamespace, err := utils.GetOperatorNamespace()
103+
if err != nil {
104+
setupLog.Error(err, "manager requires namespace to be registered for controllers to reconcile")
105+
os.Exit(1)
106+
}
107+
// ensure we always cache items from operator namespace
108+
defaultNamespaces[operatorNamespace] = cache.Config{}
109+
110+
watchNamespace, err := getWatchNamespace()
111+
if err != nil {
112+
setupLog.Error(err, "manager will only watch for resources in the operator deployed namespace")
113+
} else {
114+
for _, namespace := range strings.Split(watchNamespace, ",") {
115+
defaultNamespaces[namespace] = cache.Config{}
116+
}
117+
}
97118
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
98119
Scheme: scheme,
99120
Metrics: metricsserver.Options{
@@ -116,6 +137,7 @@ func main() {
116137
// if you are doing or is intended to do any operation such as perform cleanups
117138
// after the manager stops then its usage might be unsafe.
118139
// LeaderElectionReleaseOnCancel: true,
140+
Cache: cache.Options{DefaultNamespaces: defaultNamespaces},
119141
})
120142
if err != nil {
121143
setupLog.Error(err, "unable to start manager")
@@ -160,3 +182,14 @@ func main() {
160182
os.Exit(1)
161183
}
162184
}
185+
186+
// getWatchNamespace returns the Namespace the operator should be watching for changes
187+
func getWatchNamespace() (string, error) {
188+
var watchNamespaceEnvVar = "WATCH_NAMESPACE"
189+
190+
ns := os.Getenv(watchNamespaceEnvVar)
191+
if ns == "" {
192+
return "", fmt.Errorf("%s must be set", watchNamespaceEnvVar)
193+
}
194+
return ns, nil
195+
}

deploy/all-in-one/install.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15496,6 +15496,8 @@ spec:
1549615496
fieldPath: metadata.namespace
1549715497
- name: CSI_SERVICE_ACCOUNT_PREFIX
1549815498
value: ceph-csi-operator-
15499+
- name: WATCH_NAMESPACE
15500+
value: ""
1549915501
image: quay.io/cephcsi/ceph-csi-operator:latest
1550015502
livenessProbe:
1550115503
httpGet:

deploy/multifile/operator.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,8 @@ spec:
628628
fieldPath: metadata.namespace
629629
- name: CSI_SERVICE_ACCOUNT_PREFIX
630630
value: ceph-csi-operator-
631+
- name: WATCH_NAMESPACE
632+
value: ""
631633
image: quay.io/cephcsi/ceph-csi-operator:latest
632634
livenessProbe:
633635
httpGet:

internal/controller/defaults.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ var defaultDeploymentStrategy = appsv1.DeploymentStrategy{
6666
}
6767

6868
var operatorNamespace = utils.Call(func() string {
69-
namespace := os.Getenv("OPERATOR_NAMESPACE")
70-
if namespace == "" {
69+
namespace, err := utils.GetOperatorNamespace()
70+
if err != nil {
7171
panic("Required OPERATOR_NAMESPACE environment variable is either missing or empty")
7272
}
7373
return namespace

internal/utils/core.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,17 @@ package utils
1818

1919
import (
2020
"cmp"
21+
"fmt"
22+
"os"
2123
"slices"
2224
"strings"
2325
"sync"
2426
)
2527

28+
const (
29+
operatorNamespaceEnvVar = "OPERATOR_NAMESPACE"
30+
)
31+
2632
// RunConcurrently runs all the of the given functions concurrently returning a channel with
2733
// the functions' return values (of type error) then closes the channel when all functions return.
2834
func RunConcurrently(fnList ...func() error) chan error {
@@ -129,3 +135,11 @@ func DeleteZeroValues[T comparable](slice []T) []T {
129135
return value == zero
130136
})
131137
}
138+
139+
func GetOperatorNamespace() (string, error) {
140+
ns := os.Getenv(operatorNamespaceEnvVar)
141+
if ns == "" {
142+
return "", fmt.Errorf("%s must be set", operatorNamespaceEnvVar)
143+
}
144+
return ns, nil
145+
}

0 commit comments

Comments
 (0)