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: set backuppolicy.spec.backofflimit to default value #7192

Merged
merged 6 commits into from
May 6, 2024
Merged
1 change: 1 addition & 0 deletions apis/dataprotection/v1alpha1/backuppolicy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type BackupPolicySpec struct {
// Specifies the number of retries before marking the backup as failed.
//
// +optional
// +kubebuilder:default=2
// +kubebuilder:validation:Minimum=0
// +kubebuilder:validation:Maximum=10
BackoffLimit *int32 `json:"backoffLimit,omitempty"`
Expand Down
13 changes: 10 additions & 3 deletions controllers/apps/transformer_cluster_backup_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,11 @@ func (r *clusterBackupPolicyTransformer) syncBackupPolicy(comp *appsv1alpha1.Clu
if r.Cluster.Spec.Backup != nil && r.Cluster.Spec.Backup.RepoName != "" {
backupPolicy.Spec.BackupRepoName = &r.Cluster.Spec.Backup.RepoName
}
backupPolicy.Spec.BackoffLimit = r.backupPolicy.BackoffLimit

if r.backupPolicy.BackoffLimit == nil {
backupPolicy.Spec.BackoffLimit = &dptypes.DefaultBackOffLimit
} else {
backupPolicy.Spec.BackoffLimit = r.backupPolicy.BackoffLimit
ldming marked this conversation as resolved.
Show resolved Hide resolved
}
r.syncBackupMethods(backupPolicy, comp)
r.syncBackupPolicyTargetSpec(backupPolicy, comp)
}
Expand Down Expand Up @@ -397,7 +400,11 @@ func (r *clusterBackupPolicyTransformer) buildBackupPolicy(comp *appsv1alpha1.Cl
bpSpec.BackupRepoName = &cluster.Spec.Backup.RepoName
}
bpSpec.PathPrefix = buildBackupPathPrefix(cluster, comp.Name)
bpSpec.BackoffLimit = r.backupPolicy.BackoffLimit
if r.backupPolicy.BackoffLimit == nil {
bpSpec.BackoffLimit = &dptypes.DefaultBackOffLimit
} else {
bpSpec.BackoffLimit = r.backupPolicy.BackoffLimit
}
backupPolicy.Spec = bpSpec
r.setDefaultEncryptionConfig(backupPolicy)
r.syncBackupPolicyTargetSpec(backupPolicy, comp)
Expand Down
14 changes: 14 additions & 0 deletions controllers/dataprotection/backuppolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,20 @@ func (r *BackupPolicyReconciler) Reconcile(ctx context.Context, req ctrl.Request
return r.Status().Patch(ctx, backupPolicy, patch)
}

if backupPolicy.Spec.BackoffLimit == nil {
if err = r.setDefaultBackoffLimit(reqCtx, backupPolicy); err != nil {
return intctrlutil.CheckedRequeueWithError(err, reqCtx.Log, "")
}
return ctrl.Result{}, nil
}

Copy link
Contributor

Choose a reason for hiding this comment

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

these code can be removed and api server will set the default value if using kubebuilder marker

if err = r.validateBackupPolicy(backupPolicy); err != nil {
if err = patchStatus(dpv1alpha1.UnavailablePhase, err.Error()); err != nil {
return intctrlutil.CheckedRequeueWithError(err, reqCtx.Log, "")
}
return intctrlutil.Reconciled()
}

if err = patchStatus(dpv1alpha1.AvailablePhase, ""); err != nil {
return intctrlutil.CheckedRequeueWithError(err, reqCtx.Log, "")
}
Expand Down Expand Up @@ -133,3 +141,9 @@ func (r *BackupPolicyReconciler) deleteExternalResources(
_ *dpv1alpha1.BackupPolicy) error {
return nil
}

func (r *BackupPolicyReconciler) setDefaultBackoffLimit(reqCtx intctrlutil.RequestCtx, backupPolicy *dpv1alpha1.BackupPolicy) error {
patch := client.MergeFrom(backupPolicy.DeepCopy())
backupPolicy.Spec.BackoffLimit = &dptypes.DefaultBackOffLimit
return r.Patch(reqCtx.Ctx, backupPolicy, patch)
}
13 changes: 13 additions & 0 deletions controllers/dataprotection/backuppolicy_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (

dpv1alpha1 "github.com/apecloud/kubeblocks/apis/dataprotection/v1alpha1"
"github.com/apecloud/kubeblocks/pkg/constant"
dptypes "github.com/apecloud/kubeblocks/pkg/dataprotection/types"
"github.com/apecloud/kubeblocks/pkg/generics"
testapps "github.com/apecloud/kubeblocks/pkg/testutil/apps"
testdp "github.com/apecloud/kubeblocks/pkg/testutil/dataprotection"
Expand Down Expand Up @@ -60,6 +61,18 @@ var _ = Describe("BackupPolicy Controller test", func() {
})

Context("create a backup policy", func() {
It("test backup policy without setting backoffLimit", func() {
By("creating backupPolicy without setting backoffLimit")
bp := testdp.NewFakeBackupPolicy(&testCtx, func(backupPolicy *dpv1alpha1.BackupPolicy) {
backupPolicy.Spec.BackoffLimit = nil
})
By("expect its backoffLimit should be set to the value of DefaultBackOffLimit")
Eventually(testapps.CheckObj(&testCtx, client.ObjectKeyFromObject(bp),
func(g Gomega, bp *dpv1alpha1.BackupPolicy) {
g.Expect(*bp.Spec.BackoffLimit).Should(Equal(dptypes.DefaultBackOffLimit))
})).Should(Succeed())
})

It("backup policy should be available for target", func() {
By("creating actionSet used by backup policy")
as := testdp.NewFakeActionSet(&testCtx)
Expand Down
Loading