diff --git a/cmd/root.go b/cmd/root.go index 57427ba..b0ec277 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -56,6 +56,22 @@ var backupSessionCmd = &cobra.Command{ Short: "All the BackupSession related commands", } +var snapshotCmd = &cobra.Command{ + Use: "snapshot", + Short: "All the snapshot related commands", +} + +var deleteSnapshotCmd = &cobra.Command{ + Use: "delete", + Short: "Delete a snapshot", + Run: func(cmd *cobra.Command, args []string) { + name, _ := cmd.Flags().GetString("name") + namespace, _ := cmd.Flags().GetString("namespace") + snapshotId, _ := cmd.Flags().GetString("snapshot-id") + standalone.DeleteSnapshot(namespace, name, snapshotId) + }, +} + // rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ Use: "formolcli", @@ -83,8 +99,10 @@ func Execute() { func init() { rootCmd.AddCommand(backupSessionCmd) rootCmd.AddCommand(restoreSessionCmd) + rootCmd.AddCommand(snapshotCmd) backupSessionCmd.AddCommand(createBackupSessionCmd) restoreSessionCmd.AddCommand(startRestoreSessionCmd) + snapshotCmd.AddCommand(deleteSnapshotCmd) rootCmd.AddCommand(startServerCmd) createBackupSessionCmd.Flags().String("namespace", "", "The namespace of the BackupConfiguration containing the information about the backup.") createBackupSessionCmd.Flags().String("name", "", "The name of the BackupConfiguration containing the information about the backup.") @@ -96,4 +114,10 @@ func init() { startRestoreSessionCmd.MarkFlagRequired("namespace") startRestoreSessionCmd.MarkFlagRequired("name") startRestoreSessionCmd.MarkFlagRequired("target-name") + 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.MarkFlagRequired("snapshot-id") + deleteSnapshotCmd.MarkFlagRequired("namespace") + deleteSnapshotCmd.MarkFlagRequired("name") } diff --git a/controllers/backupsession_controller.go b/controllers/backupsession_controller.go index b1177fe..75a8e16 100644 --- a/controllers/backupsession_controller.go +++ b/controllers/backupsession_controller.go @@ -67,7 +67,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); err != nil { r.Log.Error(err, "unable to set restic env") return ctrl.Result{}, err } diff --git a/controllers/restoresession_controller.go b/controllers/restoresession_controller.go index 3da80ee..8a53c10 100644 --- a/controllers/restoresession_controller.go +++ b/controllers/restoresession_controller.go @@ -68,7 +68,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); err != nil { r.Log.Error(err, "unable to set restic env") return ctrl.Result{}, err } diff --git a/controllers/session.go b/controllers/session.go index 946d9cb..9e28e95 100644 --- a/controllers/session.go +++ b/controllers/session.go @@ -69,7 +69,7 @@ func (s Session) getResticEnv(backupConf formolv1alpha1.BackupConfiguration) (en return } -func (s Session) setResticEnv(backupConf formolv1alpha1.BackupConfiguration) error { +func (s Session) SetResticEnv(backupConf formolv1alpha1.BackupConfiguration) error { envs, err := s.getResticEnv(backupConf) for _, env := range envs { os.Setenv(env.Name, env.Value) @@ -95,9 +95,8 @@ func (s Session) CheckRepo() error { func (s Session) getSecretData(name string) map[string][]byte { secret := corev1.Secret{} - namespace := os.Getenv(formolv1alpha1.POD_NAMESPACE) if err := s.Get(s.Context, client.ObjectKey{ - Namespace: namespace, + Namespace: s.Namespace, Name: name, }, &secret); err != nil { s.Log.Error(err, "unable to get Secret", "Secret", name) diff --git a/formol b/formol index e73ef7c..f890962 160000 --- a/formol +++ b/formol @@ -1 +1 @@ -Subproject commit e73ef7c3f24ee612421b62963f443ff1a1e790dc +Subproject commit f890962221bf05fb54c9d46d839957a59d644cc8 diff --git a/standalone/root.go b/standalone/root.go index 53ec3c3..f01fed5 100644 --- a/standalone/root.go +++ b/standalone/root.go @@ -141,3 +141,26 @@ func CreateBackupSession(ref corev1.ObjectReference) { os.Exit(1) } } + +func DeleteSnapshot(namespace string, name string, snapshotId string) { + log := session.Log.WithName("DeleteSnapshot") + session.Namespace = namespace + backupConf := formolv1alpha1.BackupConfiguration{} + if err := session.Get(session.Context, client.ObjectKey{ + Namespace: namespace, + Name: name, + }, &backupConf); err != nil { + log.Error(err, "unable to get the BackupConf") + return + } + if err := session.SetResticEnv(backupConf); err != nil { + log.Error(err, "unable to set the restic env") + return + } + log.V(0).Info("deleting restic snapshot", "snapshotId", snapshotId) + cmd := exec.Command(controllers.RESTIC_EXEC, "forget", "--prune", snapshotId) + _, err := cmd.CombinedOutput() + if err != nil { + log.Error(err, "unable to delete snapshot", "snapshoId", snapshotId) + } +}