Skip to content

Commit b4b2ead

Browse files
committed
fluent-manager: Refactor CLI options parsing to separate object
Signed-off-by: Marco Franssen <marco.franssen@gmail.com>
1 parent 648fe77 commit b4b2ead

File tree

2 files changed

+93
-63
lines changed

2 files changed

+93
-63
lines changed

cmd/fluent-manager/main.go

Lines changed: 32 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package main
1919
import (
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
6866
func 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:

cmd/fluent-manager/options.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
6+
"sigs.k8s.io/controller-runtime/pkg/log/zap"
7+
)
8+
9+
type Options struct {
10+
WatchNamespaces string
11+
MetricsAddr string
12+
EnableLeaderElection bool
13+
SecureMetrics bool
14+
WebhookCertPath string
15+
WebhookCertName string
16+
WebhookCertKey string
17+
MetricsCertPath string
18+
MetricsCertName string
19+
MetricsCertKey string
20+
EnableHTTP2 bool
21+
ProbeAddr string
22+
DisabledControllers string
23+
}
24+
25+
func NewOptions(zapOpts *zap.Options) *Options {
26+
opts := new(Options)
27+
flag.StringVar(&opts.WatchNamespaces, "watch-namespaces", "",
28+
"Optional comma separated list of namespaces to watch for resources in. Defaults to cluster scope.")
29+
flag.StringVar(&opts.MetricsAddr, "metrics-bind-address", "0",
30+
"The address the metrics endpoint binds to. Use :8443 for HTTPS or :8080 for HTTP, or leave "+
31+
"as 0 to disable the metrics service.")
32+
flag.BoolVar(&opts.EnableLeaderElection, "leader-elect", false,
33+
"Enable leader election for controller manager. "+
34+
"Enabling this will ensure there is only one active controller manager.")
35+
flag.BoolVar(&opts.SecureMetrics, "metrics-secure", true,
36+
"If set, the metrics endpoint is served securely via HTTPS. Use --metrics-secure=false to use HTTP instead.")
37+
flag.StringVar(&opts.WebhookCertPath, "webhook-cert-path", "",
38+
"The directory that contains the webhook certificate.")
39+
flag.StringVar(&opts.WebhookCertName, "webhook-cert-name", "tls.crt",
40+
"The name of the webhook certificate file.")
41+
flag.StringVar(&opts.WebhookCertKey, "webhook-cert-key", "tls.key", "The name of the webhook key file.")
42+
flag.StringVar(&opts.MetricsCertPath, "metrics-cert-path", "",
43+
"The directory that contains the metrics server certificate.")
44+
flag.StringVar(&opts.MetricsCertName, "metrics-cert-name", "tls.crt",
45+
"The name of the metrics server certificate file.")
46+
flag.StringVar(&opts.MetricsCertKey, "metrics-cert-key", "tls.key", "The name of the metrics server key file.")
47+
flag.BoolVar(&opts.EnableHTTP2, "enable-http2", false,
48+
"If set, HTTP/2 will be enabled for the metrics and webhook servers")
49+
flag.StringVar(&opts.ProbeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
50+
flag.StringVar(&opts.DisabledControllers, "disable-component-controllers", "",
51+
"Optional argument that accepts two values: fluent-bit and fluentd. "+
52+
"The specific controller will not be started if it's disabled.")
53+
54+
zapOpts.BindFlags(flag.CommandLine)
55+
56+
return opts
57+
}
58+
59+
func (o *Options) ParseFlags() {
60+
flag.Parse()
61+
}

0 commit comments

Comments
 (0)