|
| 1 | +/* |
| 2 | +Copyright 2025 The llm-d Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package launcherpopulator |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + |
| 23 | + corev1 "k8s.io/api/core/v1" |
| 24 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 25 | + "k8s.io/utils/ptr" |
| 26 | + |
| 27 | + fmav1alpha1 "github.com/llm-d-incubation/llm-d-fast-model-actuation/api/fma/v1alpha1" |
| 28 | +) |
| 29 | + |
| 30 | +// makeLCOwnerRef builds a non-controlling OwnerReference for a LauncherConfig. |
| 31 | +func makeLCOwnerRef(lc *fmav1alpha1.LauncherConfig) metav1.OwnerReference { |
| 32 | + return metav1.OwnerReference{ |
| 33 | + APIVersion: fmav1alpha1.SchemeGroupVersion.String(), |
| 34 | + Kind: "LauncherConfig", |
| 35 | + Name: lc.Name, |
| 36 | + UID: lc.UID, |
| 37 | + Controller: ptr.To(false), |
| 38 | + BlockOwnerDeletion: ptr.To(false), |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +// nonNilSlice returns a single-element slice if s is non-empty, or nil otherwise. |
| 43 | +func nonNilSlice(s string) []string { |
| 44 | + if s == "" { |
| 45 | + return nil |
| 46 | + } |
| 47 | + return []string{s} |
| 48 | +} |
| 49 | + |
| 50 | +// getMatchingNodes returns nodes that match the EnhancedNodeSelector. |
| 51 | +// It returns three values: the matched nodes, user-facing selector errors (non-nil when the |
| 52 | +// LabelSelector itself is malformed — this is a user configuration error), and an internal |
| 53 | +// error (non-nil for unexpected infrastructure failures such as lister errors). |
| 54 | +// Callers should handle selectorErrs and err independently. |
| 55 | +func (ctl *controller) getMatchingNodes(ctx context.Context, selector fmav1alpha1.EnhancedNodeSelector) ([]corev1.Node, []string, error) { |
| 56 | + // Convert the label selector. A failure here is a user error (malformed LabelSelector). |
| 57 | + labelSelector, selectorErr := metav1.LabelSelectorAsSelector(&selector.LabelSelector) |
| 58 | + if selectorErr != nil { |
| 59 | + return nil, []string{fmt.Sprintf("invalid label selector: %v", selectorErr)}, nil |
| 60 | + } |
| 61 | + nodes, err := ctl.nodeLister.List(labelSelector) |
| 62 | + if err != nil { |
| 63 | + return nil, nil, fmt.Errorf("failed to list nodes using nodeLister: %w", err) |
| 64 | + } |
| 65 | + |
| 66 | + var matchedNodes []corev1.Node |
| 67 | + for _, node := range nodes { |
| 68 | + if matchesResourceConditions(node.Status.Allocatable, selector.AllocatableResources) { |
| 69 | + matchedNodes = append(matchedNodes, *node) |
| 70 | + } |
| 71 | + } |
| 72 | + return matchedNodes, nil, nil |
| 73 | +} |
0 commit comments