Skip to content

Commit 2363171

Browse files
authored
Add pause and resume command (#152)
* Add pause command Signed-off-by: hmsayem <hmsayem@appscode.com> * Add resume command Signed-off-by: hmsayem <hmsayem@appscode.com> * Refactor: remove redundant codes Signed-off-by: hmsayem <hmsayem@appscode.com> * Update long message for pause and resume command Signed-off-by: hmsayem <hmsayem@appscode.com>
1 parent e6d907b commit 2363171

File tree

6 files changed

+282
-0
lines changed

6 files changed

+282
-0
lines changed

pkg/cli.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ var (
4444
ResticTag = "latest"
4545
)
4646

47+
var (
48+
backupConfig string
49+
backupBatch string
50+
)
51+
4752
var imgRestic docker.Docker
4853

4954
func init() {

pkg/pause.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
Copyright AppsCode Inc. and Contributors
3+
4+
Licensed under the AppsCode Community License 1.0.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
https://github.com/appscode/licenses/raw/1.0.0/AppsCode-Community-1.0.0.md
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package pkg
18+
19+
import (
20+
cs "stash.appscode.dev/apimachinery/client/clientset/versioned"
21+
22+
"github.com/pkg/errors"
23+
"github.com/spf13/cobra"
24+
"k8s.io/cli-runtime/pkg/genericclioptions"
25+
)
26+
27+
func NewCmdPause(clientGetter genericclioptions.RESTClientGetter) *cobra.Command {
28+
cmd := &cobra.Command{
29+
Use: "pause",
30+
Short: `Pause Stash backup temporarily`,
31+
DisableAutoGenTag: true,
32+
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
33+
34+
cfg, err := clientGetter.ToRESTConfig()
35+
if err != nil {
36+
return errors.Wrap(err, "failed to read kubeconfig")
37+
}
38+
39+
namespace, _, err = clientGetter.ToRawKubeConfigLoader().Namespace()
40+
if err != nil {
41+
return err
42+
}
43+
44+
stashClient, err = cs.NewForConfig(cfg)
45+
if err != nil {
46+
return err
47+
}
48+
49+
return nil
50+
},
51+
}
52+
cmd.AddCommand(NewCmdPauseBackup())
53+
cmd.PersistentFlags().StringVar(&backupConfig, "backupconfig", backupConfig, "Name of the Backupconfiguration to pause")
54+
cmd.PersistentFlags().StringVar(&backupBatch, "backupbatch", backupBatch, "Name of the BackupBatch to pause")
55+
return cmd
56+
}

pkg/pause_backup.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
Copyright AppsCode Inc. and Contributors
3+
4+
Licensed under the AppsCode Community License 1.0.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
https://github.com/appscode/licenses/raw/1.0.0/AppsCode-Community-1.0.0.md
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package pkg
18+
19+
import (
20+
"context"
21+
"fmt"
22+
23+
"stash.appscode.dev/apimachinery/apis/stash/v1beta1"
24+
v1beta1_util "stash.appscode.dev/apimachinery/client/clientset/versioned/typed/stash/v1beta1/util"
25+
26+
"github.com/spf13/cobra"
27+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28+
"k8s.io/klog/v2"
29+
"k8s.io/kubectl/pkg/util/templates"
30+
)
31+
32+
var (
33+
pauseBackupExample = templates.Examples(`
34+
# Pause a BackupConfigration
35+
stash pause backup --namespace=<namespace> --backupconfig=<backup-configuration-name>
36+
stash pause backup --backup-config=sample-mongodb -n demo`)
37+
)
38+
39+
func NewCmdPauseBackup() *cobra.Command {
40+
var cmd = &cobra.Command{
41+
Use: "backup",
42+
Short: `Pause backup`,
43+
Long: `Pause backup by setting "paused" field of BackupConfiguration/BackupBatch to "true"`,
44+
Example: pauseBackupExample,
45+
DisableAutoGenTag: true,
46+
RunE: func(cmd *cobra.Command, args []string) error {
47+
if backupConfig == "" && backupBatch == "" {
48+
return fmt.Errorf("neither BackupConfiguration nor BackupBatch name has been provided")
49+
}
50+
if backupConfig != "" {
51+
if err := setBackupConfigurationPausedField(true); err != nil {
52+
return err
53+
}
54+
klog.Infof("BackupConfiguration %s/%s has been paused successfully.", namespace, backupConfig)
55+
} else {
56+
if err := setBackupBatchPausedField(true); err != nil {
57+
return err
58+
}
59+
klog.Infof("BackupBatch %s/%s has been paused successfully.", namespace, backupBatch)
60+
}
61+
return nil
62+
},
63+
}
64+
return cmd
65+
}
66+
67+
func setBackupConfigurationPausedField(value bool) error {
68+
bc, err := stashClient.StashV1beta1().BackupConfigurations(namespace).Get(context.TODO(), backupConfig, metav1.GetOptions{})
69+
if err != nil {
70+
return err
71+
}
72+
_, _, err = v1beta1_util.PatchBackupConfiguration(
73+
context.TODO(),
74+
stashClient.StashV1beta1(),
75+
bc,
76+
func(in *v1beta1.BackupConfiguration) *v1beta1.BackupConfiguration {
77+
in.Spec.Paused = value
78+
return in
79+
},
80+
metav1.PatchOptions{},
81+
)
82+
return err
83+
}
84+
85+
func setBackupBatchPausedField(value bool) error {
86+
bb, err := stashClient.StashV1beta1().BackupBatches(namespace).Get(context.TODO(), backupBatch, metav1.GetOptions{})
87+
if err != nil {
88+
return err
89+
}
90+
_, _, err = v1beta1_util.PatchBackupBatch(
91+
context.TODO(),
92+
stashClient.StashV1beta1(),
93+
bb,
94+
func(in *v1beta1.BackupBatch) *v1beta1.BackupBatch {
95+
in.Spec.Paused = value
96+
return in
97+
},
98+
metav1.PatchOptions{},
99+
)
100+
return err
101+
}

pkg/resume.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
Copyright AppsCode Inc. and Contributors
3+
4+
Licensed under the AppsCode Community License 1.0.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
https://github.com/appscode/licenses/raw/1.0.0/AppsCode-Community-1.0.0.md
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package pkg
18+
19+
import (
20+
cs "stash.appscode.dev/apimachinery/client/clientset/versioned"
21+
22+
"github.com/pkg/errors"
23+
"github.com/spf13/cobra"
24+
"k8s.io/cli-runtime/pkg/genericclioptions"
25+
)
26+
27+
func NewCmdResume(clientGetter genericclioptions.RESTClientGetter) *cobra.Command {
28+
cmd := &cobra.Command{
29+
Use: "resume",
30+
Short: `Resume stash resources`,
31+
DisableAutoGenTag: true,
32+
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
33+
34+
cfg, err := clientGetter.ToRESTConfig()
35+
if err != nil {
36+
return errors.Wrap(err, "failed to read kubeconfig")
37+
}
38+
39+
namespace, _, err = clientGetter.ToRawKubeConfigLoader().Namespace()
40+
if err != nil {
41+
return err
42+
}
43+
44+
stashClient, err = cs.NewForConfig(cfg)
45+
if err != nil {
46+
return err
47+
}
48+
49+
return nil
50+
},
51+
}
52+
cmd.AddCommand(NewCmdResumeBackup())
53+
cmd.PersistentFlags().StringVar(&backupConfig, "backupconfig", backupConfig, "Name of the Backupconfiguration")
54+
cmd.PersistentFlags().StringVar(&backupBatch, "backupbatch", backupBatch, "Name of the BackupBatch")
55+
return cmd
56+
}

pkg/resume_backup.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
Copyright AppsCode Inc. and Contributors
3+
4+
Licensed under the AppsCode Community License 1.0.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
https://github.com/appscode/licenses/raw/1.0.0/AppsCode-Community-1.0.0.md
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package pkg
18+
19+
import (
20+
"fmt"
21+
22+
"github.com/spf13/cobra"
23+
"k8s.io/klog/v2"
24+
"k8s.io/kubectl/pkg/util/templates"
25+
)
26+
27+
var (
28+
resumeBackupExample = templates.Examples(`
29+
# Resume a BackupConfigration
30+
stash resume backup --namespace=<namespace> --backupconfig=<backup-configuration-name>
31+
stash resume backup --backup-config=sample-mongodb -n demo`)
32+
)
33+
34+
func NewCmdResumeBackup() *cobra.Command {
35+
var cmd = &cobra.Command{
36+
Use: "backup",
37+
Short: `Resume backup`,
38+
Long: `Resume backup by setting "paused" field of BackupConfiguration/BackupBatch to "false"`,
39+
Example: resumeBackupExample,
40+
DisableAutoGenTag: true,
41+
RunE: func(cmd *cobra.Command, args []string) error {
42+
if backupConfig == "" && backupBatch == "" {
43+
return fmt.Errorf("neither BackupConfiguration nor BackupBatch name has been provided")
44+
}
45+
46+
if backupConfig != "" {
47+
if err := setBackupConfigurationPausedField(false); err != nil {
48+
return err
49+
}
50+
klog.Infof("BackupConfiguration %s/%s has been resumed successfully.", namespace, backupConfig)
51+
} else {
52+
if err := setBackupBatchPausedField(false); err != nil {
53+
return err
54+
}
55+
klog.Infof("BackupBatch %s/%s has been resumed successfully.", namespace, backupBatch)
56+
}
57+
return nil
58+
},
59+
}
60+
61+
return cmd
62+
}

pkg/root.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ func NewRootCmd() *cobra.Command {
5959
rootCmd.AddCommand(NewCmdUnlockRepository(f))
6060
rootCmd.AddCommand(NewCmdCreate(f))
6161
rootCmd.AddCommand(NewCmdClone(f))
62+
rootCmd.AddCommand(NewCmdPause(f))
63+
rootCmd.AddCommand(NewCmdResume(f))
6264

6365
return rootCmd
6466
}

0 commit comments

Comments
 (0)