|
| 1 | +// Copyright 2025 NVIDIA CORPORATION |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +// +kubebuilder:object:generate:=true |
| 5 | +package binder |
| 6 | + |
| 7 | +import ( |
| 8 | + "k8s.io/utils/ptr" |
| 9 | + |
| 10 | + "github.com/NVIDIA/KAI-scheduler/pkg/apis/kai/v1/common" |
| 11 | + "github.com/NVIDIA/KAI-scheduler/pkg/common/constants" |
| 12 | + v1 "k8s.io/api/core/v1" |
| 13 | + "k8s.io/apimachinery/pkg/api/resource" |
| 14 | +) |
| 15 | + |
| 16 | +const ( |
| 17 | + imageName = "binder" |
| 18 | + defaultResourceReservationImageName = "resource-reservation" |
| 19 | +) |
| 20 | + |
| 21 | +type Binder struct { |
| 22 | + Service *common.Service `json:"service,omitempty"` |
| 23 | + |
| 24 | + // ResourceReservation controls configuration for the resource reservation functionality |
| 25 | + // +kubebuilder:validation:Optional |
| 26 | + ResourceReservation *ResourceReservation `json:"resourceReservation,omitempty"` |
| 27 | + |
| 28 | + // Replicas specifies the number of replicas of the KAI binder service |
| 29 | + // +kubebuilder:validation:Optional |
| 30 | + Replicas *int32 `json:"replicas,omitempty"` |
| 31 | + |
| 32 | + // MaxConcurrentReconciles is the maximum number of concurrent reconciles for both pods and BindRequests |
| 33 | + // +kubebuilder:validation:Optional |
| 34 | + MaxConcurrentReconciles *int `json:"maxConcurrentReconciles,omitempty"` |
| 35 | + |
| 36 | + // VolumeBindingTimeoutSeconds specifies the timeout for volume binding in seconds |
| 37 | + // +kubebuilder:validation:Optional |
| 38 | + VolumeBindingTimeoutSeconds *int `json:"volumeBindingTimeoutSeconds,omitempty"` |
| 39 | + |
| 40 | + // ProbePort specifies the health check port |
| 41 | + ProbePort *int `json:"probePort,omitempty"` |
| 42 | + |
| 43 | + // MetricsPort specifies the metrics service port |
| 44 | + MetricsPort *int `json:"metricsPort,omitempty"` |
| 45 | +} |
| 46 | + |
| 47 | +func (b *Binder) SetDefaultsWhereNeeded(replicaCount *int32) { |
| 48 | + b.Service = common.SetDefault(b.Service, &common.Service{}) |
| 49 | + b.Service.Enabled = common.SetDefault(b.Service.Enabled, ptr.To(false)) |
| 50 | + b.Service.SetDefaultsWhereNeeded(imageName) |
| 51 | + |
| 52 | + if _, found := b.Service.Resources.Requests[v1.ResourceCPU]; !found { |
| 53 | + b.Service.Resources.Requests[v1.ResourceCPU] = resource.MustParse("50m") |
| 54 | + } |
| 55 | + if _, found := b.Service.Resources.Requests[v1.ResourceMemory]; !found { |
| 56 | + b.Service.Resources.Requests[v1.ResourceMemory] = resource.MustParse("200Mi") |
| 57 | + } |
| 58 | + if _, found := b.Service.Resources.Limits[v1.ResourceCPU]; !found { |
| 59 | + b.Service.Resources.Limits[v1.ResourceCPU] = resource.MustParse("100m") |
| 60 | + } |
| 61 | + if _, found := b.Service.Resources.Limits[v1.ResourceMemory]; !found { |
| 62 | + b.Service.Resources.Limits[v1.ResourceMemory] = resource.MustParse("200Mi") |
| 63 | + } |
| 64 | + |
| 65 | + b.Replicas = common.SetDefault(b.Replicas, ptr.To(ptr.Deref(replicaCount, 1))) |
| 66 | + |
| 67 | + b.ResourceReservation = common.SetDefault(b.ResourceReservation, &ResourceReservation{}) |
| 68 | + b.ResourceReservation.SetDefaultsWhereNeeded() |
| 69 | + |
| 70 | + b.ProbePort = common.SetDefault(b.ProbePort, ptr.To(8081)) |
| 71 | + b.MetricsPort = common.SetDefault(b.MetricsPort, ptr.To(8080)) |
| 72 | + |
| 73 | +} |
| 74 | + |
| 75 | +type ResourceReservation struct { |
| 76 | + // Image is the image used by the resource reservation pods |
| 77 | + // +kubebuilder:validation:Optional |
| 78 | + Image *common.Image `json:"image,omitempty"` |
| 79 | + |
| 80 | + // AllocationTimeout specifies the timeout for resource reservation pod allocation in seconds |
| 81 | + // +kubebuilder:validation:Optional |
| 82 | + AllocationTimeout *int `json:"allocationTimeout,omitempty"` |
| 83 | + |
| 84 | + // Namespace is the name of the namespace where the resource reservation pods will run |
| 85 | + // +kubebuilder:validation:Optional |
| 86 | + Namespace *string `json:"namespace,omitempty"` |
| 87 | + |
| 88 | + // ServiceAccountName is the name of the service account that will be used by the resource reservation pods |
| 89 | + // +kubebuilder:validation:Optional |
| 90 | + ServiceAccountName *string `json:"serviceAccountName,omitempty"` |
| 91 | + |
| 92 | + // AppLabel is the value that will be set for all resource reservation pods to the label `app` |
| 93 | + // +kubebuilder:validation:Optional |
| 94 | + AppLabel *string `json:"appLabel,omitempty"` |
| 95 | +} |
| 96 | + |
| 97 | +func (r *ResourceReservation) SetDefaultsWhereNeeded() { |
| 98 | + r.Image = common.SetDefault(r.Image, &common.Image{}) |
| 99 | + r.Image.Name = common.SetDefault(r.Image.Name, ptr.To(defaultResourceReservationImageName)) |
| 100 | + r.Image.SetDefaultsWhereNeeded() |
| 101 | + |
| 102 | + r.Namespace = common.SetDefault(r.Namespace, ptr.To(constants.DefaultResourceReservationName)) |
| 103 | + r.ServiceAccountName = common.SetDefault(r.ServiceAccountName, ptr.To(constants.DefaultResourceReservationName)) |
| 104 | + r.AppLabel = common.SetDefault(r.AppLabel, ptr.To(constants.DefaultResourceReservationName)) |
| 105 | +} |
0 commit comments