volcano.sh/volcano@v1.9.0/pkg/controllers/job/state/factory.go (about) 1 /* 2 Copyright 2017 The Volcano Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package state 18 19 import ( 20 v1 "k8s.io/api/core/v1" 21 22 vcbatch "volcano.sh/apis/pkg/apis/batch/v1alpha1" 23 "volcano.sh/apis/pkg/apis/bus/v1alpha1" 24 "volcano.sh/volcano/pkg/controllers/apis" 25 ) 26 27 // PhaseMap to store the pod phases. 28 type PhaseMap map[v1.PodPhase]struct{} 29 30 // UpdateStatusFn updates the job status. 31 type UpdateStatusFn func(status *vcbatch.JobStatus) (jobPhaseChanged bool) 32 33 // ActionFn will create or delete Pods according to Job's spec. 34 type ActionFn func(job *apis.JobInfo, fn UpdateStatusFn) error 35 36 // KillActionFn kill all Pods of Job with phase not in podRetainPhase. 37 type KillActionFn func(job *apis.JobInfo, podRetainPhase PhaseMap, fn UpdateStatusFn) error 38 39 // PodRetainPhaseNone stores no phase. 40 var PodRetainPhaseNone = PhaseMap{} 41 42 // PodRetainPhaseSoft stores PodSucceeded and PodFailed Phase. 43 var PodRetainPhaseSoft = PhaseMap{ 44 v1.PodSucceeded: {}, 45 v1.PodFailed: {}, 46 } 47 48 var ( 49 // SyncJob will create or delete Pods according to Job's spec. 50 SyncJob ActionFn 51 // KillJob kill all Pods of Job with phase not in podRetainPhase. 52 KillJob KillActionFn 53 ) 54 55 // State interface. 56 type State interface { 57 // Execute executes the actions based on current state. 58 Execute(act v1alpha1.Action) error 59 } 60 61 // NewState gets the state from the volcano job Phase. 62 func NewState(jobInfo *apis.JobInfo) State { 63 job := jobInfo.Job 64 switch job.Status.State.Phase { 65 case vcbatch.Pending: 66 return &pendingState{job: jobInfo} 67 case vcbatch.Running: 68 return &runningState{job: jobInfo} 69 case vcbatch.Restarting: 70 return &restartingState{job: jobInfo} 71 case vcbatch.Terminated, vcbatch.Completed, vcbatch.Failed: 72 return &finishedState{job: jobInfo} 73 case vcbatch.Terminating: 74 return &terminatingState{job: jobInfo} 75 case vcbatch.Aborting: 76 return &abortingState{job: jobInfo} 77 case vcbatch.Aborted: 78 return &abortedState{job: jobInfo} 79 case vcbatch.Completing: 80 return &completingState{job: jobInfo} 81 } 82 83 // It's pending by default. 84 return &pendingState{job: jobInfo} 85 }