@@ -19,8 +19,8 @@ package main
1919import (
2020 "flag"
2121 "fmt"
22- "net/http"
2322 "os"
23+ goruntime "runtime"
2424 "time"
2525
2626 "github.com/spf13/pflag"
@@ -34,11 +34,14 @@ import (
3434 "sigs.k8s.io/cluster-api-operator/internal/webhook"
3535 clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
3636 clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"
37+ "sigs.k8s.io/cluster-api/util/flags"
3738 "sigs.k8s.io/cluster-api/version"
3839 ctrl "sigs.k8s.io/controller-runtime"
40+ cache "sigs.k8s.io/controller-runtime/pkg/cache"
3941 "sigs.k8s.io/controller-runtime/pkg/client"
4042 "sigs.k8s.io/controller-runtime/pkg/controller"
4143 "sigs.k8s.io/controller-runtime/pkg/healthz"
44+ ctrlwebhook "sigs.k8s.io/controller-runtime/pkg/webhook"
4245
4346 operatorv1alpha1 "sigs.k8s.io/cluster-api-operator/api/v1alpha1"
4447 operatorv1 "sigs.k8s.io/cluster-api-operator/api/v1alpha2"
@@ -51,18 +54,20 @@ var (
5154 setupLog = ctrl .Log .WithName ("setup" )
5255
5356 // flags.
54- metricsBindAddr string
5557 enableLeaderElection bool
5658 leaderElectionLeaseDuration time.Duration
5759 leaderElectionRenewDeadline time.Duration
5860 leaderElectionRetryPeriod time.Duration
5961 watchFilterValue string
62+ watchNamespace string
6063 profilerAddress string
64+ enableContentionProfiling bool
6165 concurrencyNumber int
6266 syncPeriod time.Duration
6367 webhookPort int
6468 webhookCertDir string
6569 healthAddr string
70+ diagnosticsOptions = flags.DiagnosticsOptions {}
6671)
6772
6873func init () {
@@ -78,9 +83,6 @@ func init() {
7883
7984// InitFlags initializes the flags.
8085func InitFlags (fs * pflag.FlagSet ) {
81- fs .StringVar (& metricsBindAddr , "metrics-bind-addr" , "localhost:8080" ,
82- "The address the metric endpoint binds to." )
83-
8486 fs .BoolVar (& enableLeaderElection , "leader-elect" , false ,
8587 "Enable leader election for controller manager. Enabling this will ensure there is only one active controller manager." )
8688
@@ -96,9 +98,15 @@ func InitFlags(fs *pflag.FlagSet) {
9698 fs .StringVar (& watchFilterValue , "watch-filter" , "" ,
9799 fmt .Sprintf ("Label value that the controller watches to reconcile cluster-api objects. Label key is always %s. If unspecified, the controller watches for all cluster-api objects." , clusterv1 .WatchLabel ))
98100
101+ fs .StringVar (& watchNamespace , "namespace" , "" ,
102+ "Namespace that the controller watches to reconcile cluster-api objects. If unspecified, the controller watches for cluster-api objects across all namespaces." )
103+
99104 fs .StringVar (& profilerAddress , "profiler-address" , "" ,
100105 "Bind address to expose the pprof profiler (e.g. localhost:6060)" )
101106
107+ fs .BoolVar (& enableContentionProfiling , "contention-profiling" , false ,
108+ "Enable block profiling" )
109+
102110 fs .IntVar (& concurrencyNumber , "concurrency" , 1 ,
103111 "Number of core resources to process simultaneously" )
104112
@@ -112,6 +120,8 @@ func InitFlags(fs *pflag.FlagSet) {
112120
113121 fs .StringVar (& healthAddr , "health-addr" , ":9440" ,
114122 "The address the health endpoint binds to." )
123+
124+ flags .AddDiagnosticsOptions (fs , & diagnosticsOptions )
115125}
116126
117127func main () {
@@ -120,37 +130,52 @@ func main() {
120130 pflag .Parse ()
121131
122132 ctrl .SetLogger (klogr .New ())
133+ restConfig := ctrl .GetConfigOrDie ()
123134
124- if profilerAddress != "" {
125- klog .Infof ("Profiler listening for requests at %s" , profilerAddress )
135+ diagnosticsOpts := flags .GetDiagnosticsOptions (diagnosticsOptions )
126136
127- go func () {
128- server := & http.Server {
129- Addr : profilerAddress ,
130- ReadHeaderTimeout : 3 * time .Second ,
131- }
137+ var watchNamespaces map [string ]cache.Config
138+ if watchNamespace != "" {
139+ watchNamespaces = map [string ]cache.Config {
140+ watchNamespace : {},
141+ }
142+ }
132143
133- klog . Info ( server . ListenAndServe ())
134- }( )
144+ if enableContentionProfiling {
145+ goruntime . SetBlockProfileRate ( 1 )
135146 }
136147
137- mgr , err := ctrl .NewManager (ctrl .GetConfigOrDie (), ctrl.Options {
138- Scheme : scheme ,
139- MetricsBindAddress : metricsBindAddr ,
140- LeaderElection : enableLeaderElection ,
141- LeaderElectionID : "controller-leader-election-capi-operator" ,
142- LeaseDuration : & leaderElectionLeaseDuration ,
143- RenewDeadline : & leaderElectionRenewDeadline ,
144- RetryPeriod : & leaderElectionRetryPeriod ,
145- SyncPeriod : & syncPeriod ,
146- ClientDisableCacheFor : []client.Object {
147- & corev1.ConfigMap {},
148- & corev1.Secret {},
149- },
150- Port : webhookPort ,
151- CertDir : webhookCertDir ,
148+ ctrlOptions := ctrl.Options {
149+ Scheme : scheme ,
150+ LeaderElection : enableLeaderElection ,
151+ LeaderElectionID : "controller-leader-election-capi-operator" ,
152+ LeaseDuration : & leaderElectionLeaseDuration ,
153+ RenewDeadline : & leaderElectionRenewDeadline ,
154+ RetryPeriod : & leaderElectionRetryPeriod ,
152155 HealthProbeBindAddress : healthAddr ,
153- })
156+ PprofBindAddress : profilerAddress ,
157+ Metrics : diagnosticsOpts ,
158+ Cache : cache.Options {
159+ DefaultNamespaces : watchNamespaces ,
160+ SyncPeriod : & syncPeriod ,
161+ },
162+ Client : client.Options {
163+ Cache : & client.CacheOptions {
164+ DisableFor : []client.Object {
165+ & corev1.ConfigMap {},
166+ & corev1.Secret {},
167+ },
168+ },
169+ },
170+ WebhookServer : ctrlwebhook .NewServer (
171+ ctrlwebhook.Options {
172+ Port : webhookPort ,
173+ CertDir : webhookCertDir ,
174+ },
175+ ),
176+ }
177+
178+ mgr , err := ctrl .NewManager (restConfig , ctrlOptions )
154179 if err != nil {
155180 setupLog .Error (err , "unable to start manager" )
156181 os .Exit (1 )
0 commit comments