Skip to content

Commit

Permalink
fix ejob nil pointer bug
Browse files Browse the repository at this point in the history
Signed-off-by: JunjunLi <junjunli666@gmail.com>
  • Loading branch information
hellolijj committed Jul 8, 2022
1 parent 5380c57 commit 1b32918
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
2 changes: 1 addition & 1 deletion apis/apps/v1alpha1/ephemeraljob_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions config/crd/bases/apps.kruise.io_ephemeraljobs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
15 changes: 11 additions & 4 deletions pkg/controller/ephemeraljob/ephemeraljob_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 1b32918

Please sign in to comment.