Skip to content

Commit

Permalink
fix: the pod of the custom ops is always pending (#8456)
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyelei authored Nov 14, 2024
1 parent a16b2eb commit 7fa68e4
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
6 changes: 5 additions & 1 deletion controllers/apps/operations/custom/action_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,14 @@ func (e *ExecAction) buildExecPodSpec(actionCtx ActionContext,
}, execAction.Command...),
}
intctrlutil.InjectZeroResourcesLimitsIfEmpty(container)
toleration, err := getTolerations(e.Cluster, e.Comp)
if err != nil {
return nil, err
}
return &corev1.PodSpec{
Containers: []corev1.Container{*container},
// tolerate all taints
Tolerations: e.Comp.Tolerations,
Tolerations: toleration,
ImagePullSecrets: intctrlutil.BuildImagePullSecrets(),
}, nil
}
6 changes: 5 additions & 1 deletion controllers/apps/operations/custom/action_workload.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,11 @@ func (w *WorkloadAction) buildPodSpec(actionCtx ActionContext,
podSpec.RestartPolicy = corev1.RestartPolicyNever
}
if len(podSpec.Tolerations) == 0 {
podSpec.Tolerations = w.Comp.Tolerations
toleration, err := getTolerations(w.Cluster, w.Comp)
if err != nil {
return nil, err
}
podSpec.Tolerations = toleration
}
switch {
case w.OpsRequest.Spec.CustomOps.ServiceAccountName != nil:
Expand Down
12 changes: 12 additions & 0 deletions controllers/apps/operations/custom/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/apecloud/kubeblocks/pkg/common"
"github.com/apecloud/kubeblocks/pkg/constant"
"github.com/apecloud/kubeblocks/pkg/controller/component"
"github.com/apecloud/kubeblocks/pkg/controller/scheduling"
intctrlutil "github.com/apecloud/kubeblocks/pkg/controllerutil"
)

Expand Down Expand Up @@ -383,3 +384,14 @@ func getNameFromObjectKey(objectKey string) string {
}
return objectKey
}

func getTolerations(cluster *appsv1alpha1.Cluster, compSpec *appsv1alpha1.ClusterComponentSpec) ([]corev1.Toleration, error) {
schedulePolicy, err := scheduling.BuildSchedulingPolicy(cluster, compSpec)
if err != nil {
return nil, err
}
if schedulePolicy == nil {
return nil, nil
}
return schedulePolicy.Tolerations, nil
}
12 changes: 12 additions & 0 deletions controllers/apps/operations/custom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ var _ = Describe("CustomOps", func() {
GetObject()

cluster = testapps.NewClusterFactory(testCtx.DefaultNamespace, clusterName, clusterDefinitionName, clusterVersionName).
SetSchedulingPolicy(appsv1alpha1.SchedulingPolicy{
Tolerations: []corev1.Toleration{
{Operator: corev1.TolerationOpExists, Key: "test"},
},
}).
WithRandomName().AddComponentV2(consensusComp, componentDefObj.Name).SetReplicas(1).Create(&testCtx).GetObject()

fullCompName := constant.GenerateClusterComponentName(cluster.Name, consensusComp)
Expand Down Expand Up @@ -201,6 +206,8 @@ var _ = Describe("CustomOps", func() {

By("mock job is completed, expect for ops phase is Succeed")
job := &jobList.Items[0]
Expect(job.Spec.Template.Spec.Tolerations).Should(HaveLen(1))
Expect(job.Spec.Template.Spec.Tolerations[0].Key).Should(Equal("test"))
patchJobPhase(job, batchv1.JobComplete)
By("reconcile once and make the action succeed")
_, err = GetOpsManager().Reconcile(reqCtx, k8sClient, opsResource)
Expand Down Expand Up @@ -235,6 +242,11 @@ var _ = Describe("CustomOps", func() {
It("Test custom ops with sharding cluster", func() {
By("init environment for sharding cluster")
cluster = testapps.NewClusterFactory(testCtx.DefaultNamespace, "", "", "").
SetSchedulingPolicy(appsv1alpha1.SchedulingPolicy{
Tolerations: []corev1.Toleration{
{Operator: corev1.TolerationOpExists, Key: "test"},
},
}).
WithRandomName().AddShardingSpecV2(consensusComp, compDefName).Create(&testCtx).GetObject()
opsResource.Cluster = cluster

Expand Down

0 comments on commit 7fa68e4

Please sign in to comment.