Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions pkg/scheduler/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@ func (h *webhook) Handle(_ context.Context, req admission.Request) admission.Res
klog.V(5).Infof(template, pod.Namespace, pod.Name, pod.UID)
privilegedName, hasPrivileged := privilegedContainerName(pod)
hasResource := false
for idx := range pod.Spec.InitContainers {
c := &pod.Spec.InitContainers[idx]
for _, val := range device.GetDevices() {
found, err := val.MutateAdmission(c, pod)
if err != nil {
klog.Errorf("validating pod failed:%s", err.Error())
return admission.Errored(http.StatusInternalServerError, err)
}
hasResource = hasResource || found
}
}
for idx := range pod.Spec.Containers {
c := &pod.Spec.Containers[idx]
for _, val := range device.GetDevices() {
Expand Down Expand Up @@ -139,6 +150,22 @@ func fitResourceQuota(pod *corev1.Pod) bool {
// container spec here. It applies its own memory factor, defaults and
// template rounding, which is what the scheduler later records as used,
// so this keeps admission and the scheduler on the same numbers.
var initMemoryReq, initCoresReq int64
for i := range pod.Spec.InitContainers {
req := dev.GenerateResourceRequests(&pod.Spec.InitContainers[i])
if req.Nums == 0 {
continue
}
reqMem := int64(req.Memreq) * int64(req.Nums)
reqCores := int64(req.Coresreq) * int64(req.Nums)
if reqMem > initMemoryReq {
initMemoryReq = reqMem
}
if reqCores > initCoresReq {
initCoresReq = reqCores
}
}

var memoryReq, coresReq int64
for i := range pod.Spec.Containers {
req := dev.GenerateResourceRequests(&pod.Spec.Containers[i])
Expand All @@ -148,6 +175,14 @@ func fitResourceQuota(pod *corev1.Pod) bool {
memoryReq += int64(req.Memreq) * int64(req.Nums)
coresReq += int64(req.Coresreq) * int64(req.Nums)
}

if initMemoryReq > memoryReq {
memoryReq = initMemoryReq
}
if initCoresReq > coresReq {
coresReq = initCoresReq
}

if memoryReq == 0 && coresReq == 0 {
continue
}
Expand Down
88 changes: 88 additions & 0 deletions pkg/scheduler/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1070,3 +1070,91 @@ func TestPrivilegedContainerDenied(t *testing.T) {
})
}
}

func TestInitContainersWebhook(t *testing.T) {
config.SchedulerName = "hami-scheduler"
config.ForceOverwriteDefaultScheduler = true

sConfig := &config.Config{
NvidiaConfig: nvidia.NvidiaConfig{
ResourceCountName: "hami.io/gpu",
ResourceMemoryName: "hami.io/gpumem",
ResourceMemoryPercentageName: "hami.io/gpumem-percentage",
ResourceCoreName: "hami.io/gpucores",
DefaultMemory: 0,
DefaultCores: 0,
DefaultGPUNum: 1,
},
}
if err := config.InitDevicesWithConfig(sConfig); err != nil {
t.Fatalf("Failed to initialize devices with config: %v", err)
}

pod := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "test-pod",
Namespace: "default",
},
Spec: corev1.PodSpec{
InitContainers: []corev1.Container{
{
Name: "init-container1",
Resources: corev1.ResourceRequirements{
Limits: corev1.ResourceList{
"hami.io/gpu": resource.MustParse("1"),
},
},
},
},
Containers: []corev1.Container{
{
Name: "regular-container",
},
},
},
}

scheme := runtime.NewScheme()
corev1.AddToScheme(scheme)
codec := serializer.NewCodecFactory(scheme).LegacyCodec(corev1.SchemeGroupVersion)
podBytes, err := runtime.Encode(codec, pod)
if err != nil {
t.Fatalf("Error encoding pod: %v", err)
}

req := admission.Request{
AdmissionRequest: admissionv1.AdmissionRequest{
UID: "test-uid",
Namespace: "default",
Name: "test-pod",
Object: runtime.RawExtension{
Raw: podBytes,
},
},
}

wh, err := NewWebHook()
if err != nil {
t.Fatalf("Error creating WebHook: %v", err)
}

resp := wh.Handle(context.Background(), req)
if !resp.Allowed {
t.Errorf("Expected allowed response, but got denied: %v", resp)
}

// Verify that the scheduler name was patched (meaning hasResource was true)
if len(resp.Patches) == 0 {
t.Errorf("Expected patches for the scheduler name, but got none")
}

hasSchedulerPatch := false
for _, p := range resp.Patches {
if p.Operation == "add" && p.Path == "/spec/schedulerName" && p.Value == "hami-scheduler" {
hasSchedulerPatch = true
}
}
if !hasSchedulerPatch {
t.Errorf("Expected scheduler name patch for init containers, got patches: %+v", resp.Patches)
}
}