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

Commit

Permalink
Merge pull request #93 from vshn/FixBackupOfAnnotatedPods
Browse files Browse the repository at this point in the history
Fix backing up of annotated pods
  • Loading branch information
cimnine authored May 27, 2021
2 parents 16aeb47 + d6ec0ed commit 7e82ec8
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 46 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

## [v0.3.1] 2021-05-27
### Fixed
- Fix skipping backup of annotated pods ([#93])

## [v0.3.0] 2021-05-26
### Changed
- Refactor Integration tests ([#90])
Expand Down Expand Up @@ -162,7 +166,8 @@ compatibility with older operator versions. Changes to the design contain:
- Improved error detection and various bugfixes
- Timeout for initial snapshot listing, default: 30s

[Unreleased]: https://git.vshn.net/vshn/wrestic/compare/v0.3.0...master
[Unreleased]: https://github.com/vshn/wrestic/compare/v0.3.0...master
[v0.3.1]: https://github.com/vshn/wrestic/releases/tag/v0.3.1
[v0.3.0]: https://github.com/vshn/wrestic/releases/tag/v0.3.0
[v0.0.7]: https://git.vshn.net/vshn/wrestic/compare/v0.0.6...v0.0.7
[v0.0.6]: https://git.vshn.net/vshn/wrestic/compare/v0.0.5...v0.0.6
Expand All @@ -187,3 +192,4 @@ compatibility with older operator versions. Changes to the design contain:
[#89]: https://github.com/vshn/wrestic/pull/89
[#90]: https://github.com/vshn/wrestic/pull/90
[#92]: https://github.com/vshn/wrestic/pull/92
[#93]: https://github.com/vshn/wrestic/pull/93
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 ?= local.dev/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
skipBackup := false

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

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

if *restore {
commandRun = true
skipBackup = 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
skipBackup = 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 skipBackup {
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 7e82ec8

Please sign in to comment.