Skip to content

Commit c9d98db

Browse files
committed
delete all active jobs during restart
1 parent 0b8a79c commit c9d98db

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

pkg/controller/operator.go

+25
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"os"
7+
"strings"
78
"time"
89

910
v1 "github.com/openshift/api/config/v1"
@@ -141,6 +142,7 @@ func (s *Operator) Run(ctx context.Context, controller *controllercmd.Controller
141142
var insightsDataGatherObserver configobserver.InsightsDataGatherObserver
142143
var dgInformer periodic.DataGatherInformer
143144
if insightsConfigAPIEnabled {
145+
deleteAllRunningGatheringsPods(ctx, kubeClient)
144146
configInformersForTechPreview := configv1informers.NewSharedInformerFactory(configClient, 10*time.Minute)
145147
insightsDataGatherObserver, err = configobserver.NewInsightsDataGatherObserver(gatherKubeConfig,
146148
controller.EventRecorder, configInformersForTechPreview)
@@ -316,3 +318,26 @@ func isRunning(kubeConfig *rest.Config) wait.ConditionWithContextFunc {
316318
return true, nil
317319
}
318320
}
321+
322+
// deleteAllRunningGatheringsPods deletes all the active jobs (and their Pods) with the "periodic-gathering-"
323+
// prefix in the openshift-insights namespace
324+
func deleteAllRunningGatheringsPods(ctx context.Context, cli kubernetes.Interface) {
325+
jobList, err := cli.BatchV1().Jobs("openshift-insights").List(ctx, metav1.ListOptions{})
326+
if err != nil {
327+
klog.Warningf("Failed to list jobs in the Insights namespace: %v ", err)
328+
}
329+
330+
orphan := metav1.DeletePropagationBackground
331+
for _, j := range jobList.Items {
332+
if j.Status.Active > 0 && strings.HasPrefix(j.Name, "periodic-gathering-") {
333+
err := cli.BatchV1().Jobs("openshift-insights").Delete(ctx, j.Name, metav1.DeleteOptions{
334+
PropagationPolicy: &orphan,
335+
})
336+
if err != nil {
337+
klog.Warningf("Failed to delete job %s: %v", j.Name, err)
338+
} else {
339+
klog.Infof("Job %s was deleted due to container restart", j.Name)
340+
}
341+
}
342+
}
343+
}

pkg/controller/periodic/job.go

+4
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ func (j *JobController) WaitForJobCompletion(ctx context.Context, jobName string
124124
return fmt.Errorf("watcher channel was closed unexpectedly")
125125
}
126126

127+
if event.Type == watch.Deleted {
128+
return nil
129+
}
130+
127131
if event.Type != watch.Modified {
128132
continue
129133
}

0 commit comments

Comments
 (0)