Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check existence of backuprepo before creating a backup #337

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions pkg/cmd/backuprepo/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
28 changes: 28 additions & 0 deletions pkg/cmd/cluster/dataprotection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checks if existing more than one backupRepo, only need one default backupRepo

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

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
Expand Down
27 changes: 21 additions & 6 deletions pkg/cmd/cluster/dataprotection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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...)
}

Expand Down Expand Up @@ -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())
Expand Down
Loading