-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
420 lines (367 loc) · 11.6 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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
package main
import (
"context"
"errors"
"fmt"
"net/http"
"net/http/pprof"
"os"
"path"
"runtime"
"strings"
"time"
"github.com/bombsimon/logrusr/v4"
"github.com/google/uuid"
"github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/net"
"k8s.io/apiserver/pkg/server/healthz"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/leaderelection"
"k8s.io/client-go/tools/leaderelection/resourcelock"
"k8s.io/client-go/util/flowcontrol"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
"github.com/castai/cluster-controller/health"
"github.com/castai/cluster-controller/internal/actions/csr"
"github.com/castai/cluster-controller/internal/castai"
"github.com/castai/cluster-controller/internal/config"
"github.com/castai/cluster-controller/internal/controller"
"github.com/castai/cluster-controller/internal/helm"
"github.com/castai/cluster-controller/internal/k8sversion"
"github.com/castai/cluster-controller/internal/logexporter"
"github.com/castai/cluster-controller/internal/waitext"
)
// These should be set via `go build` during a release.
var (
GitCommit = "undefined"
GitRef = "no-ref"
Version = "local"
)
const (
maxRequestTimeout = 5 * time.Minute
)
func main() {
log := logrus.WithFields(logrus.Fields{})
cfg := config.Get()
binVersion := &config.ClusterControllerVersion{
GitCommit: GitCommit,
GitRef: GitRef,
Version: Version,
}
log.Infof("running castai-cluster-controller version %v", binVersion)
logger := logrus.New()
logger.SetLevel(logrus.Level(cfg.Log.Level))
logger.SetReportCaller(true)
logger.Formatter = &logrus.TextFormatter{
CallerPrettyfier: func(f *runtime.Frame) (string, string) {
filename := path.Base(f.File)
return fmt.Sprintf("%s()", f.Function), fmt.Sprintf("%s:%d", filename, f.Line)
},
}
cl, err := castai.NewRestyClient(cfg.API.URL, cfg.API.Key, cfg.TLS.CACert, logger.Level, binVersion, maxRequestTimeout)
if err != nil {
log.Fatalf("failed to create castai client: %v", err)
}
client := castai.NewClient(logger, cl, cfg.ClusterID)
e := logexporter.NewLogExporter(logger, client)
logger.AddHook(e)
logrus.RegisterExitHandler(e.Wait)
ctx := signals.SetupSignalHandler()
if err := run(ctx, client, logger, cfg, binVersion); err != nil {
logErr := &logContextError{}
if errors.As(err, &logErr) {
log = logger.WithFields(logErr.fields)
}
log.Fatalf("cluster-controller failed: %v", err)
}
}
func run(
ctx context.Context,
client castai.CastAIClient,
logger *logrus.Logger,
cfg config.Config,
binVersion *config.ClusterControllerVersion,
) (reterr error) {
fields := logrus.Fields{}
defer func() {
if reterr == nil {
return
}
reterr = &logContextError{
err: reterr,
fields: fields,
}
}()
restconfig, err := retrieveKubeConfig(logger)
if err != nil {
return err
}
restConfigLeader := rest.CopyConfig(restconfig)
restConfigDynamic := rest.CopyConfig(restconfig)
restconfig.RateLimiter = flowcontrol.NewTokenBucketRateLimiter(float32(cfg.KubeClient.QPS), cfg.KubeClient.Burst)
restConfigLeader.RateLimiter = flowcontrol.NewTokenBucketRateLimiter(float32(cfg.KubeClient.QPS), cfg.KubeClient.Burst)
restConfigDynamic.RateLimiter = flowcontrol.NewTokenBucketRateLimiter(float32(cfg.KubeClient.QPS), cfg.KubeClient.Burst)
helmClient := helm.NewClient(logger, helm.NewChartLoader(logger), restconfig)
clientset, err := kubernetes.NewForConfig(restconfig)
if err != nil {
return err
}
clientSetLeader, err := kubernetes.NewForConfig(restConfigLeader)
if err != nil {
return err
}
dynamicClient, err := dynamic.NewForConfig(restConfigDynamic)
if err != nil {
return err
}
k8sVersion, err := k8sversion.Get(clientset)
if err != nil {
return fmt.Errorf("getting kubernetes version: %w", err)
}
log := logger.WithFields(logrus.Fields{
"version": binVersion.Version,
"k8s_version": k8sVersion.Full(),
"running_on": cfg.NodeName,
"ctrl_pod_name": cfg.PodName,
})
// Set logr/klog to logrus adapter so all logging goes through logrus.
logr := logrusr.New(log)
klog.SetLogger(logr)
log.Infof("running castai-cluster-controller version %v, log-level: %v", binVersion, logger.Level)
actionsConfig := controller.Config{
PollWaitInterval: 5 * time.Second,
PollTimeout: maxRequestTimeout,
AckTimeout: 30 * time.Second,
AckRetriesCount: 3,
AckRetryWait: 1 * time.Second,
ClusterID: cfg.ClusterID,
Version: binVersion.Version,
Namespace: cfg.LeaderElection.Namespace,
}
healthzAction := health.NewHealthzProvider(health.HealthzCfg{HealthyPollIntervalLimit: (actionsConfig.PollWaitInterval + actionsConfig.PollTimeout) * 2, StartTimeLimit: 2 * time.Minute}, log)
svc := controller.NewService(
log,
actionsConfig,
k8sVersion.Full(),
clientset,
dynamicClient,
client,
helmClient,
healthzAction,
)
defer func() {
if err := svc.Close(); err != nil {
log.Errorf("failed to close controller service: %v", err)
}
}()
httpMux := http.NewServeMux()
var checks []healthz.HealthChecker
checks = append(checks, healthzAction)
var leaderHealthCheck *leaderelection.HealthzAdaptor
if cfg.LeaderElection.Enabled {
leaderHealthCheck = leaderelection.NewLeaderHealthzAdaptor(time.Minute)
checks = append(checks, leaderHealthCheck)
}
healthz.InstallHandler(httpMux, checks...)
installPprofHandlers(httpMux)
// Start http server for pprof and health checks handlers.
go func() {
addr := fmt.Sprintf(":%d", cfg.PprofPort)
log.Infof("starting pprof server on %s", addr)
//TODO: remove nolint when we have a proper solution for this
//nolint:gosec
if err := http.ListenAndServe(addr, httpMux); err != nil {
log.Errorf("failed to start pprof http server: %v", err)
}
}()
runSvc := func(ctx context.Context) {
isGKE, err := runningOnGKE(clientset, cfg)
if err != nil {
log.Fatalf("failed to determine if running on GKE: %v", err)
}
if isGKE {
log.Info("auto approve csr started as running on GKE")
csrMgr := csr.NewApprovalManager(log, clientset)
csrMgr.Start(ctx)
}
svc.Run(ctx)
}
if cfg.LeaderElection.Enabled {
// Run actions service with leader election. Blocks.
return runWithLeaderElection(ctx, log, clientSetLeader, leaderHealthCheck, cfg.LeaderElection, runSvc)
}
// Run action service. Blocks.
runSvc(ctx)
return nil
}
func runWithLeaderElection(
ctx context.Context,
log logrus.FieldLogger,
clientset kubernetes.Interface,
watchDog *leaderelection.HealthzAdaptor,
cfg config.LeaderElection,
runFunc func(ctx context.Context),
) error {
id, err := os.Hostname()
if err != nil {
return fmt.Errorf("failed to determine hostname used in leader ID: %w", err)
}
id = id + "_" + uuid.New().String()
// Start the leader election code loop.
leaderelection.RunOrDie(ctx, leaderelection.LeaderElectionConfig{
Lock: &resourcelock.LeaseLock{
LeaseMeta: metav1.ObjectMeta{
Name: cfg.LockName,
Namespace: cfg.Namespace,
},
Client: clientset.CoordinationV1(),
LockConfig: resourcelock.ResourceLockConfig{
Identity: id,
},
},
// IMPORTANT: you MUST ensure that any code you have that
// is protected by the lease must terminate **before**
// you call cancel. Otherwise, you could have a background
// loop still running and another process could
// get elected before your background loop finished, violating
// the stated goal of the lease.
ReleaseOnCancel: true,
LeaseDuration: cfg.LeaseDuration,
RenewDeadline: cfg.LeaseRenewDeadline,
RetryPeriod: 3 * time.Second,
WatchDog: watchDog,
Callbacks: leaderelection.LeaderCallbacks{
OnStartedLeading: func(ctx context.Context) {
log.WithFields(logrus.Fields{
"leaseDuration": cfg.LeaseDuration.String(),
"leaseRenewDuration": cfg.LeaseRenewDeadline.String(),
}).Infof("leader elected: %s", id)
runFunc(ctx)
},
OnStoppedLeading: func() {
// This method is always called(even if it was not a leader):
// - when controller shuts dow (for example because of SIGTERM)
// - we actually lost leader
// So we need to check what whas reason of acutally stopping.
if err := ctx.Err(); err != nil {
log.Infof("main context done, stopping controller: %v", err)
return
}
log.Infof("leader lost: %s", id)
// We don't need to exit here.
// Leader "on started leading" receive a context that gets cancelled when you're no longer the leader.
},
OnNewLeader: func(identity string) {
// We're notified when new leader elected.
if identity == id {
// I just got the lock.
return
}
log.Infof("new leader elected: %s", identity)
},
},
})
return nil
}
func installPprofHandlers(mux *http.ServeMux) {
mux.HandleFunc("/debug/pprof/", pprof.Index)
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
}
func kubeConfigFromEnv() (*rest.Config, error) {
kubepath := config.Get().Kubeconfig
if kubepath == "" {
return nil, nil
}
data, err := os.ReadFile(kubepath)
if err != nil {
return nil, fmt.Errorf("reading kubeconfig at %s: %w", kubepath, err)
}
restConfig, err := clientcmd.RESTConfigFromKubeConfig(data)
if err != nil {
return nil, fmt.Errorf("building rest config from kubeconfig at %s: %w", kubepath, err)
}
return restConfig, nil
}
func retrieveKubeConfig(log logrus.FieldLogger) (*rest.Config, error) {
kubeconfig, err := kubeConfigFromEnv()
if err != nil {
return nil, err
}
if kubeconfig != nil {
log.Debug("using kubeconfig from env variables")
return kubeconfig, nil
}
inClusterConfig, err := rest.InClusterConfig()
if err != nil {
return nil, err
}
inClusterConfig.Wrap(func(rt http.RoundTripper) http.RoundTripper {
return &kubeRetryTransport{
log: log,
next: rt,
maxRetries: 10,
retryInterval: 3 * time.Second,
}
})
log.Debug("using in cluster kubeconfig")
return inClusterConfig, nil
}
type kubeRetryTransport struct {
log logrus.FieldLogger
next http.RoundTripper
maxRetries int
retryInterval time.Duration
}
func (rt *kubeRetryTransport) RoundTrip(req *http.Request) (*http.Response, error) {
var resp *http.Response
boff := waitext.NewConstantBackoff(rt.retryInterval)
err := waitext.Retry(context.Background(), boff, rt.maxRetries, func(_ context.Context) (bool, error) {
var err error
resp, err = rt.next.RoundTrip(req)
if err != nil {
// Previously client-go contained logic to retry connection refused errors. See https://github.com/kubernetes/kubernetes/pull/88267/files
if net.IsConnectionRefused(err) {
return true, err
}
return false, err
}
return false, nil
}, func(err error) {
rt.log.Warnf("kube api server connection refused, will retry: %v", err)
})
return resp, err
}
type logContextError struct {
err error
fields logrus.Fields
}
func (e *logContextError) Error() string {
return e.err.Error()
}
func (e *logContextError) Unwrap() error {
return e.err
}
func runningOnGKE(clientset *kubernetes.Clientset, cfg config.Config) (isGKE bool, err error) {
err = waitext.Retry(context.Background(), waitext.DefaultExponentialBackoff(), 3, func(ctx context.Context) (bool, error) {
node, err := clientset.CoreV1().Nodes().Get(ctx, cfg.NodeName, metav1.GetOptions{})
if err != nil {
return true, fmt.Errorf("getting node: %w", err)
}
for k := range node.Labels {
if strings.HasPrefix(k, "cloud.google.com/") {
isGKE = true
return false, nil
}
}
return false, nil
}, func(err error) {
})
return
}