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

feat: check pending job's Pod #257

Merged
merged 2 commits into from
Aug 27, 2024
Merged
Changes from 1 commit
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
20 changes: 18 additions & 2 deletions internal/controller/stackrunjob_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (

const jobSelector = "stackrun.deployments.plural.sh"
const jobTimout = time.Minute * 40
const podTimout = time.Minute * 2
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: typo
s/podTimout/podTimeout

Same for jobTimout

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be extended to i.e. 5-10 minutes? @michaeljguarino


// StackRunJobReconciler reconciles a Job resource.
type StackRunJobReconciler struct {
Expand Down Expand Up @@ -71,7 +72,7 @@ func (r *StackRunJobReconciler) Reconcile(ctx context.Context, req ctrl.Request)
// Exit if stack run is not in running state (run status already updated),
// or if the job is still running (harness controls run status).
if stackRun.Status != console.StackStatusRunning || job.Status.CompletionTime.IsZero() {
if isActiveJobTimout(stackRun.Status, job) {
if isActiveJobTimout(stackRun.Status, job) || r.isActiveJobPodFailed(ctx, stackRun.Status, job) {
if err := r.killJob(ctx, job); err != nil {
return ctrl.Result{}, err
}
Expand Down Expand Up @@ -178,13 +179,28 @@ func getStackRunID(job *batchv1.Job) string {
return strings.TrimPrefix(job.Name, "stack-")
}

func isActiveJob(stackStatus console.StackStatus, job *batchv1.Job) bool {
return stackStatus == console.StackStatusPending && job.Status.CompletionTime.IsZero() && !job.Status.StartTime.IsZero()
}

func isActiveJobTimout(stackStatus console.StackStatus, job *batchv1.Job) bool {
if stackStatus == console.StackStatusPending && job.Status.CompletionTime.IsZero() && !job.Status.StartTime.IsZero() {
if isActiveJob(stackStatus, job) {
return time.Now().After(job.Status.StartTime.Add(jobTimout))
}
return false
}

func (r *StackRunJobReconciler) isActiveJobPodFailed(ctx context.Context, stackStatus console.StackStatus, job *batchv1.Job) bool {
if isActiveJob(stackStatus, job) {
status, err := r.getJobPodStatus(ctx, job.Spec.Selector.MatchLabels)
if err != nil || status == console.StackStatusFailed {
// in case when job's Pod wasn't created yet
return time.Now().After(job.Status.StartTime.Add(podTimout))
}
}
return false
}

// SetupWithManager sets up the controller with the Manager.
func (r *StackRunJobReconciler) SetupWithManager(mgr ctrl.Manager) error {
byAnnotation := predicate.NewPredicateFuncs(func(object k8sClient.Object) bool {
Expand Down
Loading