Skip to content

Commit d10725d

Browse files
committed
align flags with CAPI and introduce separate concurrency flags
1 parent 52e1547 commit d10725d

File tree

1 file changed

+90
-81
lines changed

1 file changed

+90
-81
lines changed

main.go

Lines changed: 90 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@ import (
2020
"errors"
2121
"flag"
2222
"fmt"
23-
"net/http"
24-
"net/http/pprof"
2523
"os"
2624
"reflect"
25+
goruntime "runtime"
2726
"time"
2827

2928
"github.com/spf13/pflag"
@@ -36,6 +35,7 @@ import (
3635
logsv1 "k8s.io/component-base/logs/api/v1"
3736
_ "k8s.io/component-base/logs/json/register"
3837
"k8s.io/klog/v2"
38+
"sigs.k8s.io/cluster-api/controllers/remote"
3939
"sigs.k8s.io/cluster-api/util/flags"
4040
ctrl "sigs.k8s.io/controller-runtime"
4141
ctrlmgr "sigs.k8s.io/controller-runtime/pkg/manager"
@@ -54,13 +54,21 @@ import (
5454
)
5555

5656
var (
57-
setupLog = ctrl.Log.WithName("entrypoint")
58-
logOptions = logs.NewOptions()
59-
60-
managerOpts manager.Options
61-
webhookOpts webhook.Options
62-
syncPeriod time.Duration
63-
profilerAddress string
57+
setupLog = ctrl.Log.WithName("entrypoint")
58+
logOptions = logs.NewOptions()
59+
controllerName = "cluster-api-vsphere-manager"
60+
61+
enableContentionProfiling bool
62+
leaderElectionLeaseDuration time.Duration
63+
leaderElectionRenewDeadline time.Duration
64+
leaderElectionRetryPeriod time.Duration
65+
managerOpts manager.Options
66+
profilerAddress string
67+
restConfigBurst int
68+
restConfigQPS float32
69+
syncPeriod time.Duration
70+
webhookOpts webhook.Options
71+
watchNamespace string
6472

6573
tlsOptions = flags.TLSOptions{}
6674

@@ -73,86 +81,99 @@ var (
7381
defaultKeepAliveDuration = constants.DefaultKeepAliveDuration
7482
)
7583

76-
var namespace string
77-
7884
// InitFlags initializes the flags.
7985
func InitFlags(fs *pflag.FlagSet) {
80-
logsv1.AddFlags(logOptions, fs)
86+
// Flags specific to CAPV
8187

82-
flag.StringVar(
83-
&managerOpts.MetricsBindAddress,
84-
"metrics-bind-addr",
85-
"localhost:8080",
86-
"The address the metric endpoint binds to.")
87-
flag.BoolVar(
88-
&managerOpts.LeaderElection,
89-
"leader-elect",
90-
true,
91-
"Enable leader election for controller manager. Enabling this will ensure there is only one active controller manager.")
92-
flag.StringVar(
88+
fs.StringVar(
9389
&managerOpts.LeaderElectionID,
9490
"leader-election-id",
9591
defaultLeaderElectionID,
9692
"Name of the config map to use as the locking resource when configuring leader election.")
97-
flag.StringVar(
98-
&namespace,
99-
"namespace",
100-
"",
101-
"Namespace that the controller watches to reconcile cluster-api objects. If unspecified, the controller watches for cluster-api objects across all namespaces.")
102-
flag.StringVar(
103-
&profilerAddress,
104-
"profiler-address",
105-
defaultProfilerAddr,
106-
"Bind address to expose the pprof profiler (e.g. localhost:6060)")
107-
flag.DurationVar(
108-
&syncPeriod,
109-
"sync-period",
110-
defaultSyncPeriod,
111-
"The interval at which cluster-api objects are synchronized")
112-
flag.IntVar(
93+
94+
fs.IntVar(
11395
&managerOpts.MaxConcurrentReconciles,
11496
"max-concurrent-reconciles",
11597
10,
11698
"The maximum number of allowed, concurrent reconciles.")
117-
flag.StringVar(
99+
100+
fs.StringVar(
118101
&managerOpts.PodName,
119102
"pod-name",
120103
defaultPodName,
121104
"The name of the pod running the controller manager.")
122-
flag.IntVar(
123-
&webhookOpts.Port,
124-
"webhook-port",
125-
defaultWebhookPort,
126-
"Webhook Server port (set to 0 to disable)")
127-
flag.StringVar(
128-
&managerOpts.HealthProbeBindAddress,
129-
"health-addr",
130-
":9440",
131-
"The address the health endpoint binds to.",
132-
)
133-
flag.StringVar(
105+
106+
fs.StringVar(
134107
&managerOpts.CredentialsFile,
135108
"credentials-file",
136109
"/etc/capv/credentials.yaml",
137110
"path to CAPV's credentials file",
138111
)
139-
flag.BoolVar(
112+
fs.BoolVar(
140113
&managerOpts.EnableKeepAlive,
141114
"enable-keep-alive",
142115
defaultEnableKeepAlive,
143116
"feature to enable keep alive handler in vsphere sessions. This functionality is enabled by default.")
144-
flag.DurationVar(
117+
fs.DurationVar(
145118
&managerOpts.KeepAliveDuration,
146119
"keep-alive-duration",
147120
defaultKeepAliveDuration,
148121
"idle time interval(minutes) in between send() requests in keepalive handler",
149122
)
150-
flag.StringVar(
123+
fs.StringVar(
151124
&managerOpts.NetworkProvider,
152125
"network-provider",
153126
"",
154127
"network provider to be used by Supervisor based clusters.",
155128
)
129+
130+
// Flags common between CAPI and CAPV
131+
132+
logsv1.AddFlags(logOptions, fs)
133+
134+
fs.StringVar(&managerOpts.MetricsBindAddress, "metrics-bind-addr", "localhost:8080",
135+
"The address the metric endpoint binds to.")
136+
137+
fs.BoolVar(&managerOpts.LeaderElection, "leader-elect", true,
138+
"Enable leader election for controller manager. Enabling this will ensure there is only one active controller manager.")
139+
140+
fs.DurationVar(&leaderElectionLeaseDuration, "leader-elect-lease-duration", 15*time.Second,
141+
"Interval at which non-leader candidates will wait to force acquire leadership (duration string)")
142+
143+
fs.DurationVar(&leaderElectionRenewDeadline, "leader-elect-renew-deadline", 10*time.Second,
144+
"Duration that the leading controller manager will retry refreshing leadership before giving up (duration string)")
145+
146+
fs.DurationVar(&leaderElectionRetryPeriod, "leader-elect-retry-period", 2*time.Second,
147+
"Duration the LeaderElector clients should wait between tries of actions (duration string)")
148+
149+
fs.StringVar(&watchNamespace, "namespace", "",
150+
"Namespace that the controller watches to reconcile cluster-api objects. If unspecified, the controller watches for cluster-api objects across all namespaces.")
151+
152+
fs.StringVar(&managerOpts.PprofBindAddress, "profiler-address", defaultProfilerAddr,
153+
"Bind address to expose the pprof profiler (e.g. localhost:6060)")
154+
155+
fs.BoolVar(&enableContentionProfiling, "contention-profiling", false,
156+
"Enable block profiling, if profiler-address is set.")
157+
158+
fs.DurationVar(&syncPeriod, "sync-period", defaultSyncPeriod,
159+
"The minimum interval at which watched resources are reconciled (e.g. 15m)")
160+
161+
fs.Float32Var(&restConfigQPS, "kube-api-qps", 20,
162+
"Maximum queries per second from the controller client to the Kubernetes API server. Defaults to 20")
163+
164+
fs.IntVar(&restConfigBurst, "kube-api-burst", 30,
165+
"Maximum number of queries that should be allowed in one burst from the controller client to the Kubernetes API server. Default 30")
166+
167+
fs.IntVar(&webhookOpts.Port, "webhook-port", defaultWebhookPort,
168+
"Webhook Server port")
169+
170+
fs.StringVar(&webhookOpts.CertDir, "webhook-cert-dir", "/tmp/k8s-webhook-server/serving-certs/",
171+
"Webhook cert dir, only used when webhook-port is specified.")
172+
173+
fs.StringVar(&managerOpts.HealthProbeBindAddress, "health-addr", ":9440",
174+
"The address the health endpoint binds to.",
175+
)
176+
156177
flags.AddTLSOptions(fs, &tlsOptions)
157178

158179
feature.MutableGates.AddFlag(fs)
@@ -176,22 +197,28 @@ func main() {
176197
// klog.Background will automatically use the right logger.
177198
ctrl.SetLogger(klog.Background())
178199

179-
if namespace != "" {
180-
managerOpts.Cache.Namespaces = []string{namespace}
200+
managerOpts.KubeConfig = ctrl.GetConfigOrDie()
201+
managerOpts.KubeConfig.QPS = restConfigQPS
202+
managerOpts.KubeConfig.Burst = restConfigBurst
203+
managerOpts.KubeConfig.UserAgent = remote.DefaultClusterAPIUserAgent(controllerName)
204+
205+
if watchNamespace != "" {
206+
managerOpts.Cache.Namespaces = []string{watchNamespace}
181207
setupLog.Info(
182208
"Watching objects only in namespace for reconciliation",
183-
"namespace", namespace)
209+
"namespace", watchNamespace)
184210
}
185211

186-
if profilerAddress != "" {
187-
setupLog.Info(
188-
"Profiler listening for requests",
189-
"profiler-address", profilerAddress)
190-
go runProfiler(profilerAddress)
212+
if profilerAddress != "" && enableContentionProfiling {
213+
goruntime.SetBlockProfileRate(1)
191214
}
215+
192216
setupLog.V(1).Info(fmt.Sprintf("feature gates: %+v\n", feature.Gates))
193217

194218
managerOpts.Cache.SyncPeriod = &syncPeriod
219+
managerOpts.LeaseDuration = &leaderElectionLeaseDuration
220+
managerOpts.RenewDeadline = &leaderElectionRenewDeadline
221+
managerOpts.RetryPeriod = &leaderElectionRetryPeriod
195222

196223
// Create a function that adds all the controllers and webhooks to the manager.
197224
addToManager := func(ctx *context.ControllerManagerContext, mgr ctrlmgr.Manager) error {
@@ -332,24 +359,6 @@ func setupChecks(mgr ctrlmgr.Manager) {
332359
}
333360
}
334361

335-
func runProfiler(addr string) {
336-
mux := http.NewServeMux()
337-
mux.HandleFunc("/debug/pprof/", pprof.Index)
338-
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
339-
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
340-
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
341-
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
342-
343-
srv := http.Server{
344-
Addr: addr,
345-
Handler: mux,
346-
ReadHeaderTimeout: 2 * time.Second,
347-
}
348-
if err := srv.ListenAndServe(); err != nil {
349-
setupLog.Error(err, "problem running profiler server")
350-
}
351-
}
352-
353362
func isCRDDeployed(mgr ctrlmgr.Manager, gvr schema.GroupVersionResource) (bool, error) {
354363
_, err := mgr.GetRESTMapper().KindFor(gvr)
355364
if err != nil {

0 commit comments

Comments
 (0)