Skip to content

Commit bcad95f

Browse files
authored
fix: inject env NVSHARE_MANAGED_MEMORY (#127)
1 parent 022eb24 commit bcad95f

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

pkg/apiserver/handler_webhook.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"bytetrade.io/web3os/app-service/pkg/appinstaller"
1212
"bytetrade.io/web3os/app-service/pkg/constants"
1313
"bytetrade.io/web3os/app-service/pkg/provider"
14+
"bytetrade.io/web3os/app-service/pkg/upgrade"
1415
"bytetrade.io/web3os/app-service/pkg/users/userspace"
1516
"bytetrade.io/web3os/app-service/pkg/utils"
1617
"bytetrade.io/web3os/app-service/pkg/webhook"
@@ -296,8 +297,21 @@ func (h *Handler) gpuLimitMutate(ctx context.Context, req *admissionv1.Admission
296297
if !gpuRequired.IsZero() && GPUType == "" {
297298
return h.sidecarWebhook.AdmissionError(req.UID, api.ErrGPUNodeNotFound)
298299
}
300+
terminus, err := upgrade.GetTerminusVersion(ctx, h.ctrlClient)
301+
if err != nil {
302+
return h.sidecarWebhook.AdmissionError(req.UID, err)
303+
}
304+
nvshareManagedMemory := ""
305+
if terminus.Spec.Settings != nil {
306+
nvshareManagedMemory = terminus.Spec.Settings[constants.EnvNvshareManagedMemory]
307+
}
308+
309+
envs := []webhook.EnvKeyValue{{
310+
Key: constants.EnvNvshareManagedMemory,
311+
Value: nvshareManagedMemory,
312+
}}
299313

300-
patchBytes, err := webhook.CreatePatchForDeployment(tpl, req.Namespace, gpuRequired, GPUType)
314+
patchBytes, err := webhook.CreatePatchForDeployment(tpl, req.Namespace, gpuRequired, GPUType, envs)
301315
if err != nil {
302316
klog.Errorf("create patch error %v", err)
303317
return h.sidecarWebhook.AdmissionError(req.UID, err)

pkg/webhook/webhook.go

+26-4
Original file line numberDiff line numberDiff line change
@@ -420,16 +420,21 @@ var resourcePath = "/spec/template/spec/containers/%d/resources/limits"
420420
var envPath = "/spec/template/spec/containers/%d/env/%s"
421421
var runtimeClassPath = "/spec/template/spec/runtimeClassName"
422422

423+
type EnvKeyValue struct {
424+
Key string
425+
Value string
426+
}
427+
423428
// 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)
426431
if err != nil {
427432
return []byte{}, err
428433
}
429434
return json.Marshal(patches)
430435
}
431436

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) {
433438
if typeKey == constants.NvidiaGPU || typeKey == constants.NvshareGPU {
434439
if tpl.Spec.RuntimeClassName != nil {
435440
patch = append(patch, patchOp{
@@ -523,8 +528,25 @@ func addResourceLimits(tpl *corev1.PodTemplateSpec, namespace string, gpuRequire
523528
}
524529
}
525530
}
526-
527531
}
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+
528550
}
529551

530552
return patch, nil

0 commit comments

Comments
 (0)