forked from kubernetes-sigs/kueue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pod_controller.go
1179 lines (1003 loc) · 35.2 KB
/
pod_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
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
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 pod
import (
"cmp"
"context"
"crypto/sha256"
"encoding/json"
"errors"
"fmt"
"slices"
"sort"
"strconv"
"strings"
"time"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
apimeta "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/apimachinery/pkg/util/validation"
"k8s.io/client-go/tools/record"
"k8s.io/klog/v2"
"k8s.io/utils/ptr"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
kueue "sigs.k8s.io/kueue/apis/kueue/v1beta1"
"sigs.k8s.io/kueue/pkg/constants"
controllerconsts "sigs.k8s.io/kueue/pkg/controller/constants"
"sigs.k8s.io/kueue/pkg/controller/jobframework"
"sigs.k8s.io/kueue/pkg/podset"
"sigs.k8s.io/kueue/pkg/util/kubeversion"
"sigs.k8s.io/kueue/pkg/util/parallelize"
utilslices "sigs.k8s.io/kueue/pkg/util/slices"
)
const (
SchedulingGateName = "kueue.x-k8s.io/admission"
FrameworkName = "pod"
gateNotFound = -1
ConditionTypeTerminationTarget = "TerminationTarget"
errMsgIncorrectGroupRoleCount = "pod group can't include more than 8 roles"
IsGroupWorkloadAnnotationKey = "kueue.x-k8s.io/is-group-workload"
IsGroupWorkloadAnnotationValue = "true"
)
// Event reasons used by the pod controller
const (
ReasonExcessPodDeleted = "ExcessPodDeleted"
ReasonOwnerReferencesAdded = "OwnerReferencesAdded"
)
var (
gvk = corev1.SchemeGroupVersion.WithKind("Pod")
errIncorrectReconcileRequest = fmt.Errorf("event handler error: got a single pod reconcile request for a pod group")
errPendingOps = jobframework.UnretryableError("waiting to observe previous operations on pods")
errPodNoSupportKubeVersion = errors.New("pod integration only supported in Kubernetes 1.27 or newer")
)
func init() {
utilruntime.Must(jobframework.RegisterIntegration(FrameworkName, jobframework.IntegrationCallbacks{
SetupIndexes: SetupIndexes,
NewReconciler: NewReconciler,
SetupWebhook: SetupWebhook,
JobType: &corev1.Pod{},
CanSupportIntegration: CanSupportIntegration,
}))
}
// +kubebuilder:rbac:groups=scheduling.k8s.io,resources=priorityclasses,verbs=list;get;watch
// +kubebuilder:rbac:groups="",resources=events,verbs=create;watch;update;patch
// +kubebuilder:rbac:groups="",resources=pods,verbs=get;list;watch;update;patch;delete
// +kubebuilder:rbac:groups="",resources=pods/status,verbs=get;patch
// +kubebuilder:rbac:groups="",resources=pods/finalizers,verbs=get;update
// +kubebuilder:rbac:groups=kueue.x-k8s.io,resources=workloads,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=kueue.x-k8s.io,resources=workloads/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=kueue.x-k8s.io,resources=workloads/finalizers,verbs=update
// +kubebuilder:rbac:groups=kueue.x-k8s.io,resources=resourceflavors,verbs=get;list;watch
type Reconciler struct {
*jobframework.JobReconciler
expectationsStore *expectationsStore
}
func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
return r.ReconcileGenericJob(ctx, req, &Pod{excessPodExpectations: r.expectationsStore})
}
func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error {
concurrency := mgr.GetControllerOptions().GroupKindConcurrency[gvk.GroupKind().String()]
ctrl.Log.V(3).Info("Setting up Pod reconciler", "concurrency", concurrency)
return ctrl.NewControllerManagedBy(mgr).
Watches(&corev1.Pod{}, &podEventHandler{cleanedUpPodsExpectations: r.expectationsStore}).Named("v1_pod").
Watches(&kueue.Workload{}, &workloadHandler{}).
WithOptions(controller.Options{
MaxConcurrentReconciles: concurrency,
}).
Complete(r)
}
func NewReconciler(c client.Client, record record.EventRecorder, opts ...jobframework.Option) jobframework.JobReconcilerInterface {
return &Reconciler{
JobReconciler: jobframework.NewReconciler(c, record, opts...),
expectationsStore: newUIDExpectations("finalizedPods"),
}
}
type Pod struct {
pod corev1.Pod
key types.NamespacedName
isFound bool
isGroup bool
unretriableGroup *bool
list corev1.PodList
excessPodExpectations *expectationsStore
satisfiedExcessPods bool
}
var (
_ jobframework.GenericJob = (*Pod)(nil)
_ jobframework.JobWithFinalize = (*Pod)(nil)
_ jobframework.ComposableJob = (*Pod)(nil)
)
func fromObject(o runtime.Object) *Pod {
out := Pod{}
out.pod = *o.(*corev1.Pod)
return &out
}
// Object returns the job instance.
func (p *Pod) Object() client.Object {
return &p.pod
}
// gateIndex returns the index of the Kueue scheduling gate for corev1.Pod.
// If the scheduling gate is not found, returns -1.
func gateIndex(p *corev1.Pod) int {
for i := range p.Spec.SchedulingGates {
if p.Spec.SchedulingGates[i].Name == SchedulingGateName {
return i
}
}
return gateNotFound
}
func isPodTerminated(p *corev1.Pod) bool {
return p.Status.Phase == corev1.PodFailed || p.Status.Phase == corev1.PodSucceeded
}
func podSuspended(p *corev1.Pod) bool {
return isPodTerminated(p) || gateIndex(p) != gateNotFound
}
func isUnretriablePod(pod corev1.Pod) bool {
return pod.Annotations[RetriableInGroupAnnotation] == "false"
}
// isUnretriableGroup returns true if at least one pod in the group
// has a RetriableInGroupAnnotation set to 'false'.
func (p *Pod) isUnretriableGroup() bool {
if p.unretriableGroup != nil {
return *p.unretriableGroup
}
for _, pod := range p.list.Items {
if isUnretriablePod(pod) {
p.unretriableGroup = ptr.To(true)
return true
}
}
p.unretriableGroup = ptr.To(false)
return false
}
// IsSuspended returns whether the job is suspended or not.
func (p *Pod) IsSuspended() bool {
if !p.isGroup {
return podSuspended(&p.pod)
}
for i := range p.list.Items {
if podSuspended(&p.list.Items[i]) {
return true
}
}
return false
}
// Suspend will suspend the job.
func (p *Pod) Suspend() {
// Not implemented because this is not called when JobWithCustomStop is implemented.
}
// ungatePod removes the kueue scheduling gate from the pod.
// Returns true if the pod has been ungated and false otherwise.
func ungatePod(pod *corev1.Pod) bool {
idx := gateIndex(pod)
if idx != gateNotFound {
pod.Spec.SchedulingGates = append(pod.Spec.SchedulingGates[:idx], pod.Spec.SchedulingGates[idx+1:]...)
return true
}
return false
}
// Run will inject the node affinity and podSet counts extracting from workload to job and unsuspend it.
func (p *Pod) Run(ctx context.Context, c client.Client, podSetsInfo []podset.PodSetInfo, recorder record.EventRecorder, msg string) error {
log := ctrl.LoggerFrom(ctx)
if !p.isGroup {
if len(podSetsInfo) != 1 {
return fmt.Errorf("%w: expecting 1 pod set got %d", podset.ErrInvalidPodsetInfo, len(podSetsInfo))
}
if ungated := ungatePod(&p.pod); !ungated {
return nil
}
if err := podset.Merge(&p.pod.ObjectMeta, &p.pod.Spec, podSetsInfo[0]); err != nil {
return err
}
err := c.Update(ctx, &p.pod)
if err != nil {
return err
}
if recorder != nil {
recorder.Event(&p.pod, corev1.EventTypeNormal, jobframework.ReasonStarted, msg)
}
return nil
}
var podsToUngate []*corev1.Pod
for i := range p.list.Items {
pod := &p.list.Items[i]
if ungated := ungatePod(pod); !ungated {
continue
}
podsToUngate = append(podsToUngate, pod)
}
if len(podsToUngate) == 0 {
return nil
}
return parallelize.Until(ctx, len(podsToUngate), func(i int) error {
pod := podsToUngate[i]
roleHash, err := getRoleHash(*pod)
if err != nil {
return err
}
podSetIndex := slices.IndexFunc(podSetsInfo, func(info podset.PodSetInfo) bool {
return info.Name == roleHash
})
if podSetIndex == -1 {
return fmt.Errorf("%w: podSetInfo with the name '%s' is not found", podset.ErrInvalidPodsetInfo, roleHash)
}
err = podset.Merge(&pod.ObjectMeta, &pod.Spec, podSetsInfo[podSetIndex])
if err != nil {
return err
}
log.V(3).Info("Starting pod in group", "podInGroup", klog.KObj(pod))
if err := c.Update(ctx, pod); err != nil {
return err
}
if recorder != nil {
recorder.Event(pod, corev1.EventTypeNormal, jobframework.ReasonStarted, msg)
}
return nil
})
}
// RunWithPodSetsInfo will inject the node affinity and podSet counts extracting from workload to job and unsuspend it.
func (p *Pod) RunWithPodSetsInfo(_ []podset.PodSetInfo) error {
// Not implemented because this is not called when JobWithCustomRun is implemented.
return fmt.Errorf("RunWithPodSetsInfo is not implemented for the Pod object")
}
// RestorePodSetsInfo will restore the original node affinity and podSet counts of the job.
func (p *Pod) RestorePodSetsInfo(_ []podset.PodSetInfo) bool {
// Not implemented since Pods cannot be updated, they can only be terminated.
return false
}
// Finished means whether the job is completed/failed or not,
// condition represents the workload finished condition.
func (p *Pod) Finished() (metav1.Condition, bool) {
finished := true
condition := metav1.Condition{
Type: kueue.WorkloadFinished,
Status: metav1.ConditionTrue,
Reason: "JobFinished",
Message: "Job finished successfully",
}
if !p.isGroup {
ph := p.pod.Status.Phase
finished = ph == corev1.PodSucceeded || ph == corev1.PodFailed
if ph == corev1.PodFailed {
condition.Message = "Job failed"
}
return condition, finished
}
isActive := false
succeededCount := 0
groupTotalCount, err := p.groupTotalCount()
if err != nil {
ctrl.Log.V(2).Error(err, "failed to check if pod group is finished")
return metav1.Condition{}, false
}
for _, pod := range p.list.Items {
if pod.Status.Phase == corev1.PodSucceeded {
succeededCount++
}
if !isPodTerminated(&pod) {
isActive = true
}
}
unretriableGroup := p.isUnretriableGroup()
if succeededCount == groupTotalCount || (!isActive && unretriableGroup) {
condition.Message = fmt.Sprintf("Pods succeeded: %d/%d.", succeededCount, groupTotalCount)
} else {
return metav1.Condition{}, false
}
return condition, finished
}
// PodSets will build workload podSets corresponding to the job.
func (p *Pod) PodSets() []kueue.PodSet {
return []kueue.PodSet{
{
Name: kueue.DefaultPodSetName,
Count: 1,
Template: corev1.PodTemplateSpec{
Spec: *p.pod.Spec.DeepCopy(),
},
},
}
}
// IsActive returns true if there are any running pods.
func (p *Pod) IsActive() bool {
for i := range p.list.Items {
if p.list.Items[i].Status.Phase == corev1.PodRunning {
return true
}
}
return false
}
func hasPodReadyTrue(conds []corev1.PodCondition) bool {
for i := range conds {
c := conds[i]
if c.Type == corev1.PodReady {
return c.Status == corev1.ConditionTrue
}
}
return false
}
// PodsReady instructs whether job derived pods are all ready now.
func (p *Pod) PodsReady() bool {
if !p.isGroup {
return hasPodReadyTrue(p.pod.Status.Conditions)
}
for i := range p.list.Items {
if !hasPodReadyTrue(p.list.Items[i].Status.Conditions) {
return false
}
}
return true
}
// GVK returns GVK (Group Version Kind) for the job.
func (p *Pod) GVK() schema.GroupVersionKind {
return gvk
}
func (p *Pod) Stop(ctx context.Context, c client.Client, _ []podset.PodSetInfo, stopReason jobframework.StopReason, eventMsg string) ([]client.Object, error) {
var podsInGroup []corev1.Pod
if p.isGroup {
podsInGroup = p.list.Items
} else {
podsInGroup = []corev1.Pod{p.pod}
}
stoppedNow := make([]client.Object, 0)
for i := range podsInGroup {
// If the workload is being deleted, delete even finished Pods.
if !podsInGroup[i].DeletionTimestamp.IsZero() || (stopReason != jobframework.StopReasonWorkloadDeleted && podSuspended(&podsInGroup[i])) {
continue
}
podInGroup := fromObject(&podsInGroup[i])
// The podset info is not relevant here, since this should mark the pod's end of life
pCopy := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
UID: podInGroup.pod.UID,
Name: podInGroup.pod.Name,
Namespace: podInGroup.pod.Namespace,
},
TypeMeta: podInGroup.pod.TypeMeta,
Status: corev1.PodStatus{
Conditions: []corev1.PodCondition{
{
Type: ConditionTypeTerminationTarget,
Status: corev1.ConditionTrue,
LastTransitionTime: metav1.Time{
Time: time.Now(),
},
Reason: "StoppedByKueue",
Message: eventMsg,
},
},
},
}
if err := c.Status().Patch(ctx, pCopy, client.Apply, client.FieldOwner(constants.KueueName)); err != nil && !apierrors.IsNotFound(err) {
return stoppedNow, err
}
if err := c.Delete(ctx, podInGroup.Object()); err != nil && !apierrors.IsNotFound(err) {
return stoppedNow, err
}
stoppedNow = append(stoppedNow, podInGroup.Object())
}
// If related workload is deleted, the generic reconciler will stop the pod group and finalize the workload.
// However, it won't finalize the pods. Since the Stop method for the pod group deletes all the pods in the
// group, the pods will be finalized here.
if p.isGroup && stopReason == jobframework.StopReasonWorkloadDeleted {
err := p.Finalize(ctx, c)
if err != nil {
return stoppedNow, err
}
}
return stoppedNow, nil
}
func SetupIndexes(ctx context.Context, indexer client.FieldIndexer) error {
return jobframework.SetupWorkloadOwnerIndex(ctx, indexer, gvk)
}
func CanSupportIntegration(opts ...jobframework.Option) (bool, error) {
options := jobframework.ProcessOptions(opts...)
v := options.KubeServerVersion.GetServerVersion()
if v.String() == "" || v.LessThan(kubeversion.KubeVersion1_27) {
return false, fmt.Errorf("kubernetesVersion %q: %w", v.String(), errPodNoSupportKubeVersion)
}
return true, nil
}
func (p *Pod) Finalize(ctx context.Context, c client.Client) error {
groupName := podGroupName(p.pod)
var podsInGroup corev1.PodList
if groupName == "" {
podsInGroup.Items = append(podsInGroup.Items, *p.Object().(*corev1.Pod))
} else {
if err := c.List(ctx, &podsInGroup, client.MatchingLabels{
GroupNameLabel: groupName,
}, client.InNamespace(p.pod.Namespace)); err != nil {
return err
}
}
return parallelize.Until(ctx, len(podsInGroup.Items), func(i int) error {
pod := &podsInGroup.Items[i]
if controllerutil.RemoveFinalizer(pod, PodFinalizer) {
return c.Update(ctx, pod)
}
return nil
})
}
func (p *Pod) Skip() bool {
// Skip pod reconciliation, if pod is found, and it's managed label is not set or incorrect.
if v, ok := p.pod.GetLabels()[ManagedLabelKey]; p.isFound && (!ok || v != ManagedLabelValue) {
return true
}
return false
}
// podGroupName returns a value of GroupNameLabel for the pod object.
// Returns an empty string if there's no such label.
func podGroupName(p corev1.Pod) string {
return p.GetLabels()[GroupNameLabel]
}
// groupTotalCount returns the value of GroupTotalCountAnnotation for the pod being reconciled at the moment.
// It doesn't check if the whole group has the same total group count annotation value.
func (p *Pod) groupTotalCount() (int, error) {
if podGroupName(p.pod) == "" {
return 0, fmt.Errorf("pod doesn't have a '%s' label", GroupNameLabel)
}
gtcAnnotation, ok := p.Object().GetAnnotations()[GroupTotalCountAnnotation]
if !ok {
return 0, fmt.Errorf("failed to extract '%s' annotation",
GroupTotalCountAnnotation)
}
gtc, err := strconv.Atoi(gtcAnnotation)
if err != nil {
return 0, err
}
if gtc < 1 {
return 0, fmt.Errorf("incorrect annotation value '%s=%s': group total count should be greater than zero",
GroupTotalCountAnnotation, gtcAnnotation)
}
return gtc, nil
}
// getRoleHash will filter all the fields of the pod that are relevant to admission (pod role) and return a sha256
// checksum of those fields. This is used to group the pods of the same roles when interacting with the workload.
func getRoleHash(p corev1.Pod) (string, error) {
if roleHash, ok := p.Annotations[RoleHashAnnotation]; ok {
return roleHash, nil
}
shape := map[string]interface{}{
"spec": map[string]interface{}{
"initContainers": containersShape(p.Spec.InitContainers),
"containers": containersShape(p.Spec.Containers),
"nodeSelector": p.Spec.NodeSelector,
"affinity": p.Spec.Affinity,
"tolerations": p.Spec.Tolerations,
"runtimeClassName": p.Spec.RuntimeClassName,
"priority": p.Spec.Priority,
"topologySpreadConstraints": p.Spec.TopologySpreadConstraints,
"overhead": p.Spec.Overhead,
"resourceClaims": p.Spec.ResourceClaims,
},
}
shapeJson, err := json.Marshal(shape)
if err != nil {
return "", err
}
// Trim hash to 8 characters and return
return fmt.Sprintf("%x", sha256.Sum256(shapeJson))[:8], nil
}
// Load loads all pods in the group
func (p *Pod) Load(ctx context.Context, c client.Client, key *types.NamespacedName) (removeFinalizers bool, err error) {
nsKey := strings.Split(key.Namespace, "/")
if len(nsKey) == 1 {
if err := c.Get(ctx, *key, &p.pod); err != nil {
return apierrors.IsNotFound(err), err
}
p.isFound = true
// If the key.Namespace doesn't contain a "group/" prefix, even though
// the pod has a group name, there's something wrong with the event handler.
if podGroupName(p.pod) != "" {
return false, errIncorrectReconcileRequest
}
return !p.pod.DeletionTimestamp.IsZero(), nil
}
p.isGroup = true
key.Namespace = nsKey[1]
p.key = *key
// Check the expectations before listing pods, otherwise a new pod can sneak in
// and update the expectations after we've retrieved active pods from the store.
p.satisfiedExcessPods = p.excessPodExpectations.Satisfied(ctrl.LoggerFrom(ctx), *key)
if err := c.List(ctx, &p.list, client.MatchingLabels{
GroupNameLabel: key.Name,
}, client.InNamespace(key.Namespace)); err != nil {
return false, err
}
if len(p.list.Items) > 0 {
p.isFound = true
p.pod = p.list.Items[0]
key.Name = p.pod.Name
}
// If none of the pods in group are found,
// the respective workload should be finalized
return !p.isFound, nil
}
func (p *Pod) constructGroupPodSets() ([]kueue.PodSet, error) {
return constructGroupPodSets(p.list.Items)
}
func constructGroupPodSets(pods []corev1.Pod) ([]kueue.PodSet, error) {
var resultPodSets []kueue.PodSet
for _, podInGroup := range pods {
if !isPodRunnableOrSucceeded(&podInGroup) {
continue
}
roleHash, err := getRoleHash(podInGroup)
if err != nil {
return nil, fmt.Errorf("failed to calculate pod role hash: %w", err)
}
podRoleFound := false
for psi := range resultPodSets {
if resultPodSets[psi].Name == roleHash {
podRoleFound = true
resultPodSets[psi].Count++
}
}
if !podRoleFound {
podSet := fromObject(&podInGroup).PodSets()
podSet[0].Name = roleHash
resultPodSets = append(resultPodSets, podSet[0])
}
}
slices.SortFunc(resultPodSets, func(a, b kueue.PodSet) int {
return cmp.Compare(a.Name, b.Name)
})
return resultPodSets, nil
}
// validatePodGroupMetadata validates metadata of all members of the pod group
func (p *Pod) validatePodGroupMetadata(r record.EventRecorder, activePods []corev1.Pod) error {
groupTotalCount, err := p.groupTotalCount()
if err != nil {
return err
}
originalQueue := jobframework.QueueName(p)
if len(activePods) < groupTotalCount {
errMsg := fmt.Sprintf("'%s' group has fewer runnable pods than expected", podGroupName(p.pod))
r.Eventf(p.Object(), corev1.EventTypeWarning, jobframework.ReasonErrWorkloadCompose, errMsg)
return jobframework.UnretryableError(errMsg)
}
for _, podInGroup := range p.list.Items {
// Skip failed pods
if podInGroup.Status.Phase == corev1.PodFailed {
continue
}
if podInGroupQueue := jobframework.QueueNameForObject(&podInGroup); podInGroupQueue != originalQueue {
return jobframework.UnretryableError(fmt.Sprintf("pods '%s' and '%s' has different queue names: %s!=%s",
p.pod.GetName(), podInGroup.GetName(),
originalQueue, podInGroupQueue))
}
tc, err := strconv.Atoi(podInGroup.GetAnnotations()[GroupTotalCountAnnotation])
if err != nil {
return fmt.Errorf("failed to extract '%s' annotation from the pod '%s': %w",
GroupTotalCountAnnotation,
podInGroup.GetName(),
err)
}
if tc != groupTotalCount {
return jobframework.UnretryableError(fmt.Sprintf("pods '%s' and '%s' has different '%s' values: %d!=%d",
p.pod.GetName(), podInGroup.GetName(),
GroupTotalCountAnnotation,
groupTotalCount, tc))
}
}
return nil
}
// runnableOrSucceededPods returns a slice of active pods in the group
func (p *Pod) runnableOrSucceededPods() []corev1.Pod {
return utilslices.Pick(p.list.Items, isPodRunnableOrSucceeded)
}
// notRunnableNorSucceededPods returns a slice of inactive pods in the group
func (p *Pod) notRunnableNorSucceededPods() []corev1.Pod {
return utilslices.Pick(p.list.Items, func(p *corev1.Pod) bool { return !isPodRunnableOrSucceeded(p) })
}
// isPodRunnableOrSucceeded returns whether the Pod can eventually run, is Running or Succeeded.
// A Pod cannot run if it's gated and has a deletionTimestamp.
func isPodRunnableOrSucceeded(p *corev1.Pod) bool {
if p.DeletionTimestamp != nil && len(p.Spec.SchedulingGates) > 0 {
return false
}
return p.Status.Phase != corev1.PodFailed
}
// lastActiveTime returns the last timestamp on which the pod was observed active:
// - the time the pod was declared Failed
// - the deletion time
func lastActiveTime(p *corev1.Pod) time.Time {
lastTransition := metav1.Now()
for _, c := range p.Status.Conditions {
if c.Type == corev1.ContainersReady {
if c.Status == corev1.ConditionFalse && c.Reason == string(corev1.PodFailed) {
lastTransition = c.LastTransitionTime
}
break
}
}
deletionTime := ptr.Deref(p.DeletionTimestamp, metav1.Now())
if lastTransition.Before(&deletionTime) {
return lastTransition.Time
}
return deletionTime.Time
}
// sortInactivePods sorts the provided pods slice based on:
// - finalizer state (pods with finalizers are first)
// - lastActiveTime (pods that were active last are first)
// - creation timestamp (newer pods are first)
func sortInactivePods(inactivePods []corev1.Pod) {
sort.Slice(inactivePods, func(i, j int) bool {
pi := &inactivePods[i]
pj := &inactivePods[j]
iFin := slices.Contains(pi.Finalizers, PodFinalizer)
jFin := slices.Contains(pj.Finalizers, PodFinalizer)
if iFin != jFin {
return iFin
}
iLastActive := lastActiveTime(pi)
jLastActive := lastActiveTime(pj)
if iLastActive.Equal(jLastActive) {
return pi.CreationTimestamp.Before(&pj.CreationTimestamp)
}
return jLastActive.Before(iLastActive)
})
}
// sortActivePods sorts the provided pods slice based on:
// - finalizer state (pods with no finalizers are last)
// - gated state (pods that are still gated are last)
// - creation timestamp (newer pods are last)
func sortActivePods(activePods []corev1.Pod) {
// Sort active pods by creation timestamp
sort.Slice(activePods, func(i, j int) bool {
pi := &activePods[i]
pj := &activePods[j]
iFin := slices.Contains(pi.Finalizers, PodFinalizer)
jFin := slices.Contains(pj.Finalizers, PodFinalizer)
// Prefer to keep pods that have a finalizer.
if iFin != jFin {
return iFin
}
iGated := gateIndex(pi) != gateNotFound
jGated := gateIndex(pj) != gateNotFound
// Prefer to keep pods that aren't gated.
if iGated != jGated {
return !iGated
}
return pi.CreationTimestamp.Before(&pj.CreationTimestamp)
})
}
func (p *Pod) removeExcessPods(ctx context.Context, c client.Client, r record.EventRecorder, extraPods []corev1.Pod) error {
if len(extraPods) == 0 {
return nil
}
log := ctrl.LoggerFrom(ctx)
// Extract all the latest created extra pods
extraPodsUIDs := utilslices.Map(extraPods, func(p *corev1.Pod) types.UID { return p.UID })
p.excessPodExpectations.ExpectUIDs(log, p.key, extraPodsUIDs)
// Finalize and delete the active pods created last
err := parallelize.Until(ctx, len(extraPods), func(i int) error {
pod := extraPods[i]
if controllerutil.RemoveFinalizer(&pod, PodFinalizer) {
log.V(3).Info("Finalizing excess pod in group", "excessPod", klog.KObj(&pod))
if err := c.Update(ctx, &pod); err != nil {
// We won't observe this cleanup in the event handler.
p.excessPodExpectations.ObservedUID(log, p.key, pod.UID)
return err
}
}
if pod.DeletionTimestamp.IsZero() {
log.V(3).Info("Deleting excess pod in group", "excessPod", klog.KObj(&pod))
if err := c.Delete(ctx, &pod); err != nil {
// We won't observe this cleanup in the event handler.
p.excessPodExpectations.ObservedUID(log, p.key, pod.UID)
return err
}
r.Event(&pod, corev1.EventTypeNormal, ReasonExcessPodDeleted, "Excess pod deleted")
}
return nil
})
if err != nil {
return err
}
return nil
}
func (p *Pod) finalizePods(ctx context.Context, c client.Client, extraPods []corev1.Pod) error {
if len(extraPods) == 0 {
return nil
}
log := ctrl.LoggerFrom(ctx)
// Extract all the latest created extra pods
extraPodsUIDs := utilslices.Map(extraPods, func(p *corev1.Pod) types.UID { return p.UID })
p.excessPodExpectations.ExpectUIDs(log, p.key, extraPodsUIDs)
err := parallelize.Until(ctx, len(extraPods), func(i int) error {
pod := extraPods[i]
if controllerutil.RemoveFinalizer(&pod, PodFinalizer) {
log.V(3).Info("Finalizing pod in group", "Pod", klog.KObj(&pod))
if err := c.Update(ctx, &pod); err != nil {
// We won't observe this cleanup in the event handler.
p.excessPodExpectations.ObservedUID(log, p.key, pod.UID)
return err
}
} else {
// We don't expect an event in this case.
p.excessPodExpectations.ObservedUID(log, p.key, pod.UID)
}
return nil
})
if err != nil {
return err
}
return nil
}
func (p *Pod) ensureWorkloadOwnedByAllMembers(ctx context.Context, c client.Client, r record.EventRecorder, workload *kueue.Workload) error {
oldOwnersCnt := len(workload.GetOwnerReferences())
for _, pod := range p.list.Items {
if err := controllerutil.SetOwnerReference(&pod, workload, c.Scheme()); err != nil {
return err
}
}
newOwnersCnt := len(workload.GetOwnerReferences())
if addedOwnersCnt := newOwnersCnt - oldOwnersCnt; addedOwnersCnt > 0 {
log := ctrl.LoggerFrom(ctx).WithValues("workload", klog.KObj(workload))
log.V(4).Info("Adding owner references for workload", "count", addedOwnersCnt)
err := c.Update(ctx, workload)
if err == nil {
r.Eventf(workload, corev1.EventTypeNormal, ReasonOwnerReferencesAdded, fmt.Sprintf("Added %d owner reference(s)", addedOwnersCnt))
}
return err
}
return nil
}
func (p *Pod) ConstructComposableWorkload(ctx context.Context, c client.Client, r record.EventRecorder) (*kueue.Workload, error) {
object := p.Object()
log := ctrl.LoggerFrom(ctx)
wl := &kueue.Workload{
ObjectMeta: metav1.ObjectMeta{
Namespace: p.pod.GetNamespace(),
Labels: map[string]string{},
Finalizers: []string{kueue.ResourceInUseFinalizerName},
},
Spec: kueue.WorkloadSpec{
QueueName: jobframework.QueueName(p),
},
}
// Construct workload for a single pod
if !p.isGroup {
wl.Spec.PodSets = p.PodSets()
wl.Name = jobframework.GetWorkloadNameForOwnerWithGVK(p.pod.GetName(), p.pod.GetUID(), p.GVK())
jobUid := string(object.GetUID())
if errs := validation.IsValidLabelValue(jobUid); len(errs) == 0 {
wl.Labels[controllerconsts.JobUIDLabel] = jobUid
} else {
log.V(2).Info(
"Validation of the owner job UID label has failed. Creating workload without the label.",
"ValidationErrors", errs,
"LabelValue", jobUid,
)
}
// add the controller ref
if err := controllerutil.SetControllerReference(object, wl, c.Scheme()); err != nil {
return nil, err
}
return wl, nil
}
if err := p.finalizePods(ctx, c, p.notRunnableNorSucceededPods()); err != nil {
return nil, err
}
activePods := p.runnableOrSucceededPods()
if wl.Annotations == nil {
wl.Annotations = make(map[string]string)
}
wl.Annotations[IsGroupWorkloadAnnotationKey] = IsGroupWorkloadAnnotationValue
err := p.validatePodGroupMetadata(r, activePods)
if err != nil {
return nil, err
}
groupTotalCount, err := p.groupTotalCount()
if err != nil {
return nil, err
}
// Cleanup extra pods if there's any
if excessPodsCount := len(activePods) - groupTotalCount; excessPodsCount > 0 {
sortActivePods(activePods)
err = p.removeExcessPods(ctx, c, r, activePods[len(activePods)-excessPodsCount:])
if err != nil {
return nil, err
}
p.list.Items = activePods[:len(activePods)-excessPodsCount]
}
// Construct workload for a pod group
wl.Spec.PodSets, err = p.constructGroupPodSets()
if err != nil {
if jobframework.IsUnretryableError(err) {
r.Eventf(object, corev1.EventTypeWarning, jobframework.ReasonErrWorkloadCompose, err.Error())
}
return nil, err
}
if len(wl.Spec.PodSets) > 8 {
return nil, jobframework.UnretryableError(errMsgIncorrectGroupRoleCount)
}
wl.Name = podGroupName(p.pod)
for _, pod := range p.list.Items {
if err := controllerutil.SetOwnerReference(&pod, wl, c.Scheme()); err != nil {
return nil, err
}
}
return wl, nil
}
func (p *Pod) ListChildWorkloads(ctx context.Context, c client.Client, key types.NamespacedName) (*kueue.WorkloadList, error) {
log := ctrl.LoggerFrom(ctx)
workloads := &kueue.WorkloadList{}
// Get related workloads for the pod group
if p.isGroup {
workload := &kueue.Workload{}
if err := c.Get(ctx, types.NamespacedName{Name: key.Name, Namespace: key.Namespace}, workload); err != nil {
if apierrors.IsNotFound(err) {
return workloads, nil
}
log.Error(err, "Unable to get related workload for the pod group")
return nil, err