This repository has been archived by the owner on Sep 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from jian-he/condition
Make UpdateJobConditions public to be used by custom operators
- Loading branch information
Showing
9 changed files
with
268 additions
and
243 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,144 +1,27 @@ | ||
package job_controller | ||
|
||
import ( | ||
common "github.com/kubeflow/common/operator/v1" | ||
v1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"github.com/kubeflow/common/operator/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
) | ||
|
||
const ( | ||
// JobCreatedReason is added in a job when it is created. | ||
JobCreatedReason = "JobCreated" | ||
// JobSucceededReason is added in a job when it is succeeded. | ||
JobSucceededReason = "JobSucceeded" | ||
// JobRunningReason is added in a job when it is running. | ||
JobRunningReason = "JobRunning" | ||
// JobFailedReason is added in a job when it is failed. | ||
JobFailedReason = "JobFailed" | ||
// JobRestarting is added in a job when it is restarting. | ||
JobRestartingReason = "JobRestarting" | ||
|
||
// labels for pods and servers. | ||
ReplicaTypeLabel = "replica-type" | ||
ReplicaIndexLabel = "replica-index" | ||
LabelGroupName = "group-name" | ||
LabelJobName = "job-name" | ||
LabelJobRole = "job-role" | ||
) | ||
|
||
func isSucceeded(status common.JobStatus) bool { | ||
return hasCondition(status, common.JobSucceeded) | ||
} | ||
|
||
func isFailed(status common.JobStatus) bool { | ||
return hasCondition(status, common.JobFailed) | ||
} | ||
|
||
func hasCondition(status common.JobStatus, condType common.JobConditionType) bool { | ||
for _, condition := range status.Conditions { | ||
if condition.Type == condType && condition.Status == v1.ConditionTrue { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func updateJobConditions(jobStatus *common.JobStatus, conditionType common.JobConditionType, reason, message string) error { | ||
condition := newCondition(conditionType, reason, message) | ||
setCondition(jobStatus, condition) | ||
return nil | ||
} | ||
|
||
// initializeReplicaStatuses initializes the ReplicaStatuses for replica. | ||
func initializeReplicaStatuses(jobStatus *common.JobStatus, rtype common.ReplicaType) { | ||
func initializeReplicaStatuses(jobStatus *v1.JobStatus, rtype v1.ReplicaType) { | ||
if jobStatus.ReplicaStatuses == nil { | ||
jobStatus.ReplicaStatuses = make(map[common.ReplicaType]*common.ReplicaStatus) | ||
jobStatus.ReplicaStatuses = make(map[v1.ReplicaType]*v1.ReplicaStatus) | ||
} | ||
|
||
jobStatus.ReplicaStatuses[rtype] = &common.ReplicaStatus{} | ||
jobStatus.ReplicaStatuses[rtype] = &v1.ReplicaStatus{} | ||
} | ||
|
||
// updateJobReplicaStatuses updates the JobReplicaStatuses according to the pod. | ||
func updateJobReplicaStatuses(jobStatus *common.JobStatus, rtype common.ReplicaType, pod *v1.Pod) { | ||
func updateJobReplicaStatuses(jobStatus *v1.JobStatus, rtype v1.ReplicaType, pod *corev1.Pod) { | ||
switch pod.Status.Phase { | ||
case v1.PodRunning: | ||
case corev1.PodRunning: | ||
jobStatus.ReplicaStatuses[rtype].Active++ | ||
case v1.PodSucceeded: | ||
case corev1.PodSucceeded: | ||
jobStatus.ReplicaStatuses[rtype].Succeeded++ | ||
case v1.PodFailed: | ||
case corev1.PodFailed: | ||
jobStatus.ReplicaStatuses[rtype].Failed++ | ||
} | ||
} | ||
|
||
// newCondition creates a new job condition. | ||
func newCondition(conditionType common.JobConditionType, reason, message string) common.JobCondition { | ||
return common.JobCondition{ | ||
Type: conditionType, | ||
Status: v1.ConditionTrue, | ||
LastUpdateTime: metav1.Now(), | ||
LastTransitionTime: metav1.Now(), | ||
Reason: reason, | ||
Message: message, | ||
} | ||
} | ||
|
||
// getCondition returns the condition with the provided type. | ||
func getCondition(status common.JobStatus, condType common.JobConditionType) *common.JobCondition { | ||
for _, condition := range status.Conditions { | ||
if condition.Type == condType { | ||
return &condition | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// setCondition updates the job to include the provided condition. | ||
// If the condition that we are about to add already exists | ||
// and has the same status and reason then we are not going to update. | ||
func setCondition(status *common.JobStatus, condition common.JobCondition) { | ||
// Do nothing if JobStatus have failed condition | ||
if isFailed(*status) { | ||
return | ||
} | ||
|
||
currentCond := getCondition(*status, condition.Type) | ||
|
||
// Do nothing if condition doesn't change | ||
if currentCond != nil && currentCond.Status == condition.Status && currentCond.Reason == condition.Reason { | ||
return | ||
} | ||
|
||
// Do not update lastTransitionTime if the status of the condition doesn't change. | ||
if currentCond != nil && currentCond.Status == condition.Status { | ||
condition.LastTransitionTime = currentCond.LastTransitionTime | ||
} | ||
|
||
// Append the updated condition to the conditions | ||
newConditions := filterOutCondition(status.Conditions, condition.Type) | ||
status.Conditions = append(newConditions, condition) | ||
} | ||
|
||
// filterOutCondition returns a new slice of job conditions without conditions with the provided type. | ||
func filterOutCondition(conditions []common.JobCondition, condType common.JobConditionType) []common.JobCondition { | ||
var newConditions []common.JobCondition | ||
for _, c := range conditions { | ||
if condType == common.JobRestarting && c.Type == common.JobRunning { | ||
continue | ||
} | ||
if condType == common.JobRunning && c.Type == common.JobRestarting { | ||
continue | ||
} | ||
|
||
if c.Type == condType { | ||
continue | ||
} | ||
|
||
// Set the running condition status to be false when current condition failed or succeeded | ||
if (condType == common.JobFailed || condType == common.JobSucceeded) && c.Type == common.JobRunning { | ||
c.Status = v1.ConditionFalse | ||
} | ||
|
||
newConditions = append(newConditions, c) | ||
} | ||
return newConditions | ||
} | ||
} |
Oops, something went wrong.