-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
Showing
6 changed files
with
282 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters