-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
323 lines (282 loc) · 10.9 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
Copyright 2023.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"crypto/tls"
"flag"
"fmt"
"os"
"runtime"
"strconv"
"strings"
"k8s.io/client-go/kubernetes"
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
// to ensure that exec-entrypoint and run can make use of them.
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/client-go/rest"
k8sruntime "k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
cliflag "k8s.io/component-base/cli/flag"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
"sigs.k8s.io/controller-runtime/pkg/webhook"
metal3iov1alpha1 "github.com/metal3-io/ironic-standalone-operator/api/v1alpha1"
"github.com/metal3-io/ironic-standalone-operator/controllers"
"github.com/metal3-io/ironic-standalone-operator/pkg/ironic"
//+kubebuilder:scaffold:imports
)
var (
scheme = k8sruntime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
)
func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(metal3iov1alpha1.AddToScheme(scheme))
//+kubebuilder:scaffold:scheme
}
const (
TLSVersion12 = "TLS12"
TLSVersion13 = "TLS13"
)
var tlsSupportedVersions = []string{TLSVersion12, TLSVersion13}
type TLSOptions struct {
TLSMaxVersion string
TLSMinVersion string
TLSCipherSuites string
}
func main() {
var metricsAddr string
var enableLeaderElection bool
var probeAddr string
var webhookPort int
var watchNamespace string
var tlsOptions TLSOptions
var controllerConcurrency int
var clusterDomain string
var versionInfo ironic.VersionInfo
tlsCipherPreferredValues := cliflag.PreferredTLSCipherNames()
tlsCipherInsecureValues := cliflag.InsecureTLSCipherNames()
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
flag.IntVar(&webhookPort, "webhook-port", 9443, "Port to use for webhooks (0 to disable)")
flag.StringVar(&watchNamespace, "namespace", os.Getenv("WATCH_NAMESPACE"),
"Namespace that the controller watches to reconcile resources.")
flag.StringVar(&tlsOptions.TLSMinVersion, "tls-min-version", TLSVersion12,
"The minimum TLS version in use by the webhook server.\n"+
fmt.Sprintf("Possible values are %s.", strings.Join(tlsSupportedVersions, ", ")))
flag.StringVar(&tlsOptions.TLSMaxVersion, "tls-max-version", TLSVersion13,
"The maximum TLS version in use by the webhook server.\n"+
fmt.Sprintf("Possible values are %s.", strings.Join(tlsSupportedVersions, ", ")))
flag.StringVar(&tlsOptions.TLSCipherSuites, "tls-cipher-suites", "",
"Comma-separated list of cipher suites for the webhook server. "+
"If omitted, the default Go cipher suites will be used. \n"+
"Preferred values: "+strings.Join(tlsCipherPreferredValues, ", ")+". \n"+
"Insecure values: "+strings.Join(tlsCipherInsecureValues, ", ")+".")
flag.IntVar(&controllerConcurrency, "controller-concurrency", 0,
"Number of resources of each type to process simultaneously.")
flag.StringVar(&clusterDomain, "cluster-domain", os.Getenv("CLUSTER_DOMAIN"),
"Domain name of the current cluster, e.g. cluster.local.")
flag.StringVar(&versionInfo.IronicImage, "ironic-image", os.Getenv("IRONIC_IMAGE"),
"Ironic image to install.")
flag.StringVar(&versionInfo.MariaDBImage, "mariadb-image", os.Getenv("MARIADB_IMAGE"),
"MariaDB image to install.")
flag.StringVar(&versionInfo.RamdiskDownloaderImage, "ramdisk-downloader-image", os.Getenv("RAMDISK_DOWNLOADER_IMAGE"),
"Ramdisk downloader image to install.")
flag.StringVar(&versionInfo.InstalledVersion, "ironic-version", os.Getenv("IRONIC_VERSION"),
"Branch of Ironic that the operator installs.")
opts := zap.Options{
Development: true,
}
opts.BindFlags(flag.CommandLine)
flag.Parse()
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
config := ctrl.GetConfigOrDie()
kubeClient := kubernetes.NewForConfigOrDie(rest.AddUserAgent(config, "ironic-standalone-operator"))
tlsOptionOverrides, err := GetTLSOptionOverrideFuncs(tlsOptions)
if err != nil {
setupLog.Error(err, "unable to add TLS settings to the webhook server")
os.Exit(1)
}
var watchNamespaces map[string]cache.Config
if watchNamespace != "" {
watchNamespaces = map[string]cache.Config{
watchNamespace: {},
}
}
ctrlOpts := ctrl.Options{
Scheme: scheme,
Metrics: metricsserver.Options{BindAddress: metricsAddr},
WebhookServer: webhook.NewServer(webhook.Options{
Port: webhookPort,
TLSOpts: tlsOptionOverrides,
}),
HealthProbeBindAddress: probeAddr,
LeaderElection: enableLeaderElection,
LeaderElectionID: "ironic.metal3.io",
LeaderElectionNamespace: watchNamespace,
Cache: cache.Options{
DefaultNamespaces: watchNamespaces,
},
}
mgr, err := ctrl.NewManager(config, ctrlOpts)
if err != nil {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
}
if err = (&controllers.IronicReconciler{
Client: mgr.GetClient(),
KubeClient: kubeClient,
Scheme: mgr.GetScheme(),
Log: ctrl.Log.WithName("controllers").WithName("Ironic"),
Domain: clusterDomain,
VersionInfo: ironic.VersionInfoWithDefaults(versionInfo),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Ironic")
os.Exit(1)
}
if webhookPort != 0 {
if err = (&metal3iov1alpha1.Ironic{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "Ironic")
os.Exit(1)
}
}
if err = (&controllers.IronicDatabaseReconciler{
Client: mgr.GetClient(),
KubeClient: kubeClient,
Scheme: mgr.GetScheme(),
Log: ctrl.Log.WithName("controllers").WithName("IronicDatabase"),
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "IronicDatabase")
os.Exit(1)
}
if webhookPort != 0 {
if err = (&metal3iov1alpha1.IronicDatabase{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "IronicDatabase")
os.Exit(1)
}
}
//+kubebuilder:scaffold:builder
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
setupLog.Error(err, "unable to set up health check")
os.Exit(1)
}
if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
setupLog.Error(err, "unable to set up ready check")
os.Exit(1)
}
setupLog.Info("starting manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
setupLog.Error(err, "problem running manager")
os.Exit(1)
}
}
// GetTLSOptionOverrideFuncs returns a list of TLS configuration overrides to be used
// by the webhook server.
func GetTLSOptionOverrideFuncs(options TLSOptions) ([]func(*tls.Config), error) {
var tlsOptions []func(config *tls.Config)
tlsMinVersion, err := GetTLSVersion(options.TLSMinVersion)
if err != nil {
return nil, err
}
tlsMaxVersion, err := GetTLSVersion(options.TLSMaxVersion)
if err != nil {
return nil, err
}
if tlsMaxVersion != 0 && tlsMinVersion > tlsMaxVersion {
return nil, fmt.Errorf("TLS version flag min version (%s) is greater than max version (%s)",
options.TLSMinVersion, options.TLSMaxVersion)
}
tlsOptions = append(tlsOptions, func(cfg *tls.Config) {
cfg.MinVersion = tlsMinVersion
})
tlsOptions = append(tlsOptions, func(cfg *tls.Config) {
cfg.MaxVersion = tlsMaxVersion
})
// Cipher suites should not be set if empty.
if tlsMinVersion >= tls.VersionTLS13 &&
options.TLSCipherSuites != "" {
setupLog.Info("warning: Cipher suites should not be set for TLS version 1.3. Ignoring ciphers")
options.TLSCipherSuites = ""
}
if options.TLSCipherSuites != "" {
tlsCipherSuites := strings.Split(options.TLSCipherSuites, ",")
suites, err := cliflag.TLSCipherSuites(tlsCipherSuites)
if err != nil {
return nil, err
}
insecureCipherValues := cliflag.InsecureTLSCipherNames()
for _, cipher := range tlsCipherSuites {
for _, insecureCipherName := range insecureCipherValues {
if insecureCipherName == cipher {
setupLog.Info(fmt.Sprintf("warning: use of insecure cipher '%s' detected.", cipher))
}
}
}
tlsOptions = append(tlsOptions, func(cfg *tls.Config) {
cfg.CipherSuites = suites
})
}
return tlsOptions, nil
}
// GetTLSVersion returns the corresponding tls.Version or error.
func GetTLSVersion(version string) (uint16, error) {
var v uint16
switch version {
case TLSVersion12:
v = tls.VersionTLS12
case TLSVersion13:
v = tls.VersionTLS13
default:
return 0, fmt.Errorf("unexpected TLS version %q (must be one of: %s)", version, strings.Join(tlsSupportedVersions, ", "))
}
return v, nil
}
func getMaxConcurrentReconciles(controllerConcurrency int) (int, error) {
if controllerConcurrency > 0 {
ctrl.Log.Info(fmt.Sprintf("controller concurrency will be set to %d according to command line flag", controllerConcurrency))
return controllerConcurrency, nil
} else if controllerConcurrency < 0 {
return 0, fmt.Errorf("controller concurrency value: %d is invalid", controllerConcurrency)
}
// controller-concurrency value is 0 i.e. no values passed via the flag
// maxConcurrentReconcile value would be set based on env var or number of CPUs.
maxConcurrentReconciles := runtime.NumCPU()
if maxConcurrentReconciles > 8 {
maxConcurrentReconciles = 8
}
if maxConcurrentReconciles < 2 {
maxConcurrentReconciles = 2
}
if mcrEnv, ok := os.LookupEnv("CONTROLLER_CONCURRENCY"); ok {
mcr, err := strconv.Atoi(mcrEnv)
if err != nil {
return 0, fmt.Errorf("CONTROLLER_CONCURRENCY value: %s is invalid: %w", mcrEnv, err)
}
if mcr > 0 {
ctrl.Log.Info(fmt.Sprintf("CONTROLLER_CONCURRENCY of %d is set via an environment variable", mcr))
maxConcurrentReconciles = mcr
} else {
ctrl.Log.Info(fmt.Sprintf("Invalid CONTROLLER_CONCURRENCY value. Operator Concurrency will be set to a default value of %d", maxConcurrentReconciles))
}
} else {
ctrl.Log.Info(fmt.Sprintf("Operator Concurrency will be set to a default value of %d", maxConcurrentReconciles))
}
return maxConcurrentReconciles, nil
}