-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
358 lines (297 loc) · 12.3 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
package main
import (
"context"
"errors"
"flag"
"fmt"
"github.com/cloudogu/k8s-backup-operator/pkg/provider"
"github.com/cloudogu/k8s-registry-lib/repository"
"os"
"time"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/kubernetes"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/controller-runtime/pkg/log"
// 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"
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"
"sigs.k8s.io/controller-runtime/pkg/metrics/server"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"github.com/cloudogu/k8s-backup-operator/pkg/additionalimages"
"github.com/cloudogu/k8s-backup-operator/pkg/api/ecosystem"
k8sv1 "github.com/cloudogu/k8s-backup-operator/pkg/api/v1"
"github.com/cloudogu/k8s-backup-operator/pkg/backup"
"github.com/cloudogu/k8s-backup-operator/pkg/backupschedule"
"github.com/cloudogu/k8s-backup-operator/pkg/cleanup"
"github.com/cloudogu/k8s-backup-operator/pkg/config"
"github.com/cloudogu/k8s-backup-operator/pkg/garbagecollection"
"github.com/cloudogu/k8s-backup-operator/pkg/requeue"
"github.com/cloudogu/k8s-backup-operator/pkg/restore"
"github.com/cloudogu/k8s-backup-operator/pkg/scheduledbackup"
// +kubebuilder:scaffold:imports
)
var (
operatorCmd = flag.NewFlagSet("operator", flag.ExitOnError)
scheduledBackupCmd = flag.NewFlagSet("scheduled-backup", flag.ExitOnError)
garbageCollectorCmd = flag.NewFlagSet("gc", flag.ExitOnError)
)
var (
scheme = runtime.NewScheme()
)
var (
// Version of the application
Version = "0.0.0"
)
var (
leaseDuration = time.Second * 60
renewDeadline = time.Second * 40
)
var (
newAdditionalImageGetter = additionalimages.NewGetter
newAdditionalImageUpdater = additionalimages.NewUpdater
newGarbageCollectionManager = garbagecollection.NewManager
newScheduledBackupManager = scheduledbackup.NewManager
)
func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(k8sv1.AddToScheme(scheme))
// +kubebuilder:scaffold:scheme
}
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
logger := log.FromContext(ctx).WithName("main")
if len(os.Args) < 2 {
fmt.Printf("expected one of the following subcommands:\n"+
" %s - start in operator mode, reconciling this operators custom resources\n"+
" %s - start in scheduled-backup mode, this is used by backup schedule cron jobs\n"+
" %s - start in garbage-collection mode, deleting backups according to the configured retention strategy\n",
operatorCmd.Name(),
scheduledBackupCmd.Name(),
garbageCollectorCmd.Name())
os.Exit(1)
}
switch os.Args[1] {
case operatorCmd.Name():
err := startOperator(ctx, operatorCmd, os.Args[2:])
if err != nil {
logger.Error(err, "failed to start operator")
fmt.Printf("failed to start operator: %s\n", err.Error())
os.Exit(1)
}
case scheduledBackupCmd.Name():
err := startScheduledBackup(ctx, scheduledBackupCmd, os.Args[2:])
if err != nil {
logger.Error(err, "failed to create scheduled backup")
fmt.Printf("failed to create scheduled backup: %s\n", err.Error())
os.Exit(1)
}
case garbageCollectorCmd.Name():
err := startGarbageCollector(ctx, garbageCollectorCmd, os.Args[2:])
if err != nil {
logger.Error(err, "failed to start garbage-collector")
fmt.Printf("failed to start garbage-collector: %s\n", err.Error())
os.Exit(1)
}
}
}
func startScheduledBackup(ctx context.Context, cmd *flag.FlagSet, args []string) error {
restConfig := ctrl.GetConfigOrDie()
namespace, err := config.GetNamespace()
if err != nil {
return fmt.Errorf("unable to get current namespace: %w", err)
}
k8sClientSet, err := kubernetes.NewForConfig(restConfig)
if err != nil {
return fmt.Errorf("unable to create k8s clientset: %w", err)
}
ecosystemClientSet, err := ecosystem.NewClientSet(restConfig, k8sClientSet)
if err != nil {
return fmt.Errorf("unable to create ecosystem clientset: %w", err)
}
options := parseScheduledBackupOptions(cmd, args)
options.Namespace = namespace
manager := newScheduledBackupManager(ecosystemClientSet, options)
return manager.ScheduleBackup(ctx)
}
func parseScheduledBackupOptions(flags *flag.FlagSet, args []string) scheduledbackup.Options {
options := scheduledbackup.Options{}
flags.StringVar(&options.Name, "name", "", "The name of the schedule that triggers this backup. The final name of the backup will this name appended with a timestamp. Required.")
flags.StringVar(&options.Provider, "provider", "", "The name of the provider that should be used for this backup. Default: velero.")
// Ignore errors; flags is set to exit on errors
_ = flags.Parse(args)
return options
}
func startGarbageCollector(ctx context.Context, flags *flag.FlagSet, args []string) error {
restConfig := ctrl.GetConfigOrDie()
namespace, err := config.GetNamespace()
if err != nil {
return fmt.Errorf("unable to get current namespace: %w", err)
}
k8sClientSet, err := kubernetes.NewForConfig(restConfig)
if err != nil {
return fmt.Errorf("unable to create k8s clientset: %w", err)
}
ecosystemClientSet, err := ecosystem.NewClientSet(restConfig, k8sClientSet)
if err != nil {
return fmt.Errorf("unable to create ecosystem clientset: %w", err)
}
retentionStrategy := parseStrategyName(flags, args)
gcManager := newGarbageCollectionManager(ecosystemClientSet, namespace, retentionStrategy)
return gcManager.CollectGarbage(ctx)
}
func parseStrategyName(flags *flag.FlagSet, args []string) string {
var strategyName string
flags.StringVar(&strategyName, "strategy", "keepAll", "The retention strategy to decide which backups to delete and which to keep.")
// Ignore errors; flags is set to exit on errors
_ = flags.Parse(args)
return strategyName
}
func startOperator(ctx context.Context, flags *flag.FlagSet, args []string) error {
operatorConfig, err := config.NewOperatorConfig(Version)
if err != nil {
return fmt.Errorf("unable to create operator config: %w", err)
}
options := getK8sManagerOptions(flags, args, operatorConfig)
restConfig := ctrl.GetConfigOrDie()
k8sManager, err := ctrl.NewManager(restConfig, options)
if err != nil {
return fmt.Errorf("unable to start manager: %w", err)
}
err = configureManager(ctx, k8sManager, operatorConfig)
if err != nil {
return fmt.Errorf("unable to configure manager: %w", err)
}
return startK8sManager(ctx, k8sManager)
}
func configureManager(ctx context.Context, k8sManager controllerManager, operatorConfig *config.OperatorConfig) error {
err := configureReconcilers(ctx, k8sManager, operatorConfig)
if err != nil {
return fmt.Errorf("unable to configure reconciler: %w", err)
}
err = addChecks(k8sManager)
if err != nil {
return fmt.Errorf("unable to add checks to the manager: %w", err)
}
return nil
}
func getK8sManagerOptions(flags *flag.FlagSet, args []string, operatorConfig *config.OperatorConfig) ctrl.Options {
controllerOpts := ctrl.Options{
Scheme: scheme,
Cache: cache.Options{DefaultNamespaces: map[string]cache.Config{
operatorConfig.Namespace: {},
}},
WebhookServer: webhook.NewServer(webhook.Options{Port: 9443}),
LeaderElectionID: "e3f6c1a7.cloudogu.com",
LeaseDuration: &leaseDuration,
RenewDeadline: &renewDeadline,
}
controllerOpts, zapOpts := parseManagerFlags(flags, args, controllerOpts)
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&zapOpts)))
return controllerOpts
}
func parseManagerFlags(flags *flag.FlagSet, args []string, ctrlOpts ctrl.Options) (ctrl.Options, zap.Options) {
var metricsAddr string
var enableLeaderElection bool
var probeAddr string
flags.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
flags.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flags.BoolVar(&enableLeaderElection, "leader-elect", false,
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
zapOpts := zap.Options{
Development: config.IsStageDevelopment(),
}
zapOpts.BindFlags(flags)
// Ignore errors; flags is set to exit on errors
_ = flags.Parse(args)
ctrlOpts.Metrics = server.Options{BindAddress: metricsAddr}
ctrlOpts.HealthProbeBindAddress = probeAddr
ctrlOpts.LeaderElection = enableLeaderElection
return ctrlOpts, zapOpts
}
func configureReconcilers(ctx context.Context, k8sManager controllerManager, operatorConfig *config.OperatorConfig) error {
var recorder eventRecorder = k8sManager.GetEventRecorderFor("k8s-backup-operator")
k8sClientSet, err := kubernetes.NewForConfig(k8sManager.GetConfig())
if err != nil {
return fmt.Errorf("unable to create k8s clientset: %w", err)
}
ecosystemClientSet, err := ecosystem.NewClientSet(k8sManager.GetConfig(), k8sClientSet)
if err != nil {
return fmt.Errorf("unable to create ecosystem clientset: %w", err)
}
namespace, err := config.GetNamespace()
if err != nil {
return fmt.Errorf("failed to get namespace: %w", err)
}
configMapClient := ecosystemClientSet.CoreV1().ConfigMaps(namespace)
globalConfig := repository.NewGlobalConfigRepository(configMapClient)
err = syncBackupsWithProviders(ctx, operatorConfig, recorder, ecosystemClientSet)
if err != nil {
return fmt.Errorf("failed to sync backups with provider backups on startup: %w", err)
}
imageGetter := newAdditionalImageGetter(k8sClientSet, operatorConfig.Namespace)
operatorImage, err := imageGetter.ImageForKey(ctx, config.OperatorImageConfigmapNameKey)
if err != nil {
return fmt.Errorf("failed to get operator image: %w", err)
}
imageConfig := additionalimages.ImageConfig{OperatorImage: operatorImage}
additionalImageUpdater := newAdditionalImageUpdater(ecosystemClientSet, operatorConfig.Namespace, recorder)
err = additionalImageUpdater.Update(ctx, imageConfig)
if err != nil {
return fmt.Errorf("failed to update additional images in existing resources: %w", err)
}
requeueHandler := requeue.NewRequeueHandler(ecosystemClientSet, recorder, operatorConfig.Namespace)
cleanupManager := cleanup.NewManager(operatorConfig.Namespace, k8sManager.GetClient(), k8sClientSet)
restoreManager := restore.NewRestoreManager(
ecosystemClientSet,
operatorConfig.Namespace,
recorder,
cleanupManager,
)
if err = (restore.NewRestoreReconciler(ecosystemClientSet, recorder, operatorConfig.Namespace, restoreManager, requeueHandler)).SetupWithManager(k8sManager); err != nil {
return fmt.Errorf("unable to create restore controller: %w", err)
}
backupManager := backup.NewBackupManager(ecosystemClientSet, operatorConfig.Namespace, recorder, globalConfig)
if err = (backup.NewBackupReconciler(ecosystemClientSet, recorder, operatorConfig.Namespace, backupManager, requeueHandler)).SetupWithManager(k8sManager); err != nil {
return fmt.Errorf("unable to create backup controller: %w", err)
}
if err = backupschedule.NewReconciler(ecosystemClientSet, recorder, operatorConfig.Namespace, requeueHandler, imageConfig).SetupWithManager(k8sManager); err != nil {
return fmt.Errorf("unable to create backupSchedule controller: %w", err)
}
// +kubebuilder:scaffold:builder
return nil
}
func syncBackupsWithProviders(ctx context.Context, operatorConfig *config.OperatorConfig, recorder eventRecorder, ecosystemClientSet *ecosystem.ClientSet) error {
var errs []error
allProviders := provider.GetAll(ctx, operatorConfig.Namespace, recorder, ecosystemClientSet)
for _, prov := range allProviders {
err := prov.SyncBackups(ctx)
if err != nil {
errs = append(errs, err)
}
}
return errors.Join(errs...)
}
func addChecks(k8sManager controllerManager) error {
if err := k8sManager.AddHealthzCheck("healthz", healthz.Ping); err != nil {
return fmt.Errorf("unable to set up health check: %w", err)
}
if err := k8sManager.AddReadyzCheck("readyz", healthz.Ping); err != nil {
return fmt.Errorf("unable to set up ready check: %w", err)
}
return nil
}
func startK8sManager(ctx context.Context, k8sManager controllerManager) error {
logger := log.FromContext(ctx).WithName("k8s-manager-start")
logger.Info("starting manager")
if err := k8sManager.Start(ctrl.SetupSignalHandler()); err != nil {
return fmt.Errorf("problem running manager: %w", err)
}
return nil
}