generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 39
/
jobset_controller.go
772 lines (687 loc) · 27.2 KB
/
jobset_controller.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
/*
Copyright 2023 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 controllers
import (
"context"
"crypto/sha1"
"encoding/hex"
"errors"
"fmt"
"strconv"
"sync"
batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
apiequality "k8s.io/apimachinery/pkg/api/equality"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
"k8s.io/client-go/util/workqueue"
"k8s.io/klog/v2"
"k8s.io/utils/ptr"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
jobset "sigs.k8s.io/jobset/api/jobset/v1alpha2"
"sigs.k8s.io/jobset/pkg/util/collections"
"sigs.k8s.io/jobset/pkg/util/placement"
)
const (
RestartsKey string = "jobset.sigs.k8s.io/restart-attempt"
maxParallelism int = 50
)
var (
jobOwnerKey = ".metadata.controller"
apiGVStr = jobset.GroupVersion.String()
)
// JobSetReconciler reconciles a JobSet object
type JobSetReconciler struct {
client.Client
Scheme *runtime.Scheme
Record record.EventRecorder
}
type childJobs struct {
// Only jobs with jobset.sigs.k8s.io/restart-attempt == jobset.status.restarts are included
// in active, successful, and failed jobs. These jobs are part of the current JobSet run.
active []*batchv1.Job
successful []*batchv1.Job
failed []*batchv1.Job
// Jobs marked for deletion are mutually exclusive with the set of jobs in active, successful, and failed.
delete []*batchv1.Job
}
func NewJobSetReconciler(client client.Client, scheme *runtime.Scheme, record record.EventRecorder) *JobSetReconciler {
return &JobSetReconciler{Client: client, Scheme: scheme, Record: record}
}
//+kubebuilder:rbac:groups="",resources=events,verbs=create;watch;update;patch
//+kubebuilder:rbac:groups=jobset.x-k8s.io,resources=jobsets,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=jobset.x-k8s.io,resources=jobsets/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=jobset.x-k8s.io,resources=jobsets/finalizers,verbs=update
//+kubebuilder:rbac:groups=batch,resources=jobs,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=batch,resources=jobs/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=core,resources=services,verbs=get;list;watch;create;update;patch;delete
// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
func (r *JobSetReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
// Get JobSet from apiserver.
var js jobset.JobSet
if err := r.Get(ctx, req.NamespacedName, &js); err != nil {
// we'll ignore not-found errors, since there is nothing we can do here.
return ctrl.Result{}, client.IgnoreNotFound(err)
}
log := ctrl.LoggerFrom(ctx).WithValues("jobset", klog.KObj(&js))
ctx = ctrl.LoggerInto(ctx, log)
log.V(2).Info("Reconciling JobSet")
// Get Jobs owned by JobSet.
ownedJobs, err := r.getChildJobs(ctx, &js)
if err != nil {
log.Error(err, "getting jobs owned by jobset")
return ctrl.Result{}, err
}
// Calculate JobsReady and update statuses for each ReplicatedJob
if err := r.calculateAndUpdateReplicatedJobsStatuses(ctx, &js, ownedJobs); err != nil {
log.Error(err, "updating replicated jobs statuses")
return ctrl.Result{}, err
}
// If JobSet is already completed or failed, clean up active child jobs.
if jobSetFinished(&js) {
if err := r.deleteJobs(ctx, ownedJobs.active); err != nil {
log.Error(err, "deleting jobs")
return ctrl.Result{}, err
}
return ctrl.Result{}, nil
}
// Delete any jobs marked for deletion.
if err := r.deleteJobs(ctx, ownedJobs.delete); err != nil {
log.Error(err, "deleting jobs")
return ctrl.Result{}, err
}
// If any jobs have failed, execute the JobSet failure policy (if any).
if len(ownedJobs.failed) > 0 {
if err := r.executeFailurePolicy(ctx, &js, ownedJobs); err != nil {
log.Error(err, "executing failure policy")
return ctrl.Result{}, err
}
return ctrl.Result{}, nil
}
// If any jobs have succeeded, execute the JobSet success policy.
if len(ownedJobs.successful) > 0 {
completed, err := r.executeSuccessPolicy(ctx, &js, ownedJobs)
if err != nil {
log.Error(err, "executing success policy")
return ctrl.Result{}, err
}
if completed {
return ctrl.Result{}, nil
}
}
// If job has not failed or succeeded, continue creating any
// jobs that are ready to be started.
if err := r.createJobs(ctx, &js, ownedJobs); err != nil {
log.Error(err, "creating jobs")
return ctrl.Result{}, err
}
// Handle suspending a jobset or resuming a suspended jobset.
jobsetSuspended := js.Spec.Suspend != nil && *js.Spec.Suspend
if jobsetSuspended {
if err := r.suspendJobSet(ctx, &js, ownedJobs); err != nil {
log.Error(err, "suspending jobset")
return ctrl.Result{}, err
}
} else {
if err := r.resumeJobSetIfNecessary(ctx, &js, ownedJobs); err != nil {
log.Error(err, "resuming jobset")
return ctrl.Result{}, err
}
}
return ctrl.Result{}, nil
}
// SetupWithManager sets up the controller with the Manager.
func (r *JobSetReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&jobset.JobSet{}).
Owns(&batchv1.Job{}).
Owns(&corev1.Service{}).
Complete(r)
}
func SetupJobSetIndexes(ctx context.Context, indexer client.FieldIndexer) error {
return indexer.IndexField(ctx, &batchv1.Job{}, jobOwnerKey, func(obj client.Object) []string {
o := obj.(*batchv1.Job)
owner := metav1.GetControllerOf(o)
if owner == nil {
return nil
}
// ...make sure it's a JobSet...
if owner.APIVersion != apiGVStr || owner.Kind != "JobSet" {
return nil
}
return []string{owner.Name}
})
}
// getChildJobs gets jobs owned by the JobSet then categorizes them by status (active, successful, failed).
// Another list (`delete`) is also added which tracks jobs marked for deletion.
func (r *JobSetReconciler) getChildJobs(ctx context.Context, js *jobset.JobSet) (*childJobs, error) {
log := ctrl.LoggerFrom(ctx)
// Get all active jobs owned by JobSet.
var childJobList batchv1.JobList
if err := r.List(ctx, &childJobList, client.InNamespace(js.Namespace), client.MatchingFields{jobOwnerKey: js.Name}); err != nil {
return nil, err
}
// Categorize each job into a bucket: active, successful, failed, or delete.
ownedJobs := childJobs{}
for i, job := range childJobList.Items {
// Jobs with jobset.sigs.k8s.io/restart-attempt < jobset.status.restarts are marked for
// deletion, as they were part of the previous JobSet run.
jobRestarts, err := strconv.Atoi(job.Labels[RestartsKey])
if err != nil {
log.Error(err, fmt.Sprintf("invalid value for label %s, must be integer", RestartsKey))
ownedJobs.delete = append(ownedJobs.delete, &childJobList.Items[i])
return nil, err
}
if int32(jobRestarts) < js.Status.Restarts {
ownedJobs.delete = append(ownedJobs.delete, &childJobList.Items[i])
continue
}
// Jobs with jobset.sigs.k8s.io/restart-attempt == jobset.status.restarts are part of
// the current JobSet run, and marked either active, successful, or failed.
_, finishedType := JobFinished(&job)
switch finishedType {
case "": // active
ownedJobs.active = append(ownedJobs.active, &childJobList.Items[i])
case batchv1.JobFailed:
ownedJobs.failed = append(ownedJobs.failed, &childJobList.Items[i])
case batchv1.JobComplete:
ownedJobs.successful = append(ownedJobs.successful, &childJobList.Items[i])
}
}
return &ownedJobs, nil
}
func (r *JobSetReconciler) calculateAndUpdateReplicatedJobsStatuses(ctx context.Context, js *jobset.JobSet, jobs *childJobs) error {
status := r.calculateReplicatedJobStatuses(ctx, js, jobs)
// Check if status ReplicatedJobsStatus has changed
if apiequality.Semantic.DeepEqual(js.Status.ReplicatedJobsStatus, status) {
return nil
}
js.Status.ReplicatedJobsStatus = status
return r.Status().Update(ctx, js)
}
func (r *JobSetReconciler) calculateReplicatedJobStatuses(ctx context.Context, js *jobset.JobSet, jobs *childJobs) []jobset.ReplicatedJobStatus {
log := ctrl.LoggerFrom(ctx)
// Prepare replicatedJobsReady for optimal iteration
replicatedJobsReady := map[string]map[string]int32{}
for _, replicatedJob := range js.Spec.ReplicatedJobs {
replicatedJobsReady[replicatedJob.Name] = map[string]int32{
"ready": 0,
"succeeded": 0,
"failed": 0,
"active": 0,
"suspended": 0,
}
}
// Calculate jobsReady for each Replicated Job
for _, job := range jobs.active {
if job.Labels == nil || (job.Labels != nil && job.Labels[jobset.ReplicatedJobNameKey] == "") {
log.Error(nil, fmt.Sprintf("job %s missing ReplicatedJobName label, can't update status", job.Name))
continue
}
ready := ptr.Deref(job.Status.Ready, 0)
// parallelism is always set as it is otherwise defaulted by k8s to 1
podsCount := *(job.Spec.Parallelism)
if job.Spec.Completions != nil && *job.Spec.Completions < podsCount {
podsCount = *job.Spec.Completions
}
if job.Status.Succeeded+ready >= podsCount {
replicatedJobsReady[job.Labels[jobset.ReplicatedJobNameKey]]["ready"]++
}
if job.Status.Active > 0 {
replicatedJobsReady[job.Labels[jobset.ReplicatedJobNameKey]]["active"]++
}
if job.Spec.Suspend != nil && *job.Spec.Suspend {
replicatedJobsReady[job.Labels[jobset.ReplicatedJobNameKey]]["suspended"]++
}
}
// Calculate succeededJobs
for _, job := range jobs.successful {
replicatedJobsReady[job.Labels[jobset.ReplicatedJobNameKey]]["succeeded"]++
}
for _, job := range jobs.failed {
replicatedJobsReady[job.Labels[jobset.ReplicatedJobNameKey]]["failed"]++
}
// Calculate ReplicatedJobsStatus
var rjStatus []jobset.ReplicatedJobStatus
for name, status := range replicatedJobsReady {
rjStatus = append(rjStatus, jobset.ReplicatedJobStatus{
Name: name,
Ready: status["ready"],
Succeeded: status["succeeded"],
Failed: status["failed"],
Active: status["active"],
Suspended: status["suspended"],
})
}
return rjStatus
}
func (r *JobSetReconciler) suspendJobSet(ctx context.Context, js *jobset.JobSet, ownedJobs *childJobs) error {
for _, job := range ownedJobs.active {
if !ptr.Deref(job.Spec.Suspend, false) {
job.Spec.Suspend = ptr.To(true)
if err := r.Update(ctx, job); err != nil {
return err
}
}
}
return r.ensureCondition(ctx, js, corev1.EventTypeNormal, metav1.Condition{
Type: string(jobset.JobSetSuspended),
Status: metav1.ConditionStatus(corev1.ConditionTrue),
LastTransitionTime: metav1.Now(),
Reason: "SuspendedJobs",
Message: "jobset is suspended",
})
}
func (r *JobSetReconciler) resumeJobSetIfNecessary(ctx context.Context, js *jobset.JobSet, ownedJobs *childJobs) error {
log := ctrl.LoggerFrom(ctx)
nodeAffinities := map[string]map[string]string{}
for _, replicatedJob := range js.Spec.ReplicatedJobs {
nodeAffinities[replicatedJob.Name] = replicatedJob.Template.Spec.Template.Spec.NodeSelector
}
// If JobSpec is unsuspended, ensure all active child Jobs are also
// unsuspended and update the suspend condition to true.
for _, job := range ownedJobs.active {
if ptr.Deref(job.Spec.Suspend, false) {
if job.Status.StartTime != nil {
job.Status.StartTime = nil
if err := r.Status().Update(ctx, job); err != nil {
return err
}
}
if job.Labels != nil && job.Labels[jobset.ReplicatedJobNameKey] != "" {
// When resuming a job, its nodeSelectors should match that of the replicatedJob template
// that it was created from, which may have been updated while it was suspended.
job.Spec.Template.Spec.NodeSelector = nodeAffinities[job.Labels[jobset.ReplicatedJobNameKey]]
} else {
log.Error(nil, "job missing ReplicatedJobName label")
}
job.Spec.Suspend = ptr.To(false)
if err := r.Update(ctx, job); err != nil {
return err
}
}
}
return r.ensureCondition(ctx, js, corev1.EventTypeNormal, metav1.Condition{
Type: string(jobset.JobSetSuspended),
Status: metav1.ConditionStatus(corev1.ConditionFalse),
LastTransitionTime: metav1.Now(),
Reason: "ResumeJobs",
Message: "jobset is resumed",
})
}
func (r *JobSetReconciler) createJobs(ctx context.Context, js *jobset.JobSet, ownedJobs *childJobs) error {
log := ctrl.LoggerFrom(ctx)
// If pod DNS hostnames are enabled, create a headless service for the JobSet
if dnsHostnamesEnabled(js) {
// Don't try to create a headless service without a subdomain
if js.Spec.Network.Subdomain == "" {
return fmt.Errorf("a subdomain should be set if dns hostnames are enabled")
}
if err := r.createHeadlessSvcIfNotExist(ctx, js); err != nil {
return err
}
}
var lock sync.Mutex
var finalErrs []error
for _, rjob := range js.Spec.ReplicatedJobs {
jobs, err := constructJobsFromTemplate(js, &rjob, ownedJobs)
if err != nil {
return err
}
workqueue.ParallelizeUntil(ctx, maxParallelism, len(jobs), func(i int) {
job := jobs[i]
// Set jobset controller as owner of the job for garbage collection and reconcilation.
if err := ctrl.SetControllerReference(js, job, r.Scheme); err != nil {
lock.Lock()
defer lock.Unlock()
finalErrs = append(finalErrs, err)
return
}
// Create the job.
// TODO(#18): Deal with the case where the job exists but is not owned by the jobset.
if err := r.Create(ctx, job); err != nil {
lock.Lock()
defer lock.Unlock()
finalErrs = append(finalErrs, err)
return
}
log.V(2).Info("successfully created job", "job", klog.KObj(job))
})
}
return errors.Join(finalErrs...)
}
// TODO: look into adopting service and updating the selector
// if it is not matching the job selector.
func (r *JobSetReconciler) createHeadlessSvcIfNotExist(ctx context.Context, js *jobset.JobSet) error {
log := ctrl.LoggerFrom(ctx)
// Check if service already exists. The service name should match the subdomain specified in
// Spec.Network.Subdomain, with default of <jobSetName> set by the webhook.
// If the service doesn't exist in the same namespace, create it.
var headlessSvc corev1.Service
if err := r.Get(ctx, types.NamespacedName{Name: js.Spec.Network.Subdomain, Namespace: js.Namespace}, &headlessSvc); err != nil {
headlessSvc := corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: js.Spec.Network.Subdomain,
Namespace: js.Namespace,
},
Spec: corev1.ServiceSpec{
ClusterIP: "None",
Selector: map[string]string{
jobset.JobSetNameKey: js.Name,
},
},
}
// Set controller owner reference for garbage collection and reconcilation.
if err := ctrl.SetControllerReference(js, &headlessSvc, r.Scheme); err != nil {
return err
}
// Create headless service.
if err := r.Create(ctx, &headlessSvc); err != nil {
return err
}
log.V(2).Info("successfully created headless service", "service", klog.KObj(&headlessSvc))
}
return nil
}
// executeSuccessPolicy checks the completed jobs against the jobset success policy
// and updates the jobset status to completed if the success policy conditions are met.
// Returns a boolean value indicating if the jobset was completed or not.
func (r *JobSetReconciler) executeSuccessPolicy(ctx context.Context, js *jobset.JobSet, ownedJobs *childJobs) (bool, error) {
if numJobsMatchingSuccessPolicy(js, ownedJobs.successful) >= numJobsExpectedToSucceed(js) {
if err := r.ensureCondition(ctx, js, corev1.EventTypeNormal, metav1.Condition{
Type: string(jobset.JobSetCompleted),
Status: metav1.ConditionStatus(corev1.ConditionTrue),
Reason: "AllJobsCompleted",
Message: "jobset completed successfully",
}); err != nil {
return false, err
}
return true, nil
}
return false, nil
}
func (r *JobSetReconciler) executeFailurePolicy(ctx context.Context, js *jobset.JobSet, ownedJobs *childJobs) error {
// If no failure policy is defined, the default failure policy is to mark the JobSet
// as failed if any of its jobs have failed.
if js.Spec.FailurePolicy == nil {
return r.failJobSet(ctx, js)
}
// To reach this point a job must have failed.
return r.executeRestartPolicy(ctx, js, ownedJobs)
}
func (r *JobSetReconciler) executeRestartPolicy(ctx context.Context, js *jobset.JobSet, ownedJobs *childJobs) error {
if js.Spec.FailurePolicy.MaxRestarts == 0 {
return r.failJobSet(ctx, js)
}
return r.restartPolicyRecreateAll(ctx, js, ownedJobs)
}
func (r *JobSetReconciler) restartPolicyRecreateAll(ctx context.Context, js *jobset.JobSet, ownedJobs *childJobs) error {
log := ctrl.LoggerFrom(ctx)
// If JobSet has reached max number of restarts, mark it as failed and return.
if js.Status.Restarts >= js.Spec.FailurePolicy.MaxRestarts {
return r.ensureCondition(ctx, js, corev1.EventTypeWarning, metav1.Condition{
Type: string(jobset.JobSetFailed),
Status: metav1.ConditionStatus(corev1.ConditionTrue),
Reason: "ReachedMaxRestarts",
Message: "jobset failed due to reaching max number of restarts",
})
}
// Increment JobSet restarts. This will trigger reconciliation and result in deletions
// of old jobs not part of the current jobSet run.
js.Status.Restarts += 1
if err := r.updateStatus(ctx, js, corev1.EventTypeWarning, "Restarting", fmt.Sprintf("restarting jobset, attempt %d", js.Status.Restarts)); err != nil {
return err
}
log.V(2).Info("attempting restart", "restart attempt", js.Status.Restarts)
return nil
}
func (r *JobSetReconciler) deleteJobs(ctx context.Context, jobsForDeletion []*batchv1.Job) error {
log := ctrl.LoggerFrom(ctx)
lock := &sync.Mutex{}
var finalErrs []error
workqueue.ParallelizeUntil(ctx, maxParallelism, len(jobsForDeletion), func(i int) {
targetJob := jobsForDeletion[i]
// Delete job. This deletion event will trigger another reconciliation,
// where the jobs are recreated.
backgroundPolicy := metav1.DeletePropagationBackground
if err := r.Delete(ctx, targetJob, &client.DeleteOptions{PropagationPolicy: &backgroundPolicy}); client.IgnoreNotFound(err) != nil {
lock.Lock()
defer lock.Unlock()
finalErrs = append(finalErrs, err)
return
}
log.V(2).Info("successfully deleted job", "job", klog.KObj(targetJob), "restart attempt", targetJob.Labels[targetJob.Labels[RestartsKey]])
})
return errors.Join(finalErrs...)
}
// updateStatus updates the status of a JobSet.
func (r *JobSetReconciler) updateStatus(ctx context.Context, js *jobset.JobSet, eventType, eventReason, eventMsg string) error {
if err := r.Status().Update(ctx, js); err != nil {
return err
}
r.Record.Eventf(js, eventType, eventReason, eventMsg)
return nil
}
func (r *JobSetReconciler) ensureCondition(ctx context.Context, js *jobset.JobSet, eventType string, condition metav1.Condition) error {
if !updateCondition(js, condition) {
return nil
}
if err := r.Status().Update(ctx, js); err != nil {
return err
}
r.Record.Eventf(js, eventType, condition.Type, condition.Reason)
return nil
}
func (r *JobSetReconciler) failJobSet(ctx context.Context, js *jobset.JobSet) error {
return r.ensureCondition(ctx, js, corev1.EventTypeWarning, metav1.Condition{
Type: string(jobset.JobSetFailed),
Status: metav1.ConditionStatus(corev1.ConditionTrue),
Reason: "FailedJobs",
Message: "jobset failed due to one or more job failures",
})
}
func updateCondition(js *jobset.JobSet, condition metav1.Condition) bool {
condition.LastTransitionTime = metav1.Now()
for i, val := range js.Status.Conditions {
if condition.Type == val.Type && condition.Status != val.Status {
js.Status.Conditions[i] = condition
// Condition found but different status so we should update
return true
} else if condition.Type == val.Type && condition.Status == val.Status {
// Duplicate condition so no update
return false
}
}
// condition doesn't exist, update only if the status is true
if condition.Status == metav1.ConditionTrue {
js.Status.Conditions = append(js.Status.Conditions, condition)
return true
}
return false
}
func constructJobsFromTemplate(js *jobset.JobSet, rjob *jobset.ReplicatedJob, ownedJobs *childJobs) ([]*batchv1.Job, error) {
var jobs []*batchv1.Job
for jobIdx := 0; jobIdx < int(rjob.Replicas); jobIdx++ {
jobName := placement.GenJobName(js.Name, rjob.Name, jobIdx)
if create := shouldCreateJob(jobName, ownedJobs); !create {
continue
}
job, err := constructJob(js, rjob, jobIdx)
if err != nil {
return nil, err
}
jobs = append(jobs, job)
}
return jobs, nil
}
func constructJob(js *jobset.JobSet, rjob *jobset.ReplicatedJob, jobIdx int) (*batchv1.Job, error) {
job := &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Labels: collections.CloneMap(rjob.Template.Labels),
Annotations: collections.CloneMap(rjob.Template.Annotations),
Name: placement.GenJobName(js.Name, rjob.Name, jobIdx),
Namespace: js.Namespace,
},
Spec: *rjob.Template.Spec.DeepCopy(),
}
// Label and annotate both job and pod template spec.
labelAndAnnotateObject(job, js, rjob, jobIdx)
labelAndAnnotateObject(&job.Spec.Template, js, rjob, jobIdx)
// If enableDNSHostnames is set, update job spec to set subdomain as
// job name (a headless service with same name as job will be created later).
if dnsHostnamesEnabled(js) {
job.Spec.Template.Spec.Subdomain = js.Spec.Network.Subdomain
}
// If this job should be exclusive per topology, configure the scheduling constraints accordingly.
if _, ok := js.Annotations[jobset.ExclusiveKey]; ok {
// If user has set the nodeSelectorStrategy annotation flag, add the job name label as a
// nodeSelector, and add a toleration for the no schedule taint.
// The node label and node taint must be added separately by a user/script.
if _, exists := js.Annotations[jobset.NodeSelectorStrategyKey]; exists {
addNamespacedJobNodeSelector(job)
addTaintToleration(job)
}
// Otherwise, we fall back to the default strategy of the pod webhook assigning exclusive affinities
// to the leader pod, preventing follower pod creation until leader pod is scheduled, then
// assigning the follower pods to the same node as the leader pods.
}
// if Suspend is set, then we assume all jobs will be suspended also.
jobsetSuspended := js.Spec.Suspend != nil && *js.Spec.Suspend
job.Spec.Suspend = ptr.To(jobsetSuspended)
return job, nil
}
func addTaintToleration(job *batchv1.Job) {
job.Spec.Template.Spec.Tolerations = append(job.Spec.Template.Spec.Tolerations,
corev1.Toleration{
Key: jobset.NoScheduleTaintKey,
Operator: corev1.TolerationOpExists,
Effect: corev1.TaintEffectNoSchedule,
},
)
}
func shouldCreateJob(jobName string, ownedJobs *childJobs) bool {
// Check if this job exists already.
// TODO: maybe we can use a job map here so we can do O(1) lookups
// to check if the job already exists, rather than a linear scan
// through all the jobs owned by the jobset.
for _, job := range collections.Concat(ownedJobs.active, ownedJobs.successful, ownedJobs.failed, ownedJobs.delete) {
if jobName == job.Name {
return false
}
}
return true
}
func labelAndAnnotateObject(obj metav1.Object, js *jobset.JobSet, rjob *jobset.ReplicatedJob, jobIdx int) {
jobName := placement.GenJobName(js.Name, rjob.Name, jobIdx)
labels := collections.CloneMap(obj.GetLabels())
labels[jobset.JobSetNameKey] = js.Name
labels[jobset.ReplicatedJobNameKey] = rjob.Name
labels[RestartsKey] = strconv.Itoa(int(js.Status.Restarts))
labels[jobset.ReplicatedJobReplicas] = strconv.Itoa(int(rjob.Replicas))
labels[jobset.JobIndexKey] = strconv.Itoa(jobIdx)
labels[jobset.JobKey] = jobHashKey(js.Namespace, jobName)
annotations := collections.CloneMap(obj.GetAnnotations())
annotations[jobset.JobSetNameKey] = js.Name
annotations[jobset.ReplicatedJobNameKey] = rjob.Name
annotations[RestartsKey] = strconv.Itoa(int(js.Status.Restarts))
annotations[jobset.ReplicatedJobReplicas] = strconv.Itoa(int(rjob.Replicas))
annotations[jobset.JobIndexKey] = strconv.Itoa(jobIdx)
annotations[jobset.JobKey] = jobHashKey(js.Namespace, jobName)
// Only set exclusive key label/annotation on jobs/pods if the parent JobSet
// is using exclusive placement.
if _, exists := js.Annotations[jobset.ExclusiveKey]; exists {
annotations[jobset.ExclusiveKey] = js.Annotations[jobset.ExclusiveKey]
}
obj.SetLabels(labels)
obj.SetAnnotations(annotations)
}
func JobFinished(job *batchv1.Job) (bool, batchv1.JobConditionType) {
for _, c := range job.Status.Conditions {
if (c.Type == batchv1.JobComplete || c.Type == batchv1.JobFailed) && c.Status == corev1.ConditionTrue {
return true, c.Type
}
}
return false, ""
}
func GenSubdomain(js *jobset.JobSet) string {
// If we have selected an explicit network name, use it
if js.Spec.Network.Subdomain != "" {
return js.Spec.Network.Subdomain
}
return js.Name
}
func addNamespacedJobNodeSelector(job *batchv1.Job) {
if job.Spec.Template.Spec.NodeSelector == nil {
job.Spec.Template.Spec.NodeSelector = make(map[string]string)
}
job.Spec.Template.Spec.NodeSelector[jobset.NamespacedJobKey] = namespacedJobName(job.Namespace, job.Name)
}
// Human readable namespaced job name. We must use '_' to separate namespace and job instead of '/'
// since the '/' character is not allowed in label values.
func namespacedJobName(ns, jobName string) string {
return fmt.Sprintf("%s_%s", ns, jobName)
}
// jobHashKey returns the SHA1 hash of the namespaced job name (i.e. <namespace>/<jobName>).
func jobHashKey(ns string, jobName string) string {
return sha1Hash(fmt.Sprintf("%s/%s", ns, jobName))
}
// sha1Hash accepts an input string and returns the 40 character SHA1 hash digest of the input string.
func sha1Hash(s string) string {
h := sha1.New()
h.Write([]byte(s))
return hex.EncodeToString(h.Sum(nil))
}
func jobSetFinished(js *jobset.JobSet) bool {
for _, c := range js.Status.Conditions {
if (c.Type == string(jobset.JobSetCompleted) || c.Type == string(jobset.JobSetFailed)) && c.Status == metav1.ConditionTrue {
return true
}
}
return false
}
func dnsHostnamesEnabled(js *jobset.JobSet) bool {
return js.Spec.Network.EnableDNSHostnames != nil && *js.Spec.Network.EnableDNSHostnames
}
func jobMatchesSuccessPolicy(js *jobset.JobSet, job *batchv1.Job) bool {
return len(js.Spec.SuccessPolicy.TargetReplicatedJobs) == 0 || collections.Contains(js.Spec.SuccessPolicy.TargetReplicatedJobs, job.ObjectMeta.Labels[jobset.ReplicatedJobNameKey])
}
func replicatedJobMatchesSuccessPolicy(js *jobset.JobSet, rjob *jobset.ReplicatedJob) bool {
return len(js.Spec.SuccessPolicy.TargetReplicatedJobs) == 0 || collections.Contains(js.Spec.SuccessPolicy.TargetReplicatedJobs, rjob.Name)
}
func numJobsMatchingSuccessPolicy(js *jobset.JobSet, jobs []*batchv1.Job) int {
total := 0
for _, job := range jobs {
if jobMatchesSuccessPolicy(js, job) {
total += 1
}
}
return total
}
func numJobsExpectedToSucceed(js *jobset.JobSet) int {
total := 0
switch js.Spec.SuccessPolicy.Operator {
case jobset.OperatorAny:
total = 1
case jobset.OperatorAll:
for _, rjob := range js.Spec.ReplicatedJobs {
if replicatedJobMatchesSuccessPolicy(js, &rjob) {
total += int(rjob.Replicas)
}
}
}
return total
}