Skip to content

Commit

Permalink
Merge pull request #623 from equinor/bug/unable-to-stop-running-batch…
Browse files Browse the repository at this point in the history
…-job

fix bug preventing stop of batch jobs with status running
  • Loading branch information
nilsgstrabo authored May 14, 2024
2 parents 9cc98b2 + 0bf9a9c commit 51013af
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
31 changes: 13 additions & 18 deletions api/environments/environment_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2266,6 +2266,7 @@ func Test_StopJob(t *testing.T) {
{name: "validJob2", jobStatus: v1.RadixBatchJobStatus{Name: "validJob2", Phase: ""}},
{name: "validJob3", jobStatus: v1.RadixBatchJobStatus{Name: "validJob3", Phase: v1.BatchJobPhaseWaiting}},
{name: "validJob4", jobStatus: v1.RadixBatchJobStatus{Name: "validJob4", Phase: v1.BatchJobPhaseActive}},
{name: "validJob5", jobStatus: v1.RadixBatchJobStatus{Name: "validJob5", Phase: v1.BatchJobPhaseRunning}},
}
invalidJobs := []JobTestData{
{name: "invalidJob1", jobStatus: v1.RadixBatchJobStatus{Name: "invalidJob1", Phase: v1.BatchJobPhaseSucceeded}},
Expand Down Expand Up @@ -2294,40 +2295,34 @@ func Test_StopJob(t *testing.T) {
WithActiveFrom(time.Now()))
require.NoError(t, err)

jobSpecList := append(
slice.Map(validJobs, func(j JobTestData) v1.RadixBatchJob { return v1.RadixBatchJob{Name: j.name} }),
slice.Map(invalidJobs, func(j JobTestData) v1.RadixBatchJob { return v1.RadixBatchJob{Name: j.name} })...,
)
jobStatuses := append(
slice.Map(validJobs, func(j JobTestData) v1.RadixBatchJobStatus { return j.jobStatus }),
slice.Map(invalidJobs, func(j JobTestData) v1.RadixBatchJobStatus { return j.jobStatus })...,
)
// Insert test data
testData := []v1.RadixBatch{
{
ObjectMeta: metav1.ObjectMeta{
Name: batchTypeBatchName,
Labels: labels.Merge(labels.ForApplicationName(anyAppName), labels.ForComponentName(anyJobName), labels.ForBatchType(kube.RadixBatchTypeBatch)),
},
Spec: v1.RadixBatchSpec{
Jobs: []v1.RadixBatchJob{
{Name: validJobs[0].name}, {Name: validJobs[1].name}, {Name: validJobs[2].name}, {Name: validJobs[3].name},
{Name: invalidJobs[0].name}, {Name: invalidJobs[1].name}, {Name: invalidJobs[2].name},
}},
Spec: v1.RadixBatchSpec{Jobs: jobSpecList},
Status: v1.RadixBatchStatus{
JobStatuses: []v1.RadixBatchJobStatus{
validJobs[0].jobStatus, validJobs[1].jobStatus, validJobs[2].jobStatus, validJobs[3].jobStatus,
invalidJobs[0].jobStatus, invalidJobs[1].jobStatus, invalidJobs[2].jobStatus,
},
JobStatuses: jobStatuses,
},
},
{
ObjectMeta: metav1.ObjectMeta{
Name: batchTypeJobName,
Labels: labels.Merge(labels.ForApplicationName(anyAppName), labels.ForComponentName(anyJobName), labels.ForBatchType(kube.RadixBatchTypeJob)),
},
Spec: v1.RadixBatchSpec{
Jobs: []v1.RadixBatchJob{
{Name: validJobs[0].name}, {Name: validJobs[1].name}, {Name: validJobs[2].name}, {Name: validJobs[3].name},
{Name: invalidJobs[0].name}, {Name: invalidJobs[1].name}, {Name: invalidJobs[2].name},
}},
Spec: v1.RadixBatchSpec{Jobs: jobSpecList},
Status: v1.RadixBatchStatus{
JobStatuses: []v1.RadixBatchJobStatus{
validJobs[0].jobStatus, validJobs[1].jobStatus, validJobs[2].jobStatus, validJobs[3].jobStatus,
invalidJobs[0].jobStatus, invalidJobs[1].jobStatus, invalidJobs[2].jobStatus,
},
JobStatuses: jobStatuses,
},
},
}
Expand Down
9 changes: 7 additions & 2 deletions api/environments/job_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -553,12 +553,17 @@ func getReplicaStatusByJobPodStatusPhase(statusPhase radixv1.RadixBatchJobPodPha

// check if batch can be stopped
func isBatchStoppable(condition radixv1.RadixBatchCondition) bool {
return condition.Type == "" || condition.Type == radixv1.BatchConditionTypeActive || condition.Type == radixv1.BatchConditionTypeWaiting
return condition.Type == "" ||
condition.Type == radixv1.BatchConditionTypeActive ||
condition.Type == radixv1.BatchConditionTypeWaiting
}

// check if batch job can be stopped
func isBatchJobStoppable(status radixv1.RadixBatchJobStatus) bool {
return status.Phase == "" || status.Phase == radixv1.BatchJobPhaseActive || status.Phase == radixv1.BatchJobPhaseWaiting
return status.Phase == "" ||
status.Phase == radixv1.BatchJobPhaseActive ||
status.Phase == radixv1.BatchJobPhaseWaiting ||
status.Phase == radixv1.BatchJobPhaseRunning
}

func batchNotFoundError(batchName string) error {
Expand Down

0 comments on commit 51013af

Please sign in to comment.