|
| 1 | +// Copyright 2025 NVIDIA CORPORATION |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package leader_worker_set |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "strconv" |
| 9 | + |
| 10 | + v1 "k8s.io/api/core/v1" |
| 11 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 12 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 13 | + |
| 14 | + "github.com/NVIDIA/KAI-scheduler/pkg/podgrouper/podgroup" |
| 15 | + "github.com/NVIDIA/KAI-scheduler/pkg/podgrouper/podgrouper/plugins/defaultgrouper" |
| 16 | +) |
| 17 | + |
| 18 | +const ( |
| 19 | + startupPolicyLeaderReady = "LeaderReady" |
| 20 | + startupPolicyLeaderCreated = "LeaderCreated" |
| 21 | + |
| 22 | + // LWS annotation and label keys |
| 23 | + lwsSizeAnnotation = "leaderworkerset.sigs.k8s.io/size" |
| 24 | + lwsGroupIndexLabel = "leaderworkerset.sigs.k8s.io/group-index" |
| 25 | + lwsWorkerIndexLabel = "leaderworkerset.sigs.k8s.io/worker-index" |
| 26 | +) |
| 27 | + |
| 28 | +type LwsGrouper struct { |
| 29 | + *defaultgrouper.DefaultGrouper |
| 30 | +} |
| 31 | + |
| 32 | +func NewLwsGrouper(defaultGrouper *defaultgrouper.DefaultGrouper) *LwsGrouper { |
| 33 | + return &LwsGrouper{ |
| 34 | + DefaultGrouper: defaultGrouper, |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +func (lwsGrouper *LwsGrouper) Name() string { |
| 39 | + return "LWS Grouper" |
| 40 | +} |
| 41 | + |
| 42 | +// +kubebuilder:rbac:groups=leaderworkerset.x-k8s.io,resources=leaderworkersets,verbs=get;list;watch |
| 43 | +// +kubebuilder:rbac:groups=leaderworkerset.x-k8s.io,resources=leaderworkersets/finalizers,verbs=patch;update;create |
| 44 | + |
| 45 | +func (lwsGrouper *LwsGrouper) GetPodGroupMetadata( |
| 46 | + lwsJob *unstructured.Unstructured, pod *v1.Pod, _ ...*metav1.PartialObjectMetadata, |
| 47 | +) (*podgroup.Metadata, error) { |
| 48 | + podGroupMetadata, err := lwsGrouper.DefaultGrouper.GetPodGroupMetadata(lwsJob, pod) |
| 49 | + if err != nil { |
| 50 | + return nil, err |
| 51 | + } |
| 52 | + |
| 53 | + groupSize, err := lwsGrouper.getLwsGroupSize(lwsJob) |
| 54 | + if err != nil { |
| 55 | + return nil, err |
| 56 | + } |
| 57 | + |
| 58 | + startupPolicy, err := lwsGrouper.getStartupPolicy(lwsJob) |
| 59 | + if err != nil { |
| 60 | + return nil, err |
| 61 | + } |
| 62 | + |
| 63 | + // Initialize podGroupMetadata with the group size |
| 64 | + switch startupPolicy { |
| 65 | + case startupPolicyLeaderReady: |
| 66 | + if err := handleLeaderReadyPolicy(pod, podGroupMetadata, groupSize); err != nil { |
| 67 | + return nil, fmt.Errorf("error handling leader ready policy: %w", err) |
| 68 | + } |
| 69 | + case startupPolicyLeaderCreated: |
| 70 | + podGroupMetadata.MinAvailable = groupSize |
| 71 | + default: |
| 72 | + return nil, fmt.Errorf("unknown startupPolicy: %s", startupPolicy) |
| 73 | + } |
| 74 | + |
| 75 | + if groupIndexStr, ok := pod.Labels[lwsGroupIndexLabel]; ok { |
| 76 | + if groupIndex, err := strconv.Atoi(groupIndexStr); err == nil { |
| 77 | + podGroupMetadata.Name = fmt.Sprintf("%s-group-%d", podGroupMetadata.Name, groupIndex) |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + return podGroupMetadata, nil |
| 82 | +} |
| 83 | + |
| 84 | +func (lwsGrouper *LwsGrouper) getLwsGroupSize(lwsJob *unstructured.Unstructured) (int32, error) { |
| 85 | + size, found, err := unstructured.NestedInt64(lwsJob.Object, "spec", "leaderWorkerTemplate", "size") |
| 86 | + if err != nil { |
| 87 | + return 0, fmt.Errorf("failed to get leaderWorkerTemplate.size from LWS %s/%s with error: %w", |
| 88 | + lwsJob.GetNamespace(), lwsJob.GetName(), err) |
| 89 | + } |
| 90 | + if !found { |
| 91 | + return 0, fmt.Errorf("leaderWorkerTemplate.size not found in LWS %s/%s", lwsJob.GetNamespace(), lwsJob.GetName()) |
| 92 | + } |
| 93 | + if size <= 0 { |
| 94 | + return 0, fmt.Errorf("invalid leaderWorkerTemplate.size %d in LWS %s/%s", size, lwsJob.GetNamespace(), lwsJob.GetName()) |
| 95 | + } |
| 96 | + return int32(size), nil |
| 97 | +} |
| 98 | + |
| 99 | +// getStartupPolicy extracts the startup policy from the LWS object |
| 100 | +func (lwsGrouper *LwsGrouper) getStartupPolicy(lwsJob *unstructured.Unstructured) (string, error) { |
| 101 | + policy, found, err := unstructured.NestedString(lwsJob.Object, "spec", "startupPolicy") |
| 102 | + if err != nil { |
| 103 | + return "", fmt.Errorf("failed to get startupPolicy from LWS %s/%s: %w", |
| 104 | + lwsJob.GetNamespace(), lwsJob.GetName(), err) |
| 105 | + } |
| 106 | + if !found { |
| 107 | + // Default to LeaderCreated if not specified |
| 108 | + return startupPolicyLeaderCreated, nil |
| 109 | + } |
| 110 | + return policy, nil |
| 111 | +} |
| 112 | + |
| 113 | +func handleLeaderReadyPolicy(pod *v1.Pod, podGroupMetadata *podgroup.Metadata, fallbackSize int32) error { |
| 114 | + groupSize := fallbackSize |
| 115 | + |
| 116 | + // Check for the size annotation on the pod |
| 117 | + if sizeStr, ok := pod.Annotations[lwsSizeAnnotation]; ok { |
| 118 | + if parsed, err := strconv.Atoi(sizeStr); err == nil { |
| 119 | + groupSize = int32(parsed) |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + workerIndex, hasWorkerIndex := pod.Labels[lwsWorkerIndexLabel] |
| 124 | + isLeader := hasWorkerIndex && workerIndex == "0" |
| 125 | + isScheduled := pod.Spec.NodeName != "" |
| 126 | + |
| 127 | + if isLeader && !isScheduled { |
| 128 | + // Leader pod not yet scheduled, only need leader to be available |
| 129 | + podGroupMetadata.MinAvailable = 1 |
| 130 | + } else { |
| 131 | + // Either worker pod or leader is already scheduled |
| 132 | + podGroupMetadata.MinAvailable = groupSize |
| 133 | + } |
| 134 | + |
| 135 | + return nil |
| 136 | +} |
0 commit comments