Skip to content

Commit 8dc9d39

Browse files
Improving the condition for adding HF_TOKEN on NIM Service (#711)
* Improving the condition for adding HF_TOKEN on NIM Service Signed-off-by: Vishesh Tanksale <[email protected]> * Addressing review comments Signed-off-by: Vishesh Tanksale <[email protected]> * Addressing review comments Signed-off-by: Vishesh Tanksale <[email protected]> * Addressing review comments Signed-off-by: Vishesh Tanksale <[email protected]> --------- Signed-off-by: Vishesh Tanksale <[email protected]>
1 parent 99ed385 commit 8dc9d39

File tree

8 files changed

+1174
-42
lines changed

8 files changed

+1174
-42
lines changed

api/apps/v1alpha1/common_types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ const (
3636
DefaultNamedPortGRPC = "grpc"
3737
// DefaultNamedPortMetrics is the default name for metrics port.
3838
DefaultNamedPortMetrics = "metrics"
39+
// NGCAPIKey is the environment variable name for NGC API key.
40+
NGCAPIKey = "NGC_API_KEY"
41+
// HFToken is the environment variable name for Hugging Face token.
42+
HFToken = "HF_TOKEN"
3943
)
4044

4145
// Expose defines attributes to expose the service.

api/apps/v1alpha1/nimcache_types.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,11 @@ func (n *NIMCache) IsUniversalNIM() bool {
331331
return false
332332
}
333333

334+
// IsHFModel returns true if the NIMCache is for a Hugging Face model.
335+
func (n *NIMCache) IsHFModel() bool {
336+
return n.Spec.Source.HF != nil || n.Spec.Source.DataStore != nil
337+
}
338+
334339
// IsOptimizedNIM returns true if the NIMCache is for an optimized NIM.
335340
func (n *NIMCache) IsOptimizedNIM() bool {
336341
// Universal NIM is when the modelEndpoint is set in the NGCSource.

api/apps/v1alpha1/nimservice_types.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"fmt"
2626
"maps"
2727
"os"
28+
"strings"
2829

2930
kserveconstants "github.com/kserve/kserve/pkg/constants"
3031
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
@@ -283,13 +284,13 @@ func (n *NIMService) GetStandardEnv() []corev1.EnvVar {
283284
Value: utils.DefaultModelStorePath,
284285
},
285286
{
286-
Name: "NGC_API_KEY",
287+
Name: NGCAPIKey,
287288
ValueFrom: &corev1.EnvVarSource{
288289
SecretKeyRef: &corev1.SecretKeySelector{
289290
LocalObjectReference: corev1.LocalObjectReference{
290291
Name: n.Spec.AuthSecret,
291292
},
292-
Key: "NGC_API_KEY",
293+
Key: NGCAPIKey,
293294
},
294295
},
295296
},
@@ -983,6 +984,17 @@ func (n *NIMService) IsHTTPRouteEnabled() bool {
983984
return n.Spec.Router.Gateway != nil && n.Spec.Router.Gateway.HTTPRoutesEnabled
984985
}
985986

987+
// IsHFModel returns true if the NIMService is using an HF model.
988+
func (n *NIMService) IsHFModel() bool {
989+
env := utils.FindEnvByValue(n.GetEnv(), "NIM_MODEL_NAME")
990+
if env != nil {
991+
if strings.HasPrefix(env.Value, "hf://") {
992+
return true
993+
}
994+
}
995+
return false
996+
}
997+
986998
func (n *NIMService) GetHTTPRouteSpec() gatewayv1.HTTPRouteSpec {
987999
return n.Spec.Router.GenerateGatewayHTTPRouteSpec(n.GetNamespace(), n.GetName(), n.GetServicePort())
9881000
}

internal/controller/nimbuild_controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -642,13 +642,13 @@ func (r *NIMBuildReconciler) constructEngineBuildPod(nimBuild *appsv1alpha1.NIMB
642642
Value: inputNimProfile.Name,
643643
},
644644
{
645-
Name: "NGC_API_KEY",
645+
Name: appsv1alpha1.NGCAPIKey,
646646
ValueFrom: &corev1.EnvVarSource{
647647
SecretKeyRef: &corev1.SecretKeySelector{
648648
LocalObjectReference: corev1.LocalObjectReference{
649649
Name: nimCache.Spec.Source.NGC.AuthSecret,
650650
},
651-
Key: "NGC_API_KEY",
651+
Key: appsv1alpha1.NGCAPIKey,
652652
},
653653
},
654654
},

internal/controller/platform/kserve/nimservice.go

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -477,28 +477,24 @@ func (r *NIMServiceReconciler) renderAndSyncInferenceService(ctx context.Context
477477
},
478478
}, isvcParams.Env)
479479
}
480-
// If NIMCache is not provided, and the model is from Hugging Face, add the HF_TOKEN to the environment variables
481-
if nimService.GetNIMCacheName() == "" {
482-
env := utils.FindEnvByValue(isvcParams.Env, "NIM_MODEL_NAME")
483-
if env != nil {
484-
if strings.HasPrefix(env.Value, "hf://") {
485-
isvcParams.Env = utils.RemoveEnvVar(isvcParams.Env, "NGC_API_KEY")
486-
isvcParams.Env = utils.MergeEnvVars(isvcParams.Env, []corev1.EnvVar{
487-
{
488-
Name: "HF_TOKEN",
489-
ValueFrom: &corev1.EnvVarSource{
490-
SecretKeyRef: &corev1.SecretKeySelector{
491-
LocalObjectReference: corev1.LocalObjectReference{
492-
Name: nimService.Spec.AuthSecret,
493-
},
494-
Key: "HF_TOKEN",
495-
},
480+
// If NIMCache or NIMService is a Hugging Face Multi-LLM NIM, add the HF_TOKEN to the environment variables
481+
if nimCache.IsHFModel() || nimService.IsHFModel() {
482+
isvcParams.Env = utils.RemoveEnvVar(isvcParams.Env, appsv1alpha1.NGCAPIKey)
483+
isvcParams.Env = utils.MergeEnvVars(isvcParams.Env, []corev1.EnvVar{
484+
{
485+
Name: appsv1alpha1.HFToken,
486+
ValueFrom: &corev1.EnvVarSource{
487+
SecretKeyRef: &corev1.SecretKeySelector{
488+
LocalObjectReference: corev1.LocalObjectReference{
489+
Name: nimService.Spec.AuthSecret,
496490
},
491+
Key: appsv1alpha1.HFToken,
497492
},
498-
})
499-
}
500-
}
493+
},
494+
},
495+
})
501496
}
497+
502498
// Setup volume mounts with model store
503499
isvcParams.Volumes = nimService.GetVolumes(modelPVC)
504500
isvcParams.VolumeMounts = nimService.GetVolumeMounts(modelPVC)

0 commit comments

Comments
 (0)