forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
job.go
291 lines (254 loc) · 10.6 KB
/
job.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
Copyright 2017 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 e2e_federation
import (
"fmt"
"strings"
"time"
batchv1 "k8s.io/api/batch/v1"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
fedclientset "k8s.io/kubernetes/federation/client/clientset_generated/federation_clientset"
fedutil "k8s.io/kubernetes/federation/pkg/federation-controller/util"
"k8s.io/kubernetes/test/e2e/framework"
fedframework "k8s.io/kubernetes/test/e2e_federation/framework"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/kubernetes/pkg/api"
)
const (
FederationJobName = "federation-job"
)
var _ = framework.KubeDescribe("Federation jobs [Feature:Federation]", func() {
f := fedframework.NewDefaultFederatedFramework("federation-job")
Describe("Job objects [NoCluster]", func() {
AfterEach(func() {
fedframework.SkipUnlessFederated(f.ClientSet)
// Delete all jobs.
nsName := f.FederationNamespace.Name
deleteAllJobsOrFail(f.FederationClientset, nsName)
})
It("should be created and deleted successfully", func() {
fedframework.SkipUnlessFederated(f.ClientSet)
nsName := f.FederationNamespace.Name
job := createJobOrFail(f.FederationClientset, nsName)
By(fmt.Sprintf("Creation of job %q in namespace %q succeeded. Deleting job.", job.Name, nsName))
// Cleanup
err := f.FederationClientset.Batch().Jobs(nsName).Delete(job.Name, &metav1.DeleteOptions{})
framework.ExpectNoError(err, "Error deleting job %q in namespace %q", job.Name, job.Namespace)
By(fmt.Sprintf("Deletion of job %q in namespace %q succeeded.", job.Name, nsName))
})
})
// e2e cases for federated job controller
Describe("Federated Job", func() {
var (
clusters fedframework.ClusterSlice
)
BeforeEach(func() {
fedframework.SkipUnlessFederated(f.ClientSet)
clusters = f.GetRegisteredClusters()
})
AfterEach(func() {
nsName := f.FederationNamespace.Name
deleteAllJobsOrFail(f.FederationClientset, nsName)
})
It("should create and update matching jobs in underlying clusters", func() {
nsName := f.FederationNamespace.Name
job := createJobOrFail(f.FederationClientset, nsName)
defer func() {
// cleanup. deletion of jobs is not supported for underlying clusters
By(fmt.Sprintf("Deleting job %q/%q", nsName, job.Name))
waitForJobOrFail(f.FederationClientset, nsName, job.Name, clusters)
f.FederationClientset.Batch().Jobs(nsName).Delete(job.Name, &metav1.DeleteOptions{})
}()
waitForJobOrFail(f.FederationClientset, nsName, job.Name, clusters)
By(fmt.Sprintf("Successfuly created and synced job %q/%q to clusters", nsName, job.Name))
})
It("should be deleted from underlying clusters when OrphanDependents is false", func() {
fedframework.SkipUnlessFederated(f.ClientSet)
nsName := f.FederationNamespace.Name
orphanDependents := false
verifyCascadingDeletionForJob(f.FederationClientset, clusters, &orphanDependents, nsName)
By(fmt.Sprintf("Verified that jobs were deleted from underlying clusters"))
})
It("should not be deleted from underlying clusters when OrphanDependents is true", func() {
fedframework.SkipUnlessFederated(f.ClientSet)
nsName := f.FederationNamespace.Name
orphanDependents := true
verifyCascadingDeletionForJob(f.FederationClientset, clusters, &orphanDependents, nsName)
By(fmt.Sprintf("Verified that jobs were not deleted from underlying clusters"))
})
It("should not be deleted from underlying clusters when OrphanDependents is nil", func() {
fedframework.SkipUnlessFederated(f.ClientSet)
nsName := f.FederationNamespace.Name
verifyCascadingDeletionForJob(f.FederationClientset, clusters, nil, nsName)
By(fmt.Sprintf("Verified that jobs were not deleted from underlying clusters"))
})
})
})
// deleteAllJobsOrFail deletes all jobs in the given namespace name.
func deleteAllJobsOrFail(clientset *fedclientset.Clientset, nsName string) {
jobList, err := clientset.Batch().Jobs(nsName).List(metav1.ListOptions{})
Expect(err).NotTo(HaveOccurred())
orphanDependents := false
for _, job := range jobList.Items {
deleteJobOrFail(clientset, nsName, job.Name, &orphanDependents)
}
}
// verifyCascadingDeletionForJob verifies that job are deleted
// from underlying clusters when orphan dependents is false and they are not
// deleted when orphan dependents is true.
func verifyCascadingDeletionForJob(clientset *fedclientset.Clientset, clusters fedframework.ClusterSlice, orphanDependents *bool, nsName string) {
job := createJobOrFail(clientset, nsName)
jobName := job.Name
// Check subclusters if the job was created there.
By(fmt.Sprintf("Waiting for job %s to be created in all underlying clusters", jobName))
err := wait.Poll(5*time.Second, 2*time.Minute, func() (bool, error) {
for _, cluster := range clusters {
_, err := cluster.Batch().Jobs(nsName).Get(jobName, metav1.GetOptions{})
if err != nil && errors.IsNotFound(err) {
return false, nil
}
if err != nil {
return false, err
}
}
return true, nil
})
framework.ExpectNoError(err, "Not all jobs created")
By(fmt.Sprintf("Deleting job %s", jobName))
deleteJobOrFail(clientset, nsName, jobName, orphanDependents)
By(fmt.Sprintf("Verifying job %s in underlying clusters", jobName))
errMessages := []string{}
// job should be present in underlying clusters unless orphanDependents is false.
shouldExist := orphanDependents == nil || *orphanDependents == true
for _, cluster := range clusters {
clusterName := cluster.Name
_, err := cluster.Batch().Jobs(nsName).Get(jobName, metav1.GetOptions{})
if shouldExist && errors.IsNotFound(err) {
errMessages = append(errMessages, fmt.Sprintf("unexpected NotFound error for job %s in cluster %s, expected job to exist", jobName, clusterName))
} else if !shouldExist && !errors.IsNotFound(err) {
errMessages = append(errMessages, fmt.Sprintf("expected NotFound error for job %s in cluster %s, got error: %v", jobName, clusterName, err))
}
}
if len(errMessages) != 0 {
framework.Failf("%s", strings.Join(errMessages, "; "))
}
}
func waitForJobOrFail(c *fedclientset.Clientset, namespace string, jobName string, clusters fedframework.ClusterSlice) {
err := waitForJob(c, namespace, jobName, clusters)
framework.ExpectNoError(err, "Failed to verify job %q/%q, err: %v", namespace, jobName, err)
}
func waitForJob(c *fedclientset.Clientset, namespace string, jobName string, clusters fedframework.ClusterSlice) error {
err := wait.Poll(10*time.Second, fedframework.FederatedDefaultTestTimeout, func() (bool, error) {
fjob, err := c.Batch().Jobs(namespace).Get(jobName, metav1.GetOptions{})
if err != nil {
return false, err
}
succeeded := int32(0)
for _, cluster := range clusters {
job, err := cluster.Batch().Jobs(namespace).Get(jobName, metav1.GetOptions{})
if err != nil && !errors.IsNotFound(err) {
By(fmt.Sprintf("Failed getting job: %q/%q/%q, err: %v", cluster.Name, namespace, jobName, err))
return false, err
}
if err == nil {
if !verifyJob(fjob, job) {
By(fmt.Sprintf("Job meta or spec not match for cluster %q:\n federation: %v\n cluster: %v", cluster.Name, fjob, job))
return false, nil
}
succeeded += job.Status.Succeeded
}
}
if succeeded == fjob.Status.Succeeded &&
(fjob.Spec.Completions != nil && succeeded == *fjob.Spec.Completions) {
return true, nil
}
By(fmt.Sprintf("Job statuses not match, federation succeeded: %v/%v, clusters succeeded: %v\n",
fjob.Status.Succeeded, func(p *int32) int32 {
if p != nil {
return *p
} else {
return -1
}
}(fjob.Spec.Completions), succeeded))
return false, nil
})
return err
}
func verifyJob(fedJob, localJob *batchv1.Job) bool {
localJobObj, _ := api.Scheme.DeepCopy(localJob)
localJob = localJobObj.(*batchv1.Job)
localJob.Spec.ManualSelector = fedJob.Spec.ManualSelector
localJob.Spec.Completions = fedJob.Spec.Completions
localJob.Spec.Parallelism = fedJob.Spec.Parallelism
return fedutil.ObjectMetaAndSpecEquivalent(fedJob, localJob)
}
func createJobOrFail(clientset *fedclientset.Clientset, namespace string) *batchv1.Job {
if clientset == nil || len(namespace) == 0 {
Fail(fmt.Sprintf("Internal error: invalid parameters passed to createJobOrFail: clientset: %v, namespace: %v", clientset, namespace))
}
By(fmt.Sprintf("Creating federation job %q in namespace %q", FederationJobName, namespace))
job := newJobForFed(namespace, FederationJobName, 5, 5)
_, err := clientset.Batch().Jobs(namespace).Create(job)
framework.ExpectNoError(err, "Creating job %q in namespace %q", job.Name, namespace)
By(fmt.Sprintf("Successfully created federation job %q in namespace %q", FederationJobName, namespace))
return job
}
func deleteJobOrFail(clientset *fedclientset.Clientset, nsName string, jobName string, orphanDependents *bool) {
By(fmt.Sprintf("Deleting job %q in namespace %q", jobName, nsName))
err := clientset.Batch().Jobs(nsName).Delete(jobName, &metav1.DeleteOptions{OrphanDependents: orphanDependents})
if err != nil && !errors.IsNotFound(err) {
framework.ExpectNoError(err, "Error deleting job %q in namespace %q", jobName, nsName)
}
// Wait for the job to be deleted.
err = wait.Poll(10*time.Second, fedframework.FederatedDefaultTestTimeout, func() (bool, error) {
_, err := clientset.Batch().Jobs(nsName).Get(jobName, metav1.GetOptions{})
if err != nil && errors.IsNotFound(err) {
return true, nil
}
return false, err
})
if err != nil {
framework.Failf("Error in deleting job %s: %v", jobName, err)
}
}
func newJobForFed(namespace string, name string, completions int32, parallelism int32) *batchv1.Job {
return &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
Spec: batchv1.JobSpec{
Parallelism: ¶llelism,
Completions: &completions,
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{"name": "fjob"},
},
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "sleep",
Image: "gcr.io/google_containers/busybox:1.24",
Command: []string{"sleep", "1"},
},
},
RestartPolicy: v1.RestartPolicyNever,
},
},
},
}
}