Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Remove dup code #1022

Merged
merged 2 commits into from
Jun 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 13 additions & 22 deletions pkg/controller.v1/tensorflow/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,17 +178,6 @@ func (tc *TFController) updateTFJobStatus(tfjob *tfv1.TFJob) error {

// updateTFJobConditions updates the conditions of the given tfjob.
func updateTFJobConditions(tfjob *tfv1.TFJob, conditionType common.JobConditionType, reason, message string) error {
// Check if the condition exists in the conditions.
// See https://github.com/kubeflow/pytorch-operator/issues/88
for i, c := range tfjob.Status.Conditions {
// Found the condition, thus no need to update the LastTransitionTime.
if c.Type == conditionType && c.Status == v1.ConditionTrue {
tfjob.Status.Conditions[i].LastUpdateTime = metav1.Now()
tfjob.Status.Conditions[i].Reason = reason
tfjob.Status.Conditions[i].Message = message
return nil
}
}
condition := newCondition(conditionType, reason, message)
setCondition(&tfjob.Status, condition)
return nil
Expand Down Expand Up @@ -260,24 +249,26 @@ func isFailed(status common.JobStatus) bool {
// 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 TFJobStatus have failed condition
if isFailed(*status) {
// Do nothing if TFJobStatus is completed.
if isFailed(*status) || isSucceeded(*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
if currentCond != nil {
if currentCond.Status == condition.Status &&
currentCond.Reason == condition.Reason &&
currentCond.Message == condition.Message {
// Do nothing if the condition does not change.
return
} else if currentCond.Status == condition.Status {
// Do not update lastTransitionTime if the status of the condition doesn't change.
condition.LastTransitionTime = currentCond.LastTransitionTime
}
}

// Append the updated condition to the
// Append the updated condition to the status.Conditions.
newConditions := filterOutCondition(status.Conditions, condition.Type)
status.Conditions = append(newConditions, condition)
}
Expand Down
35 changes: 13 additions & 22 deletions pkg/controller.v1beta2/tensorflow/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,17 +156,6 @@ func (tc *TFController) updateTFJobStatus(tfjob *tfv1beta2.TFJob) error {

// updateTFJobConditions updates the conditions of the given tfjob.
func updateTFJobConditions(tfjob *tfv1beta2.TFJob, conditionType common.JobConditionType, reason, message string) error {
// Check if the condition exists in the conditions.
// See https://github.com/kubeflow/pytorch-operator/issues/88
for i, c := range tfjob.Status.Conditions {
// Found the condition, thus no need to update the LastTransitionTime.
if c.Type == conditionType && c.Status == v1.ConditionTrue {
tfjob.Status.Conditions[i].LastUpdateTime = metav1.Now()
tfjob.Status.Conditions[i].Reason = reason
tfjob.Status.Conditions[i].Message = message
return nil
}
}
condition := newCondition(conditionType, reason, message)
setCondition(&tfjob.Status, condition)
return nil
Expand Down Expand Up @@ -238,24 +227,26 @@ func isFailed(status common.JobStatus) bool {
// 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 TFJobStatus have failed condition
if isFailed(*status) {
// Do nothing if TFJobStatus is completed.
if isFailed(*status) || isSucceeded(*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
if currentCond != nil {
if currentCond.Status == condition.Status &&
currentCond.Reason == condition.Reason &&
currentCond.Message == condition.Message {
// Do nothing if the condition does not change.
return
} else if currentCond.Status == condition.Status {
// Do not update lastTransitionTime if the status of the condition doesn't change.
condition.LastTransitionTime = currentCond.LastTransitionTime
}
}

// Append the updated condition to the
// Append the updated condition to the status.Conditions.
newConditions := filterOutCondition(status.Conditions, condition.Type)
status.Conditions = append(newConditions, condition)
}
Expand Down