Skip to content

Commit

Permalink
[UPDATE] Formatting go files to old style (Remove idea customziation …
Browse files Browse the repository at this point in the history
…formatter)

Signed-off-by: Pooya Azarpour <pooya_azarpour@yahoo.com>
  • Loading branch information
poyaz committed Apr 6, 2024
1 parent f6b0f12 commit 8713c81
Show file tree
Hide file tree
Showing 11 changed files with 251 additions and 367 deletions.
6 changes: 1 addition & 5 deletions api/v1/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
44 changes: 18 additions & 26 deletions cmd/restic/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -144,30 +140,26 @@ 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
}

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
}

Expand Down
110 changes: 43 additions & 67 deletions operator/archivecontroller/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Expand All @@ -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()

Expand Down Expand Up @@ -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()
Expand All @@ -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...)
}
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Loading

0 comments on commit 8713c81

Please sign in to comment.