Skip to content

Commit

Permalink
fix: add tolerations for cronjob (#3881)
Browse files Browse the repository at this point in the history
(cherry picked from commit 903226e)
  • Loading branch information
dengshaojiang committed Jun 20, 2023
1 parent 7d20635 commit de7c9f2
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
5 changes: 5 additions & 0 deletions controllers/dataprotection/backuppolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,10 @@ func (r *BackupPolicyReconciler) buildCronJob(
if err != nil {
return nil, err
}
tolerationPodSpec := corev1.PodSpec{}
if err = addTolerations(&tolerationPodSpec); err != nil {
return nil, err
}
var ttl metav1.Duration
if backupPolicy.Spec.Retention != nil && backupPolicy.Spec.Retention.TTL != nil {
ttl = metav1.Duration{Duration: dataprotectionv1alpha1.ToDuration(backupPolicy.Spec.Retention.TTL)}
Expand All @@ -298,6 +302,7 @@ func (r *BackupPolicyReconciler) buildCronJob(
ServiceAccount: viper.GetString("KUBEBLOCKS_SERVICEACCOUNT_NAME"),
MgrNamespace: viper.GetString(constant.CfgKeyCtrlrMgrNS),
Image: viper.GetString(constant.KBToolsImage),
Tolerations: &tolerationPodSpec,
}
backupPolicyOptionsByte, err := json.Marshal(options)
if err != nil {
Expand Down
13 changes: 13 additions & 0 deletions controllers/dataprotection/backuppolicy_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ var _ = Describe("Backup Policy Controller", func() {

BeforeEach(func() {
viper.Set(constant.CfgKeyCtrlrMgrNS, mgrNamespace)
viper.Set(constant.CfgKeyCtrlrMgrAffinity,
"{\"nodeAffinity\":{\"preferredDuringSchedulingIgnoredDuringExecution\":[{\"preference\":{\"matchExpressions\":[{\"key\":\"kb-controller\",\"operator\":\"In\",\"values\":[\"true\"]}]},\"weight\":100}]}}")
viper.Set(constant.CfgKeyCtrlrMgrTolerations,
"[{\"key\":\"key1\", \"operator\": \"Exists\", \"effect\": \"NoSchedule\"}]")
viper.Set(constant.CfgKeyCtrlrMgrNodeSelector, "{\"beta.kubernetes.io/arch\":\"amd64\"}")

By("By creating a backupTool")
backupTool := testapps.CreateCustomizedObj(&testCtx, "backup/backuptool.yaml",
&dpv1alpha1.BackupTool{}, testapps.RandomizedObjName())
Expand All @@ -125,6 +131,9 @@ var _ = Describe("Backup Policy Controller", func() {

AfterEach(func() {
viper.SetDefault(constant.CfgKeyCtrlrMgrNS, testCtx.DefaultNamespace)
viper.Set(constant.CfgKeyCtrlrMgrAffinity, "")
viper.Set(constant.CfgKeyCtrlrMgrTolerations, "")
viper.Set(constant.CfgKeyCtrlrMgrNodeSelector, "")
})

Context("creates a backup policy", func() {
Expand Down Expand Up @@ -152,6 +161,10 @@ var _ = Describe("Backup Policy Controller", func() {
})).Should(Succeed())
Eventually(testapps.CheckObj(&testCtx, getCronjobKey(dpv1alpha1.BackupTypeDataFile), func(g Gomega, fetched *batchv1.CronJob) {
g.Expect(fetched.Spec.Schedule).To(Equal(defaultSchedule))
g.Expect(fetched.Spec.JobTemplate.Spec.Template.Spec.Tolerations).ShouldNot(BeEmpty())
g.Expect(fetched.Spec.JobTemplate.Spec.Template.Spec.NodeSelector).ShouldNot(BeEmpty())
g.Expect(fetched.Spec.JobTemplate.Spec.Template.Spec.Affinity).ShouldNot(BeNil())
g.Expect(fetched.Spec.JobTemplate.Spec.Template.Spec.Affinity.NodeAffinity).ShouldNot(BeNil())
})).Should(Succeed())
})
It("limit backups to 1", func() {
Expand Down
8 changes: 8 additions & 0 deletions controllers/dataprotection/cue/cronjob.cue
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ options: {
ttl: string
serviceAccount: string
image: string
tolerations: {
tolerations: [...]
affinity: {...}
nodeSelector: {...}
}
}

cronjob: {
Expand All @@ -47,6 +52,9 @@ cronjob: {
jobTemplate: spec: template: spec: {
restartPolicy: "Never"
serviceAccountName: options.serviceAccount
affinity: options.tolerations.affinity
tolerations: options.tolerations.tolerations
nodeSelector: options.tolerations.nodeSelector
containers: [{
name: "backup-policy"
image: options.image
Expand Down
2 changes: 2 additions & 0 deletions controllers/dataprotection/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"time"

"github.com/spf13/viper"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down Expand Up @@ -72,4 +73,5 @@ type backupPolicyOptions struct {
TTL metav1.Duration `json:"ttl,omitempty"`
ServiceAccount string `json:"serviceAccount"`
Image string `json:"image"`
Tolerations *corev1.PodSpec `json:"tolerations"`
}

0 comments on commit de7c9f2

Please sign in to comment.