@@ -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,6 +34,7 @@ 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"
3940 "sigs.k8s.io/controller-runtime/pkg/client"
@@ -50,18 +51,20 @@ var (
5051 setupLog = ctrl .Log .WithName ("setup" )
5152
5253 // flags.
53- metricsBindAddr string
5454 enableLeaderElection bool
5555 leaderElectionLeaseDuration time.Duration
5656 leaderElectionRenewDeadline time.Duration
5757 leaderElectionRetryPeriod time.Duration
5858 watchFilterValue string
59+ watchNamespace string
5960 profilerAddress string
61+ enableContentionProfiling bool
6062 concurrencyNumber int
6163 syncPeriod time.Duration
6264 webhookPort int
6365 webhookCertDir string
6466 healthAddr string
67+ diagnosticsOptions = flags.DiagnosticsOptions {}
6568)
6669
6770func init () {
@@ -77,8 +80,6 @@ func init() {
7780
7881// InitFlags initializes the flags.
7982func InitFlags (fs * pflag.FlagSet ) {
80- fs .StringVar (& metricsBindAddr , "metrics-bind-addr" , ":8080" ,
81- "The address the metric endpoint binds to." )
8283
8384 fs .BoolVar (& enableLeaderElection , "leader-elect" , false ,
8485 "Enable leader election for controller manager. Enabling this will ensure there is only one active controller manager." )
@@ -95,9 +96,15 @@ func InitFlags(fs *pflag.FlagSet) {
9596 fs .StringVar (& watchFilterValue , "watch-filter" , "" ,
9697 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 ))
9798
99+ fs .StringVar (& watchNamespace , "namespace" , "" ,
100+ "Namespace that the controller watches to reconcile cluster-api objects. If unspecified, the controller watches for cluster-api objects across all namespaces." )
101+
98102 fs .StringVar (& profilerAddress , "profiler-address" , "" ,
99103 "Bind address to expose the pprof profiler (e.g. localhost:6060)" )
100104
105+ fs .BoolVar (& enableContentionProfiling , "contention-profiling" , false ,
106+ "Enable block profiling" )
107+
101108 fs .IntVar (& concurrencyNumber , "concurrency" , 1 ,
102109 "Number of core resources to process simultaneously" )
103110
@@ -111,45 +118,102 @@ func InitFlags(fs *pflag.FlagSet) {
111118
112119 fs .StringVar (& healthAddr , "health-addr" , ":9440" ,
113120 "The address the health endpoint binds to." )
121+
122+ flags .AddDiagnosticsOptions (fs , & diagnosticsOptions )
114123}
115124
125+ // Add RBAC for the authorized diagnostics endpoint.
126+ // +kubebuilder:rbac:groups=authentication.k8s.io,resources=tokenreviews,verbs=create
127+ // +kubebuilder:rbac:groups=authorization.k8s.io,resources=subjectaccessreviews,verbs=create
128+
116129func main () {
117130 InitFlags (pflag .CommandLine )
118131 pflag .CommandLine .AddGoFlagSet (flag .CommandLine )
119132 pflag .Parse ()
120133
121134 ctrl .SetLogger (klogr .New ())
135+ restConfig := ctrl .GetConfigOrDie ()
122136
123- if profilerAddress != "" {
124- klog .Infof ("Profiler listening for requests at %s" , profilerAddress )
137+ diagnosticsOpts := flags .GetDiagnosticsOptions (diagnosticsOptions )
125138
126- go func () {
127- server := & http.Server {
128- Addr : profilerAddress ,
129- ReadHeaderTimeout : 3 * time .Second ,
130- }
139+ // var watchNamespaces map[string]cache.Config
140+ // if watchNamespace != "" {
141+ // watchNamespaces = map[string]cache.Config{
142+ // watchNamespace: {},
143+ // }
144+ // }
131145
132- klog . Info ( server . ListenAndServe ())
133- }( )
146+ if enableContentionProfiling {
147+ goruntime . SetBlockProfileRate ( 1 )
134148 }
135149
136- mgr , err := ctrl .NewManager (ctrl .GetConfigOrDie (), ctrl.Options {
137- Scheme : scheme ,
138- MetricsBindAddress : metricsBindAddr ,
139- LeaderElection : enableLeaderElection ,
140- LeaderElectionID : "controller-leader-election-capi-operator" ,
141- LeaseDuration : & leaderElectionLeaseDuration ,
142- RenewDeadline : & leaderElectionRenewDeadline ,
143- RetryPeriod : & leaderElectionRetryPeriod ,
144- SyncPeriod : & syncPeriod ,
145- ClientDisableCacheFor : []client.Object {
146- & corev1.ConfigMap {},
147- & corev1.Secret {},
150+ // req, _ := labels.NewRequirement(clusterv1.ClusterNameLabel, selection.Exists, nil)
151+ // clusterSecretCacheSelector := labels.NewSelector().Add(*req)
152+
153+ ctrlOptions := ctrl.Options {
154+ Scheme : scheme ,
155+ LeaderElection : enableLeaderElection ,
156+ LeaderElectionID : "controller-leader-election-capi-operator" ,
157+ LeaseDuration : & leaderElectionLeaseDuration ,
158+ RenewDeadline : & leaderElectionRenewDeadline ,
159+ RetryPeriod : & leaderElectionRetryPeriod ,
160+ Client : client.Options {
161+ Cache : & client.CacheOptions {
162+ DisableFor : []client.Object {
163+ & corev1.ConfigMap {},
164+ & corev1.Secret {},
165+ },
166+ },
148167 },
149- Port : webhookPort ,
150- CertDir : webhookCertDir ,
168+ // WebhookServer: webhook.NewServer(
169+ // webhook.Options{
170+ // Port: webhookPort,
171+ // CertDir: webhookCertDir,
172+ // },
173+ // ),
151174 HealthProbeBindAddress : healthAddr ,
152- })
175+ PprofBindAddress : profilerAddress ,
176+ Metrics : diagnosticsOpts ,
177+ // Cache: cache.Options{
178+ // DefaultNamespaces: watchNamespaces,
179+ // SyncPeriod: &syncPeriod,
180+ // ByObject: map[client.Object]cache.ByObject{
181+ // // Note: Only Secrets with the cluster name label are cached.
182+ // // The default client of the manager won't use the cache for secrets at all (see Client.Cache.DisableFor).
183+ // // The cached secrets will only be used by the secretCachingClient we create below.
184+ // &corev1.Secret{}: {
185+ // Label: clusterSecretCacheSelector,
186+ // },
187+ // },
188+ // },
189+ }
190+
191+ // mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
192+ // Scheme: scheme,
193+ // MetricsBindAddress: metricsBindAddr,
194+ // LeaderElection: enableLeaderElection,
195+ // LeaderElectionID: "controller-leader-election-capi-operator",
196+ // LeaseDuration: &leaderElectionLeaseDuration,
197+ // RenewDeadline: &leaderElectionRenewDeadline,
198+ // RetryPeriod: &leaderElectionRetryPeriod,
199+ // Client: client.Options{
200+ // Cache: &client.CacheOptions{
201+ // DisableFor: []client.Object{
202+ // &corev1.ConfigMap{},
203+ // &corev1.Secret{},
204+ // },
205+ // },
206+ // },
207+ // WebhookServer: webhook.NewServer(
208+ // webhook.Options{
209+ // Port: webhookPort,
210+ // CertDir: webhookCertDir,
211+ // },
212+ // ),
213+ // HealthProbeBindAddress: healthAddr,
214+ // })
215+
216+ mgr , err := ctrl .NewManager (restConfig , ctrlOptions )
153217 if err != nil {
154218 setupLog .Error (err , "unable to start manager" )
155219 os .Exit (1 )
0 commit comments