@@ -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"
@@ -51,18 +52,20 @@ var (
5152 setupLog = ctrl .Log .WithName ("setup" )
5253
5354 // flags.
54- metricsBindAddr string
5555 enableLeaderElection bool
5656 leaderElectionLeaseDuration time.Duration
5757 leaderElectionRenewDeadline time.Duration
5858 leaderElectionRetryPeriod time.Duration
5959 watchFilterValue string
60+ watchNamespace string
6061 profilerAddress string
62+ enableContentionProfiling bool
6163 concurrencyNumber int
6264 syncPeriod time.Duration
6365 webhookPort int
6466 webhookCertDir string
6567 healthAddr string
68+ diagnosticsOptions = flags.DiagnosticsOptions {}
6669)
6770
6871func init () {
@@ -78,9 +81,6 @@ func init() {
7881
7982// InitFlags initializes the flags.
8083func InitFlags (fs * pflag.FlagSet ) {
81- fs .StringVar (& metricsBindAddr , "metrics-bind-addr" , ":8080" ,
82- "The address the metric endpoint binds to." )
83-
8484 fs .BoolVar (& enableLeaderElection , "leader-elect" , false ,
8585 "Enable leader election for controller manager. Enabling this will ensure there is only one active controller manager." )
8686
@@ -96,9 +96,15 @@ func InitFlags(fs *pflag.FlagSet) {
9696 fs .StringVar (& watchFilterValue , "watch-filter" , "" ,
9797 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 ))
9898
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+
99102 fs .StringVar (& profilerAddress , "profiler-address" , "" ,
100103 "Bind address to expose the pprof profiler (e.g. localhost:6060)" )
101104
105+ fs .BoolVar (& enableContentionProfiling , "contention-profiling" , false ,
106+ "Enable block profiling" )
107+
102108 fs .IntVar (& concurrencyNumber , "concurrency" , 1 ,
103109 "Number of core resources to process simultaneously" )
104110
@@ -112,45 +118,49 @@ func InitFlags(fs *pflag.FlagSet) {
112118
113119 fs .StringVar (& healthAddr , "health-addr" , ":9440" ,
114120 "The address the health endpoint binds to." )
121+
122+ flags .AddDiagnosticsOptions (fs , & diagnosticsOptions )
115123}
116124
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+
117129func main () {
118130 InitFlags (pflag .CommandLine )
119131 pflag .CommandLine .AddGoFlagSet (flag .CommandLine )
120132 pflag .Parse ()
121133
122134 ctrl .SetLogger (klogr .New ())
135+ restConfig := ctrl .GetConfigOrDie ()
123136
124- if profilerAddress != "" {
125- klog .Infof ("Profiler listening for requests at %s" , profilerAddress )
126-
127- go func () {
128- server := & http.Server {
129- Addr : profilerAddress ,
130- ReadHeaderTimeout : 3 * time .Second ,
131- }
137+ diagnosticsOpts := flags .GetDiagnosticsOptions (diagnosticsOptions )
132138
133- klog . Info ( server . ListenAndServe ())
134- }( )
139+ if enableContentionProfiling {
140+ goruntime . SetBlockProfileRate ( 1 )
135141 }
136142
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 {},
143+ ctrlOptions := ctrl.Options {
144+ Scheme : scheme ,
145+ LeaderElection : enableLeaderElection ,
146+ LeaderElectionID : "controller-leader-election-capi-operator" ,
147+ LeaseDuration : & leaderElectionLeaseDuration ,
148+ RenewDeadline : & leaderElectionRenewDeadline ,
149+ RetryPeriod : & leaderElectionRetryPeriod ,
150+ Client : client.Options {
151+ Cache : & client.CacheOptions {
152+ DisableFor : []client.Object {
153+ & corev1.ConfigMap {},
154+ & corev1.Secret {},
155+ },
156+ },
149157 },
150- Port : webhookPort ,
151- CertDir : webhookCertDir ,
152158 HealthProbeBindAddress : healthAddr ,
153- })
159+ PprofBindAddress : profilerAddress ,
160+ Metrics : diagnosticsOpts ,
161+ }
162+
163+ mgr , err := ctrl .NewManager (restConfig , ctrlOptions )
154164 if err != nil {
155165 setupLog .Error (err , "unable to start manager" )
156166 os .Exit (1 )
0 commit comments