Skip to content

Commit

Permalink
fix comments
Browse files Browse the repository at this point in the history
  • Loading branch information
hzxuzhonghu committed May 13, 2019
1 parent 2e25e83 commit b4c2a85
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 85 deletions.
12 changes: 9 additions & 3 deletions pkg/controllers/job/job_controller_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ func (cc *Controller) killJob(jobInfo *apis.JobInfo, updateStatus state.UpdateSt
}

if updateStatus != nil {
updateStatus(&job.Status)
if updateStatus(&job.Status) {
job.Status.State.LastTransitionTime = metav1.Now()
}
}

// Update Job status
Expand Down Expand Up @@ -165,7 +167,9 @@ func (cc *Controller) createJob(jobInfo *apis.JobInfo, updateStatus state.Update
}

if updateStatus != nil {
updateStatus(&job.Status)
if updateStatus(&job.Status) {
job.Status.State.LastTransitionTime = metav1.Now()
}
}

if job, err := cc.vkClients.BatchV1alpha1().Jobs(job.Namespace).UpdateStatus(job); err != nil {
Expand Down Expand Up @@ -316,7 +320,9 @@ func (cc *Controller) syncJob(jobInfo *apis.JobInfo, updateStatus state.UpdateSt
}

if updateStatus != nil {
updateStatus(&job.Status)
if updateStatus(&job.Status) {
job.Status.State.LastTransitionTime = metav1.Now()
}
}

if job, err := cc.vkClients.BatchV1alpha1().Jobs(job.Namespace).UpdateStatus(job); err != nil {
Expand Down
6 changes: 2 additions & 4 deletions pkg/controllers/job/state/aborted.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ limitations under the License.
package state

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

vkv1 "volcano.sh/volcano/pkg/apis/batch/v1alpha1"
"volcano.sh/volcano/pkg/controllers/apis"
)
Expand All @@ -30,10 +28,10 @@ type abortedState struct {
func (as *abortedState) Execute(action vkv1.Action) error {
switch action {
case vkv1.ResumeJobAction:
return KillJob(as.job, func(status *vkv1.JobStatus) {
return KillJob(as.job, func(status *vkv1.JobStatus) bool {
status.State.Phase = vkv1.Restarting
status.State.LastTransitionTime = metav1.Now()
status.RetryCount++
return true
})
default:
return KillJob(as.job, nil)
Expand Down
10 changes: 5 additions & 5 deletions pkg/controllers/job/state/aborting.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,19 @@ func (ps *abortingState) Execute(action vkv1.Action) error {
switch action {
case vkv1.ResumeJobAction:
// Already in Restarting phase, just sync it
return KillJob(ps.job, func(status *vkv1.JobStatus) {
status.State.Phase = vkv1.Restarting
status.State.LastTransitionTime = metav1.Now()
return KillJob(ps.job, func(status *vkv1.JobStatus) bool {
status.RetryCount++
return false
})
default:
return KillJob(ps.job, func(status *vkv1.JobStatus) {
return KillJob(ps.job, func(status *vkv1.JobStatus) bool {
// If any "alive" pods, still in Aborting phase
if status.Terminating != 0 || status.Pending != 0 || status.Running != 0 {
status.State.Phase = vkv1.Aborting
return false
} else {
status.State.Phase = vkv1.Aborted
status.State.LastTransitionTime = metav1.Now()
return true
}
})
}
Expand Down
8 changes: 3 additions & 5 deletions pkg/controllers/job/state/completing.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ limitations under the License.
package state

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

vkv1 "volcano.sh/volcano/pkg/apis/batch/v1alpha1"
"volcano.sh/volcano/pkg/controllers/apis"
)
Expand All @@ -28,13 +26,13 @@ type completingState struct {
}

func (ps *completingState) Execute(action vkv1.Action) error {
return KillJob(ps.job, func(status *vkv1.JobStatus) {
return KillJob(ps.job, func(status *vkv1.JobStatus) bool {
// If any "alive" pods, still in Completing phase
if status.Terminating != 0 || status.Pending != 0 || status.Running != 0 {
status.State.Phase = vkv1.Completing
return false
} else {
status.State.Phase = vkv1.Completed
status.State.LastTransitionTime = metav1.Now()
return true
}
})
}
2 changes: 1 addition & 1 deletion pkg/controllers/job/state/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"volcano.sh/volcano/pkg/controllers/apis"
)

type UpdateStatusFn func(status *vkv1.JobStatus)
type UpdateStatusFn func(status *vkv1.JobStatus) (jobPhaseChanged bool)
type ActionFn func(job *apis.JobInfo, fn UpdateStatusFn) error

var (
Expand Down
25 changes: 10 additions & 15 deletions pkg/controllers/job/state/inqueue.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ limitations under the License.
package state

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

vkv1 "volcano.sh/volcano/pkg/apis/batch/v1alpha1"
"volcano.sh/volcano/pkg/controllers/apis"
)
Expand All @@ -30,44 +28,41 @@ type inqueueState struct {
func (ps *inqueueState) Execute(action vkv1.Action) error {
switch action {
case vkv1.RestartJobAction:
return KillJob(ps.job, func(status *vkv1.JobStatus) {
return KillJob(ps.job, func(status *vkv1.JobStatus) bool {
phase := vkv1.Pending
if status.Terminating != 0 {
phase = vkv1.Restarting
status.RetryCount++
}
status.State.LastTransitionTime = metav1.Now()
status.State.Phase = phase
return true
})

case vkv1.AbortJobAction:
return KillJob(ps.job, func(status *vkv1.JobStatus) {
return KillJob(ps.job, func(status *vkv1.JobStatus) bool {
phase := vkv1.Pending
if status.Terminating != 0 {
phase = vkv1.Aborting
}
status.State.LastTransitionTime = metav1.Now()
status.State.Phase = phase
return true
})
case vkv1.CompleteJobAction:
return KillJob(ps.job, func(status *vkv1.JobStatus) {
return KillJob(ps.job, func(status *vkv1.JobStatus) bool {
phase := vkv1.Completed
if status.Terminating != 0 {
phase = vkv1.Completing
}
status.State.LastTransitionTime = metav1.Now()
status.State.Phase = phase
return true
})
default:
return SyncJob(ps.job, func(status *vkv1.JobStatus) {
phase := vkv1.Inqueue

return SyncJob(ps.job, func(status *vkv1.JobStatus) bool {
if ps.job.Job.Spec.MinAvailable <= status.Running+status.Succeeded+status.Failed {
status.State.LastTransitionTime = metav1.Now()
phase = vkv1.Running
status.State.Phase = vkv1.Running
return true
}

status.State.Phase = phase
return false
})
}
return nil
Expand Down
22 changes: 10 additions & 12 deletions pkg/controllers/job/state/pending.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ limitations under the License.
package state

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

vkv1 "volcano.sh/volcano/pkg/apis/batch/v1alpha1"
"volcano.sh/volcano/pkg/controllers/apis"
)
Expand All @@ -30,49 +28,49 @@ type pendingState struct {
func (ps *pendingState) Execute(action vkv1.Action) error {
switch action {
case vkv1.RestartJobAction:
return KillJob(ps.job, func(status *vkv1.JobStatus) {
return KillJob(ps.job, func(status *vkv1.JobStatus) bool {
phase := vkv1.Pending
if status.Terminating != 0 {
phase = vkv1.Restarting
status.RetryCount++
}
status.State.Phase = phase
status.State.LastTransitionTime = metav1.Now()
return true
})

case vkv1.AbortJobAction:
return KillJob(ps.job, func(status *vkv1.JobStatus) {
return KillJob(ps.job, func(status *vkv1.JobStatus) bool {
phase := vkv1.Pending
if status.Terminating != 0 {
phase = vkv1.Aborting
}
status.State.Phase = phase
status.State.LastTransitionTime = metav1.Now()
return true
})
case vkv1.CompleteJobAction:
return KillJob(ps.job, func(status *vkv1.JobStatus) {
return KillJob(ps.job, func(status *vkv1.JobStatus) bool {
phase := vkv1.Completed
if status.Terminating != 0 {
phase = vkv1.Completing
}
status.State.Phase = phase
status.State.LastTransitionTime = metav1.Now()
return true
})
case vkv1.EnqueueAction:
return SyncJob(ps.job, func(status *vkv1.JobStatus) {
return SyncJob(ps.job, func(status *vkv1.JobStatus) bool {
phase := vkv1.Inqueue

if ps.job.Job.Spec.MinAvailable <= status.Running+status.Succeeded+status.Failed {
phase = vkv1.Running
}

status.State.Phase = phase
status.State.LastTransitionTime = metav1.Now()
return true
})
default:
return CreateJob(ps.job, func(status *vkv1.JobStatus) {
return CreateJob(ps.job, func(status *vkv1.JobStatus) bool {
status.State.Phase = vkv1.Pending
status.State.LastTransitionTime = metav1.Now()
return true
})
}
}
16 changes: 6 additions & 10 deletions pkg/controllers/job/state/restarting.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ limitations under the License.
package state

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

vkv1 "volcano.sh/volcano/pkg/apis/batch/v1alpha1"
"volcano.sh/volcano/pkg/controllers/apis"
)
Expand All @@ -28,9 +26,7 @@ type restartingState struct {
}

func (ps *restartingState) Execute(action vkv1.Action) error {
return KillJob(ps.job, func(status *vkv1.JobStatus) {
phase := vkv1.Restarting

return KillJob(ps.job, func(status *vkv1.JobStatus) bool {
// Get the maximum number of retries.
maxRetry := DefaultMaxRetry
if ps.job.Job.Spec.MaxRetry != 0 {
Expand All @@ -39,21 +35,21 @@ func (ps *restartingState) Execute(action vkv1.Action) error {

if status.RetryCount >= maxRetry {
// Failed is the phase that the job is restarted failed reached the maximum number of retries.
phase = vkv1.Failed
status.State.LastTransitionTime = metav1.Now()
status.State.Phase = vkv1.Failed
return true
} else {
total := int32(0)
for _, task := range ps.job.Job.Spec.Tasks {
total += task.Replicas
}

if total-status.Terminating >= status.MinAvailable {
phase = vkv1.Pending
status.State.LastTransitionTime = metav1.Now()
status.State.Phase = vkv1.Pending
return true
}
}

status.State.Phase = phase
return false
})

}
43 changes: 18 additions & 25 deletions pkg/controllers/job/state/running.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ limitations under the License.
package state

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

vkv1 "volcano.sh/volcano/pkg/apis/batch/v1alpha1"
"volcano.sh/volcano/pkg/controllers/apis"
)
Expand All @@ -30,55 +28,50 @@ type runningState struct {
func (ps *runningState) Execute(action vkv1.Action) error {
switch action {
case vkv1.RestartJobAction:
return KillJob(ps.job, func(status *vkv1.JobStatus) {
phase := vkv1.Running
return KillJob(ps.job, func(status *vkv1.JobStatus) bool {
if status.Terminating != 0 {
phase = vkv1.Restarting
status.State.LastTransitionTime = metav1.Now()
status.State.Phase = vkv1.Restarting
status.RetryCount++
return true
}

status.State.Phase = phase
return false
})
case vkv1.AbortJobAction:
return KillJob(ps.job, func(status *vkv1.JobStatus) {
phase := vkv1.Running
return KillJob(ps.job, func(status *vkv1.JobStatus) bool {
if status.Terminating != 0 {
phase = vkv1.Aborting
status.State.LastTransitionTime = metav1.Now()
status.State.Phase = vkv1.Aborting
return true
}

status.State.Phase = phase
return false
})
case vkv1.TerminateJobAction:
return KillJob(ps.job, func(status *vkv1.JobStatus) {
phase := vkv1.Running
return KillJob(ps.job, func(status *vkv1.JobStatus) bool {
if status.Terminating != 0 {
phase = vkv1.Terminating
status.State.LastTransitionTime = metav1.Now()
status.State.Phase = vkv1.Terminating
return true
}

status.State.Phase = phase
return false
})
case vkv1.CompleteJobAction:
return KillJob(ps.job, func(status *vkv1.JobStatus) {
return KillJob(ps.job, func(status *vkv1.JobStatus) bool {
phase := vkv1.Completed
if status.Terminating != 0 {
phase = vkv1.Completing
}

status.State.Phase = phase
status.State.LastTransitionTime = metav1.Now()
return true
})
default:
return SyncJob(ps.job, func(status *vkv1.JobStatus) {
phase := vkv1.Running
return SyncJob(ps.job, func(status *vkv1.JobStatus) bool {
if status.Succeeded+status.Failed == TotalTasks(ps.job.Job) {
phase = vkv1.Completed
status.State.LastTransitionTime = metav1.Now()
status.State.Phase = vkv1.Completed
return true
}

status.State.Phase = phase
return false
})
}
}
Loading

0 comments on commit b4c2a85

Please sign in to comment.