diff --git a/pkg/device/mthreads/device.go b/pkg/device/mthreads/device.go index 65fa21ccca..df81d1a379 100644 --- a/pkg/device/mthreads/device.go +++ b/pkg/device/mthreads/device.go @@ -97,9 +97,15 @@ func (dev *MthreadsDevices) MutateAdmission(ctr *corev1.Container, p *corev1.Pod if count.Value() <= 0 { return false, fmt.Errorf("%s must be greater than 0", MthreadsResourceCount) } + if ctr.Resources.Limits == nil { + ctr.Resources.Limits = corev1.ResourceList{} + } if count.Value() > 1 { ctr.Resources.Limits[corev1.ResourceName(MthreadsResourceCores)] = *resource.NewQuantity(count.Value()*int64(coresPerMthreadsGPU), resource.DecimalSI) ctr.Resources.Limits[corev1.ResourceName(MthreadsResourceMemory)] = *resource.NewQuantity(count.Value()*int64(memoryPerMthreadsGPU), resource.DecimalSI) + if p.Annotations == nil { + p.Annotations = make(map[string]string) + } p.Annotations["mthreads.com/request-gpu-num"] = fmt.Sprint(count.Value()) return ok, nil } diff --git a/pkg/device/mthreads/device_test.go b/pkg/device/mthreads/device_test.go index 6f8934ef5e..f45c2a4f46 100644 --- a/pkg/device/mthreads/device_test.go +++ b/pkg/device/mthreads/device_test.go @@ -64,6 +64,65 @@ func Test_MutateAdmission(t *testing.T) { }, want: true, }, + { + name: "count over one without pod annotations", + args: struct { + ctr *corev1.Container + p *corev1.Pod + }{ + ctr: &corev1.Container{ + Resources: corev1.ResourceRequirements{ + Limits: corev1.ResourceList{ + "mthreads.com/vgpu": *resource.NewQuantity(2, resource.DecimalSI), + }, + }, + }, + p: &corev1.Pod{}, + }, + want: true, + }, + { + name: "count over one requested without limits", + args: struct { + ctr *corev1.Container + p *corev1.Pod + }{ + ctr: &corev1.Container{ + Resources: corev1.ResourceRequirements{ + Requests: corev1.ResourceList{ + "mthreads.com/vgpu": *resource.NewQuantity(2, resource.DecimalSI), + }, + }, + }, + p: &corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Annotations: map[string]string{}, + }, + }, + }, + want: true, + }, + { + name: "count one requested without limits", + args: struct { + ctr *corev1.Container + p *corev1.Pod + }{ + ctr: &corev1.Container{ + Resources: corev1.ResourceRequirements{ + Requests: corev1.ResourceList{ + "mthreads.com/vgpu": *resource.NewQuantity(1, resource.DecimalSI), + }, + }, + }, + p: &corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Annotations: map[string]string{}, + }, + }, + }, + want: true, + }, { name: "don't set to count limit", args: struct {