From 8713c813f673929e959760c342e9be0aba3dfff9 Mon Sep 17 00:00:00 2001 From: Pooya Azarpour Date: Sat, 6 Apr 2024 13:40:33 +0330 Subject: [PATCH] [UPDATE] Formatting go files to old style (Remove idea customziation formatter) Signed-off-by: Pooya Azarpour --- api/v1/backend.go | 6 +- cmd/restic/integration_test.go | 44 +++--- operator/archivecontroller/executor.go | 110 ++++++--------- operator/backupcontroller/backup_utils.go | 87 ++++++------ operator/cfg/config.go | 2 + operator/checkcontroller/executor.go | 82 +++++------- operator/prunecontroller/executor.go | 91 ++++++------- operator/restorecontroller/executor.go | 140 ++++++++------------ operator/restorecontroller/executor_test.go | 49 +++---- restic/cli/restic.go | 2 +- restic/s3/client.go | 5 +- 11 files changed, 251 insertions(+), 367 deletions(-) diff --git a/api/v1/backend.go b/api/v1/backend.go index dd60cffab..27f152642 100644 --- a/api/v1/backend.go +++ b/api/v1/backend.go @@ -92,11 +92,7 @@ func IsNil(v interface{}) bool { return v == nil || (reflect.ValueOf(v).Kind() == reflect.Ptr && reflect.ValueOf(v).IsNil()) } -func addEnvVarFromSecret( - vars map[string]*corev1.EnvVarSource, - key string, - ref *corev1.SecretKeySelector, -) { +func addEnvVarFromSecret(vars map[string]*corev1.EnvVarSource, key string, ref *corev1.SecretKeySelector) { if ref != nil { vars[key] = &corev1.EnvVarSource{ SecretKeyRef: ref, diff --git a/cmd/restic/integration_test.go b/cmd/restic/integration_test.go index 9532ceea4..90d33dbd6 100644 --- a/cmd/restic/integration_test.go +++ b/cmd/restic/integration_test.go @@ -59,11 +59,9 @@ func newTestErrorChannel() chan error { func (w *webhookserver) runWebServer(t *testing.T) { mux := http.NewServeMux() - mux.HandleFunc( - "/", func(wr http.ResponseWriter, r *http.Request) { - w.jsonData, _ = io.ReadAll(r.Body) - }, - ) + mux.HandleFunc("/", func(wr http.ResponseWriter, r *http.Request) { + w.jsonData, _ = io.ReadAll(r.Body) + }) srv := &testServer{ Server: http.Server{ @@ -112,11 +110,9 @@ func initTest(t *testing.T) *testEnvironment { cleanupDirs(t) createTestFiles(t) - t.Cleanup( - func() { - cleanupDirs(t) - }, - ) + t.Cleanup(func() { + cleanupDirs(t) + }) webhook := startWebhookWebserver(t, ctx) s3client := connectToS3Server(t, ctx) @@ -144,12 +140,10 @@ func connectToS3Server(t *testing.T, ctx context.Context) *s3.Client { _ = s3client.DeleteBucket(ctx) t.Logf("Ensured that the bucket '%s' does not exist", repo) - t.Cleanup( - func() { - _ = s3client.DeleteBucket(ctx) - t.Logf("Removing the bucket '%s'", repo) - }, - ) + t.Cleanup(func() { + _ = s3client.DeleteBucket(ctx) + t.Logf("Removing the bucket '%s'", repo) + }) return s3client } @@ -157,17 +151,15 @@ func startWebhookWebserver(t *testing.T, ctx context.Context) *webhookserver { webhook := &webhookserver{} webhook.runWebServer(t) t.Logf("Started webserver on '%s'", webhook.srv.Addr) - t.Cleanup( - func() { - if webhook.srv == nil { - t.Log("Webserver not running.") - return - } + t.Cleanup(func() { + if webhook.srv == nil { + t.Log("Webserver not running.") + return + } - t.Logf("Stopping the webserver on '%s'", webhook.srv.Addr) - webhook.srv.Shutdown(ctx) - }, - ) + t.Logf("Stopping the webserver on '%s'", webhook.srv.Addr) + webhook.srv.Shutdown(ctx) + }) return webhook } diff --git a/operator/archivecontroller/executor.go b/operator/archivecontroller/executor.go index 02309048e..487c77345 100644 --- a/operator/archivecontroller/executor.go +++ b/operator/archivecontroller/executor.go @@ -3,14 +3,13 @@ package archivecontroller import ( "context" + "github.com/k8up-io/k8up/v2/operator/executor" + "github.com/k8up-io/k8up/v2/operator/utils" batchv1 "k8s.io/api/batch/v1" corev1 "k8s.io/api/core/v1" controllerruntime "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" - "github.com/k8up-io/k8up/v2/operator/executor" - "github.com/k8up-io/k8up/v2/operator/utils" - k8upv1 "github.com/k8up-io/k8up/v2/api/v1" "github.com/k8up-io/k8up/v2/operator/cfg" "github.com/k8up-io/k8up/v2/operator/job" @@ -48,33 +47,25 @@ func (a *ArchiveExecutor) Execute(ctx context.Context) error { batchJob.Name = a.jobName() batchJob.Namespace = a.archive.Namespace - _, err := controllerutil.CreateOrUpdate( - ctx, a.Client, batchJob, func() error { - mutateErr := job.MutateBatchJob(batchJob, a.archive, a.Config) - if mutateErr != nil { - return mutateErr - } + _, err := controllerutil.CreateOrUpdate(ctx, a.Client, batchJob, func() error { + mutateErr := job.MutateBatchJob(batchJob, a.archive, a.Config) + if mutateErr != nil { + return mutateErr + } - batchJob.Spec.Template.Spec.Containers[0].Env = a.setupEnvVars(ctx, a.archive) - a.archive.Spec.AppendEnvFromToContainer(&batchJob.Spec.Template.Spec.Containers[0]) - batchJob.Spec.Template.Spec.Containers[0].VolumeMounts = a.attachMoreVolumeMounts() - batchJob.Spec.Template.Spec.Volumes = a.attachMoreVolumes() + batchJob.Spec.Template.Spec.Containers[0].Env = a.setupEnvVars(ctx, a.archive) + a.archive.Spec.AppendEnvFromToContainer(&batchJob.Spec.Template.Spec.Containers[0]) + batchJob.Spec.Template.Spec.Containers[0].VolumeMounts = a.attachMoreVolumeMounts() + batchJob.Spec.Template.Spec.Volumes = a.attachMoreVolumes() - args, argsErr := a.setupArgs() - batchJob.Spec.Template.Spec.Containers[0].Args = args + args, argsErr := a.setupArgs() + batchJob.Spec.Template.Spec.Containers[0].Args = args - return argsErr - }, - ) + return argsErr + }) if err != nil { log.Error(err, "could not create job") - a.SetConditionFalseWithMessage( - ctx, - k8upv1.ConditionReady, - k8upv1.ReasonCreationFailed, - "could not create job: %v", - err, - ) + a.SetConditionFalseWithMessage(ctx, k8upv1.ConditionReady, k8upv1.ReasonCreationFailed, "could not create job: %v", err) return err } @@ -96,10 +87,7 @@ func (a *ArchiveExecutor) setupArgs() ([]string, error) { return args, nil } -func (a *ArchiveExecutor) setupEnvVars( - ctx context.Context, - archive *k8upv1.Archive, -) []corev1.EnvVar { +func (a *ArchiveExecutor) setupEnvVars(ctx context.Context, archive *k8upv1.Archive) []corev1.EnvVar { log := controllerruntime.LoggerFrom(ctx) vars := executor.NewEnvVarConverter() @@ -129,14 +117,7 @@ func (a *ArchiveExecutor) setupEnvVars( err := vars.Merge(executor.DefaultEnv(a.Obj.GetNamespace())) if err != nil { - log.Error( - err, - "error while merging the environment variables", - "name", - a.Obj.GetName(), - "namespace", - a.Obj.GetNamespace(), - ) + log.Error(err, "error while merging the environment variables", "name", a.Obj.GetName(), "namespace", a.Obj.GetNamespace()) } return vars.Convert() @@ -154,35 +135,28 @@ func (a *ArchiveExecutor) appendOptionsArgs() []string { args = append(args, []string{"-caCert", a.archive.Spec.Backend.Options.CACert}...) } if a.archive.Spec.Backend.Options.ClientCert != "" && a.archive.Spec.Backend.Options.ClientKey != "" { - args = append( - args, - []string{ - "-clientCert", - a.archive.Spec.Backend.Options.ClientCert, - "-clientKey", - a.archive.Spec.Backend.Options.ClientKey, - }..., - ) + addMoreArgs := []string{ + "-clientCert", + a.archive.Spec.Backend.Options.ClientCert, + "-clientKey", + a.archive.Spec.Backend.Options.ClientKey, + } + args = append(args, addMoreArgs...) } } if a.archive.Spec.RestoreSpec != nil && a.archive.Spec.RestoreMethod.Options != nil { if a.archive.Spec.RestoreMethod.Options.CACert != "" { - args = append( - args, - []string{"-restoreCaCert", a.archive.Spec.RestoreMethod.Options.CACert}..., - ) + args = append(args, []string{"-restoreCaCert", a.archive.Spec.RestoreMethod.Options.CACert}...) } if a.archive.Spec.RestoreMethod.Options.ClientCert != "" && a.archive.Spec.RestoreMethod.Options.ClientKey != "" { - args = append( - args, - []string{ - "-restoreClientCert", - a.archive.Spec.RestoreMethod.Options.ClientCert, - "-restoreClientKey", - a.archive.Spec.RestoreMethod.Options.ClientKey, - }..., - ) + addMoreArgs := []string{ + "-restoreClientCert", + a.archive.Spec.RestoreMethod.Options.ClientCert, + "-restoreClientKey", + a.archive.Spec.RestoreMethod.Options.ClientKey, + } + args = append(args, addMoreArgs...) } } @@ -215,12 +189,11 @@ func (a *ArchiveExecutor) attachMoreVolumes() []corev1.Volume { continue } - moreVolumes = append( - moreVolumes, corev1.Volume{ - Name: vol.Name, - VolumeSource: volumeSource, - }, - ) + addVolume := corev1.Volume{ + Name: vol.Name, + VolumeSource: volumeSource, + } + moreVolumes = append(moreVolumes, addVolume) } return moreVolumes @@ -253,8 +226,11 @@ func (a *ArchiveExecutor) attachMoreVolumeMounts() []corev1.VolumeMount { } } - ku8pVolumeMount := corev1.VolumeMount{Name: _dataDirName, MountPath: cfg.Config.PodVarDir} - volumeMount = append(volumeMount, ku8pVolumeMount) + addVolumeMount := corev1.VolumeMount{ + Name: _dataDirName, + MountPath: cfg.Config.PodVarDir, + } + volumeMount = append(volumeMount, addVolumeMount) return volumeMount } diff --git a/operator/backupcontroller/backup_utils.go b/operator/backupcontroller/backup_utils.go index 796ee4527..03828cb72 100644 --- a/operator/backupcontroller/backup_utils.go +++ b/operator/backupcontroller/backup_utils.go @@ -3,16 +3,14 @@ package backupcontroller import ( "context" "fmt" - "path" - + "github.com/k8up-io/k8up/v2/operator/executor" + "github.com/k8up-io/k8up/v2/operator/utils" corev1 "k8s.io/api/core/v1" rbacv1 "k8s.io/api/rbac/v1" + "path" controllerruntime "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" - "github.com/k8up-io/k8up/v2/operator/executor" - "github.com/k8up-io/k8up/v2/operator/utils" - "github.com/k8up-io/k8up/v2/operator/cfg" ) @@ -34,10 +32,7 @@ func (b *BackupExecutor) newVolumeMounts(claims []corev1.Volume) []corev1.Volume return mounts } -func containsAccessMode( - s []corev1.PersistentVolumeAccessMode, - e corev1.PersistentVolumeAccessMode, -) bool { +func containsAccessMode(s []corev1.PersistentVolumeAccessMode, e corev1.PersistentVolumeAccessMode) bool { for _, a := range s { if a == e { return true @@ -50,11 +45,9 @@ func (b *BackupExecutor) createServiceAccountAndBinding(ctx context.Context) err sa := &corev1.ServiceAccount{} sa.Name = cfg.Config.ServiceAccount sa.Namespace = b.backup.Namespace - _, err := controllerruntime.CreateOrUpdate( - ctx, b.Config.Client, sa, func() error { - return nil - }, - ) + _, err := controllerruntime.CreateOrUpdate(ctx, b.Config.Client, sa, func() error { + return nil + }) if err != nil { return err } @@ -65,23 +58,21 @@ func (b *BackupExecutor) createServiceAccountAndBinding(ctx context.Context) err roleBinding := &rbacv1.RoleBinding{} roleBinding.Name = cfg.Config.PodExecRoleName + "-namespaced" roleBinding.Namespace = b.backup.Namespace - _, err = controllerruntime.CreateOrUpdate( - ctx, b.Config.Client, roleBinding, func() error { - roleBinding.Subjects = []rbacv1.Subject{ - { - Kind: "ServiceAccount", - Namespace: b.backup.Namespace, - Name: sa.Name, - }, - } - roleBinding.RoleRef = rbacv1.RoleRef{ - Kind: "ClusterRole", - Name: "k8up-executor", - APIGroup: "rbac.authorization.k8s.io", - } - return nil - }, - ) + _, err = controllerruntime.CreateOrUpdate(ctx, b.Config.Client, roleBinding, func() error { + roleBinding.Subjects = []rbacv1.Subject{ + { + Kind: "ServiceAccount", + Namespace: b.backup.Namespace, + Name: sa.Name, + }, + } + roleBinding.RoleRef = rbacv1.RoleRef{ + Kind: "ClusterRole", + Name: "k8up-executor", + APIGroup: "rbac.authorization.k8s.io", + } + return nil + }) return err } @@ -145,12 +136,11 @@ func (b *BackupExecutor) attachMoreVolumes() []corev1.Volume { continue } - moreVolumes = append( - moreVolumes, corev1.Volume{ - Name: vol.Name, - VolumeSource: volumeSource, - }, - ) + addVolume := corev1.Volume{ + Name: vol.Name, + VolumeSource: volumeSource, + } + moreVolumes = append(moreVolumes, addVolume) } return moreVolumes @@ -163,8 +153,11 @@ func (b *BackupExecutor) attachMoreVolumeMounts() []corev1.VolumeMount { volumeMount = *b.backup.Spec.Backend.VolumeMounts } - ku8pVolumeMount := corev1.VolumeMount{Name: _dataDirName, MountPath: cfg.Config.PodVarDir} - volumeMount = append(volumeMount, ku8pVolumeMount) + addVolumeMount := corev1.VolumeMount{ + Name: _dataDirName, + MountPath: cfg.Config.PodVarDir, + } + volumeMount = append(volumeMount, addVolumeMount) return volumeMount } @@ -180,15 +173,13 @@ func (b *BackupExecutor) appendOptionsArgs() []string { args = append(args, []string{"-caCert", b.backup.Spec.Backend.Options.CACert}...) } if b.backup.Spec.Backend.Options.ClientCert != "" && b.backup.Spec.Backend.Options.ClientKey != "" { - args = append( - args, - []string{ - "-clientCert", - b.backup.Spec.Backend.Options.ClientCert, - "-clientKey", - b.backup.Spec.Backend.Options.ClientKey, - }..., - ) + addMoreArgs := []string{ + "-clientCert", + b.backup.Spec.Backend.Options.ClientCert, + "-clientKey", + b.backup.Spec.Backend.Options.ClientKey, + } + args = append(args, addMoreArgs...) } return args diff --git a/operator/cfg/config.go b/operator/cfg/config.go index afa52a96f..bf76d5665 100644 --- a/operator/cfg/config.go +++ b/operator/cfg/config.go @@ -2,6 +2,7 @@ package cfg import ( "fmt" + corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" ) @@ -81,6 +82,7 @@ type Configuration struct { EnableLeaderElection bool OperatorNamespace string + // the var data dir for read/write k8up data or temp file in the backup pod PodVarDir string // Allows to pass options to restic, see https://restic.readthedocs.io/en/stable/manual_rest.html?highlight=--option#usage-help diff --git a/operator/checkcontroller/executor.go b/operator/checkcontroller/executor.go index a97e10f1d..d228d2a8a 100644 --- a/operator/checkcontroller/executor.go +++ b/operator/checkcontroller/executor.go @@ -3,14 +3,12 @@ package checkcontroller import ( "context" + "github.com/k8up-io/k8up/v2/operator/executor" "github.com/k8up-io/k8up/v2/operator/utils" - batchv1 "k8s.io/api/batch/v1" corev1 "k8s.io/api/core/v1" controllerruntime "sigs.k8s.io/controller-runtime" - "github.com/k8up-io/k8up/v2/operator/executor" - k8upv1 "github.com/k8up-io/k8up/v2/api/v1" "github.com/k8up-io/k8up/v2/operator/cfg" "github.com/k8up-io/k8up/v2/operator/job" @@ -48,33 +46,26 @@ func (c *CheckExecutor) Execute(ctx context.Context) error { batchJob.Name = c.jobName() batchJob.Namespace = c.check.Namespace - _, err := controllerruntime.CreateOrUpdate( - ctx, c.Client, batchJob, func() error { - mutateErr := job.MutateBatchJob(batchJob, c.check, c.Config) - if mutateErr != nil { - return mutateErr - } + _, err := controllerruntime.CreateOrUpdate(ctx, c.Client, batchJob, func() error { + mutateErr := job.MutateBatchJob(batchJob, c.check, c.Config) + if mutateErr != nil { + return mutateErr + } - batchJob.Spec.Template.Spec.Containers[0].Env = c.setupEnvVars(ctx) - c.check.Spec.AppendEnvFromToContainer(&batchJob.Spec.Template.Spec.Containers[0]) - batchJob.Spec.Template.Spec.Containers[0].VolumeMounts = c.attachMoreVolumeMounts() - batchJob.Spec.Template.Spec.Volumes = c.attachMoreVolumes() - batchJob.Labels[job.K8upExclusive] = "true" + batchJob.Spec.Template.Spec.Containers[0].Env = c.setupEnvVars(ctx) + c.check.Spec.AppendEnvFromToContainer(&batchJob.Spec.Template.Spec.Containers[0]) + batchJob.Spec.Template.Spec.Containers[0].VolumeMounts = c.attachMoreVolumeMounts() + batchJob.Spec.Template.Spec.Volumes = c.attachMoreVolumes() + batchJob.Labels[job.K8upExclusive] = "true" - args, argsErr := c.setupArgs() - batchJob.Spec.Template.Spec.Containers[0].Args = args + args, argsErr := c.setupArgs() + batchJob.Spec.Template.Spec.Containers[0].Args = args - return argsErr - }, + return argsErr + }, ) if err != nil { - c.SetConditionFalseWithMessage( - ctx, - k8upv1.ConditionReady, - k8upv1.ReasonCreationFailed, - "could not create job: %v", - err, - ) + c.SetConditionFalseWithMessage(ctx, k8upv1.ConditionReady, k8upv1.ReasonCreationFailed, "could not create job: %v", err) return err } c.SetStarted(ctx, "the job '%v/%v' was created", batchJob.Namespace, batchJob.Name) @@ -109,14 +100,7 @@ func (c *CheckExecutor) setupEnvVars(ctx context.Context) []corev1.EnvVar { err := vars.Merge(executor.DefaultEnv(c.Obj.GetNamespace())) if err != nil { - log.Error( - err, - "error while merging the environment variables", - "name", - c.Obj.GetName(), - "namespace", - c.Obj.GetNamespace(), - ) + log.Error(err, "error while merging the environment variables", "name", c.Obj.GetName(), "namespace", c.Obj.GetNamespace()) } return vars.Convert() @@ -136,15 +120,13 @@ func (c *CheckExecutor) appendOptionsArgs() []string { args = append(args, []string{"-caCert", c.check.Spec.Backend.Options.CACert}...) } if c.check.Spec.Backend.Options.ClientCert != "" && c.check.Spec.Backend.Options.ClientKey != "" { - args = append( - args, - []string{ - "-clientCert", - c.check.Spec.Backend.Options.ClientCert, - "-clientKey", - c.check.Spec.Backend.Options.ClientKey, - }..., - ) + addMoreArgs := []string{ + "-clientCert", + c.check.Spec.Backend.Options.ClientCert, + "-clientKey", + c.check.Spec.Backend.Options.ClientKey, + } + args = append(args, addMoreArgs...) } return args @@ -176,12 +158,11 @@ func (c *CheckExecutor) attachMoreVolumes() []corev1.Volume { continue } - moreVolumes = append( - moreVolumes, corev1.Volume{ - Name: vol.Name, - VolumeSource: volumeSource, - }, - ) + addVolume := corev1.Volume{ + Name: vol.Name, + VolumeSource: volumeSource, + } + moreVolumes = append(moreVolumes, addVolume) } return moreVolumes @@ -194,7 +175,10 @@ func (c *CheckExecutor) attachMoreVolumeMounts() []corev1.VolumeMount { volumeMount = *c.check.Spec.Backend.VolumeMounts } - ku8pVolumeMount := corev1.VolumeMount{Name: _dataDirName, MountPath: cfg.Config.PodVarDir} + ku8pVolumeMount := corev1.VolumeMount{ + Name: _dataDirName, + MountPath: cfg.Config.PodVarDir, + } volumeMount = append(volumeMount, ku8pVolumeMount) return volumeMount diff --git a/operator/prunecontroller/executor.go b/operator/prunecontroller/executor.go index 5caded3cc..7c71ced5a 100644 --- a/operator/prunecontroller/executor.go +++ b/operator/prunecontroller/executor.go @@ -5,15 +5,13 @@ import ( "strconv" "strings" + "github.com/k8up-io/k8up/v2/operator/executor" "github.com/k8up-io/k8up/v2/operator/utils" - batchv1 "k8s.io/api/batch/v1" corev1 "k8s.io/api/core/v1" controllerruntime "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" - "github.com/k8up-io/k8up/v2/operator/executor" - k8upv1 "github.com/k8up-io/k8up/v2/api/v1" "github.com/k8up-io/k8up/v2/operator/cfg" "github.com/k8up-io/k8up/v2/operator/job" @@ -41,34 +39,26 @@ func (p *PruneExecutor) Execute(ctx context.Context) error { batchJob.Name = p.jobName() batchJob.Namespace = p.prune.Namespace - _, err := controllerutil.CreateOrUpdate( - ctx, p.Client, batchJob, func() error { - mutateErr := job.MutateBatchJob(batchJob, p.prune, p.Config) - if mutateErr != nil { - return mutateErr - } - - batchJob.Spec.Template.Spec.Containers[0].Env = p.setupEnvVars(ctx, p.prune) - batchJob.Spec.Template.Spec.ServiceAccountName = cfg.Config.ServiceAccount - p.prune.Spec.AppendEnvFromToContainer(&batchJob.Spec.Template.Spec.Containers[0]) - batchJob.Spec.Template.Spec.Containers[0].VolumeMounts = p.attachMoreVolumeMounts() - batchJob.Spec.Template.Spec.Volumes = p.attachMoreVolumes() - batchJob.Labels[job.K8upExclusive] = "true" - - args, argsErr := p.setupArgs() - batchJob.Spec.Template.Spec.Containers[0].Args = args - - return argsErr - }, - ) + _, err := controllerutil.CreateOrUpdate(ctx, p.Client, batchJob, func() error { + mutateErr := job.MutateBatchJob(batchJob, p.prune, p.Config) + if mutateErr != nil { + return mutateErr + } + + batchJob.Spec.Template.Spec.Containers[0].Env = p.setupEnvVars(ctx, p.prune) + batchJob.Spec.Template.Spec.ServiceAccountName = cfg.Config.ServiceAccount + p.prune.Spec.AppendEnvFromToContainer(&batchJob.Spec.Template.Spec.Containers[0]) + batchJob.Spec.Template.Spec.Containers[0].VolumeMounts = p.attachMoreVolumeMounts() + batchJob.Spec.Template.Spec.Volumes = p.attachMoreVolumes() + batchJob.Labels[job.K8upExclusive] = "true" + + args, argsErr := p.setupArgs() + batchJob.Spec.Template.Spec.Containers[0].Args = args + + return argsErr + }) if err != nil { - p.SetConditionFalseWithMessage( - ctx, - k8upv1.ConditionReady, - k8upv1.ReasonCreationFailed, - "could not create job: %v", - err, - ) + p.SetConditionFalseWithMessage(ctx, k8upv1.ConditionReady, k8upv1.ReasonCreationFailed, "could not create job: %v", err) return err } @@ -146,14 +136,7 @@ func (p *PruneExecutor) setupEnvVars(ctx context.Context, prune *k8upv1.Prune) [ err := vars.Merge(executor.DefaultEnv(p.Obj.GetNamespace())) if err != nil { - log.Error( - err, - "error while merging the environment variables", - "name", - p.Obj.GetName(), - "namespace", - p.Obj.GetNamespace(), - ) + log.Error(err, "error while merging the environment variables", "name", p.Obj.GetName(), "namespace", p.Obj.GetNamespace()) } return vars.Convert() @@ -169,15 +152,13 @@ func (p *PruneExecutor) appendOptionsArgs() []string { args = append(args, []string{"-caCert", p.prune.Spec.Backend.Options.CACert}...) } if p.prune.Spec.Backend.Options.ClientCert != "" && p.prune.Spec.Backend.Options.ClientKey != "" { - args = append( - args, - []string{ - "-clientCert", - p.prune.Spec.Backend.Options.ClientCert, - "-clientKey", - p.prune.Spec.Backend.Options.ClientKey, - }..., - ) + addMoreArgs := []string{ + "-clientCert", + p.prune.Spec.Backend.Options.ClientCert, + "-clientKey", + p.prune.Spec.Backend.Options.ClientKey, + } + args = append(args, addMoreArgs...) } return args @@ -209,12 +190,11 @@ func (p *PruneExecutor) attachMoreVolumes() []corev1.Volume { continue } - moreVolumes = append( - moreVolumes, corev1.Volume{ - Name: vol.Name, - VolumeSource: volumeSource, - }, - ) + addVolume := corev1.Volume{ + Name: vol.Name, + VolumeSource: volumeSource, + } + moreVolumes = append(moreVolumes, addVolume) } return moreVolumes @@ -227,7 +207,10 @@ func (p *PruneExecutor) attachMoreVolumeMounts() []corev1.VolumeMount { volumeMount = *p.prune.Spec.Backend.VolumeMounts } - ku8pVolumeMount := corev1.VolumeMount{Name: _dataDirName, MountPath: cfg.Config.PodVarDir} + ku8pVolumeMount := corev1.VolumeMount{ + Name: _dataDirName, + MountPath: cfg.Config.PodVarDir, + } volumeMount = append(volumeMount, ku8pVolumeMount) return volumeMount diff --git a/operator/restorecontroller/executor.go b/operator/restorecontroller/executor.go index c86c29f14..2cd619a2a 100644 --- a/operator/restorecontroller/executor.go +++ b/operator/restorecontroller/executor.go @@ -5,15 +5,13 @@ import ( "errors" "fmt" + "github.com/k8up-io/k8up/v2/operator/executor" "github.com/k8up-io/k8up/v2/operator/utils" - batchv1 "k8s.io/api/batch/v1" corev1 "k8s.io/api/core/v1" controllerruntime "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" - "github.com/k8up-io/k8up/v2/operator/executor" - k8upv1 "github.com/k8up-io/k8up/v2/api/v1" "github.com/k8up-io/k8up/v2/operator/cfg" "github.com/k8up-io/k8up/v2/operator/job" @@ -53,13 +51,7 @@ func (r *RestoreExecutor) Execute(ctx context.Context) error { restoreJob, err := r.createRestoreObject(ctx, restore) if err != nil { log.Error(err, "unable to create or update restore object") - r.SetConditionFalseWithMessage( - ctx, - k8upv1.ConditionReady, - k8upv1.ReasonCreationFailed, - "unable to create restore object: %v", - err, - ) + r.SetConditionFalseWithMessage(ctx, k8upv1.ConditionReady, k8upv1.ReasonCreationFailed, "unable to create restore object: %v", err) return nil } @@ -79,28 +71,23 @@ func (r *RestoreExecutor) createRestoreObject( batchJob := &batchv1.Job{} batchJob.Name = r.jobName() batchJob.Namespace = restore.Namespace - _, err := controllerutil.CreateOrUpdate( - ctx, r.Client, batchJob, func() error { - mutateErr := job.MutateBatchJob(batchJob, restore, r.Config) - if mutateErr != nil { - return mutateErr - } - batchJob.Labels[job.K8upExclusive] = "true" - batchJob.Spec.Template.Spec.Containers[0].Env = r.setupEnvVars(ctx, restore) - restore.Spec.AppendEnvFromToContainer(&batchJob.Spec.Template.Spec.Containers[0]) - - volumes, volumeMounts := r.volumeConfig(restore) - batchJob.Spec.Template.Spec.Volumes = append(volumes, r.attachMoreVolumes()...) - batchJob.Spec.Template.Spec.Containers[0].VolumeMounts = append( - volumeMounts, - r.attachMoreVolumeMounts()..., - ) - - args, argsErr := r.setupArgs(restore) - batchJob.Spec.Template.Spec.Containers[0].Args = args - return argsErr - }, - ) + _, err := controllerutil.CreateOrUpdate(ctx, r.Client, batchJob, func() error { + mutateErr := job.MutateBatchJob(batchJob, restore, r.Config) + if mutateErr != nil { + return mutateErr + } + batchJob.Labels[job.K8upExclusive] = "true" + batchJob.Spec.Template.Spec.Containers[0].Env = r.setupEnvVars(ctx, restore) + restore.Spec.AppendEnvFromToContainer(&batchJob.Spec.Template.Spec.Containers[0]) + + volumes, volumeMounts := r.volumeConfig(restore) + batchJob.Spec.Template.Spec.Volumes = append(volumes, r.attachMoreVolumes()...) + batchJob.Spec.Template.Spec.Containers[0].VolumeMounts = append(volumeMounts, r.attachMoreVolumeMounts()...) + + args, argsErr := r.setupArgs(restore) + batchJob.Spec.Template.Spec.Containers[0].Args = args + return argsErr + }) return batchJob, err } @@ -129,11 +116,7 @@ func (r *RestoreExecutor) setupArgs(restore *k8upv1.Restore) ([]string, error) { case restore.Spec.RestoreMethod.S3 != nil: args = append(args, "-restoreType", "s3") default: - return nil, fmt.Errorf( - "undefined restore method (-restoreType) on '%v/%v'", - restore.Namespace, - restore.Name, - ) + return nil, fmt.Errorf("undefined restore method (-restoreType) on '%v/%v'", restore.Namespace, restore.Name) } args = append(args, r.appendOptionsArgs()...) @@ -147,15 +130,13 @@ func (r *RestoreExecutor) volumeConfig(restore *k8upv1.Restore) ( ) { volumes := make([]corev1.Volume, 0) if restore.Spec.RestoreMethod.S3 == nil { - volumes = append( - volumes, - corev1.Volume{ - Name: restore.Spec.RestoreMethod.Folder.ClaimName, - VolumeSource: corev1.VolumeSource{ - PersistentVolumeClaim: restore.Spec.RestoreMethod.Folder.PersistentVolumeClaimVolumeSource, - }, + addVolume := corev1.Volume{ + Name: restore.Spec.RestoreMethod.Folder.ClaimName, + VolumeSource: corev1.VolumeSource{ + PersistentVolumeClaim: restore.Spec.RestoreMethod.Folder.PersistentVolumeClaimVolumeSource, }, - ) + } + volumes = append(volumes, addVolume) } mounts := make([]corev1.VolumeMount, 0) @@ -170,10 +151,7 @@ func (r *RestoreExecutor) volumeConfig(restore *k8upv1.Restore) ( return volumes, mounts } -func (r *RestoreExecutor) setupEnvVars( - ctx context.Context, - restore *k8upv1.Restore, -) []corev1.EnvVar { +func (r *RestoreExecutor) setupEnvVars(ctx context.Context, restore *k8upv1.Restore) []corev1.EnvVar { log := controllerruntime.LoggerFrom(ctx) vars := executor.NewEnvVarConverter() @@ -199,14 +177,7 @@ func (r *RestoreExecutor) setupEnvVars( err := vars.Merge(executor.DefaultEnv(r.Obj.GetNamespace())) if err != nil { - log.Error( - err, - "error while merging the environment variables", - "name", - r.Obj.GetName(), - "namespace", - r.Obj.GetNamespace(), - ) + log.Error(err, "error while merging the environment variables", "name", r.Obj.GetName(), "namespace", r.Obj.GetNamespace()) } return vars.Convert() @@ -220,35 +191,28 @@ func (r *RestoreExecutor) appendOptionsArgs() []string { args = append(args, []string{"--caCert", r.restore.Spec.Backend.Options.CACert}...) } if r.restore.Spec.Backend.Options.ClientCert != "" && r.restore.Spec.Backend.Options.ClientKey != "" { - args = append( - args, - []string{ - "--clientCert", - r.restore.Spec.Backend.Options.ClientCert, - "--clientKey", - r.restore.Spec.Backend.Options.ClientKey, - }..., - ) + addMoreArgs := []string{ + "--clientCert", + r.restore.Spec.Backend.Options.ClientCert, + "--clientKey", + r.restore.Spec.Backend.Options.ClientKey, + } + args = append(args, addMoreArgs...) } } if r.restore.Spec.RestoreMethod != nil && r.restore.Spec.RestoreMethod.Options != nil { if r.restore.Spec.RestoreMethod.Options.CACert != "" { - args = append( - args, - []string{"--restoreCaCert", r.restore.Spec.RestoreMethod.Options.CACert}..., - ) + args = append(args, []string{"--restoreCaCert", r.restore.Spec.RestoreMethod.Options.CACert}...) } if r.restore.Spec.RestoreMethod.Options.ClientCert != "" && r.restore.Spec.RestoreMethod.Options.ClientKey != "" { - args = append( - args, - []string{ - "--restoreClientCert", - r.restore.Spec.RestoreMethod.Options.ClientCert, - "--restoreClientKey", - r.restore.Spec.RestoreMethod.Options.ClientKey, - }..., - ) + addMoreArgs := []string{ + "--restoreClientCert", + r.restore.Spec.RestoreMethod.Options.ClientCert, + "--restoreClientKey", + r.restore.Spec.RestoreMethod.Options.ClientKey, + } + args = append(args, addMoreArgs...) } } @@ -281,12 +245,11 @@ func (r *RestoreExecutor) attachMoreVolumes() []corev1.Volume { continue } - moreVolumes = append( - moreVolumes, corev1.Volume{ - Name: vol.Name, - VolumeSource: volumeSource, - }, - ) + addVolume := corev1.Volume{ + Name: vol.Name, + VolumeSource: volumeSource, + } + moreVolumes = append(moreVolumes, addVolume) } return moreVolumes @@ -319,8 +282,11 @@ func (r *RestoreExecutor) attachMoreVolumeMounts() []corev1.VolumeMount { } } - ku8pVolumeMount := corev1.VolumeMount{Name: _dataDirName, MountPath: cfg.Config.PodVarDir} - volumeMount = append(volumeMount, ku8pVolumeMount) + addVolumeMount := corev1.VolumeMount{ + Name: _dataDirName, + MountPath: cfg.Config.PodVarDir, + } + volumeMount = append(volumeMount, addVolumeMount) return volumeMount } diff --git a/operator/restorecontroller/executor_test.go b/operator/restorecontroller/executor_test.go index 454735886..6be32d384 100644 --- a/operator/restorecontroller/executor_test.go +++ b/operator/restorecontroller/executor_test.go @@ -4,12 +4,11 @@ import ( "context" "testing" + k8upv1 "github.com/k8up-io/k8up/v2/api/v1" + "github.com/k8up-io/k8up/v2/operator/job" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" corev1 "k8s.io/api/core/v1" - - k8upv1 "github.com/k8up-io/k8up/v2/api/v1" - "github.com/k8up-io/k8up/v2/operator/job" ) type PVCExpectation struct { @@ -132,17 +131,15 @@ func TestRestore_setupEnvVars(t *testing.T) { } for name, tt := range tests { - t.Run( - name, func(t *testing.T) { - e := NewRestoreExecutor(*newConfig()) - envVars := e.setupEnvVars(context.TODO(), tt.GivenResource) + t.Run(name, func(t *testing.T) { + e := NewRestoreExecutor(*newConfig()) + envVars := e.setupEnvVars(context.TODO(), tt.GivenResource) - actualEnvVars, actualSecretKeyRefs := extractVarsAndSecretRefs(envVars) + actualEnvVars, actualSecretKeyRefs := extractVarsAndSecretRefs(envVars) - assert.Equal(t, actualEnvVars, tt.ExpectedEnvVars) - assert.Equal(t, actualSecretKeyRefs, tt.ExpectedSecretKeyRefs) - }, - ) + assert.Equal(t, actualEnvVars, tt.ExpectedEnvVars) + assert.Equal(t, actualSecretKeyRefs, tt.ExpectedSecretKeyRefs) + }) } } @@ -186,15 +183,13 @@ func TestRestore_volumeConfig(t *testing.T) { } for name, tt := range tests { - t.Run( - name, func(t *testing.T) { - e := NewRestoreExecutor(*newConfig()) - volumes, mounts := e.volumeConfig(tt.GivenResource) + t.Run(name, func(t *testing.T) { + e := NewRestoreExecutor(*newConfig()) + volumes, mounts := e.volumeConfig(tt.GivenResource) - assertVolumes(t, tt.ExpectedPVC, volumes) - assertVolumeMounts(t, tt.ExpectedVolumeMount, mounts) - }, - ) + assertVolumes(t, tt.ExpectedPVC, volumes) + assertVolumeMounts(t, tt.ExpectedVolumeMount, mounts) + }) } } @@ -252,14 +247,12 @@ func TestRestore_args(t *testing.T) { } for name, tt := range tests { - t.Run( - name, func(t *testing.T) { - e := NewRestoreExecutor(*newConfig()) - args, err := e.setupArgs(tt.GivenResource) + t.Run(name, func(t *testing.T) { + e := NewRestoreExecutor(*newConfig()) + args, err := e.setupArgs(tt.GivenResource) - require.NoError(t, err) - assert.Equal(t, tt.ExpectedArgs, args) - }, - ) + require.NoError(t, err) + assert.Equal(t, tt.ExpectedArgs, args) + }) } } diff --git a/restic/cli/restic.go b/restic/cli/restic.go index 12aa8bf26..c65770ad7 100644 --- a/restic/cli/restic.go +++ b/restic/cli/restic.go @@ -2,13 +2,13 @@ package cli import ( "context" - "github.com/k8up-io/k8up/v2/operator/utils" "path" "path/filepath" "strings" "github.com/go-logr/logr" + "github.com/k8up-io/k8up/v2/operator/utils" "github.com/k8up-io/k8up/v2/restic/cfg" "github.com/k8up-io/k8up/v2/restic/dto" ) diff --git a/restic/s3/client.go b/restic/s3/client.go index d83a86509..b65ffa1d3 100644 --- a/restic/s3/client.go +++ b/restic/s3/client.go @@ -5,13 +5,14 @@ import ( "crypto/tls" "crypto/x509" "fmt" - "github.com/minio/minio-go/v7" - "github.com/minio/minio-go/v7/pkg/credentials" "io" "net/http" "net/url" "os" "strings" + + "github.com/minio/minio-go/v7" + "github.com/minio/minio-go/v7/pkg/credentials" ) // Client wraps the minio s3 client