forked from openshift/osde2e
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prunejob.go
105 lines (91 loc) · 3.21 KB
/
prunejob.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package operators
import (
"context"
"fmt"
"log"
"time"
"github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/openshift/osde2e/pkg/common/alert"
viper "github.com/openshift/osde2e/pkg/common/concurrentviper"
"github.com/openshift/osde2e/pkg/common/config"
"github.com/openshift/osde2e/pkg/common/helper"
"github.com/openshift/osde2e/pkg/common/util"
batchv1 "k8s.io/api/batch/v1"
batchv1beta1 "k8s.io/api/batch/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/rand"
)
var pruneJobsTestName string = "[Suite: operators] [OSD] Prune jobs"
func init() {
alert.RegisterGinkgoAlert(pruneJobsTestName, "SD-SREP", "Haoran Wang", "sd-cicd-alerts", "sd-cicd@redhat.com", 4)
}
var _ = ginkgo.Describe(pruneJobsTestName, func() {
h := helper.New()
ginkgo.Context("pruner jobs should works", func() {
namespace := "openshift-sre-pruning"
cronJobs := []string{"builds-pruner", "deployments-pruner"}
for _, cronJob := range cronJobs {
util.GinkgoIt(cronJob+" should run successfully", func() {
getOpts := metav1.GetOptions{}
cjob, err := h.Kube().BatchV1beta1().CronJobs(namespace).Get(context.TODO(), cronJob, getOpts)
Expect(err).NotTo(HaveOccurred())
job := createJobFromCronJob(cjob)
job, err = h.Kube().BatchV1().Jobs(namespace).Create(context.TODO(), job, metav1.CreateOptions{})
defer func() {
err = h.Kube().BatchV1().Jobs(namespace).Delete(context.TODO(), job.Name, metav1.DeleteOptions{})
Expect(err).NotTo(HaveOccurred())
}()
Expect(err).NotTo(HaveOccurred())
err = waitJobComplete(h, namespace, job.Name)
Expect(err).NotTo(HaveOccurred())
}, float64(viper.GetFloat64(config.Tests.PollingTimeout)))
}
})
})
func createJobFromCronJob(cronJob *batchv1beta1.CronJob) *batchv1.Job {
annotations := make(map[string]string)
annotations["managed.openshift.io/instantiate"] = "manual"
for k, v := range cronJob.Spec.JobTemplate.Annotations {
annotations[k] = v
}
jobName := cronJob.Name + "-" + rand.String(5)
return &batchv1.Job{
TypeMeta: metav1.TypeMeta{APIVersion: batchv1.SchemeGroupVersion.String(), Kind: "Job"},
ObjectMeta: metav1.ObjectMeta{
Name: jobName,
Annotations: annotations,
Labels: cronJob.Spec.JobTemplate.Labels,
},
Spec: cronJob.Spec.JobTemplate.Spec,
}
}
func waitJobComplete(h *helper.H, namespace, jobName string) error {
var err error
var job *batchv1.Job
// interval is the duration in seconds between polls
// values here for humans
interval := 5
// convert time.Duration type
timeoutDuration := time.Duration(viper.GetFloat64(config.Tests.PollingTimeout)) * time.Minute
intervalDuration := time.Duration(interval) * time.Second
start := time.Now()
Loop:
for {
job, err = h.Kube().BatchV1().Jobs(namespace).Get(context.TODO(), jobName, metav1.GetOptions{})
elapsed := time.Since(start)
switch {
case err == nil && job.Status.Succeeded == 1:
// Success
break Loop
default:
if elapsed < timeoutDuration {
log.Printf("Waiting %v for %s job to complete", (timeoutDuration - elapsed), jobName)
time.Sleep(intervalDuration)
} else {
return fmt.Errorf("failed to wait job: %s %s complete before timeout", namespace, jobName)
}
}
}
return nil
}