@@ -20,6 +20,7 @@ import (
20
20
"context"
21
21
"flag"
22
22
"os"
23
+ "strings"
23
24
24
25
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
25
26
// to ensure that exec-entrypoint and run can make use of them.
@@ -30,6 +31,7 @@ import (
30
31
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
31
32
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
32
33
ctrl "sigs.k8s.io/controller-runtime"
34
+ "sigs.k8s.io/controller-runtime/pkg/cache"
33
35
"sigs.k8s.io/controller-runtime/pkg/healthz"
34
36
"sigs.k8s.io/controller-runtime/pkg/log/zap"
35
37
@@ -59,12 +61,16 @@ func main() {
59
61
var enableLeaderElection bool
60
62
var probeAddr string
61
63
var skipCheck bool
64
+ var watchNamespaces string
65
+ var watchAllNamespaces bool
62
66
flag .StringVar (& metricsAddr , "metrics-bind-address" , ":8080" , "The address the metric endpoint binds to." )
63
67
flag .StringVar (& probeAddr , "health-probe-bind-address" , ":8081" , "The address the probe endpoint binds to." )
64
68
flag .BoolVar (& enableLeaderElection , "leader-elect" , false ,
65
69
"Enable leader election for controller manager. " +
66
70
"Enabling this will ensure there is only one active controller manager." )
67
71
flag .BoolVar (& skipCheck , "skip-check" , false , "Skip check" )
72
+ flag .StringVar (& watchNamespaces , "watch-namespaces" , "" , "Watch namespaces" )
73
+ flag .BoolVar (& watchAllNamespaces , "watch-all-namespaces" , false , "Watch all namespaces" )
68
74
opts := zap.Options {
69
75
Development : true ,
70
76
}
@@ -73,7 +79,7 @@ func main() {
73
79
74
80
ctrl .SetLogger (zap .New (zap .UseFlagOptions (& opts )))
75
81
76
- mgr , err := ctrl . NewManager ( ctrl . GetConfigOrDie (), ctrl.Options {
82
+ crtlOptions := ctrl.Options {
77
83
Scheme : scheme ,
78
84
HealthProbeBindAddress : probeAddr ,
79
85
LeaderElection : enableLeaderElection ,
@@ -89,16 +95,35 @@ func main() {
89
95
// if you are doing or is intended to do any operation such as perform cleanups
90
96
// after the manager stops then its usage might be unsafe.
91
97
// LeaderElectionReleaseOnCancel: true,
92
- })
98
+ }
99
+
100
+ watchNamespacesList := strings .Split (watchNamespaces , "," )
101
+ namespaceMap := map [string ]cache.Config {}
102
+ namespaceMapBool := map [string ]bool {}
103
+ for _ , namespace := range watchNamespacesList {
104
+ namespaceMap [namespace ] = cache.Config {}
105
+ namespaceMapBool [namespace ] = true
106
+ }
107
+
108
+ if ! watchAllNamespaces {
109
+ crtlOptions .Cache = cache.Options {
110
+ DefaultNamespaces : namespaceMap ,
111
+ }
112
+ } else {
113
+ namespaceMapBool = nil
114
+ }
115
+
116
+ mgr , err := ctrl .NewManager (ctrl .GetConfigOrDie (), crtlOptions )
93
117
if err != nil {
94
118
setupLog .Error (err , "unable to start manager" )
95
119
os .Exit (1 )
96
120
}
97
121
98
122
if err = (& resourcescontrollers.BentoRequestReconciler {
99
- Client : mgr .GetClient (),
100
- Scheme : mgr .GetScheme (),
101
- Recorder : mgr .GetEventRecorderFor ("yatai-image-builder" ),
123
+ Client : mgr .GetClient (),
124
+ Scheme : mgr .GetScheme (),
125
+ Recorder : mgr .GetEventRecorderFor ("yatai-image-builder" ),
126
+ NamespaceMap : namespaceMapBool ,
102
127
}).SetupWithManager (mgr ); err != nil {
103
128
setupLog .Error (err , "unable to create controller" , "controller" , "BentoRequest" )
104
129
os .Exit (1 )
0 commit comments