@@ -420,16 +420,21 @@ var resourcePath = "/spec/template/spec/containers/%d/resources/limits"
420
420
var envPath = "/spec/template/spec/containers/%d/env/%s"
421
421
var runtimeClassPath = "/spec/template/spec/runtimeClassName"
422
422
423
+ type EnvKeyValue struct {
424
+ Key string
425
+ Value string
426
+ }
427
+
423
428
// CreatePatchForDeployment add gpu env for deployment and returns patch bytes.
424
- func CreatePatchForDeployment (tpl * corev1.PodTemplateSpec , namespace string , gpuRequired * resource.Quantity , typeKey string ) ([]byte , error ) {
425
- patches , err := addResourceLimits (tpl , namespace , gpuRequired , typeKey )
429
+ func CreatePatchForDeployment (tpl * corev1.PodTemplateSpec , namespace string , gpuRequired * resource.Quantity , typeKey string , envKeyValues [] EnvKeyValue ) ([]byte , error ) {
430
+ patches , err := addResourceLimits (tpl , namespace , gpuRequired , typeKey , envKeyValues )
426
431
if err != nil {
427
432
return []byte {}, err
428
433
}
429
434
return json .Marshal (patches )
430
435
}
431
436
432
- func addResourceLimits (tpl * corev1.PodTemplateSpec , namespace string , gpuRequired * resource.Quantity , typeKey string ) (patch []patchOp , err error ) {
437
+ func addResourceLimits (tpl * corev1.PodTemplateSpec , namespace string , gpuRequired * resource.Quantity , typeKey string , envKeyValues [] EnvKeyValue ) (patch []patchOp , err error ) {
433
438
if typeKey == constants .NvidiaGPU || typeKey == constants .NvshareGPU {
434
439
if tpl .Spec .RuntimeClassName != nil {
435
440
patch = append (patch , patchOp {
@@ -523,8 +528,25 @@ func addResourceLimits(tpl *corev1.PodTemplateSpec, namespace string, gpuRequire
523
528
}
524
529
}
525
530
}
526
-
527
531
}
532
+ envNames := make ([]string , 0 )
533
+ for envIdx , env := range container .Env {
534
+ for _ , e := range envKeyValues {
535
+ if e .Value == "" {
536
+ continue
537
+ }
538
+ if env .Name == e .Key {
539
+ envNames = append (envNames , env .Name )
540
+ patch = append (patch , genPatchesForEnv (constants .PatchOpReplace , i , envIdx , e .Key , e .Value )... )
541
+ }
542
+ }
543
+ }
544
+ for _ , env := range envKeyValues {
545
+ if ! funk .Contains (envNames , env .Key ) {
546
+ patch = append (patch , genPatchesForEnv (constants .PatchOpAdd , i , - 1 , env .Key , env .Value )... )
547
+ }
548
+ }
549
+
528
550
}
529
551
530
552
return patch , nil
0 commit comments