Skip to content

Commit 180bd7e

Browse files
committed
Limit namespaces scope
1 parent bed1c2f commit 180bd7e

File tree

6 files changed

+127
-28
lines changed

6 files changed

+127
-28
lines changed

charts/axonops-developer-operator/Chart.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ kubeVersion: ">= 1.24.0-0"
1111
type: application
1212

1313
# Chart version
14-
version: 0.1.0
14+
version: 0.2.0
1515

1616
# Latest container tag
17-
appVersion: v0.1.0
17+
appVersion: v0.1.0-beta1
1818

1919
maintainers:
2020
- email: info@axonops.com

charts/axonops-developer-operator/templates/deployment.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,18 @@ spec:
5757
- containerPort: {{ .Values.metricsPort | default 8080 }}
5858
name: metrics
5959
protocol: TCP
60+
livenessProbe:
61+
httpGet:
62+
path: /healthz
63+
port: 8081
64+
initialDelaySeconds: 3
65+
periodSeconds: 3
66+
readinessProbe:
67+
httpGet:
68+
path: /healthz
69+
port: 8081
70+
initialDelaySeconds: 3
71+
periodSeconds: 3
6072
{{- with .Values.nodeSelector }}
6173
nodeSelector:
6274
{{- toYaml . | nindent 8 }}

charts/axonops-developer-operator/templates/serviceaccount.yaml

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ rules:
1010
- "networking"
1111
resources:
1212
- "ingresses"
13-
- "services"
1413
verbs:
1514
- "get"
1615
- "list"
@@ -21,7 +20,18 @@ rules:
2120
- apiGroups:
2221
- "apps"
2322
resources:
24-
- "ingresses"
23+
- "deployments"
24+
- "statefulsets"
25+
verbs:
26+
- "get"
27+
- "list"
28+
- "watch"
29+
- "update"
30+
- "delete"
31+
- "create"
32+
- apiGroups:
33+
- ""
34+
resources:
2535
- "services"
2636
verbs:
2737
- "get"
@@ -40,7 +50,7 @@ rules:
4050
- apiGroups:
4151
- "axonops.com"
4252
resources:
43-
- "axonopscassandra"
53+
- "axonopscassandras"
4454
verbs:
4555
- "get"
4656
- "list"

charts/axonops-developer-operator/values.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ enableDbSecrets: true
2727

2828
# additional arguments to operator
2929
args: []
30+
# - metrics-bind-address=:8080
31+
# - health-probe-bind-address=:8081
32+
# - leader-elect=true
33+
# - leader-election-id=axonops-developer-operator
34+
# - watch-namespaces=default,one,two
35+
36+
# additional environment variables to operator
37+
env: []
38+
# - name: MY_ENV_VAR
39+
# value: "my value"
3040

3141
environmentSecret: ""
3242

cmd/main.go

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"crypto/tls"
2222
"flag"
2323
"os"
24+
"strings"
2425

2526
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
2627
// to ensure that exec-entrypoint and run can make use of them.
@@ -30,6 +31,7 @@ import (
3031
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
3132
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
3233
ctrl "sigs.k8s.io/controller-runtime"
34+
"sigs.k8s.io/controller-runtime/pkg/cache"
3335
"sigs.k8s.io/controller-runtime/pkg/healthz"
3436
"sigs.k8s.io/controller-runtime/pkg/log/zap"
3537
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
@@ -58,6 +60,7 @@ func main() {
5860
var probeAddr string
5961
var secureMetrics bool
6062
var enableHTTP2 bool
63+
var watchNamespaces string
6164
flag.StringVar(&metricsAddr, "metrics-bind-address", "0", "The address the metric endpoint binds to. "+
6265
"Use the port :8080. If not set, it will be '0 in order to disable the metrics server")
6366
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
@@ -68,6 +71,7 @@ func main() {
6871
"If set the metrics endpoint is served securely")
6972
flag.BoolVar(&enableHTTP2, "enable-http2", false,
7073
"If set, HTTP/2 will be enabled for the metrics and webhook servers")
74+
flag.StringVar(&watchNamespaces, "watch-namespaces", "", "Comma separated list of namespaces that vals-operator will watch.")
7175
opts := zap.Options{
7276
Development: true,
7377
}
@@ -96,6 +100,34 @@ func main() {
96100
TLSOpts: tlsOpts,
97101
})
98102

103+
var cacheOptions cache.Options
104+
105+
if watchNamespaces != "" {
106+
setupLog.Info("watching namespaces", "namespaces", watchNamespaces)
107+
108+
// Split the watchNamespaces string into a slice of namespaces
109+
namespaces := strings.Split(watchNamespaces, ",")
110+
111+
// Create a map to hold namespace configurations
112+
namespaceConfigs := make(map[string]cache.Config)
113+
114+
// Add each namespace to the map
115+
for _, ns := range namespaces {
116+
// Trim any whitespace from the namespace
117+
ns = strings.TrimSpace(ns)
118+
if ns != "" {
119+
namespaceConfigs[ns] = cache.Config{}
120+
}
121+
}
122+
123+
// Set the cache options with the namespace configurations
124+
cacheOptions = cache.Options{
125+
DefaultNamespaces: namespaceConfigs,
126+
}
127+
128+
setupLog.Info("configured cache for namespaces", "count", len(namespaceConfigs))
129+
}
130+
99131
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
100132
Scheme: scheme,
101133
Metrics: metricsserver.Options{
@@ -107,17 +139,7 @@ func main() {
107139
HealthProbeBindAddress: probeAddr,
108140
LeaderElection: enableLeaderElection,
109141
LeaderElectionID: "c9da0915.axonops.com",
110-
// LeaderElectionReleaseOnCancel defines if the leader should step down voluntarily
111-
// when the Manager ends. This requires the binary to immediately end when the
112-
// Manager is stopped, otherwise, this setting is unsafe. Setting this significantly
113-
// speeds up voluntary leader transitions as the new leader don't have to wait
114-
// LeaseDuration time first.
115-
//
116-
// In the default scaffold provided, the program ends immediately after
117-
// the manager stops, so would be fine to enable this option. However,
118-
// if you are doing or is intended to do any operation such as perform cleanups
119-
// after the manager stops then its usage might be unsafe.
120-
// LeaderElectionReleaseOnCancel: true,
142+
Cache: cacheOptions,
121143
})
122144
if err != nil {
123145
setupLog.Error(err, "unable to start manager")

0 commit comments

Comments
 (0)