From c29b0c158c3ba8031174fb42e6dd956043572dd2 Mon Sep 17 00:00:00 2001 From: Junjun Li Date: Mon, 11 Jul 2022 14:01:30 +0800 Subject: [PATCH] fix ejob nil pointer bug (#1016) Signed-off-by: JunjunLi --- apis/apps/v1alpha1/ephemeraljob_types.go | 2 +- .../crd/bases/apps.kruise.io_ephemeraljobs.yaml | 4 ++-- .../ephemeraljob/ephemeraljob_controller.go | 15 +++++++++++---- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/apis/apps/v1alpha1/ephemeraljob_types.go b/apis/apps/v1alpha1/ephemeraljob_types.go index ef6d018f87..65d11b9b81 100644 --- a/apis/apps/v1alpha1/ephemeraljob_types.go +++ b/apis/apps/v1alpha1/ephemeraljob_types.go @@ -37,7 +37,7 @@ type EphemeralJobSpec struct { // Replicas indicates a part of the quantity from matched pods by selector. // Usually it is used for gray scale working. - // if Replicas exceeded the matched number by selector, replicas will not work. + // if Replicas exceeded the matched number by selector or not be set, replicas will not work. Replicas *int32 `json:"replicas,omitempty"` // Parallelism specifies the maximum desired number of pods which matches running ephemeral containers. diff --git a/config/crd/bases/apps.kruise.io_ephemeraljobs.yaml b/config/crd/bases/apps.kruise.io_ephemeraljobs.yaml index 3e73107de6..024b64f0c8 100644 --- a/config/crd/bases/apps.kruise.io_ephemeraljobs.yaml +++ b/config/crd/bases/apps.kruise.io_ephemeraljobs.yaml @@ -88,8 +88,8 @@ spec: replicas: description: Replicas indicates a part of the quantity from matched pods by selector. Usually it is used for gray scale working. if - Replicas exceeded the matched number by selector, replicas will - not work. + Replicas exceeded the matched number by selector or not be set, + replicas will not work. format: int32 type: integer selector: diff --git a/pkg/controller/ephemeraljob/ephemeraljob_controller.go b/pkg/controller/ephemeraljob/ephemeraljob_controller.go index 28fdaefe76..50d9edc3e1 100644 --- a/pkg/controller/ephemeraljob/ephemeraljob_controller.go +++ b/pkg/controller/ephemeraljob/ephemeraljob_controller.go @@ -255,7 +255,7 @@ func (r *ReconcileEphemeralJob) filterPods(job *appsv1alpha1.EphemeralJob) ([]*v continue } - if len(targetPods) < int(*job.Spec.Replicas) { + if job.Spec.Replicas == nil || len(targetPods) < int(*job.Spec.Replicas) { targetPods = append(targetPods, &podList.Items[i]) } } @@ -375,20 +375,27 @@ func (r *ReconcileEphemeralJob) calculateStatus(job *appsv1alpha1.EphemeralJob, return err } + var replicas int32 + if job.Spec.Replicas == nil { + replicas = job.Status.Matches + } else { + replicas = *job.Spec.Replicas + } + if job.Status.Matches == 0 { job.Status.Phase = appsv1alpha1.EphemeralJobWaiting job.Status.Conditions = addConditions(job.Status.Conditions, appsv1alpha1.EJobMatchedEmpty, "MatchEmpty", "job match no pods") - } else if job.Status.Succeeded == *job.Spec.Replicas && job.Status.Succeeded > 0 { + } else if job.Status.Succeeded == replicas && job.Status.Succeeded > 0 { job.Status.CompletionTime = timeNow() job.Status.Phase = appsv1alpha1.EphemeralJobSucceeded job.Status.Conditions = addConditions(job.Status.Conditions, appsv1alpha1.EJobSucceeded, "JobSucceeded", "job success to run all tasks") } else if job.Status.Running > 0 { job.Status.Phase = appsv1alpha1.EphemeralJobRunning - } else if job.Status.Failed == *job.Spec.Replicas { + } else if job.Status.Failed == replicas { job.Status.CompletionTime = timeNow() job.Status.Phase = appsv1alpha1.EphemeralJobFailed job.Status.Conditions = addConditions(job.Status.Conditions, appsv1alpha1.EJobFailed, "JobFailed", "job failed to run all tasks") - } else if job.Status.Waiting == *job.Spec.Replicas { + } else if job.Status.Waiting == replicas { job.Status.Phase = appsv1alpha1.EphemeralJobWaiting } else { job.Status.Phase = appsv1alpha1.EphemeralJobUnknown