@@ -19,8 +19,8 @@ package main
19
19
import (
20
20
"flag"
21
21
"fmt"
22
- "net/http"
23
22
"os"
23
+ goruntime "runtime"
24
24
"time"
25
25
26
26
"github.com/spf13/pflag"
@@ -34,6 +34,7 @@ import (
34
34
"sigs.k8s.io/cluster-api-operator/internal/webhook"
35
35
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
36
36
clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"
37
+ "sigs.k8s.io/cluster-api/util/flags"
37
38
"sigs.k8s.io/cluster-api/version"
38
39
ctrl "sigs.k8s.io/controller-runtime"
39
40
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -51,18 +52,20 @@ var (
51
52
setupLog = ctrl .Log .WithName ("setup" )
52
53
53
54
// flags.
54
- metricsBindAddr string
55
55
enableLeaderElection bool
56
56
leaderElectionLeaseDuration time.Duration
57
57
leaderElectionRenewDeadline time.Duration
58
58
leaderElectionRetryPeriod time.Duration
59
59
watchFilterValue string
60
+ watchNamespace string
60
61
profilerAddress string
62
+ enableContentionProfiling bool
61
63
concurrencyNumber int
62
64
syncPeriod time.Duration
63
65
webhookPort int
64
66
webhookCertDir string
65
67
healthAddr string
68
+ diagnosticsOptions = flags.DiagnosticsOptions {}
66
69
)
67
70
68
71
func init () {
@@ -78,9 +81,6 @@ func init() {
78
81
79
82
// InitFlags initializes the flags.
80
83
func InitFlags (fs * pflag.FlagSet ) {
81
- fs .StringVar (& metricsBindAddr , "metrics-bind-addr" , ":8080" ,
82
- "The address the metric endpoint binds to." )
83
-
84
84
fs .BoolVar (& enableLeaderElection , "leader-elect" , false ,
85
85
"Enable leader election for controller manager. Enabling this will ensure there is only one active controller manager." )
86
86
@@ -96,9 +96,15 @@ func InitFlags(fs *pflag.FlagSet) {
96
96
fs .StringVar (& watchFilterValue , "watch-filter" , "" ,
97
97
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 ))
98
98
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
+
99
102
fs .StringVar (& profilerAddress , "profiler-address" , "" ,
100
103
"Bind address to expose the pprof profiler (e.g. localhost:6060)" )
101
104
105
+ fs .BoolVar (& enableContentionProfiling , "contention-profiling" , false ,
106
+ "Enable block profiling" )
107
+
102
108
fs .IntVar (& concurrencyNumber , "concurrency" , 1 ,
103
109
"Number of core resources to process simultaneously" )
104
110
@@ -112,45 +118,49 @@ func InitFlags(fs *pflag.FlagSet) {
112
118
113
119
fs .StringVar (& healthAddr , "health-addr" , ":9440" ,
114
120
"The address the health endpoint binds to." )
121
+
122
+ flags .AddDiagnosticsOptions (fs , & diagnosticsOptions )
115
123
}
116
124
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
+
117
129
func main () {
118
130
InitFlags (pflag .CommandLine )
119
131
pflag .CommandLine .AddGoFlagSet (flag .CommandLine )
120
132
pflag .Parse ()
121
133
122
134
ctrl .SetLogger (klogr .New ())
135
+ restConfig := ctrl .GetConfigOrDie ()
123
136
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 )
132
138
133
- klog . Info ( server . ListenAndServe ())
134
- }( )
139
+ if enableContentionProfiling {
140
+ goruntime . SetBlockProfileRate ( 1 )
135
141
}
136
142
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
+ },
149
157
},
150
- Port : webhookPort ,
151
- CertDir : webhookCertDir ,
152
158
HealthProbeBindAddress : healthAddr ,
153
- })
159
+ PprofBindAddress : profilerAddress ,
160
+ Metrics : diagnosticsOpts ,
161
+ }
162
+
163
+ mgr , err := ctrl .NewManager (restConfig , ctrlOptions )
154
164
if err != nil {
155
165
setupLog .Error (err , "unable to start manager" )
156
166
os .Exit (1 )
0 commit comments