Skip to content
This repository has been archived by the owner on Mar 2, 2022. It is now read-only.

Commit

Permalink
Fix backing up of annotated pods
Browse files Browse the repository at this point in the history
  • Loading branch information
cimnine committed May 27, 2021
1 parent 16aeb47 commit d033914
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 45 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ minio-download: .test/minio ## Download minio

restic-download: .test/restic ## Download restic

docker: ## Builds the docker image. Overwrite the tag by changing the 'docker_tag' variable.
@docker build -t $(docker_tag) .

$(minio_pid): export MINIO_ACCESS_KEY = $(minio_root_user)
$(minio_pid): export MINIO_SECRET_KEY = $(minio_root_password)
$(minio_pid): minio-download
Expand Down
2 changes: 2 additions & 0 deletions Makefile.vars.mk
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ minio_root_user ?= accesskey
minio_root_password ?= secretkey
minio_pid ?= $(minio_path).pid
minio_url ?= https://dl.min.io/server/minio/release/$(os)-$(arch)/minio

docker_tag ?= vshn/wrestic:snapshot
104 changes: 59 additions & 45 deletions cmd/wrestic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,26 +97,26 @@ func run(resticCLI *restic.Restic, mainLogger logr.Logger) error {
}
}

commandRun := false
dontBackup := false

if *prune {
commandRun = true
dontBackup = true
if err := resticCLI.Prune(tags); err != nil {
mainLogger.Error(err, "prune job failed")
return err
}
}

if *check {
commandRun = true
dontBackup = true
if err := resticCLI.Check(); err != nil {
mainLogger.Error(err, "check job failed")
return err
}
}

if *restore {
commandRun = true
dontBackup = true
if err := resticCLI.Restore(*restoreSnap, restic.RestoreOptions{
RestoreType: restic.RestoreType(*restoreType),
RestoreDir: os.Getenv(restic.RestoreDirEnv),
Expand All @@ -134,59 +134,73 @@ func run(resticCLI *restic.Restic, mainLogger logr.Logger) error {
}

if *archive {
commandRun = true
dontBackup = true
if err := resticCLI.Archive(*restoreFilter, *verifyRestore, tags); err != nil {
mainLogger.Error(err, "archive job failed")
return err
}
}

if !commandRun {
commandAnnotation := os.Getenv(commandEnv)
if commandAnnotation == "" {
commandAnnotation = "k8up.syn.tools/backupcommand"
}
fileextAnnotation := os.Getenv(fileextEnv)
if fileextAnnotation == "" {
fileextAnnotation = "k8up.syn.tools/file-extension"
}
if dontBackup {
return nil
}

_, serviceErr := os.Stat("/var/run/secrets/kubernetes.io")
_, kubeconfigErr := os.Stat(kubernetes.Kubeconfig)

if serviceErr == nil && kubeconfigErr == nil {

podLister := kubernetes.NewPodLister(commandAnnotation, fileextAnnotation, os.Getenv(restic.Hostname), mainLogger)

podList, err := podLister.ListPods()

if err == nil {
for _, pod := range podList {
data, err := kubernetes.PodExec(pod, mainLogger)
if err != nil {
mainLogger.Error(errors.New("error occured during data stream from k8s"), "pod execution was interrupted")
return err
}
filename := fmt.Sprintf("/%s-%s", os.Getenv(restic.Hostname), pod.ContainerName)
err = resticCLI.StdinBackup(data, filename, pod.FileExtension, tags)
if err != nil {
mainLogger.Error(err, "backup commands failed")
return err
}
}
mainLogger.Info("all pod commands have finished successfully")
} else {
mainLogger.Error(err, "could not list pods", "namespace", os.Getenv(restic.Hostname))
}
}
err := backupAnnotatedPods(resticCLI, mainLogger)
if err != nil {
mainLogger.Error(err, "backup job failed", "step", "backup of annotated pods")
return err
}
mainLogger.Info("backups of annotated jobs have finished successfully")

err := resticCLI.Backup(getBackupDir(), tags)
err = resticCLI.Backup(getBackupDir(), tags)
if err != nil {
mainLogger.Error(err, "backup job failed", "step", "backup of dir failed", "dir", getBackupDir())
return err
}

return nil
}

func backupAnnotatedPods(resticCLI *restic.Restic, mainLogger logr.Logger) error {
commandAnnotation := os.Getenv(commandEnv)
if commandAnnotation == "" {
commandAnnotation = "k8up.syn.tools/backupcommand"
}
fileextAnnotation := os.Getenv(fileextEnv)
if fileextAnnotation == "" {
fileextAnnotation = "k8up.syn.tools/file-extension"
}

_, serviceErr := os.Stat("/var/run/secrets/kubernetes.io")
_, kubeconfigErr := os.Stat(kubernetes.Kubeconfig)

if serviceErr != nil && kubeconfigErr != nil {
mainLogger.Info("No kubernetes credentials configured: Can't check for annotated Pods.")
return nil
}

podLister := kubernetes.NewPodLister(commandAnnotation, fileextAnnotation, os.Getenv(restic.Hostname), mainLogger)
podList, err := podLister.ListPods()

if err != nil {
mainLogger.Error(err, "could not list pods", "namespace", os.Getenv(restic.Hostname))
return nil
}

for _, pod := range podList {
data, err := kubernetes.PodExec(pod, mainLogger)
if err != nil {
mainLogger.Error(err, "backup job failed")
mainLogger.Error(errors.New("error occurred during data stream from k8s"), "pod execution was interrupted")
return err
}
filename := fmt.Sprintf("/%s-%s", os.Getenv(restic.Hostname), pod.ContainerName)
err = resticCLI.StdinBackup(data, filename, pod.FileExtension, tags)
if err != nil {
mainLogger.Error(err, "backup commands failed")
return err
}

}

return nil
}

Expand Down

0 comments on commit d033914

Please sign in to comment.