From 910dbbbbd584d194e9ab92cca533d4dc6899c9b9 Mon Sep 17 00:00:00 2001 From: Jean-Marc ANDRE Date: Fri, 28 Apr 2023 15:05:54 +0200 Subject: [PATCH] Restic repository now includes the targetName to avoid concurrency when multiple targets are doing backup simulteanously --- cmd/root.go | 5 ++++- controllers/backupsession_controller.go | 2 +- controllers/backupsession_controller_helpers.go | 2 +- controllers/restoresession_controller.go | 2 +- controllers/restoresession_controller_helper.go | 2 +- controllers/session.go | 11 ++++++----- formol | 2 +- standalone/root.go | 4 ++-- 8 files changed, 17 insertions(+), 13 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 3336016..729175e 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -81,8 +81,9 @@ var deleteSnapshotCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { name, _ := cmd.Flags().GetString("name") namespace, _ := cmd.Flags().GetString("namespace") + targetName, _ := cmd.Flags().GetString("target-name") snapshotId, _ := cmd.Flags().GetString("snapshot-id") - standalone.DeleteSnapshot(namespace, name, snapshotId) + standalone.DeleteSnapshot(namespace, name, targetName, snapshotId) }, } @@ -138,7 +139,9 @@ func init() { deleteSnapshotCmd.Flags().String("snapshot-id", "", "The snapshot id to delete") deleteSnapshotCmd.Flags().String("namespace", "", "The namespace of the BackupConfiguration containing the information about the backup.") deleteSnapshotCmd.Flags().String("name", "", "The name of the BackupConfiguration containing the information about the backup.") + deleteSnapshotCmd.Flags().String("target-name", "", "The name of the backup target.") deleteSnapshotCmd.MarkFlagRequired("snapshot-id") deleteSnapshotCmd.MarkFlagRequired("namespace") deleteSnapshotCmd.MarkFlagRequired("name") + deleteSnapshotCmd.MarkFlagRequired("target-name") } diff --git a/controllers/backupsession_controller.go b/controllers/backupsession_controller.go index 1c487e0..27a1d63 100644 --- a/controllers/backupsession_controller.go +++ b/controllers/backupsession_controller.go @@ -72,7 +72,7 @@ func (r *BackupSessionReconciler) Reconcile(ctx context.Context, req ctrl.Reques } // Do preliminary checks with the repository - if err = r.SetResticEnv(backupConf); err != nil { + if err = r.SetResticEnv(backupConf, target.TargetName); err != nil { r.Log.Error(err, "unable to set restic env") return ctrl.Result{}, err } diff --git a/controllers/backupsession_controller_helpers.go b/controllers/backupsession_controller_helpers.go index cb2e9a3..2df46e8 100644 --- a/controllers/backupsession_controller_helpers.go +++ b/controllers/backupsession_controller_helpers.go @@ -71,7 +71,7 @@ func (r *BackupSessionReconciler) backupSnapshot(target formolv1alpha1.Target) ( sidecar := formolv1alpha1.GetSidecar(r.backupConf, target) sidecar.Args = append([]string{"backupsession", "backup", "--namespace", r.Namespace, "--name", r.Name, "--target-name", target.TargetName}, paths...) sidecar.VolumeMounts = vms - if env, err := r.getResticEnv(r.backupConf); err != nil { + if env, err := r.getResticEnv(r.backupConf, target.TargetName); err != nil { r.Log.Error(err, "unable to get restic env") return err } else { diff --git a/controllers/restoresession_controller.go b/controllers/restoresession_controller.go index 8ebdcc0..899836c 100644 --- a/controllers/restoresession_controller.go +++ b/controllers/restoresession_controller.go @@ -69,7 +69,7 @@ func (r *RestoreSessionReconciler) Reconcile(ctx context.Context, req ctrl.Reque } // Do preliminary checks with the repository - if err = r.SetResticEnv(backupConf); err != nil { + if err = r.SetResticEnv(backupConf, target.TargetName); err != nil { r.Log.Error(err, "unable to set restic env") return ctrl.Result{}, err } diff --git a/controllers/restoresession_controller_helper.go b/controllers/restoresession_controller_helper.go index 15c1096..a35b083 100644 --- a/controllers/restoresession_controller_helper.go +++ b/controllers/restoresession_controller_helper.go @@ -34,7 +34,7 @@ func (r *RestoreSessionReconciler) restoreInitContainer(target formolv1alpha1.Ta for i, _ := range initContainer.VolumeMounts { initContainer.VolumeMounts[i].ReadOnly = false } - if env, err := r.getResticEnv(r.backupConf); err != nil { + if env, err := r.getResticEnv(r.backupConf, target.TargetName); err != nil { r.Log.Error(err, "unable to get restic env") return err } else { diff --git a/controllers/session.go b/controllers/session.go index 07c3be2..569828b 100644 --- a/controllers/session.go +++ b/controllers/session.go @@ -40,7 +40,7 @@ const ( RESTIC_EXEC = "/usr/bin/restic" ) -func (s Session) getResticEnv(backupConf formolv1alpha1.BackupConfiguration) (envs []corev1.EnvVar, err error) { +func (s Session) getResticEnv(backupConf formolv1alpha1.BackupConfiguration, targetName string) (envs []corev1.EnvVar, err error) { repo := formolv1alpha1.Repo{} if err = s.Get(s.Context, client.ObjectKey{ Namespace: backupConf.Namespace, @@ -52,11 +52,12 @@ func (s Session) getResticEnv(backupConf formolv1alpha1.BackupConfiguration) (en if repo.Spec.Backend.S3 != nil { envs = append(envs, corev1.EnvVar{ Name: formolv1alpha1.RESTIC_REPOSITORY, - Value: fmt.Sprintf("s3:http://%s/%s/%s-%s", + Value: fmt.Sprintf("s3:http://%s/%s/%s-%s/%s", repo.Spec.Backend.S3.Server, repo.Spec.Backend.S3.Bucket, strings.ToUpper(backupConf.Namespace), - strings.ToLower(backupConf.Name)), + strings.ToLower(backupConf.Name), + targetName), }) data := s.getSecretData(repo.Spec.RepositorySecrets) @@ -76,8 +77,8 @@ func (s Session) getResticEnv(backupConf formolv1alpha1.BackupConfiguration) (en return } -func (s Session) SetResticEnv(backupConf formolv1alpha1.BackupConfiguration) error { - envs, err := s.getResticEnv(backupConf) +func (s Session) SetResticEnv(backupConf formolv1alpha1.BackupConfiguration, targetName string) error { + envs, err := s.getResticEnv(backupConf, targetName) for _, env := range envs { os.Setenv(env.Name, env.Value) } diff --git a/formol b/formol index 92ea7f3..6f150cc 160000 --- a/formol +++ b/formol @@ -1 +1 @@ -Subproject commit 92ea7f38729eebebd3cabff4a391bdb878c4e387 +Subproject commit 6f150cc36de7f879e2ddd89d126de84b77af0651 diff --git a/standalone/root.go b/standalone/root.go index fdfe80d..ae40008 100644 --- a/standalone/root.go +++ b/standalone/root.go @@ -199,7 +199,7 @@ func CreateBackupSession(ref corev1.ObjectReference) { } } -func DeleteSnapshot(namespace string, name string, snapshotId string) { +func DeleteSnapshot(namespace string, name string, targetName string, snapshotId string) { log := session.Log.WithName("DeleteSnapshot") session.Namespace = namespace backupConf := formolv1alpha1.BackupConfiguration{} @@ -210,7 +210,7 @@ func DeleteSnapshot(namespace string, name string, snapshotId string) { log.Error(err, "unable to get the BackupConf") return } - if err := session.SetResticEnv(backupConf); err != nil { + if err := session.SetResticEnv(backupConf, targetName); err != nil { log.Error(err, "unable to set the restic env") return }