-
Notifications
You must be signed in to change notification settings - Fork 706
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set a Running condition when the XGBoostJob is completed and doesn't …
…have a Running condition Signed-off-by: Yuki Iwai <[email protected]>
- Loading branch information
Showing
4 changed files
with
163 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package xgboost | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/sirupsen/logrus" | ||
|
||
commonv1 "github.com/kubeflow/common/pkg/apis/common/v1" | ||
commonutil "github.com/kubeflow/common/pkg/util" | ||
) | ||
|
||
func setRunningCondition(logger *logrus.Entry, jobName string, jobStatus *commonv1.JobStatus) error { | ||
msg := fmt.Sprintf("XGBoostJob %s is running.", jobName) | ||
if condition := findStatusCondition(jobStatus.Conditions, commonv1.JobRunning); condition == nil { | ||
err := commonutil.UpdateJobConditions(jobStatus, commonv1.JobRunning, xgboostJobRunningReason, msg) | ||
if err != nil { | ||
logger.Infof("Append job condition error: %v", err) | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func findStatusCondition(conditions []commonv1.JobCondition, conditionType commonv1.JobConditionType) *commonv1.JobCondition { | ||
for i := range conditions { | ||
if conditions[i].Type == conditionType { | ||
return &conditions[i] | ||
} | ||
} | ||
return nil | ||
} |
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 |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package xgboost | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/google/go-cmp/cmp/cmpopts" | ||
"github.com/sirupsen/logrus" | ||
corev1 "k8s.io/api/core/v1" | ||
|
||
commonv1 "github.com/kubeflow/common/pkg/apis/common/v1" | ||
) | ||
|
||
var ignoreJobConditionsTimeOpts = cmpopts.IgnoreFields(commonv1.JobCondition{}, "LastUpdateTime", "LastTransitionTime") | ||
|
||
func TestSetRunningCondition(t *testing.T) { | ||
jobName := "test-xbgoostjob" | ||
logger := logrus.NewEntry(logrus.New()) | ||
tests := map[string]struct { | ||
input []commonv1.JobCondition | ||
want []commonv1.JobCondition | ||
}{ | ||
"input doesn't have a running condition": { | ||
input: []commonv1.JobCondition{ | ||
{ | ||
Type: commonv1.JobSucceeded, | ||
Reason: "XGBoostJobSucceeded", | ||
Message: "XGBoostJob test-xbgoostjob is successfully completed.", | ||
Status: corev1.ConditionTrue, | ||
}, | ||
}, | ||
want: []commonv1.JobCondition{ | ||
{ | ||
Type: commonv1.JobSucceeded, | ||
Reason: "XGBoostJobSucceeded", | ||
Message: "XGBoostJob test-xbgoostjob is successfully completed.", | ||
Status: corev1.ConditionTrue, | ||
}, | ||
{ | ||
Type: commonv1.JobRunning, | ||
Reason: "XGBoostJobRunning", | ||
Message: "XGBoostJob test-xbgoostjob is running.", | ||
Status: corev1.ConditionTrue, | ||
}, | ||
}, | ||
}, | ||
"input has a running condition": { | ||
input: []commonv1.JobCondition{ | ||
{ | ||
Type: commonv1.JobFailed, | ||
Reason: "XGBoostJobFailed", | ||
Message: "XGBoostJob test-sgboostjob is failed because 2 Worker replica(s) failed.", | ||
Status: corev1.ConditionTrue, | ||
}, | ||
{ | ||
Type: commonv1.JobRunning, | ||
Reason: "XGBoostJobRunning", | ||
Message: "XGBoostJob test-xbgoostjob is running.", | ||
Status: corev1.ConditionTrue, | ||
}, | ||
}, | ||
want: []commonv1.JobCondition{ | ||
{ | ||
Type: commonv1.JobFailed, | ||
Reason: "XGBoostJobFailed", | ||
Message: "XGBoostJob test-sgboostjob is failed because 2 Worker replica(s) failed.", | ||
Status: corev1.ConditionTrue, | ||
}, | ||
{ | ||
Type: commonv1.JobRunning, | ||
Reason: "XGBoostJobRunning", | ||
Message: "XGBoostJob test-xbgoostjob is running.", | ||
Status: corev1.ConditionTrue, | ||
}, | ||
}, | ||
}, | ||
} | ||
for name, tc := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
jobStatus := &commonv1.JobStatus{Conditions: tc.input} | ||
err := setRunningCondition(logger, jobName, jobStatus) | ||
if err != nil { | ||
t.Fatalf("failed to update job condition: %v", err) | ||
} | ||
if diff := cmp.Diff(tc.want, jobStatus.Conditions, ignoreJobConditionsTimeOpts); len(diff) != 0 { | ||
t.Fatalf("Unexpected conditions from setRunningCondition (-want,+got):\n%s", diff) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestFindStatusCondition(t *testing.T) { | ||
tests := map[string]struct { | ||
conditions []commonv1.JobCondition | ||
want *commonv1.JobCondition | ||
}{ | ||
"conditions have a running condition": { | ||
conditions: []commonv1.JobCondition{ | ||
{ | ||
Type: commonv1.JobRunning, | ||
}, | ||
}, | ||
want: &commonv1.JobCondition{ | ||
Type: commonv1.JobRunning, | ||
}, | ||
}, | ||
"condition doesn't have a running condition": { | ||
conditions: []commonv1.JobCondition{ | ||
{ | ||
Type: commonv1.JobSucceeded, | ||
}, | ||
}, | ||
want: nil, | ||
}, | ||
} | ||
for name, tc := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
got := findStatusCondition(tc.conditions, commonv1.JobRunning) | ||
if diff := cmp.Diff(tc.want, got, ignoreJobConditionsTimeOpts); len(diff) != 0 { | ||
t.Fatalf("Unexpected jobConditions from findStatusCondition (-want,got):\n%s", diff) | ||
} | ||
}) | ||
} | ||
} |
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