-
Notifications
You must be signed in to change notification settings - Fork 64
/
main.go
297 lines (246 loc) · 10.2 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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package main
import (
"context"
"flag"
"fmt"
"net/http"
"os"
"strings"
"time"
debug "github.com/talos-systems/go-debug"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
"golang.org/x/sync/errgroup"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"k8s.io/client-go/tools/record"
capi "sigs.k8s.io/cluster-api/api/v1alpha3"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
infrav1 "github.com/talos-systems/sidero/app/caps-controller-manager/api/v1alpha3"
metalv1alpha1 "github.com/talos-systems/sidero/app/sidero-controller-manager/api/v1alpha1"
"github.com/talos-systems/sidero/app/sidero-controller-manager/controllers"
"github.com/talos-systems/sidero/app/sidero-controller-manager/internal/ipxe"
"github.com/talos-systems/sidero/app/sidero-controller-manager/internal/metadata"
"github.com/talos-systems/sidero/app/sidero-controller-manager/internal/power/api"
"github.com/talos-systems/sidero/app/sidero-controller-manager/internal/server"
"github.com/talos-systems/sidero/app/sidero-controller-manager/internal/tftp"
"github.com/talos-systems/sidero/app/sidero-controller-manager/pkg/constants"
"github.com/talos-systems/sidero/internal/client"
// +kubebuilder:scaffold:imports
)
const (
defaultMaxConcurrentReconciles = 10
debugAddr = ":9992"
httpPort = 8081
)
var (
// TalosRelease is set as a build argument.
TalosRelease string
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
)
//nolint:wsl
func init() {
_ = clientgoscheme.AddToScheme(scheme)
_ = capi.AddToScheme(scheme)
_ = metalv1alpha1.AddToScheme(scheme)
_ = infrav1.AddToScheme(scheme)
// +kubebuilder:scaffold:scheme
}
func main() {
var (
metricsAddr string
apiEndpoint string
apiPort int
extraAgentKernelArgs string
bootFromDiskMethod string
enableLeaderElection bool
autoAcceptServers bool
insecureWipe bool
autoBMCSetup bool
serverRebootTimeout time.Duration
testPowerSimulatedExplicitFailureProb float64
testPowerSimulatedSilentFailureProb float64
)
flag.StringVar(&apiEndpoint, "api-endpoint", "", "The endpoint (hostname or IP address) Sidero can be reached at from the servers.")
flag.IntVar(&apiPort, "api-port", httpPort, "The TCP port Sidero components can be reached at from the servers.")
flag.StringVar(&metricsAddr, "metrics-addr", ":8081", "The address the metric endpoint binds to.")
flag.StringVar(&extraAgentKernelArgs, "extra-agent-kernel-args", "", "A comma delimited list of key-value pairs to be added to the agent environment kernel parameters.")
flag.StringVar(&bootFromDiskMethod, "boot-from-disk-method", string(ipxe.BootIPXEExit), "Default method to use to boot server from disk if it hits iPXE endpoint after install.")
flag.BoolVar(&enableLeaderElection, "enable-leader-election", true, "Enable leader election for controller manager. Enabling this will ensure there is only one active controller manager.")
flag.BoolVar(&autoAcceptServers, "auto-accept-servers", false, "Add servers as 'accepted' when they register with Sidero API.")
flag.BoolVar(&insecureWipe, "insecure-wipe", true, "Wipe head of the disk only (if false, wipe whole disk).")
flag.BoolVar(&autoBMCSetup, "auto-bmc-setup", true, "Attempt to setup BMC info automatically when agent boots.")
flag.DurationVar(&serverRebootTimeout, "server-reboot-timeout", constants.DefaultServerRebootTimeout, "Timeout to wait for the server to restart and start wipe.")
flag.Float64Var(&testPowerSimulatedExplicitFailureProb, "test-power-simulated-explicit-failure-prob", 0, "Test failure simulation setting.")
flag.Float64Var(&testPowerSimulatedSilentFailureProb, "test-power-simulated-silent-failure-prob", 0, "Test failure simulation setting.")
flag.Parse()
// we can't continue without it
if TalosRelease == "" {
panic("TalosRelease is not set during the build")
}
// workaround for clusterctl not accepting empty value as default value
if extraAgentKernelArgs == "-" {
extraAgentKernelArgs = ""
}
if apiEndpoint == "-" {
apiEndpoint = ""
}
if apiEndpoint == "" {
if endpoint, ok := os.LookupEnv("API_ENDPOINT"); ok {
apiEndpoint = endpoint
} else {
setupLog.Error(fmt.Errorf("no api endpoint found"), "")
os.Exit(1)
}
}
ctrl.SetLogger(zap.New(func(o *zap.Options) {
o.Development = true
}))
go func() {
debugLogFunc := func(msg string) {
setupLog.Info(msg)
}
if err := debug.ListenAndServe(context.TODO(), debugAddr, debugLogFunc); err != nil {
setupLog.Error(err, "failed to start debug server")
os.Exit(1)
}
}()
// only for testing, doesn't affect production, default values simulate no failures
api.DefaultDice = api.NewFailureDice(testPowerSimulatedExplicitFailureProb, testPowerSimulatedSilentFailureProb)
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
LeaderElection: enableLeaderElection,
LeaderElectionID: "controller-leader-election-sidero-controller-manager",
Port: 9443,
})
if err != nil {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
}
clientset, err := kubernetes.NewForConfig(mgr.GetConfig())
if err != nil {
setupLog.Error(err, "unable to create k8s client")
os.Exit(1)
}
eventBroadcaster := record.NewBroadcaster()
eventBroadcaster.StartRecordingToSink(
&typedcorev1.EventSinkImpl{
Interface: clientset.CoreV1().Events(""),
})
recorder := eventBroadcaster.NewRecorder(
mgr.GetScheme(),
corev1.EventSource{Component: "sidero-controller-manager"})
if err = (&controllers.EnvironmentReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("Environment"),
Scheme: mgr.GetScheme(),
TalosRelease: TalosRelease,
APIEndpoint: apiEndpoint,
APIPort: uint16(apiPort),
}).SetupWithManager(mgr, controller.Options{MaxConcurrentReconciles: defaultMaxConcurrentReconciles}); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Environment")
os.Exit(1)
}
if err = (&controllers.ServerReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("Server"),
Scheme: mgr.GetScheme(),
APIReader: mgr.GetAPIReader(),
Recorder: recorder,
RebootTimeout: serverRebootTimeout,
}).SetupWithManager(mgr, controller.Options{MaxConcurrentReconciles: defaultMaxConcurrentReconciles}); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Server")
os.Exit(1)
}
if err = (&controllers.ServerClassReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("ServerClass"),
Scheme: mgr.GetScheme(),
}).SetupWithManager(mgr, controller.Options{MaxConcurrentReconciles: defaultMaxConcurrentReconciles}); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "ServerClass")
os.Exit(1)
}
// +kubebuilder:scaffold:builder
setupLog.Info("starting TFTP server")
go func() {
if err := tftp.ServeTFTP(); err != nil {
setupLog.Error(err, "unable to start TFTP server", "controller", "Environment")
os.Exit(1)
}
}()
httpMux := http.NewServeMux()
setupLog.Info("starting iPXE server")
if err := ipxe.RegisterIPXE(httpMux, apiEndpoint, apiPort, extraAgentKernelArgs, ipxe.BootFromDisk(bootFromDiskMethod), apiPort, mgr.GetClient()); err != nil {
setupLog.Error(err, "unable to start iPXE server", "controller", "Environment")
os.Exit(1)
}
setupLog.Info("starting metadata server")
if err := metadata.RegisterServer(httpMux, mgr.GetClient()); err != nil {
setupLog.Error(err, "unable to start metadata server", "controller", "Environment")
os.Exit(1)
}
setupLog.Info("starting internal API server")
apiRecorder := eventBroadcaster.NewRecorder(
mgr.GetScheme(),
corev1.EventSource{Component: "sidero-server"})
grpcServer := server.CreateServer(mgr.GetClient(), apiRecorder, mgr.GetScheme(), autoAcceptServers, insecureWipe, autoBMCSetup, serverRebootTimeout)
k8sClient, err := client.NewClient(nil)
if err != nil {
setupLog.Error(err, `failed to create k8s client`)
os.Exit(1)
}
if err = controllers.ReconcileServerClassAny(context.TODO(), k8sClient); err != nil {
setupLog.Error(err, `failed to reconcile ServerClass "any"`)
os.Exit(1)
}
if err = controllers.ReconcileEnvironmentDefault(context.TODO(), k8sClient, TalosRelease, apiEndpoint, uint16(apiPort)); err != nil {
setupLog.Error(err, `failed to reconcile Environment "default"`)
os.Exit(1)
}
setupLog.Info("starting manager and HTTP server")
var eg errgroup.Group
eg.Go(func() error {
err := mgr.Start(ctrl.SetupSignalHandler())
if err != nil {
setupLog.Error(err, "problem running manager")
}
return err
})
eg.Go(func() error {
// Go standard library doesn't support running HTTP/2 on non-TLS HTTP connections.
// Package h2c provides handling for HTTP/2 over plaintext connection.
// gRPC provides its own HTTP/2 server implementation, so that's not an issue for gRPC,
// but as we unify all endpoints under a single HTTP endpoint, we have to provide additional
// layer of support here.
h2s := &http2.Server{}
grpcHandler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if req.ProtoMajor == 2 && strings.HasPrefix(
req.Header.Get("Content-Type"), "application/grpc") {
// grpcServer provides internal gRPC API server
grpcServer.ServeHTTP(w, req)
return
}
// httpMux contains iPXE server and metadata server handlers
httpMux.ServeHTTP(w, req)
})
err := http.ListenAndServe(fmt.Sprintf(":%d", httpPort), h2c.NewHandler(grpcHandler, h2s))
if err != nil {
setupLog.Error(err, "problem running HTTP server")
}
return err
})
if err := eg.Wait(); err != nil {
os.Exit(1)
}
}