Skip to content

Commit 8839052

Browse files
authored
chore: lifecycle action job supports default cluster tolerations (#7971)
1 parent 59d3f79 commit 8839052

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

controllers/apps/operations/switchover_util.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,8 @@ func renderSwitchoverCmdJob(ctx context.Context,
225225
for i := range job.Spec.Template.Spec.Containers {
226226
intctrlutil.InjectZeroResourcesLimitsIfEmpty(&job.Spec.Template.Spec.Containers[i])
227227
}
228-
if len(cluster.Spec.Tolerations) > 0 {
229-
job.Spec.Template.Spec.Tolerations = cluster.Spec.Tolerations
228+
if err := component.BuildJobTolerations(job, cluster); err != nil {
229+
return nil, err
230230
}
231231
return job, nil
232232
}

pkg/controller/component/action_utils.go

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ package component
2121

2222
import (
2323
"context"
24+
"encoding/json"
2425
"fmt"
2526
"strings"
2627
"time"
@@ -39,6 +40,7 @@ import (
3940
"github.com/apecloud/kubeblocks/pkg/controller/job"
4041
"github.com/apecloud/kubeblocks/pkg/controller/model"
4142
intctrlutil "github.com/apecloud/kubeblocks/pkg/controllerutil"
43+
viper "github.com/apecloud/kubeblocks/pkg/viperx"
4244
)
4345

4446
// LifeCycleActionType represents the lifecycle action type.
@@ -216,8 +218,8 @@ func renderActionCmdJob(ctx context.Context, cli client.Reader, actionCtx *Actio
216218
},
217219
},
218220
}
219-
if len(actionCtx.cluster.Spec.Tolerations) > 0 {
220-
jobObj.Spec.Template.Spec.Tolerations = actionCtx.cluster.Spec.Tolerations
221+
if err := BuildJobTolerations(jobObj, actionCtx.cluster); err != nil {
222+
return nil, err
221223
}
222224
for i := range jobObj.Spec.Template.Spec.Containers {
223225
intctrlutil.InjectZeroResourcesLimitsIfEmpty(&jobObj.Spec.Template.Spec.Containers[i])
@@ -241,6 +243,34 @@ func renderActionCmdJob(ctx context.Context, cli client.Reader, actionCtx *Actio
241243
return renderedJob, nil
242244
}
243245

246+
// BuildJobTolerations builds the job tolerations.
247+
func BuildJobTolerations(job *batchv1.Job, cluster *appsv1alpha1.Cluster) error {
248+
// build data plane tolerations from config
249+
var tolerations []corev1.Toleration
250+
if val := viper.GetString(constant.CfgKeyDataPlaneTolerations); val != "" {
251+
if err := json.Unmarshal([]byte(val), &tolerations); err != nil {
252+
return err
253+
}
254+
}
255+
256+
if len(job.Spec.Template.Spec.Tolerations) > 0 {
257+
job.Spec.Template.Spec.Tolerations = append(job.Spec.Template.Spec.Tolerations, tolerations...)
258+
} else {
259+
job.Spec.Template.Spec.Tolerations = tolerations
260+
}
261+
262+
// build job tolerations from legacy cluster.spec.Tolerations
263+
if len(cluster.Spec.Tolerations) > 0 {
264+
job.Spec.Template.Spec.Tolerations = append(job.Spec.Template.Spec.Tolerations, cluster.Spec.Tolerations...)
265+
}
266+
267+
// build job tolerations from cluster.spec.SchedulingPolicy.Tolerations
268+
if cluster.Spec.SchedulingPolicy != nil && len(cluster.Spec.SchedulingPolicy.Tolerations) > 0 {
269+
job.Spec.Template.Spec.Tolerations = append(job.Spec.Template.Spec.Tolerations, cluster.Spec.SchedulingPolicy.Tolerations...)
270+
}
271+
return nil
272+
}
273+
244274
// buildLifecycleActionEnvs builds the environment variables for lifecycle actions.
245275
func buildLifecycleActionEnvs(ctx context.Context,
246276
cli client.Reader,

0 commit comments

Comments
 (0)