Skip to content

Commit d3754d7

Browse files
Md. Emruz Hossaintamalsaha
authored andcommitted
Implement offline backup for multiple replica (#335)
fixes #244
1 parent 13a2d14 commit d3754d7

File tree

4 files changed

+75
-12
lines changed

4 files changed

+75
-12
lines changed

backup.go

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ import (
77

88
"github.com/appscode/go/log"
99
"github.com/appscode/kutil/meta"
10+
api "github.com/appscode/stash/apis/stash/v1alpha1"
1011
cs "github.com/appscode/stash/client/clientset/versioned"
1112
"github.com/appscode/stash/pkg/backup"
1213
"github.com/appscode/stash/pkg/docker"
14+
"github.com/appscode/stash/pkg/scale"
1315
"github.com/appscode/stash/pkg/util"
1416
"github.com/spf13/cobra"
1517
"k8s.io/client-go/kubernetes"
@@ -70,10 +72,33 @@ func NewCmdBackup() *cobra.Command {
7072
if err = ctrl.BackupScheduler(); err != nil {
7173
log.Fatal(err)
7274
}
73-
} else {
74-
log.Infoln("Running backup once")
75-
if err = ctrl.Backup(); err != nil {
76-
log.Fatal(err)
75+
} else { // for offline backup
76+
if opt.Workload.Kind == api.KindDaemonSet || opt.Workload.Kind == api.KindStatefulSet {
77+
log.Infoln("Running backup once")
78+
if err = ctrl.Backup(); err != nil {
79+
log.Fatal(err)
80+
}
81+
} else {
82+
//if replica > 1 we should not take backup
83+
replica, err := util.WorkloadReplicas(kubeClient, opt.Namespace, opt.Workload.Kind, opt.Workload.Name)
84+
if err != nil {
85+
log.Fatal(err)
86+
}
87+
88+
if replica > 1 {
89+
log.Infof("Skipping backup. Reason: Backup type offline and replica > 1")
90+
} else {
91+
log.Infoln("Running backup once")
92+
if err = ctrl.Backup(); err != nil {
93+
log.Fatal(err)
94+
}
95+
96+
// offline backup done. now scale up replica to original replica number
97+
err = scale.ScaleUpWorkload(kubeClient, opt)
98+
if err != nil {
99+
log.Fatal(err)
100+
}
101+
}
77102
}
78103
}
79104
log.Infoln("Exiting Stash Backup")

root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ func NewRootCmd() *cobra.Command {
5555
rootCmd.AddCommand(NewCmdBackup())
5656
rootCmd.AddCommand(NewCmdRecover())
5757
rootCmd.AddCommand(NewCmdCheck())
58+
rootCmd.AddCommand(NewCmdScaleDown())
5859

5960
return rootCmd
6061
}

scaledown.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package cmds
2+
3+
import (
4+
"github.com/appscode/go/log"
5+
"github.com/appscode/kutil/meta"
6+
"github.com/appscode/stash/pkg/scale"
7+
"github.com/spf13/cobra"
8+
"k8s.io/client-go/kubernetes"
9+
"k8s.io/client-go/tools/clientcmd"
10+
)
11+
12+
func NewCmdScaleDown() *cobra.Command {
13+
var (
14+
masterURL string
15+
kubeconfigPath string
16+
opt = scale.Options{
17+
Namespace: meta.Namespace(),
18+
}
19+
)
20+
21+
cmd := &cobra.Command{
22+
Use: "scaledown",
23+
Short: "Scale down workload",
24+
DisableAutoGenTag: true,
25+
Run: func(cmd *cobra.Command, args []string) {
26+
config, err := clientcmd.BuildConfigFromFlags(masterURL, kubeconfigPath)
27+
if err != nil {
28+
log.Fatalf("Could not get Kubernetes config: %s", err)
29+
}
30+
kubeClient := kubernetes.NewForConfigOrDie(config)
31+
32+
ctrl := scale.New(kubeClient, opt)
33+
err = ctrl.ScaleDownWorkload()
34+
if err != nil {
35+
log.Fatal(err)
36+
}
37+
38+
},
39+
}
40+
cmd.Flags().StringVar(&masterURL, "master", masterURL, "The address of the Kubernetes API server (overrides any value in kubeconfig)")
41+
cmd.Flags().StringVar(&kubeconfigPath, "kubeconfig", kubeconfigPath, "Path to kubeconfig file with authorization information (the master location is set by the master flag).")
42+
cmd.Flags().StringVar(&opt.Selector, "selector", opt.Selector, "Label used to select Restic's workload")
43+
44+
return cmd
45+
}

server/options.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ package server
22

33
import (
44
"flag"
5-
"fmt"
65
"time"
76

87
stringz "github.com/appscode/go/strings"
98
v "github.com/appscode/go/version"
10-
"github.com/appscode/kutil/discovery"
119
cs "github.com/appscode/stash/client/clientset/versioned"
1210
hookapi "github.com/appscode/stash/pkg/admission/api"
1311
"github.com/appscode/stash/pkg/admission/plugin"
@@ -78,11 +76,5 @@ func (s *ControllerOptions) ApplyTo(cfg *controller.ControllerConfig) error {
7876
}
7977
cfg.AdmissionHooks = []hookapi.AdmissionHook{&plugin.CRDValidator{}}
8078

81-
// get kube api server version
82-
cfg.KubectlImageTag, err = discovery.GetBaseVersion(cfg.KubeClient.Discovery())
83-
if err != nil {
84-
return fmt.Errorf("failed to detect server version, reason: %s", err)
85-
}
86-
8779
return nil
8880
}

0 commit comments

Comments
 (0)