-
Notifications
You must be signed in to change notification settings - Fork 38.6k
/
ttl_after_finished.go
157 lines (131 loc) · 5.27 KB
/
ttl_after_finished.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package apps
import (
"context"
"fmt"
"time"
batchv1 "k8s.io/api/batch/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
clientset "k8s.io/client-go/kubernetes"
"k8s.io/kubernetes/pkg/util/slice"
"k8s.io/kubernetes/test/e2e/framework"
e2ejob "k8s.io/kubernetes/test/e2e/framework/job"
admissionapi "k8s.io/pod-security-admission/api"
"github.com/onsi/ginkgo/v2"
)
const (
dummyFinalizer = "k8s.io/dummy-finalizer"
// JobTimeout is how long to wait for a job to finish.
JobTimeout = 15 * time.Minute
)
var _ = SIGDescribe("TTLAfterFinished", func() {
f := framework.NewDefaultFramework("ttlafterfinished")
f.NamespacePodSecurityEnforceLevel = admissionapi.LevelBaseline
ginkgo.It("job should be deleted once it finishes after TTL seconds", func(ctx context.Context) {
testFinishedJob(ctx, f)
})
})
func cleanupJob(ctx context.Context, f *framework.Framework, job *batchv1.Job) {
ns := f.Namespace.Name
c := f.ClientSet
framework.Logf("Remove the Job's dummy finalizer; the Job should be deleted cascadingly")
removeFinalizerFunc := func(j *batchv1.Job) {
j.ObjectMeta.Finalizers = slice.RemoveString(j.ObjectMeta.Finalizers, dummyFinalizer, nil)
}
_, err := updateJobWithRetries(ctx, c, ns, job.Name, removeFinalizerFunc)
framework.ExpectNoError(err)
e2ejob.WaitForJobGone(ctx, c, ns, job.Name, wait.ForeverTestTimeout)
err = e2ejob.WaitForAllJobPodsGone(ctx, c, ns, job.Name)
framework.ExpectNoError(err)
}
func testFinishedJob(ctx context.Context, f *framework.Framework) {
ns := f.Namespace.Name
c := f.ClientSet
parallelism := int32(1)
completions := int32(1)
backoffLimit := int32(2)
ttl := int32(10)
job := e2ejob.NewTestJob("randomlySucceedOrFail", "rand-non-local", v1.RestartPolicyNever, parallelism, completions, nil, backoffLimit)
job.Spec.TTLSecondsAfterFinished = &ttl
job.ObjectMeta.Finalizers = []string{dummyFinalizer}
ginkgo.DeferCleanup(cleanupJob, f, job)
framework.Logf("Create a Job %s/%s with TTL", ns, job.Name)
job, err := e2ejob.CreateJob(ctx, c, ns, job)
framework.ExpectNoError(err)
framework.Logf("Wait for the Job to finish")
err = e2ejob.WaitForJobFinish(ctx, c, ns, job.Name)
framework.ExpectNoError(err)
framework.Logf("Wait for TTL after finished controller to delete the Job")
err = waitForJobDeleting(ctx, c, ns, job.Name)
framework.ExpectNoError(err)
framework.Logf("Check Job's deletionTimestamp and compare with the time when the Job finished")
job, err = e2ejob.GetJob(ctx, c, ns, job.Name)
framework.ExpectNoError(err)
jobFinishTime := finishTime(job)
finishTimeUTC := jobFinishTime.UTC()
if jobFinishTime.IsZero() {
framework.Fail("Expected job finish time not to be zero.")
}
deleteAtUTC := job.ObjectMeta.DeletionTimestamp.UTC()
framework.ExpectNotEqual(deleteAtUTC, nil)
expireAtUTC := finishTimeUTC.Add(time.Duration(ttl) * time.Second)
if deleteAtUTC.Before(expireAtUTC) {
framework.Fail("Expected job's deletion time to be after expiration time.")
}
}
// finishTime returns finish time of the specified job.
func finishTime(finishedJob *batchv1.Job) metav1.Time {
var finishTime metav1.Time
for _, c := range finishedJob.Status.Conditions {
if (c.Type == batchv1.JobComplete || c.Type == batchv1.JobFailed) && c.Status == v1.ConditionTrue {
return c.LastTransitionTime
}
}
return finishTime
}
// updateJobWithRetries updates job with retries.
func updateJobWithRetries(ctx context.Context, c clientset.Interface, namespace, name string, applyUpdate func(*batchv1.Job)) (job *batchv1.Job, err error) {
jobs := c.BatchV1().Jobs(namespace)
var updateErr error
pollErr := wait.PollImmediateWithContext(ctx, framework.Poll, JobTimeout, func(ctx context.Context) (bool, error) {
if job, err = jobs.Get(ctx, name, metav1.GetOptions{}); err != nil {
return false, err
}
// Apply the update, then attempt to push it to the apiserver.
applyUpdate(job)
if job, err = jobs.Update(ctx, job, metav1.UpdateOptions{}); err == nil {
framework.Logf("Updating job %s", name)
return true, nil
}
updateErr = err
return false, nil
})
if pollErr == wait.ErrWaitTimeout {
pollErr = fmt.Errorf("couldn't apply the provided updated to job %q: %v", name, updateErr)
}
return job, pollErr
}
// waitForJobDeleting uses c to wait for the Job jobName in namespace ns to have
// a non-nil deletionTimestamp (i.e. being deleted).
func waitForJobDeleting(ctx context.Context, c clientset.Interface, ns, jobName string) error {
return wait.PollImmediateWithContext(ctx, framework.Poll, JobTimeout, func(ctx context.Context) (bool, error) {
curr, err := c.BatchV1().Jobs(ns).Get(ctx, jobName, metav1.GetOptions{})
if err != nil {
return false, err
}
return curr.ObjectMeta.DeletionTimestamp != nil, nil
})
}