Skip to content

Commit

Permalink
[UPDATE] Generating new crd according to adding VolumeMounts to Backe…
Browse files Browse the repository at this point in the history
…ndSpec and RestoreMethodSpec

Also these changes appends:
- Running linter
- Fixing check null pointer error if BackendSpec or Volume of Spec is null
- Fixing check add duplicate VolumeMount in archive and restore API
- Refactoring setupArgs

Signed-off-by: poyaz <pooya_azarpour@yahoo.com>
  • Loading branch information
poyaz committed Mar 22, 2024
1 parent a0f78d7 commit c42e748
Show file tree
Hide file tree
Showing 5 changed files with 347 additions and 220 deletions.
123 changes: 80 additions & 43 deletions operator/archivecontroller/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ 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 @@ -46,25 +48,33 @@ 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 @@ -77,19 +87,19 @@ func (a *ArchiveExecutor) jobName() string {
}

func (a *ArchiveExecutor) setupArgs() ([]string, error) {
args := a.appendOptionsArgs()

args = append(args, []string{"-archive", "-restoreType", "s3"}...)
if a.archive.Spec.RestoreSpec != nil {
if len(a.archive.Spec.RestoreSpec.Tags) > 0 {
args = append(args, executor.BuildTagArgs(a.archive.Spec.RestoreSpec.Tags)...)
}
args := []string{"-varDir", cfg.Config.PodVarDir, "-archive", "-restoreType", "s3"}
if a.archive.Spec.RestoreSpec != nil && len(a.archive.Spec.RestoreSpec.Tags) > 0 {
args = append(args, executor.BuildTagArgs(a.archive.Spec.RestoreSpec.Tags)...)
}
args = append(args, a.appendOptionsArgs()...)

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 @@ -119,7 +129,14 @@ func (a *ArchiveExecutor) setupEnvVars(ctx context.Context, archive *k8upv1.Arch

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 @@ -132,36 +149,37 @@ func (a *ArchiveExecutor) cleanupOldArchives(ctx context.Context, archive *k8upv
func (a *ArchiveExecutor) appendOptionsArgs() []string {
var args []string

args = append(args, []string{"--varDir", cfg.Config.PodVarDir}...)

if a.archive.Spec.Backend.Options != nil {
if a.archive.Spec.Backend != nil && a.archive.Spec.Backend.Options != nil {
if a.archive.Spec.Backend.Options.CACert != "" {
args = append(args, []string{"--caCert", a.archive.Spec.Backend.Options.CACert}...)
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",
"-clientCert",
a.archive.Spec.Backend.Options.ClientCert,
"--clientKey",
"-clientKey",
a.archive.Spec.Backend.Options.ClientKey,
}...,
)
}
}

if a.archive.Spec.RestoreMethod.Options != nil {
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",
"-restoreClientCert",
a.archive.Spec.RestoreMethod.Options.ClientCert,
"--restoreClientKey",
"-restoreClientKey",
a.archive.Spec.RestoreMethod.Options.ClientKey,
}...,
)
Expand Down Expand Up @@ -197,10 +215,12 @@ func (a *ArchiveExecutor) attachMoreVolumes() []corev1.Volume {
continue
}

moreVolumes = append(moreVolumes, corev1.Volume{
Name: vol.Name,
VolumeSource: volumeSource,
})
moreVolumes = append(
moreVolumes, corev1.Volume{
Name: vol.Name,
VolumeSource: volumeSource,
},
)
}

return moreVolumes
Expand All @@ -209,11 +229,28 @@ func (a *ArchiveExecutor) attachMoreVolumes() []corev1.Volume {
func (a *ArchiveExecutor) attachMoreVolumeMounts() []corev1.VolumeMount {
var volumeMount []corev1.VolumeMount

if a.archive.Spec.Backend.S3 != nil && !utils.ZeroLen(a.archive.Spec.Backend.S3.VolumeMounts) {
volumeMount = *a.archive.Spec.Backend.S3.VolumeMounts
if a.archive.Spec.Backend != nil && !utils.ZeroLen(a.archive.Spec.Backend.VolumeMounts) {
volumeMount = append(volumeMount, *a.archive.Spec.Backend.VolumeMounts...)
}
if a.archive.Spec.Backend.Rest != nil && !utils.ZeroLen(a.archive.Spec.Backend.Rest.VolumeMounts) {
volumeMount = *a.archive.Spec.Backend.Rest.VolumeMounts
if a.archive.Spec.RestoreMethod != nil && !utils.ZeroLen(a.archive.Spec.RestoreMethod.VolumeMounts) {
for _, v1 := range *a.archive.Spec.RestoreMethod.VolumeMounts {
vm1 := v1
var isExist bool

for _, v2 := range volumeMount {
vm2 := v2
if vm1.Name == vm2.Name && vm1.MountPath == vm2.MountPath {
isExist = true
break
}
}

if isExist {
continue
}

volumeMount = append(volumeMount, vm1)
}
}

ku8pVolumeMount := corev1.VolumeMount{Name: _dataDirName, MountPath: cfg.Config.PodVarDir}
Expand Down
84 changes: 45 additions & 39 deletions operator/backupcontroller/backup_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ package backupcontroller
import (
"context"
"fmt"
"github.com/k8up-io/k8up/v2/operator/executor"
"github.com/k8up-io/k8up/v2/operator/utils"
"path"

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"
)

Expand All @@ -32,7 +34,10 @@ 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
Expand All @@ -45,9 +50,11 @@ 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
}
Expand All @@ -58,30 +65,32 @@ 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
}

func (b *BackupExecutor) setupArgs() ([]string, error) {
args := b.appendOptionsArgs()

args := []string{"--varDir", cfg.Config.PodVarDir}
if len(b.backup.Spec.Tags) > 0 {
args = append(args, executor.BuildTagArgs(b.backup.Spec.Tags)...)
}
args = append(args, b.appendOptionsArgs()...)

return args, nil
}
Expand Down Expand Up @@ -136,10 +145,12 @@ func (b *BackupExecutor) attachMoreVolumes() []corev1.Volume {
continue
}

moreVolumes = append(moreVolumes, corev1.Volume{
Name: vol.Name,
VolumeSource: volumeSource,
})
moreVolumes = append(
moreVolumes, corev1.Volume{
Name: vol.Name,
VolumeSource: volumeSource,
},
)
}

return moreVolumes
Expand All @@ -148,11 +159,8 @@ func (b *BackupExecutor) attachMoreVolumes() []corev1.Volume {
func (b *BackupExecutor) attachMoreVolumeMounts() []corev1.VolumeMount {
var volumeMount []corev1.VolumeMount

if b.backup.Spec.Backend.S3 != nil && !utils.ZeroLen(b.backup.Spec.Backend.S3.VolumeMounts) {
volumeMount = *b.backup.Spec.Backend.S3.VolumeMounts
}
if b.backup.Spec.Backend.Rest != nil && !utils.ZeroLen(b.backup.Spec.Backend.Rest.VolumeMounts) {
volumeMount = *b.backup.Spec.Backend.Rest.VolumeMounts
if b.backup.Spec.Backend != nil && !utils.ZeroLen(b.backup.Spec.Backend.VolumeMounts) {
volumeMount = *b.backup.Spec.Backend.VolumeMounts
}

ku8pVolumeMount := corev1.VolumeMount{Name: _dataDirName, MountPath: cfg.Config.PodVarDir}
Expand All @@ -164,22 +172,20 @@ func (b *BackupExecutor) attachMoreVolumeMounts() []corev1.VolumeMount {
func (b *BackupExecutor) appendOptionsArgs() []string {
var args []string

args = append(args, []string{"--varDir", cfg.Config.PodVarDir}...)

if b.backup.Spec.Backend.Options == nil {
if !(b.backup.Spec.Backend != nil && b.backup.Spec.Backend.Options != nil) {
return args
}

if b.backup.Spec.Backend.Options.CACert != "" {
args = append(args, []string{"--caCert", b.backup.Spec.Backend.Options.CACert}...)
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",
"-clientCert",
b.backup.Spec.Backend.Options.ClientCert,
"--clientKey",
"-clientKey",
b.backup.Spec.Backend.Options.ClientKey,
}...,
)
Expand Down
Loading

0 comments on commit c42e748

Please sign in to comment.