-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
275 lines (248 loc) · 8.59 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
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and IronCore contributors
// SPDX-License-Identifier: Apache-2.0
package main
import (
"context"
"fmt"
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
"time"
"github.com/go-logr/logr"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"k8s.io/apimachinery/pkg/runtime"
kscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/klog/v2"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/metrics/server"
ipamv1alpha1 "github.com/ironcore-dev/ipam/api/ipam/v1alpha1"
computev1alpha1 "github.com/ironcore-dev/ironcore/api/compute/v1alpha1"
oobv1alpha1 "github.com/ironcore-dev/oob/api/v1alpha1"
"github.com/ironcore-dev/oob/controllers"
"github.com/ironcore-dev/oob/internal/log"
"github.com/ironcore-dev/oob/servers"
)
func usage() {
name := filepath.Base(os.Args[0])
_, _ = fmt.Fprintf(os.Stderr, "Usage: %s [--option]...\n", name)
_, _ = fmt.Fprintf(os.Stderr, "Options:\n")
pflag.PrintDefaults()
}
func exitUsage(err error) {
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%s: %s\n", filepath.Base(os.Args[0]), err)
}
pflag.Usage()
os.Exit(2)
}
type params struct {
dev bool
leaderElect bool
healthProbeBindAddress string
metricsBindAddress string
kubeconfig string
namespace string
macPrefixes string
credentialsExpBuffer time.Duration
shutdownTimeout time.Duration
consoleServerCert string
consoleServerKey string
subnetLabelName string
subnetLabelValue string
}
func parseCmdLine() params {
pflag.Usage = usage
pflag.ErrHelp = nil
pflag.CommandLine = pflag.NewFlagSet(os.Args[0], pflag.ContinueOnError)
pflag.Bool("dev", false, "Log human-readable messages at debug level.")
pflag.Bool("leader-elect", false, "Enable leader election for controller manager to ensure there is only one active controller manager.")
pflag.String("health-probe-bind-address", "", "The address that the health probe server will listen on.")
pflag.String("metrics-bind-address", "0", "The address that the metrics server will listen on.")
pflag.String("kubeconfig", "", "Use a kubeconfig to run out of cluster.")
pflag.String("namespace", "", "Limit monitoring to a specific namespace.")
pflag.String("mac-prefixes", "macPrefixes.yaml", "Read MAC address prefixes from the specified file.")
pflag.Duration("credentials-exp-buffer", 125*time.Hour, "Renew expiring credentials this long before they are set to expire expire. See https://golang.org/pkg/time/#ParseDuration")
pflag.Duration("shutdown-timeout", 5*time.Minute, "Wait this long before issuing an immediate shutdown, if graceful shutdown has not succeeded. See https://golang.org/pkg/time/#ParseDuration")
pflag.String("console-server-cert", "", "Use a TLS certificate for the console server. If not set, do not start the console server.")
pflag.String("console-server-key", "", "Use a TLS key for the console server. If not set, do not start the console server.")
pflag.String("subnet-label-name", "", "Label name used to filter ip.ipam objects. Not specifying it will reconcile ALL ip.ipam objects.")
pflag.String("subnet-label-value", "", "Label value used to filter ip.ipam objects.")
var help bool
pflag.BoolVarP(&help, "help", "h", false, "Show this help message.")
err := viper.BindPFlags(pflag.CommandLine)
if err != nil {
exitUsage(err)
}
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
viper.AutomaticEnv()
err = pflag.CommandLine.Parse(os.Args[1:])
if err != nil {
exitUsage(err)
}
if help {
exitUsage(nil)
}
return params{
dev: viper.GetBool("dev"),
leaderElect: viper.GetBool("leader-elect"),
healthProbeBindAddress: viper.GetString("health-probe-bind-address"),
metricsBindAddress: viper.GetString("metrics-bind-address"),
kubeconfig: viper.GetString("kubeconfig"),
namespace: viper.GetString("namespace"),
macPrefixes: viper.GetString("mac-prefixes"),
credentialsExpBuffer: viper.GetDuration("credentials-exp-buffer"),
shutdownTimeout: viper.GetDuration("shutdown-timeout"),
consoleServerCert: viper.GetString("console-server-cert"),
consoleServerKey: viper.GetString("console-server-key"),
subnetLabelName: viper.GetString("subnet-label-name"),
subnetLabelValue: viper.GetString("subnet-label-value"),
}
}
func main() {
p := parseCmdLine()
var exitCode int
defer func() { os.Exit(exitCode) }()
ctx, stop := signal.NotifyContext(log.Setup(context.Background(), p.dev, false, os.Stderr), syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGHUP)
defer stop()
log.Info(ctx, "Starting OOB operator")
defer func() { log.Info(ctx, "Exiting", "exitCode", exitCode) }()
l := logr.FromContextOrDiscard(ctx)
klog.SetLogger(l)
ctrl.SetLogger(l)
scheme := runtime.NewScheme()
err := kscheme.AddToScheme(scheme)
if err != nil {
log.Error(ctx, fmt.Errorf("cannot create type scheme: %w", err))
exitCode = 1
return
}
err = ipamv1alpha1.AddToScheme(scheme)
if err != nil {
log.Error(ctx, fmt.Errorf("cannot create type scheme: %w", err))
exitCode = 1
return
}
err = oobv1alpha1.AddToScheme(scheme)
if err != nil {
log.Error(ctx, fmt.Errorf("cannot create type scheme: %w", err))
exitCode = 1
return
}
err = computev1alpha1.AddToScheme(scheme)
if err != nil {
log.Error(ctx, fmt.Errorf("cannot create type scheme: %w", err))
exitCode = 1
return
}
//+kubebuilder:scaffold:scheme
if p.namespace == "" {
var ns []byte
ns, err = os.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace")
if err != nil && !os.IsNotExist(err) {
log.Error(ctx, fmt.Errorf("cannot determine in-cluster namespace: %w", err))
exitCode = 1
return
}
p.namespace = string(ns)
}
var kcfg *rest.Config
kcfg, err = ctrl.GetConfig()
if err != nil {
log.Error(ctx, fmt.Errorf("cannot get kubeconfig: %w", err))
exitCode = 1
return
}
var mgr manager.Manager
mgr, err = ctrl.NewManager(kcfg, ctrl.Options{
BaseContext: func() context.Context {
return ctx
},
Scheme: scheme,
LeaderElection: p.leaderElect,
LeaderElectionID: "oob.ironcore.dev",
HealthProbeBindAddress: p.healthProbeBindAddress,
Metrics: server.Options{
BindAddress: p.metricsBindAddress,
},
})
if err != nil {
log.Error(ctx, fmt.Errorf("cannot create manager: %w", err))
exitCode = 1
return
}
var ipReconciler *controllers.IPReconciler
ipReconciler, err = controllers.NewIPReconciler(p.namespace, p.subnetLabelName, p.subnetLabelValue)
if err != nil {
log.Error(ctx, fmt.Errorf("cannot create controller: %w", err), "controller", "IP")
exitCode = 1
return
}
err = ipReconciler.SetupWithManager(mgr)
if err != nil {
log.Error(ctx, fmt.Errorf("cannot create controller: %w", err), "controller", "IP")
exitCode = 1
return
}
var oobReconciler *controllers.OOBReconciler
oobReconciler, err = controllers.NewOOBReconciler(p.namespace, p.credentialsExpBuffer, p.shutdownTimeout)
if err != nil {
log.Error(ctx, fmt.Errorf("cannot create controller: %w", err), "controller", "OOB")
exitCode = 1
return
}
if p.macPrefixes != "" {
err = oobReconciler.LoadMACPrefixes(ctx, p.macPrefixes)
if err != nil {
log.Error(ctx, fmt.Errorf("cannot read MAC prefixes: %w", err), "controller", "OOB")
exitCode = 1
return
}
}
err = oobReconciler.SetupWithManager(mgr)
if err != nil {
log.Error(ctx, fmt.Errorf("cannot create controller: %w", err), "controller", "OOB")
exitCode = 1
return
}
if p.consoleServerCert != "" && p.consoleServerKey != "" {
var consoleServer *servers.ConsoleServer
consoleServer, err = servers.NewConsoleServer(":12319", p.consoleServerCert, p.consoleServerKey)
if err != nil {
log.Error(ctx, fmt.Errorf("cannot create server: %w", err), "server", "console")
exitCode = 1
return
}
err = consoleServer.SetupWithManager(mgr)
if err != nil {
log.Error(ctx, fmt.Errorf("cannot create server: %w", err), "server", "console")
exitCode = 1
return
}
}
//+kubebuilder:scaffold:builder
err = mgr.AddHealthzCheck("health", healthz.Ping)
if err != nil {
log.Error(ctx, fmt.Errorf("cannot set up health check: %w", err))
exitCode = 1
return
}
err = mgr.AddReadyzCheck("check", healthz.Ping)
if err != nil {
log.Error(ctx, fmt.Errorf("cannot set up ready check: %w", err))
exitCode = 1
return
}
log.Info(ctx, "Starting manager")
err = mgr.Start(ctx)
if err != nil {
log.Error(ctx, err)
exitCode = 1
return
}
}