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 4 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
1 change: 1 addition & 0 deletions manifests/03-clusterrole.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,7 @@ rules:
- get
- list
- delete
- watch
- apiGroups:
- apps
resources:
Expand Down
51 changes: 34 additions & 17 deletions pkg/controller/periodic/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ package periodic
import (
"context"
"fmt"
"time"

batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes"
)

Expand Down Expand Up @@ -108,19 +106,38 @@ func (j *JobController) CreateGathererJob(ctx context.Context, dataGatherName, i
return j.kubeClient.BatchV1().Jobs(insightsNamespace).Create(ctx, gj, metav1.CreateOptions{})
}

// WaitForJobCompletion polls the Kubernetes API every 20 seconds and checks if the job finished.
func (j *JobController) WaitForJobCompletion(ctx context.Context, job *batchv1.Job) error {
return wait.PollUntilContextCancel(ctx, 20*time.Second, true, func(ctx context.Context) (done bool, err error) {
j, err := j.kubeClient.BatchV1().Jobs(insightsNamespace).Get(ctx, job.Name, metav1.GetOptions{})
if errors.IsNotFound(err) {
return false, err
}
if j.Status.Succeeded > 0 {
return true, nil
}
if j.Status.Failed > 0 {
return true, fmt.Errorf("job %s failed", job.Name)
// WaitForJobCompletion listen the Kubernetes events to check if job finished.
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", jobName)})
if err != nil {
return err
}
defer watcher.Stop()

for {
select {
case <-ctx.Done():
return ctx.Err()
case event, ok := <-watcher.ResultChan():
if !ok {
return fmt.Errorf("watcher channel was closed unexpectedly")
}

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("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: %s", job.Name, job.Status.Conditions[0].Message)
rluders marked this conversation as resolved.
Show resolved Hide resolved
}
}
return false, nil
})
}
}
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