diff --git a/pkg/cmd/backuprepo/common.go b/pkg/cmd/backuprepo/common.go index 4c667cd90..f794776e0 100644 --- a/pkg/cmd/backuprepo/common.go +++ b/pkg/cmd/backuprepo/common.go @@ -29,10 +29,11 @@ import ( "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/dynamic" - "github.com/apecloud/kbcli/pkg/types" - "github.com/apecloud/kbcli/pkg/util" dpv1alpha1 "github.com/apecloud/kubeblocks/apis/dataprotection/v1alpha1" storagev1alpha1 "github.com/apecloud/kubeblocks/apis/storage/v1alpha1" + + "github.com/apecloud/kbcli/pkg/types" + "github.com/apecloud/kbcli/pkg/util" ) const ( diff --git a/pkg/cmd/cluster/dataprotection.go b/pkg/cmd/cluster/dataprotection.go index f3cf87f2b..217d7f44e 100644 --- a/pkg/cmd/cluster/dataprotection.go +++ b/pkg/cmd/cluster/dataprotection.go @@ -206,6 +206,34 @@ func (o *CreateBackupOptions) Validate() error { o.BackupSpec.BackupMethod, backupPolicy.Name, strings.Join(availableMethods, ", ")) } + // check if the backup repo exists in backup policy + backupRepoName := backupPolicy.Spec.BackupRepoName + if backupRepoName != nil { + _, err := o.Dynamic.Resource(types.BackupRepoGVR()).Get(context.Background(), *backupRepoName, metav1.GetOptions{}) + if err != nil { + return err + } + } else { + backupRepoList, err := o.Dynamic.Resource(types.BackupRepoGVR()).List(context.Background(), metav1.ListOptions{}) + if err != nil { + return err + } + if len(backupRepoList.Items) == 0 { + return fmt.Errorf("No backuprepo found") + } + var defaultBackupRepos []unstructured.Unstructured + for _, item := range backupRepoList.Items { + if item.GetAnnotations()[dptypes.DefaultBackupRepoAnnotationKey] == "true" { + defaultBackupRepos = append(defaultBackupRepos, item) + } + } + if len(defaultBackupRepos) == 0 { + return fmt.Errorf("No default backuprepo exists") + } + if len(defaultBackupRepos) > 1 { + return fmt.Errorf("Cluster %s has multiple default backuprepos", o.Name) + } + } // TODO: check if pvc exists // valid retention period diff --git a/pkg/cmd/cluster/dataprotection_test.go b/pkg/cmd/cluster/dataprotection_test.go index 303fac5eb..e01a5559c 100644 --- a/pkg/cmd/cluster/dataprotection_test.go +++ b/pkg/cmd/cluster/dataprotection_test.go @@ -97,7 +97,7 @@ var _ = Describe("DataProtection", func() { }) Context("backup", func() { - initClient := func(policies ...*dpv1alpha1.BackupPolicy) { + initClient := func(objs ...runtime.Object) { clusterDef := testing.FakeClusterDef() cluster := testing.FakeCluster(testing.ClusterName, testing.Namespace) clusterDefLabel := map[string]string{ @@ -108,9 +108,7 @@ var _ = Describe("DataProtection", func() { objects := []runtime.Object{ cluster, clusterDef, &pods.Items[0], } - for _, v := range policies { - objects = append(objects, v) - } + objects = append(objects, objs...) tf.FakeDynamicClient = testing.FakeDynamicClient(objects...) } @@ -216,18 +214,35 @@ var _ = Describe("DataProtection", func() { o.Dynamic = tf.FakeDynamicClient Expect(o.Validate().Error()).Should(ContainSubstring("backup method can not be empty, you can specify it by --method")) + By("test without default backup repo") + repo := testing.FakeBackupRepo(repoName, false) + initClient(defaultBackupPolicy, repo) + o.Dynamic = tf.FakeDynamicClient + o.BackupSpec.BackupMethod = testing.BackupMethodName + Expect(o.Validate()).Should(MatchError(fmt.Errorf("No default backuprepo exists"))) + + By("test with two default backup repos") + repo1 := testing.FakeBackupRepo("repo1", true) + repo2 := testing.FakeBackupRepo("repo2", true) + initClient(defaultBackupPolicy, repo1, repo2) + o.Dynamic = tf.FakeDynamicClient + o.BackupSpec.BackupMethod = testing.BackupMethodName + Expect(o.Validate()).Should(MatchError(fmt.Errorf("Cluster %s has multiple default backuprepos", o.Name))) + By("test with one default backupPolicy") - initClient(defaultBackupPolicy) + defaultRepo := testing.FakeBackupRepo("default-repo", true) + initClient(defaultBackupPolicy, defaultRepo) o.Dynamic = tf.FakeDynamicClient o.BackupSpec.BackupMethod = testing.BackupMethodName Expect(o.Validate()).Should(Succeed()) }) It("run backup command", func() { + defaultRepo := testing.FakeBackupRepo("default-repo", true) defaultBackupPolicy := testing.FakeBackupPolicy(policyName, testing.ClusterName) otherBackupPolicy := testing.FakeBackupPolicy("otherPolicy", testing.ClusterName) otherBackupPolicy.Annotations = map[string]string{} - initClient(defaultBackupPolicy, otherBackupPolicy) + initClient(defaultBackupPolicy, otherBackupPolicy, defaultRepo) By("test backup with default backupPolicy") cmd := NewCreateBackupCmd(tf, streams) Expect(cmd).ShouldNot(BeNil())