|
4 | 4 | "context"
|
5 | 5 | "fmt"
|
6 | 6 | "os"
|
| 7 | + "strings" |
7 | 8 | "time"
|
8 | 9 |
|
9 | 10 | v1 "github.com/openshift/api/config/v1"
|
@@ -141,6 +142,7 @@ func (s *Operator) Run(ctx context.Context, controller *controllercmd.Controller
|
141 | 142 | var insightsDataGatherObserver configobserver.InsightsDataGatherObserver
|
142 | 143 | var dgInformer periodic.DataGatherInformer
|
143 | 144 | if insightsConfigAPIEnabled {
|
| 145 | + deleteAllRunningGatheringsPods(ctx, kubeClient) |
144 | 146 | configInformersForTechPreview := configv1informers.NewSharedInformerFactory(configClient, 10*time.Minute)
|
145 | 147 | insightsDataGatherObserver, err = configobserver.NewInsightsDataGatherObserver(gatherKubeConfig,
|
146 | 148 | controller.EventRecorder, configInformersForTechPreview)
|
@@ -316,3 +318,26 @@ func isRunning(kubeConfig *rest.Config) wait.ConditionWithContextFunc {
|
316 | 318 | return true, nil
|
317 | 319 | }
|
318 | 320 | }
|
| 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 | +} |
0 commit comments