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

NO-JIRA: (refactor) job completion uses event instead polling #888

Merged
Merged
Show file tree
Hide file tree
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
13 changes: 7 additions & 6 deletions pkg/controller/periodic/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ func (j *JobController) CreateGathererJob(ctx context.Context, dataGatherName, i
}

// WaitForJobCompletion listen the Kubernetes events to check if job finished.
func (j *JobController) WaitForJobCompletion(ctx context.Context, job *batchv1.Job) error {
func (j *JobController) WaitForJobCompletion(ctx context.Context, jobName string) error {
watcher, err := j.kubeClient.BatchV1().Jobs(insightsNamespace).
Watch(ctx, metav1.ListOptions{FieldSelector: fmt.Sprintf("metadata.name=%s", job.Name)})
Watch(ctx, metav1.ListOptions{FieldSelector: fmt.Sprintf("metadata.name=%s", jobName)})
if err != nil {
return err
}
Expand All @@ -123,19 +123,20 @@ func (j *JobController) WaitForJobCompletion(ctx context.Context, job *batchv1.J
if !ok {
return fmt.Errorf("watcher channel was closed unexpectedly")
}
if event.Type == watch.Error {
return fmt.Errorf("watcher received error event: %v", event.Object)

if event.Type != watch.Modified {
continue
}

job := event.Object.(*batchv1.Job)
rluders marked this conversation as resolved.
Show resolved Hide resolved
if job == nil {
return fmt.Errorf("cannot cast event objeto into job: %v", event.Object)
return fmt.Errorf("failed to cast job event: %v", event.Object)
}
if job.Status.Succeeded > 0 {
return nil
}
if job.Status.Failed > 0 {
return fmt.Errorf("job %s failed", job.Name)
return fmt.Errorf("job %s failed: %s", job.Name, job.Status.Conditions[0].Message)
rluders marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/periodic/periodic.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,15 +346,15 @@ func (c *Controller) runJobAndCheckResults(ctx context.Context, dataGather *insi
}

klog.Infof("Created new gathering job %v", gj.Name)
err = c.jobController.WaitForJobCompletion(ctx, gj)
err = c.jobController.WaitForJobCompletion(ctx, gj.Name)
if err != nil {
if errors.Is(err, context.DeadlineExceeded) {
klog.Errorf("Failed to read job status: %v", err)
return
}
klog.Error(err)
}
klog.Infof("Job completed %s", gj.Name)
klog.Infof("Job completed %s", "")
rluders marked this conversation as resolved.
Show resolved Hide resolved
dataGatherFinished, err := c.dataGatherClient.DataGathers().Get(ctx, dataGather.Name, metav1.GetOptions{})
if err != nil {
klog.Errorf("Failed to get DataGather resource %s: %v", dataGather.Name, err)
Expand Down