Skip to content

Commit

Permalink
chore: lifecycle action job supports default cluster tolerations (#7971)
Browse files Browse the repository at this point in the history
  • Loading branch information
Y-Rookie authored Aug 15, 2024
1 parent 59d3f79 commit 8839052
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
4 changes: 2 additions & 2 deletions controllers/apps/operations/switchover_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,8 @@ func renderSwitchoverCmdJob(ctx context.Context,
for i := range job.Spec.Template.Spec.Containers {
intctrlutil.InjectZeroResourcesLimitsIfEmpty(&job.Spec.Template.Spec.Containers[i])
}
if len(cluster.Spec.Tolerations) > 0 {
job.Spec.Template.Spec.Tolerations = cluster.Spec.Tolerations
if err := component.BuildJobTolerations(job, cluster); err != nil {
return nil, err
}
return job, nil
}
Expand Down
34 changes: 32 additions & 2 deletions pkg/controller/component/action_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package component

import (
"context"
"encoding/json"
"fmt"
"strings"
"time"
Expand All @@ -39,6 +40,7 @@ import (
"github.com/apecloud/kubeblocks/pkg/controller/job"
"github.com/apecloud/kubeblocks/pkg/controller/model"
intctrlutil "github.com/apecloud/kubeblocks/pkg/controllerutil"
viper "github.com/apecloud/kubeblocks/pkg/viperx"
)

// LifeCycleActionType represents the lifecycle action type.
Expand Down Expand Up @@ -216,8 +218,8 @@ func renderActionCmdJob(ctx context.Context, cli client.Reader, actionCtx *Actio
},
},
}
if len(actionCtx.cluster.Spec.Tolerations) > 0 {
jobObj.Spec.Template.Spec.Tolerations = actionCtx.cluster.Spec.Tolerations
if err := BuildJobTolerations(jobObj, actionCtx.cluster); err != nil {
return nil, err
}
for i := range jobObj.Spec.Template.Spec.Containers {
intctrlutil.InjectZeroResourcesLimitsIfEmpty(&jobObj.Spec.Template.Spec.Containers[i])
Expand All @@ -241,6 +243,34 @@ func renderActionCmdJob(ctx context.Context, cli client.Reader, actionCtx *Actio
return renderedJob, nil
}

// BuildJobTolerations builds the job tolerations.
func BuildJobTolerations(job *batchv1.Job, cluster *appsv1alpha1.Cluster) error {
// build data plane tolerations from config
var tolerations []corev1.Toleration
if val := viper.GetString(constant.CfgKeyDataPlaneTolerations); val != "" {
if err := json.Unmarshal([]byte(val), &tolerations); err != nil {
return err
}
}

if len(job.Spec.Template.Spec.Tolerations) > 0 {
job.Spec.Template.Spec.Tolerations = append(job.Spec.Template.Spec.Tolerations, tolerations...)
} else {
job.Spec.Template.Spec.Tolerations = tolerations
}

// build job tolerations from legacy cluster.spec.Tolerations
if len(cluster.Spec.Tolerations) > 0 {
job.Spec.Template.Spec.Tolerations = append(job.Spec.Template.Spec.Tolerations, cluster.Spec.Tolerations...)
}

// build job tolerations from cluster.spec.SchedulingPolicy.Tolerations
if cluster.Spec.SchedulingPolicy != nil && len(cluster.Spec.SchedulingPolicy.Tolerations) > 0 {
job.Spec.Template.Spec.Tolerations = append(job.Spec.Template.Spec.Tolerations, cluster.Spec.SchedulingPolicy.Tolerations...)
}
return nil
}

// buildLifecycleActionEnvs builds the environment variables for lifecycle actions.
func buildLifecycleActionEnvs(ctx context.Context,
cli client.Reader,
Expand Down

0 comments on commit 8839052

Please sign in to comment.