Restic repository now includes the targetName to avoid concurrency when multiple targets are doing backup simulteanously

This commit is contained in:
Jean-Marc ANDRE 2023-04-28 15:05:54 +02:00
parent 05cb6bd1cb
commit 910dbbbbd5
8 changed files with 17 additions and 13 deletions

View File

@ -81,8 +81,9 @@ var deleteSnapshotCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
name, _ := cmd.Flags().GetString("name") name, _ := cmd.Flags().GetString("name")
namespace, _ := cmd.Flags().GetString("namespace") namespace, _ := cmd.Flags().GetString("namespace")
targetName, _ := cmd.Flags().GetString("target-name")
snapshotId, _ := cmd.Flags().GetString("snapshot-id") 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("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("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("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("snapshot-id")
deleteSnapshotCmd.MarkFlagRequired("namespace") deleteSnapshotCmd.MarkFlagRequired("namespace")
deleteSnapshotCmd.MarkFlagRequired("name") deleteSnapshotCmd.MarkFlagRequired("name")
deleteSnapshotCmd.MarkFlagRequired("target-name")
} }

View File

@ -72,7 +72,7 @@ func (r *BackupSessionReconciler) Reconcile(ctx context.Context, req ctrl.Reques
} }
// Do preliminary checks with the repository // 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") r.Log.Error(err, "unable to set restic env")
return ctrl.Result{}, err return ctrl.Result{}, err
} }

View File

@ -71,7 +71,7 @@ func (r *BackupSessionReconciler) backupSnapshot(target formolv1alpha1.Target) (
sidecar := formolv1alpha1.GetSidecar(r.backupConf, 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.Args = append([]string{"backupsession", "backup", "--namespace", r.Namespace, "--name", r.Name, "--target-name", target.TargetName}, paths...)
sidecar.VolumeMounts = vms 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") r.Log.Error(err, "unable to get restic env")
return err return err
} else { } else {

View File

@ -69,7 +69,7 @@ func (r *RestoreSessionReconciler) Reconcile(ctx context.Context, req ctrl.Reque
} }
// Do preliminary checks with the repository // 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") r.Log.Error(err, "unable to set restic env")
return ctrl.Result{}, err return ctrl.Result{}, err
} }

View File

@ -34,7 +34,7 @@ func (r *RestoreSessionReconciler) restoreInitContainer(target formolv1alpha1.Ta
for i, _ := range initContainer.VolumeMounts { for i, _ := range initContainer.VolumeMounts {
initContainer.VolumeMounts[i].ReadOnly = false 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") r.Log.Error(err, "unable to get restic env")
return err return err
} else { } else {

View File

@ -40,7 +40,7 @@ const (
RESTIC_EXEC = "/usr/bin/restic" 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{} repo := formolv1alpha1.Repo{}
if err = s.Get(s.Context, client.ObjectKey{ if err = s.Get(s.Context, client.ObjectKey{
Namespace: backupConf.Namespace, Namespace: backupConf.Namespace,
@ -52,11 +52,12 @@ func (s Session) getResticEnv(backupConf formolv1alpha1.BackupConfiguration) (en
if repo.Spec.Backend.S3 != nil { if repo.Spec.Backend.S3 != nil {
envs = append(envs, corev1.EnvVar{ envs = append(envs, corev1.EnvVar{
Name: formolv1alpha1.RESTIC_REPOSITORY, 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.Server,
repo.Spec.Backend.S3.Bucket, repo.Spec.Backend.S3.Bucket,
strings.ToUpper(backupConf.Namespace), strings.ToUpper(backupConf.Namespace),
strings.ToLower(backupConf.Name)), strings.ToLower(backupConf.Name),
targetName),
}) })
data := s.getSecretData(repo.Spec.RepositorySecrets) data := s.getSecretData(repo.Spec.RepositorySecrets)
@ -76,8 +77,8 @@ func (s Session) getResticEnv(backupConf formolv1alpha1.BackupConfiguration) (en
return return
} }
func (s Session) SetResticEnv(backupConf formolv1alpha1.BackupConfiguration) error { func (s Session) SetResticEnv(backupConf formolv1alpha1.BackupConfiguration, targetName string) error {
envs, err := s.getResticEnv(backupConf) envs, err := s.getResticEnv(backupConf, targetName)
for _, env := range envs { for _, env := range envs {
os.Setenv(env.Name, env.Value) os.Setenv(env.Name, env.Value)
} }

2
formol

@ -1 +1 @@
Subproject commit 92ea7f38729eebebd3cabff4a391bdb878c4e387 Subproject commit 6f150cc36de7f879e2ddd89d126de84b77af0651

View File

@ -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") log := session.Log.WithName("DeleteSnapshot")
session.Namespace = namespace session.Namespace = namespace
backupConf := formolv1alpha1.BackupConfiguration{} backupConf := formolv1alpha1.BackupConfiguration{}
@ -210,7 +210,7 @@ func DeleteSnapshot(namespace string, name string, snapshotId string) {
log.Error(err, "unable to get the BackupConf") log.Error(err, "unable to get the BackupConf")
return 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") log.Error(err, "unable to set the restic env")
return return
} }