github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cmd/dataprotection/backuppolicy.go (about) 1 /* 2 Copyright (C) 2022-2023 ApeCloud Co., Ltd 3 4 This file is part of KubeBlocks project 5 6 This program is free software: you can redistribute it and/or modify 7 it under the terms of the GNU Affero General Public License as published by 8 the Free Software Foundation, either version 3 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU Affero General Public License for more details. 15 16 You should have received a copy of the GNU Affero General Public License 17 along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 package dataprotection 21 22 import ( 23 "github.com/spf13/cobra" 24 "k8s.io/cli-runtime/pkg/genericclioptions" 25 cmdutil "k8s.io/kubectl/pkg/cmd/util" 26 "k8s.io/kubectl/pkg/util/templates" 27 28 "github.com/1aal/kubeblocks/pkg/cli/cmd/cluster" 29 "github.com/1aal/kubeblocks/pkg/cli/list" 30 "github.com/1aal/kubeblocks/pkg/cli/printer" 31 "github.com/1aal/kubeblocks/pkg/cli/types" 32 "github.com/1aal/kubeblocks/pkg/cli/util" 33 ) 34 35 var ( 36 listBackupPolicyExample = templates.Examples(` 37 # list all backup policies 38 kbcli cluster list-backup-policy 39 40 # list backup policies with specified name 41 kbcli cluster list-backup-policy mypolicy 42 43 # list backup policies of the specified cluster 44 kbcli cluster list-backup-policy --cluster mycluster 45 `) 46 47 describeBackupPolicyExample = templates.Examples(` 48 # describe a backup policy 49 kbcli cluster describe-backup-policy mypolicy 50 51 # describe the default backup policy of the specified cluster 52 kbcli cluster describe-backup-policy --cluster mycluster 53 `) 54 ) 55 56 func newListBackupPolicyCmd(f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Command { 57 o := list.NewListOptions(f, streams, types.BackupPolicyGVR()) 58 clusterName := "" 59 cmd := &cobra.Command{ 60 Use: "list-backup-policy", 61 Short: "List backup policies", 62 Aliases: []string{"list-bp"}, 63 Example: listBackupPolicyExample, 64 ValidArgsFunction: util.ResourceNameCompletionFunc(f, types.BackupPolicyGVR()), 65 Run: func(cmd *cobra.Command, args []string) { 66 if clusterName != "" { 67 o.LabelSelector = util.BuildLabelSelectorByNames(o.LabelSelector, []string{clusterName}) 68 } 69 o.Names = args 70 cmdutil.BehaviorOnFatal(printer.FatalWithRedColor) 71 util.CheckErr(o.Complete()) 72 util.CheckErr(cluster.PrintBackupPolicyList(*o)) 73 }, 74 } 75 cmd.Flags().StringVar(&clusterName, "cluster", "", "The cluster name") 76 o.AddFlags(cmd) 77 78 return cmd 79 } 80 81 func newDescribeBackupPolicyCmd(f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Command { 82 o := cluster.DescribeBackupPolicyOptions{ 83 IOStreams: streams, 84 Factory: f, 85 } 86 cmd := &cobra.Command{ 87 Use: "describe-backup-policy", 88 Short: "Describe a backup policy", 89 Aliases: []string{"desc-backup-policy"}, 90 ValidArgsFunction: util.ResourceNameCompletionFunc(f, types.BackupPolicyGVR()), 91 Example: describeBackupPolicyExample, 92 Run: func(cmd *cobra.Command, args []string) { 93 o.Names = args 94 o.LabelSelector = util.BuildLabelSelectorByNames(o.LabelSelector, o.ClusterNames) 95 cmdutil.BehaviorOnFatal(printer.FatalWithRedColor) 96 util.CheckErr(o.Complete()) 97 util.CheckErr(o.Validate()) 98 util.CheckErr(o.Run()) 99 }, 100 } 101 cmd.Flags().StringSliceVar(&o.ClusterNames, "cluster", []string{}, "The cluster name") 102 return cmd 103 }