Skip to content

Commit cd2f8ec

Browse files
authored
[cherry-pick] Fix output format + Add PreBackupActions logic (#113) (#116)
Signed-off-by: hossainemruz <emruz@appscode.com>
1 parent d89559c commit cd2f8ec

File tree

108 files changed

+16473
-3550
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

108 files changed

+16473
-3550
lines changed

charts/stash-postgres/templates/postgres-backup-function.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ spec:
2525
# target information
2626
- --namespace=${NAMESPACE:=default}
2727
- --appbinding=${TARGET_NAME:=}
28+
- --backupsession=${BACKUP_SESSION:=}
2829
# cleanup information
2930
- --retention-keep-last=${RETENTION_KEEP_LAST:=0}
3031
- --retention-keep-hourly=${RETENTION_KEEP_HOURLY:=0}

charts/stash-postgres/templates/postgres-backup-task.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ spec:
1616
params:
1717
- name: outputDir
1818
value: /tmp/output
19+
- name: secretVolume
20+
value: secret-volume
1921
volumes:
2022
- name: secret-volume
2123
secret:

charts/stash-postgres/templates/postgres-restore-task.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ spec:
1616
params:
1717
- name: outputDir
1818
value: /tmp/output
19+
- name: secretVolume
20+
value: secret-volume
1921
volumes:
2022
- name: secret-volume
2123
secret:

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ require (
1616
k8s.io/apimachinery v0.18.3
1717
k8s.io/client-go v12.0.0+incompatible
1818
k8s.io/kube-openapi v0.0.0-20200410145947-61e04a5be9a6
19-
kmodules.xyz/client-go v0.0.0-20200630053911-20d035822d35
19+
kmodules.xyz/client-go v0.0.0-20200714060506-0b57b4060595
2020
kmodules.xyz/crd-schema-fuzz v0.0.0-20200521005638-2433a187de95
2121
kmodules.xyz/custom-resources v0.0.0-20200604135349-9e9f5c4fdba9
2222
kmodules.xyz/offshoot-api v0.0.0-20200521035628-e135bf07b226
2323
sigs.k8s.io/yaml v1.2.0
24-
stash.appscode.dev/apimachinery v0.10.0-beta.1
24+
stash.appscode.dev/apimachinery v0.10.0-beta.1.0.20200717091944-a8f3bb469467
2525
)
2626

2727
replace bitbucket.org/ww/goautoneg => gomodules.xyz/goautoneg v0.0.0-20120707110453-a547fc61f48d

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -913,8 +913,8 @@ kmodules.xyz/client-go v0.0.0-20200521005126-35ce6bd4ed46/go.mod h1:sY/eoe4ktxZE
913913
kmodules.xyz/client-go v0.0.0-20200521065424-173e32c78a20/go.mod h1:sY/eoe4ktxZEoHpr5NpAQ5s22VSwTE8psJtKVeVgLRY=
914914
kmodules.xyz/client-go v0.0.0-20200525195850-2fd180961371 h1:PPawDOMyDHGeDPN8j1epNozaIB/Z7MlJsXpwm/r4jgk=
915915
kmodules.xyz/client-go v0.0.0-20200525195850-2fd180961371/go.mod h1:sY/eoe4ktxZEoHpr5NpAQ5s22VSwTE8psJtKVeVgLRY=
916-
kmodules.xyz/client-go v0.0.0-20200630053911-20d035822d35 h1:gDzZWVvgAaEBzo4lxMGhPUWqySgFyFDkcqw3NskZiwQ=
917-
kmodules.xyz/client-go v0.0.0-20200630053911-20d035822d35/go.mod h1:sY/eoe4ktxZEoHpr5NpAQ5s22VSwTE8psJtKVeVgLRY=
916+
kmodules.xyz/client-go v0.0.0-20200714060506-0b57b4060595 h1:KgvN4M8VbHU6JV4rgYzsUKYhUtF1uCt//dLOyEdG330=
917+
kmodules.xyz/client-go v0.0.0-20200714060506-0b57b4060595/go.mod h1:sY/eoe4ktxZEoHpr5NpAQ5s22VSwTE8psJtKVeVgLRY=
918918
kmodules.xyz/constants v0.0.0-20200506032633-a21e58ceec72/go.mod h1:DbiFk1bJ1KEO94t1SlAn7tzc+Zz95rSXgyUKa2nzPmY=
919919
kmodules.xyz/crd-schema-fuzz v0.0.0-20200521005638-2433a187de95 h1:v0S/+ftzL6Xrs9XevgchAOJyPKlRQXPiZf87xotj3X4=
920920
kmodules.xyz/crd-schema-fuzz v0.0.0-20200521005638-2433a187de95/go.mod h1:jpu8xFsDKd6kAWUAKk8oTu/GQGBWqhrcaDeOJdaCJnk=
@@ -947,6 +947,6 @@ sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=
947947
sigs.k8s.io/yaml v1.2.0 h1:kr/MCeFWJWTwyaHoR9c8EjH9OumOmoF9YGiZd7lFm/Q=
948948
sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc=
949949
sourcegraph.com/sqs/pbtypes v0.0.0-20180604144634-d3ebe8f20ae4/go.mod h1:ketZ/q3QxT9HOBeFhu6RdvsftgpsbFHBF5Cas6cDKZ0=
950-
stash.appscode.dev/apimachinery v0.10.0-beta.1 h1:R1IhAhZ/iUbs2M+v9LYJacDCOmc4lk00JzSjVBrxOG0=
951-
stash.appscode.dev/apimachinery v0.10.0-beta.1/go.mod h1:v3rBovazmdCv/9rAA5U7LpDRFhVqcLsagnLlbPfD2Eg=
950+
stash.appscode.dev/apimachinery v0.10.0-beta.1.0.20200717091944-a8f3bb469467 h1:ghoHaD+TRpDGB1sjA6F+4wT4uKNF/hhbulzARzO7AXE=
951+
stash.appscode.dev/apimachinery v0.10.0-beta.1.0.20200717091944-a8f3bb469467/go.mod h1:lyIgBXoTj0cugPfVQC5IjOsMiQEx0YQuvr3KycGltyU=
952952
vbom.ml/util v0.0.0-20160121211510-db5cfe13f5cc/go.mod h1:so/NYdZXCz+E3ZpW0uAoCj6uzU2+8OWDFv/HxUSs7kI=

pkg/backup.go

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,16 @@ import (
2323
"strings"
2424

2525
api_v1beta1 "stash.appscode.dev/apimachinery/apis/stash/v1beta1"
26+
stash "stash.appscode.dev/apimachinery/client/clientset/versioned"
2627
"stash.appscode.dev/apimachinery/pkg/restic"
28+
api_util "stash.appscode.dev/apimachinery/pkg/util"
2729

2830
"github.com/appscode/go/flags"
2931
"github.com/spf13/cobra"
3032
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3133
"k8s.io/client-go/kubernetes"
3234
"k8s.io/client-go/tools/clientcmd"
35+
appcatalog "kmodules.xyz/custom-resources/apis/appcatalog/v1alpha1"
3336
appcatalog_cs "kmodules.xyz/custom-resources/client/clientset/versioned"
3437
v1 "kmodules.xyz/offshoot-api/api/v1"
3538
)
@@ -67,20 +70,32 @@ func NewCmdBackup() *cobra.Command {
6770
if err != nil {
6871
return err
6972
}
73+
opt.stashClient, err = stash.NewForConfig(config)
74+
if err != nil {
75+
return err
76+
}
7077
opt.catalogClient, err = appcatalog_cs.NewForConfig(config)
7178
if err != nil {
7279
return err
7380
}
81+
targetRef := api_v1beta1.TargetRef{
82+
APIVersion: appcatalog.SchemeGroupVersion.String(),
83+
Kind: appcatalog.ResourceKindApp,
84+
Name: opt.appBindingName,
85+
}
7486

7587
var backupOutput *restic.BackupOutput
76-
backupOutput, err = opt.backupPostgreSQL()
88+
backupOutput, err = opt.backupPostgreSQL(targetRef)
7789
if err != nil {
7890
backupOutput = &restic.BackupOutput{
79-
HostBackupStats: []api_v1beta1.HostBackupStats{
80-
{
81-
Hostname: opt.backupOptions.Host,
82-
Phase: api_v1beta1.HostBackupFailed,
83-
Error: err.Error(),
91+
BackupTargetStatus: api_v1beta1.BackupTargetStatus{
92+
Ref: targetRef,
93+
Stats: []api_v1beta1.HostBackupStats{
94+
{
95+
Hostname: opt.backupOptions.Host,
96+
Phase: api_v1beta1.HostBackupFailed,
97+
Error: err.Error(),
98+
},
8499
},
85100
},
86101
}
@@ -101,6 +116,7 @@ func NewCmdBackup() *cobra.Command {
101116
cmd.Flags().StringVar(&masterURL, "master", masterURL, "The address of the Kubernetes API server (overrides any value in kubeconfig)")
102117
cmd.Flags().StringVar(&kubeconfigPath, "kubeconfig", kubeconfigPath, "Path to kubeconfig file with authorization information (the master location is set by the master flag).")
103118
cmd.Flags().StringVar(&opt.namespace, "namespace", "default", "Namespace of Backup/Restore Session")
119+
cmd.Flags().StringVar(&opt.backupSessionName, "backupsession", opt.backupSessionName, "Name of the Backup Session")
104120
cmd.Flags().StringVar(&opt.appBindingName, "appbinding", opt.appBindingName, "Name of the app binding")
105121

106122
cmd.Flags().StringVar(&opt.setupOptions.Provider, "provider", opt.setupOptions.Provider, "Backend provider (i.e. gcs, s3, azure etc)")
@@ -130,10 +146,25 @@ func NewCmdBackup() *cobra.Command {
130146
return cmd
131147
}
132148

133-
func (opt *postgresOptions) backupPostgreSQL() (*restic.BackupOutput, error) {
149+
func (opt *postgresOptions) backupPostgreSQL(targetRef api_v1beta1.TargetRef) (*restic.BackupOutput, error) {
150+
// if any pre-backup actions has been assigned to it, execute them
151+
actionOptions := api_util.ActionOptions{
152+
StashClient: opt.stashClient,
153+
TargetRef: targetRef,
154+
SetupOptions: opt.setupOptions,
155+
BackupSessionName: opt.backupSessionName,
156+
Namespace: opt.namespace,
157+
}
158+
err := api_util.ExecutePreBackupActions(actionOptions)
159+
if err != nil {
160+
return nil, err
161+
}
162+
// wait until the backend repository has been initialized.
163+
err = api_util.WaitForBackendRepository(actionOptions)
164+
if err != nil {
165+
return nil, err
166+
}
134167
// apply nice, ionice settings from env
135-
var err error
136-
137168
opt.setupOptions.Nice, err = v1.NiceSettingsFromEnv()
138169
if err != nil {
139170
return nil, err
@@ -194,6 +225,6 @@ func (opt *postgresOptions) backupPostgreSQL() (*restic.BackupOutput, error) {
194225
}
195226

196227
// Run backup
197-
return resticWrapper.RunBackup(opt.backupOptions)
228+
return resticWrapper.RunBackup(opt.backupOptions, targetRef)
198229

199230
}

pkg/restore.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3030
"k8s.io/client-go/kubernetes"
3131
"k8s.io/client-go/tools/clientcmd"
32+
appcatalog "kmodules.xyz/custom-resources/apis/appcatalog/v1alpha1"
3233
appcatalog_cs "kmodules.xyz/custom-resources/client/clientset/versioned"
3334
v1 "kmodules.xyz/offshoot-api/api/v1"
3435
)
@@ -71,15 +72,23 @@ func NewCmdRestore() *cobra.Command {
7172
return err
7273
}
7374

75+
targetRef := api_v1beta1.TargetRef{
76+
APIVersion: appcatalog.SchemeGroupVersion.String(),
77+
Kind: appcatalog.ResourceKindApp,
78+
Name: opt.appBindingName,
79+
}
7480
var restoreOutput *restic.RestoreOutput
75-
restoreOutput, err = opt.restorePostgreSQL()
81+
restoreOutput, err = opt.restorePostgreSQL(targetRef)
7682
if err != nil {
7783
restoreOutput = &restic.RestoreOutput{
78-
HostRestoreStats: []api_v1beta1.HostRestoreStats{
79-
{
80-
Hostname: opt.dumpOptions.Host,
81-
Phase: api_v1beta1.HostRestoreFailed,
82-
Error: err.Error(),
84+
RestoreTargetStatus: api_v1beta1.RestoreMemberStatus{
85+
Ref: targetRef,
86+
Stats: []api_v1beta1.HostRestoreStats{
87+
{
88+
Hostname: opt.dumpOptions.Host,
89+
Phase: api_v1beta1.HostRestoreFailed,
90+
Error: err.Error(),
91+
},
8392
},
8493
},
8594
}
@@ -120,7 +129,7 @@ func NewCmdRestore() *cobra.Command {
120129
return cmd
121130
}
122131

123-
func (opt *postgresOptions) restorePostgreSQL() (*restic.RestoreOutput, error) {
132+
func (opt *postgresOptions) restorePostgreSQL(targetRef api_v1beta1.TargetRef) (*restic.RestoreOutput, error) {
124133
// apply nice, ionice settings from env
125134
var err error
126135

@@ -177,5 +186,5 @@ func (opt *postgresOptions) restorePostgreSQL() (*restic.RestoreOutput, error) {
177186
}
178187

179188
// Run dump
180-
return resticWrapper.Dump(opt.dumpOptions)
189+
return resticWrapper.Dump(opt.dumpOptions, targetRef)
181190
}

pkg/util.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package pkg
1919
import (
2020
"fmt"
2121

22+
stash "stash.appscode.dev/apimachinery/client/clientset/versioned"
2223
"stash.appscode.dev/apimachinery/pkg/restic"
2324

2425
"github.com/appscode/go/log"
@@ -41,14 +42,16 @@ const (
4142

4243
type postgresOptions struct {
4344
kubeClient kubernetes.Interface
45+
stashClient stash.Interface
4446
catalogClient appcatalog_cs.Interface
4547

46-
namespace string
47-
appBindingName string
48-
backupCMD string
49-
pgArgs string
50-
outputDir string
51-
waitTimeout int32
48+
namespace string
49+
backupSessionName string
50+
appBindingName string
51+
backupCMD string
52+
pgArgs string
53+
outputDir string
54+
waitTimeout int32
5255

5356
setupOptions restic.SetupOptions
5457
backupOptions restic.BackupOptions

vendor/kmodules.xyz/client-go/Makefile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
# Copyright 2019 AppsCode Inc.
2-
# Copyright 2016 The Kubernetes Authors.
1+
# Copyright AppsCode Inc. and Contributors
32
#
43
# Licensed under the Apache License, Version 2.0 (the "License");
54
# you may not use this file except in compliance with the License.

vendor/kmodules.xyz/client-go/api/v1/conditions.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright The Kmodules Authors.
2+
Copyright AppsCode Inc. and Contributors
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.

0 commit comments

Comments
 (0)