Skip to content

Commit a1777e5

Browse files
authored
fix: no env exist in yaml cause inject error (#131)
1 parent bd6d98b commit a1777e5

File tree

2 files changed

+24
-61
lines changed

2 files changed

+24
-61
lines changed

pkg/constants/constants.go

-5
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,6 @@ const (
7171
SidecarInitContainerName = "terminus-sidecar-init"
7272

7373
ByteTradeAuthor = "bytetrade.io"
74-
EnvOrionVGPU = "ORION_VGPU"
75-
EnvOrionClientID = "ORION_CLIENT_ID"
76-
EnvOrionTaskName = "ORION_TASK_NAME"
77-
EnvOrionGMEM = "ORION_GMEM"
78-
EnvOrionReserved = "ORION_RESERVED"
7974
NvshareGPU = "nvshare.com/gpu"
8075
NvidiaGPU = "nvidia.com/gpu"
8176
VirtAiTechVGPU = "virtaitech.com/gpu"

pkg/webhook/webhook.go

+24-56
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"errors"
77
"fmt"
88
"io/ioutil"
9-
"math"
109
"net/http"
1110
"sort"
1211
"strconv"
@@ -478,66 +477,35 @@ func addResourceLimits(tpl *corev1.PodTemplateSpec, namespace string, gpuRequire
478477
Value: t["limits"],
479478
})
480479
}
481-
if typeKey == constants.VirtAiTechVGPU {
482-
gmem := int(math.Ceil(gpuRequired.AsApproximateFloat64() / 1024 / 1024))
483-
envNames := make([]string, 0)
484-
for envIdx, env := range container.Env {
485-
if env.Name == constants.EnvOrionVGPU {
486-
envNames = append(envNames, env.Name)
487-
patch = append(patch, genPatchesForEnv(constants.PatchOpReplace, i, envIdx, constants.EnvOrionVGPU, "1")...)
488-
}
489-
if env.Name == constants.EnvOrionClientID {
490-
envNames = append(envNames, env.Name)
491-
patch = append(patch, genPatchesForEnv(constants.PatchOpReplace, i, envIdx, constants.EnvOrionClientID, namespace)...)
492-
}
493-
if env.Name == constants.EnvOrionTaskName {
494-
envNames = append(envNames, env.Name)
495-
patch = append(patch, genPatchesForEnv(constants.PatchOpReplace, i, envIdx, constants.EnvOrionTaskName,
496-
fmt.Sprintf("%s-%s", namespace, container.Name))...)
497-
}
498-
if env.Name == constants.EnvOrionGMEM {
499-
envNames = append(envNames, env.Name)
500-
patch = append(patch, genPatchesForEnv(constants.PatchOpReplace, i, envIdx, constants.EnvOrionGMEM, strconv.Itoa(gmem))...)
501-
}
502-
if env.Name == constants.EnvOrionReserved {
503-
envNames = append(envNames, env.Name)
504-
patch = append(patch, genPatchesForEnv(constants.PatchOpReplace, i, envIdx, constants.EnvOrionReserved, "0")...)
505-
}
506-
}
507-
508-
envs := []string{constants.EnvOrionVGPU, constants.EnvOrionClientID, constants.EnvOrionTaskName,
509-
constants.EnvOrionGMEM, constants.EnvOrionReserved}
510-
511-
for _, name := range envs {
512-
if !funk.Contains(envNames, name) {
513-
if name == constants.EnvOrionVGPU {
514-
patch = append(patch, genPatchesForEnv(constants.PatchOpAdd, i, -1, name, "1")...)
515-
}
516-
if name == constants.EnvOrionClientID {
517-
patch = append(patch, genPatchesForEnv(constants.PatchOpAdd, i, -1, name, namespace)...)
518-
}
519-
if name == constants.EnvOrionTaskName {
520-
patch = append(patch, genPatchesForEnv(constants.PatchOpAdd, i, -1, name,
521-
fmt.Sprintf("%s-%s", namespace, container.Name))...)
522-
}
523-
if name == constants.EnvOrionGMEM {
524-
patch = append(patch, genPatchesForEnv(constants.PatchOpAdd, i, -1, name, strconv.Itoa(gmem))...)
525-
}
526-
if name == constants.EnvOrionReserved {
527-
patch = append(patch, genPatchesForEnv(constants.PatchOpAdd, i, -1, name, "0")...)
528-
}
529-
}
530-
}
531-
}
532480
envNames := make([]string, 0)
533-
for envIdx, env := range container.Env {
481+
if len(container.Env) == 0 {
482+
value := make([]map[string]string, 0)
534483
for _, e := range envKeyValues {
535484
if e.Value == "" {
536485
continue
537486
}
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)...)
487+
envNames = append(envNames, e.Key)
488+
value = append(value, map[string]string{
489+
"name": e.Key,
490+
"value": e.Value,
491+
})
492+
}
493+
op := patchOp{
494+
Op: "add",
495+
Path: fmt.Sprintf("/spec/template/spec/containers/%d/env", i),
496+
Value: value,
497+
}
498+
patch = append(patch, op)
499+
} else {
500+
for envIdx, env := range container.Env {
501+
for _, e := range envKeyValues {
502+
if e.Value == "" {
503+
continue
504+
}
505+
if env.Name == e.Key {
506+
envNames = append(envNames, env.Name)
507+
patch = append(patch, genPatchesForEnv(constants.PatchOpReplace, i, envIdx, e.Key, e.Value)...)
508+
}
541509
}
542510
}
543511
}

0 commit comments

Comments
 (0)