@@ -19,7 +19,6 @@ package main
1919import (
2020 "crypto/tls"
2121 "errors"
22- "flag"
2322 "os"
2423 "path/filepath"
2524 "strings"
@@ -64,53 +63,15 @@ func init() {
6463 // +kubebuilder:scaffold:scheme
6564}
6665
67- // nolint:gocyclo
6866func main () {
69- var metricsAddr string
70- var metricsCertPath , metricsCertName , metricsCertKey string
71- var webhookCertPath , webhookCertName , webhookCertKey string
72- var enableLeaderElection bool
73- var probeAddr string
74- var secureMetrics bool
75- var enableHTTP2 bool
76- var watchNamespaces string
7767 var logPath string
78- var disabledControllers string
7968 var tlsOpts []func (* tls.Config )
8069
81- flag .StringVar (& watchNamespaces , "watch-namespaces" , "" ,
82- "Optional comma separated list of namespaces to watch for resources in. Defaults to cluster scope." )
83- flag .StringVar (& metricsAddr , "metrics-bind-address" , "0" ,
84- "The address the metrics endpoint binds to. Use :8443 for HTTPS or :8080 for HTTP, or leave " +
85- "as 0 to disable the metrics service." )
86- flag .BoolVar (& enableLeaderElection , "leader-elect" , false ,
87- "Enable leader election for controller manager. " +
88- "Enabling this will ensure there is only one active controller manager." )
89- flag .BoolVar (& secureMetrics , "metrics-secure" , true ,
90- "If set, the metrics endpoint is served securely via HTTPS. Use --metrics-secure=false to use HTTP instead." )
91- flag .StringVar (& webhookCertPath , "webhook-cert-path" , "" ,
92- "The directory that contains the webhook certificate." )
93- flag .StringVar (& webhookCertName , "webhook-cert-name" , "tls.crt" ,
94- "The name of the webhook certificate file." )
95- flag .StringVar (& webhookCertKey , "webhook-cert-key" , "tls.key" , "The name of the webhook key file." )
96- flag .StringVar (& metricsCertPath , "metrics-cert-path" , "" ,
97- "The directory that contains the metrics server certificate." )
98- flag .StringVar (& metricsCertName , "metrics-cert-name" , "tls.crt" ,
99- "The name of the metrics server certificate file." )
100- flag .StringVar (& metricsCertKey , "metrics-cert-key" , "tls.key" , "The name of the metrics server key file." )
101- flag .BoolVar (& enableHTTP2 , "enable-http2" , false ,
102- "If set, HTTP/2 will be enabled for the metrics and webhook servers" )
103- flag .StringVar (& probeAddr , "health-probe-bind-address" , ":8081" , "The address the probe endpoint binds to." )
104- flag .StringVar (& disabledControllers , "disable-component-controllers" , "" ,
105- "Optional argument that accepts two values: fluent-bit and fluentd. " +
106- "The specific controller will not be started if it's disabled." )
107- opts := zap.Options {
108- Development : true ,
109- }
110- opts .BindFlags (flag .CommandLine )
111- flag .Parse ()
70+ zapOpts := & zap.Options {Development : true }
71+ opts := NewOptions (zapOpts )
72+ opts .ParseFlags ()
11273
113- ctrl .SetLogger (zap .New (zap .UseFlagOptions (& opts )))
74+ ctrl .SetLogger (zap .New (zap .UseFlagOptions (zapOpts )))
11475
11576 // if the enable-http2 flag is false (the default), http/2 should be disabled
11677 // due to its vulnerabilities. More specifically, disabling http/2 will
@@ -123,7 +84,7 @@ func main() {
12384 c .NextProtos = []string {"http/1.1" }
12485 }
12586
126- if ! enableHTTP2 {
87+ if ! opts . EnableHTTP2 {
12788 tlsOpts = append (tlsOpts , disableHTTP2 )
12889 }
12990
@@ -132,14 +93,18 @@ func main() {
13293
13394 // Initial webhook TLS options
13495 webhookTLSOpts := tlsOpts
135- if len (webhookCertPath ) > 0 {
136- setupLog .Info ("Initializing webhook certificate watcher using provided certificates" ,
137- "webhook-cert-path" , webhookCertPath , "webhook-cert-name" , webhookCertName , "webhook-cert-key" , webhookCertKey )
96+ if len (opts .WebhookCertPath ) > 0 {
97+ setupLog .Info (
98+ "Initializing webhook certificate watcher using provided certificates" ,
99+ "webhook-cert-path" , opts .WebhookCertPath ,
100+ "webhook-cert-name" , opts .WebhookCertName ,
101+ "webhook-cert-key" , opts .WebhookCertKey ,
102+ )
138103
139104 var err error
140105 webhookCertWatcher , err = certwatcher .New (
141- filepath .Join (webhookCertPath , webhookCertName ),
142- filepath .Join (webhookCertPath , webhookCertKey ),
106+ filepath .Join (opts . WebhookCertPath , opts . WebhookCertName ),
107+ filepath .Join (opts . WebhookCertPath , opts . WebhookCertKey ),
143108 )
144109 if err != nil {
145110 setupLog .Error (err , "Failed to initialize webhook certificate watcher" )
@@ -160,12 +125,12 @@ func main() {
160125 // - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.20.4/pkg/metrics/server
161126 // - https://book.kubebuilder.io/reference/metrics.html
162127 metricsServerOptions := metricsserver.Options {
163- BindAddress : metricsAddr ,
164- SecureServing : secureMetrics ,
128+ BindAddress : opts . MetricsAddr ,
129+ SecureServing : opts . SecureMetrics ,
165130 TLSOpts : tlsOpts ,
166131 }
167132
168- if secureMetrics {
133+ if opts . SecureMetrics {
169134 // FilterProvider is used to protect the metrics endpoint with authn/authz.
170135 // These configurations ensure that only authorized users and service accounts
171136 // can access the metrics endpoint. The RBAC are configured in 'config/rbac/kustomization.yaml'. More info:
@@ -180,14 +145,18 @@ func main() {
180145 // - [METRICS-WITH-CERTS] at config/default/kustomization.yaml to generate and use certificates
181146 // managed by cert-manager for the metrics server.
182147 // - [PROMETHEUS-WITH-CERTS] at config/prometheus/kustomization.yaml for TLS certification.
183- if len (metricsCertPath ) > 0 {
184- setupLog .Info ("Initializing metrics certificate watcher using provided certificates" ,
185- "metrics-cert-path" , metricsCertPath , "metrics-cert-name" , metricsCertName , "metrics-cert-key" , metricsCertKey )
148+ if len (opts .MetricsCertPath ) > 0 {
149+ setupLog .Info (
150+ "Initializing metrics certificate watcher using provided certificates" ,
151+ "metrics-cert-path" , opts .MetricsCertPath ,
152+ "metrics-cert-name" , opts .MetricsCertName ,
153+ "metrics-cert-key" , opts .MetricsCertKey ,
154+ )
186155
187156 var err error
188157 metricsCertWatcher , err = certwatcher .New (
189- filepath .Join (metricsCertPath , metricsCertName ),
190- filepath .Join (metricsCertPath , metricsCertKey ),
158+ filepath .Join (opts . MetricsCertPath , opts . MetricsCertName ),
159+ filepath .Join (opts . MetricsCertPath , opts . MetricsCertKey ),
191160 )
192161 if err != nil {
193162 setupLog .Error (err , "to initialize metrics certificate watcher" , "error" , err )
@@ -203,8 +172,8 @@ func main() {
203172 Scheme : scheme ,
204173 Metrics : metricsServerOptions ,
205174 WebhookServer : webhookServer ,
206- HealthProbeBindAddress : probeAddr ,
207- LeaderElection : enableLeaderElection ,
175+ HealthProbeBindAddress : opts . ProbeAddr ,
176+ LeaderElection : opts . EnableLeaderElection ,
208177 LeaderElectionID : "45c4fdd2.fluent.io" ,
209178 // LeaderElectionReleaseOnCancel defines if the leader should step down voluntarily
210179 // when the Manager ends. This requires the binary to immediately end when the
@@ -220,12 +189,12 @@ func main() {
220189 }
221190
222191 namespacedController := false
223- if watchNamespaces != "" {
192+ if opts . WatchNamespaces != "" {
224193 config := cache.Config {}
225194 namespacedController = true
226195
227196 ctrlOpts .Cache .DefaultNamespaces = make (map [string ]cache.Config )
228- for namespace := range strings .SplitSeq (watchNamespaces , "," ) {
197+ for namespace := range strings .SplitSeq (opts . WatchNamespaces , "," ) {
229198 ctrlOpts .Cache .DefaultNamespaces [namespace ] = config
230199 }
231200 }
@@ -239,8 +208,8 @@ func main() {
239208 }
240209
241210 fluentBitEnabled , fluentdEnabled := true , true
242- if disabledControllers != "" {
243- switch disabledControllers {
211+ if opts . DisabledControllers != "" {
212+ switch opts . DisabledControllers {
244213 case fluentBitName :
245214 fluentBitEnabled = false
246215 case fluentdName :
0 commit comments