Skip to content

Commit

Permalink
Add pause and resume command (#152)
Browse files Browse the repository at this point in the history
* Add pause command

Signed-off-by: hmsayem <[email protected]>

* Add resume command

Signed-off-by: hmsayem <[email protected]>

* Refactor: remove redundant codes

Signed-off-by: hmsayem <[email protected]>

* Update long message for pause and resume command

Signed-off-by: hmsayem <[email protected]>
  • Loading branch information
hmsayem authored Feb 8, 2022
1 parent e6d907b commit 2363171
Show file tree
Hide file tree
Showing 6 changed files with 282 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pkg/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ var (
ResticTag = "latest"
)

var (
backupConfig string
backupBatch string
)

var imgRestic docker.Docker

func init() {
Expand Down
56 changes: 56 additions & 0 deletions pkg/pause.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
Copyright AppsCode Inc. and Contributors
Licensed under the AppsCode Community License 1.0.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://github.com/appscode/licenses/raw/1.0.0/AppsCode-Community-1.0.0.md
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package pkg

import (
cs "stash.appscode.dev/apimachinery/client/clientset/versioned"

"github.com/pkg/errors"
"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericclioptions"
)

func NewCmdPause(clientGetter genericclioptions.RESTClientGetter) *cobra.Command {
cmd := &cobra.Command{
Use: "pause",
Short: `Pause Stash backup temporarily`,
DisableAutoGenTag: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {

cfg, err := clientGetter.ToRESTConfig()
if err != nil {
return errors.Wrap(err, "failed to read kubeconfig")
}

namespace, _, err = clientGetter.ToRawKubeConfigLoader().Namespace()
if err != nil {
return err
}

stashClient, err = cs.NewForConfig(cfg)
if err != nil {
return err
}

return nil
},
}
cmd.AddCommand(NewCmdPauseBackup())
cmd.PersistentFlags().StringVar(&backupConfig, "backupconfig", backupConfig, "Name of the Backupconfiguration to pause")
cmd.PersistentFlags().StringVar(&backupBatch, "backupbatch", backupBatch, "Name of the BackupBatch to pause")
return cmd
}
101 changes: 101 additions & 0 deletions pkg/pause_backup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
Copyright AppsCode Inc. and Contributors
Licensed under the AppsCode Community License 1.0.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://github.com/appscode/licenses/raw/1.0.0/AppsCode-Community-1.0.0.md
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package pkg

import (
"context"
"fmt"

"stash.appscode.dev/apimachinery/apis/stash/v1beta1"
v1beta1_util "stash.appscode.dev/apimachinery/client/clientset/versioned/typed/stash/v1beta1/util"

"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog/v2"
"k8s.io/kubectl/pkg/util/templates"
)

var (
pauseBackupExample = templates.Examples(`
# Pause a BackupConfigration
stash pause backup --namespace=<namespace> --backupconfig=<backup-configuration-name>
stash pause backup --backup-config=sample-mongodb -n demo`)
)

func NewCmdPauseBackup() *cobra.Command {
var cmd = &cobra.Command{
Use: "backup",
Short: `Pause backup`,
Long: `Pause backup by setting "paused" field of BackupConfiguration/BackupBatch to "true"`,
Example: pauseBackupExample,
DisableAutoGenTag: true,
RunE: func(cmd *cobra.Command, args []string) error {
if backupConfig == "" && backupBatch == "" {
return fmt.Errorf("neither BackupConfiguration nor BackupBatch name has been provided")
}
if backupConfig != "" {
if err := setBackupConfigurationPausedField(true); err != nil {
return err
}
klog.Infof("BackupConfiguration %s/%s has been paused successfully.", namespace, backupConfig)
} else {
if err := setBackupBatchPausedField(true); err != nil {
return err
}
klog.Infof("BackupBatch %s/%s has been paused successfully.", namespace, backupBatch)
}
return nil
},
}
return cmd
}

func setBackupConfigurationPausedField(value bool) error {
bc, err := stashClient.StashV1beta1().BackupConfigurations(namespace).Get(context.TODO(), backupConfig, metav1.GetOptions{})
if err != nil {
return err
}
_, _, err = v1beta1_util.PatchBackupConfiguration(
context.TODO(),
stashClient.StashV1beta1(),
bc,
func(in *v1beta1.BackupConfiguration) *v1beta1.BackupConfiguration {
in.Spec.Paused = value
return in
},
metav1.PatchOptions{},
)
return err
}

func setBackupBatchPausedField(value bool) error {
bb, err := stashClient.StashV1beta1().BackupBatches(namespace).Get(context.TODO(), backupBatch, metav1.GetOptions{})
if err != nil {
return err
}
_, _, err = v1beta1_util.PatchBackupBatch(
context.TODO(),
stashClient.StashV1beta1(),
bb,
func(in *v1beta1.BackupBatch) *v1beta1.BackupBatch {
in.Spec.Paused = value
return in
},
metav1.PatchOptions{},
)
return err
}
56 changes: 56 additions & 0 deletions pkg/resume.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
Copyright AppsCode Inc. and Contributors
Licensed under the AppsCode Community License 1.0.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://github.com/appscode/licenses/raw/1.0.0/AppsCode-Community-1.0.0.md
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package pkg

import (
cs "stash.appscode.dev/apimachinery/client/clientset/versioned"

"github.com/pkg/errors"
"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericclioptions"
)

func NewCmdResume(clientGetter genericclioptions.RESTClientGetter) *cobra.Command {
cmd := &cobra.Command{
Use: "resume",
Short: `Resume stash resources`,
DisableAutoGenTag: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {

cfg, err := clientGetter.ToRESTConfig()
if err != nil {
return errors.Wrap(err, "failed to read kubeconfig")
}

namespace, _, err = clientGetter.ToRawKubeConfigLoader().Namespace()
if err != nil {
return err
}

stashClient, err = cs.NewForConfig(cfg)
if err != nil {
return err
}

return nil
},
}
cmd.AddCommand(NewCmdResumeBackup())
cmd.PersistentFlags().StringVar(&backupConfig, "backupconfig", backupConfig, "Name of the Backupconfiguration")
cmd.PersistentFlags().StringVar(&backupBatch, "backupbatch", backupBatch, "Name of the BackupBatch")
return cmd
}
62 changes: 62 additions & 0 deletions pkg/resume_backup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
Copyright AppsCode Inc. and Contributors
Licensed under the AppsCode Community License 1.0.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://github.com/appscode/licenses/raw/1.0.0/AppsCode-Community-1.0.0.md
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package pkg

import (
"fmt"

"github.com/spf13/cobra"
"k8s.io/klog/v2"
"k8s.io/kubectl/pkg/util/templates"
)

var (
resumeBackupExample = templates.Examples(`
# Resume a BackupConfigration
stash resume backup --namespace=<namespace> --backupconfig=<backup-configuration-name>
stash resume backup --backup-config=sample-mongodb -n demo`)
)

func NewCmdResumeBackup() *cobra.Command {
var cmd = &cobra.Command{
Use: "backup",
Short: `Resume backup`,
Long: `Resume backup by setting "paused" field of BackupConfiguration/BackupBatch to "false"`,
Example: resumeBackupExample,
DisableAutoGenTag: true,
RunE: func(cmd *cobra.Command, args []string) error {
if backupConfig == "" && backupBatch == "" {
return fmt.Errorf("neither BackupConfiguration nor BackupBatch name has been provided")
}

if backupConfig != "" {
if err := setBackupConfigurationPausedField(false); err != nil {
return err
}
klog.Infof("BackupConfiguration %s/%s has been resumed successfully.", namespace, backupConfig)
} else {
if err := setBackupBatchPausedField(false); err != nil {
return err
}
klog.Infof("BackupBatch %s/%s has been resumed successfully.", namespace, backupBatch)
}
return nil
},
}

return cmd
}
2 changes: 2 additions & 0 deletions pkg/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ func NewRootCmd() *cobra.Command {
rootCmd.AddCommand(NewCmdUnlockRepository(f))
rootCmd.AddCommand(NewCmdCreate(f))
rootCmd.AddCommand(NewCmdClone(f))
rootCmd.AddCommand(NewCmdPause(f))
rootCmd.AddCommand(NewCmdResume(f))

return rootCmd
}

0 comments on commit 2363171

Please sign in to comment.