Skip to content

Commit d8835b2

Browse files
authored
fix: support CAPI manager flags (#1215)
Adds support of the modern CAPI manager flags to align some of them with the new CAPI operator version (v0.24.0). Deprecates the following flags: - metrics-bind-address: interchanged with --diagnostics-address - metrics-secure: interchanged with --insecure-diagnostics - enable-http2: not needed anymore Fixes #1214 Signed-off-by: Michael Morgen <[email protected]>
1 parent 9ba014a commit d8835b2

File tree

5 files changed

+79
-31
lines changed

5 files changed

+79
-31
lines changed

cmd/main.go

Lines changed: 62 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525

2626
"k8s.io/client-go/discovery"
2727

28+
"github.com/spf13/pflag"
2829
corev1 "k8s.io/api/core/v1"
2930
"k8s.io/apimachinery/pkg/labels"
3031
"k8s.io/apimachinery/pkg/runtime"
@@ -35,6 +36,7 @@ import (
3536
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
3637
_ "k8s.io/client-go/plugin/pkg/client/auth"
3738
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
39+
"sigs.k8s.io/cluster-api/util/flags"
3840
ctrl "sigs.k8s.io/controller-runtime"
3941
"sigs.k8s.io/controller-runtime/pkg/cache"
4042
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -62,6 +64,7 @@ var (
6264
controlPlaneController: true,
6365
infrastructureController: true,
6466
}
67+
managerOptions = flags.ManagerOptions{}
6568
)
6669

6770
const (
@@ -86,29 +89,32 @@ func init() {
8689
}
8790

8891
func main() {
89-
var metricsAddr string
92+
var metricsAddr string // deprecated, use capi's diagnostics-address instead
9093
var enableLeaderElection bool
91-
var secureMetrics bool
92-
var enableHTTP2 bool
94+
var secureMetrics bool // deprecated, use capi's insecure-diagnostics instead
95+
var enableHTTP2 bool // deprecated
9396
var probeAddr string
9497
var enabledController string
95-
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8443", "The address the metric endpoint binds to. "+
98+
99+
pflag.CommandLine.StringVar(&metricsAddr, "metrics-bind-address", ":8443", "[Deprecated, use --diagnostics-address instead] The address the metric endpoint binds to. "+
96100
"Use :8080 for http and :8443 for https. Setting to 0 will disable the metrics endpoint.")
97-
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
98-
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
101+
pflag.CommandLine.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
102+
pflag.CommandLine.BoolVar(&enableLeaderElection, "leader-elect", false,
99103
"Enable leader election for controller manager. "+
100104
"Enabling this will ensure there is only one active controller manager.")
101-
flag.BoolVar(&secureMetrics, "metrics-secure", true,
102-
"If set, the metrics endpoint is served securely via HTTPS. Use --metrics-secure=false to use HTTP instead.")
103-
flag.BoolVar(&enableHTTP2, "enable-http2", false,
104-
"If set, HTTP/2 will be enabled for the metrics and webhook servers")
105+
pflag.CommandLine.BoolVar(&secureMetrics, "metrics-secure", true,
106+
"[Deprecated, use --insecure-diagnostics instead] If set, the metrics endpoint is served securely via HTTPS. Use --metrics-secure=false to use HTTP instead.")
107+
pflag.CommandLine.BoolVar(&enableHTTP2, "enable-http2", false,
108+
"[Deprecated] If set, HTTP/2 will be enabled for the metrics and webhook servers")
105109

106-
flag.StringVar(&enabledController, "enable-controller", "", "The controller to enable. Default: all")
110+
pflag.CommandLine.StringVar(&enabledController, "enable-controller", "", "The controller to enable. Default: all")
107111
opts := zap.Options{
108112
Development: true,
109113
}
110114
opts.BindFlags(flag.CommandLine)
111-
flag.Parse()
115+
flags.AddManagerOptions(pflag.CommandLine, &managerOptions)
116+
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
117+
pflag.Parse()
112118

113119
if enabledController != "" && enabledController != allControllers {
114120
enabledControllers = map[string]bool{
@@ -118,23 +124,52 @@ func main() {
118124

119125
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
120126

121-
var tlsOpts []func(*tls.Config)
122-
disableHTTP2 := func(c *tls.Config) {
123-
setupLog.Info("disabling http/2")
124-
c.NextProtos = []string{"http/1.1"}
125-
}
126-
if !enableHTTP2 {
127-
tlsOpts = append(tlsOpts, disableHTTP2)
128-
}
127+
// NOTE: support both the deprecated and capi metrics flags
128+
// TODO: remove the next block in favor of:
129+
// _, metricsOpts, err := flags.GetManagerOptions(managerOptions)
130+
// if err != nil {
131+
// setupLog.Error(err, "unable to start manager: invalid flags")
132+
// os.Exit(1)
133+
// }
134+
135+
var metricsOpts metricsserver.Options
136+
{
137+
tlsOpts, newMetricsOpts, err := flags.GetManagerOptions(managerOptions)
138+
if err != nil {
139+
setupLog.Error(err, "unable to start manager: invalid flags")
140+
os.Exit(1)
141+
}
129142

130-
metricsOpts := metricsserver.Options{
131-
BindAddress: metricsAddr,
132-
SecureServing: secureMetrics,
133-
TLSOpts: tlsOpts,
134-
}
143+
// this protocols list is not required starting golang.org/x/[email protected]
144+
// see: https://github.com/advisories/GHSA-qppj-fm5r-hxr3
145+
// see: https://github.com/advisories/GHSA-4374-p667-p6c8
146+
disableHTTP2 := func(c *tls.Config) {
147+
setupLog.Info("disabling http/2")
148+
c.NextProtos = []string{"http/1.1"}
149+
}
150+
if !enableHTTP2 {
151+
tlsOpts = append(tlsOpts, disableHTTP2)
152+
}
153+
154+
metricsOpts = *newMetricsOpts
155+
metricsOpts.TLSOpts = tlsOpts
156+
157+
diagnosticsAddressSet := pflag.CommandLine.Changed("diagnostics-address") && pflag.Lookup("diagnostics-address").Value.String() != ":8443"
158+
insecureDiagnosticsSet := pflag.CommandLine.Changed("insecure-diagnostics")
135159

136-
if secureMetrics {
137-
metricsOpts.FilterProvider = filters.WithAuthenticationAndAuthorization
160+
if !diagnosticsAddressSet {
161+
metricsOpts.BindAddress = metricsAddr
162+
setupLog.Info("Using legacy metrics configuration",
163+
"bindAddress", metricsAddr)
164+
}
165+
if !insecureDiagnosticsSet {
166+
metricsOpts.SecureServing = secureMetrics
167+
if secureMetrics {
168+
metricsOpts.FilterProvider = filters.WithAuthenticationAndAuthorization
169+
}
170+
setupLog.Info("Using legacy metrics configuration",
171+
"secureServing", secureMetrics)
172+
}
138173
}
139174

140175
req, _ := labels.NewRequirement(clusterv1.ClusterNameLabel, selection.Exists, nil)

config/clusterapi/bootstrap/manager_config_patch.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ spec:
1818
- name: manager
1919
args:
2020
- "--health-probe-bind-address=:8081"
21-
- "--metrics-bind-address=127.0.0.1:8080"
21+
- "--diagnostics-address=127.0.0.1:8080"
2222
- "--leader-elect"
2323
- "--enable-controller=bootstrap"

config/clusterapi/infrastructure/manager_config_patch.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ spec:
1818
- name: manager
1919
args:
2020
- "--health-probe-bind-address=:8081"
21-
- "--metrics-bind-address=127.0.0.1:8080"
21+
- "--diagnostics-address=127.0.0.1:8080"
2222
- "--leader-elect"
2323
- "--enable-controller=infrastructure"

config/manager/manager.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ spec:
7070
- /manager
7171
args:
7272
- "--health-probe-bind-address=:8081"
73-
- "--metrics-bind-address=127.0.0.1:8080"
73+
- "--diagnostics-address=127.0.0.1:8080"
7474
- "--leader-elect"
7575
env:
7676
# Needed to make RemoteMachine provisioner to skip SSH key validation

go.mod

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ require (
1414
github.com/k0sproject/version v0.6.0
1515
github.com/onsi/gomega v1.36.0
1616
github.com/pkg/errors v0.9.1
17+
github.com/spf13/pflag v1.0.6
1718
github.com/stretchr/testify v1.10.0
1819
gopkg.in/yaml.v2 v2.4.0
1920
gopkg.in/yaml.v3 v3.0.1
@@ -161,7 +162,6 @@ require (
161162
github.com/spf13/afero v1.11.0 // indirect
162163
github.com/spf13/cast v1.7.0 // indirect
163164
github.com/spf13/cobra v1.8.1 // indirect
164-
github.com/spf13/pflag v1.0.6 // indirect
165165
github.com/spf13/viper v1.19.0 // indirect
166166
github.com/stoewer/go-strcase v1.2.0 // indirect
167167
github.com/subosito/gotenv v1.6.0 // indirect
@@ -221,3 +221,16 @@ require (
221221
require sigs.k8s.io/cluster-api v1.9.11
222222

223223
replace github.com/weaveworks/footloose => github.com/ncopa/footloose v0.0.0-20220210144732-fe970537b890
224+
225+
replace (
226+
k8s.io/cri-client => k8s.io/cri-client v0.31.3
227+
k8s.io/csi-translation-lib => k8s.io/csi-translation-lib v0.31.3
228+
k8s.io/dynamic-resource-allocation => k8s.io/dynamic-resource-allocation v0.31.3
229+
k8s.io/endpointslice => k8s.io/endpointslice v0.31.3
230+
k8s.io/kube-controller-manager => k8s.io/kube-controller-manager v0.31.3
231+
k8s.io/kube-proxy => k8s.io/kube-proxy v0.31.3
232+
k8s.io/kube-scheduler => k8s.io/kube-scheduler v0.31.3
233+
k8s.io/legacy-cloud-providers => k8s.io/legacy-cloud-providers v0.31.3
234+
k8s.io/pod-security-admission => k8s.io/pod-security-admission v0.31.3
235+
k8s.io/sample-apiserver => k8s.io/sample-apiserver v0.31.3
236+
)

0 commit comments

Comments
 (0)