forked from NVIDIA/k8s-nim-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnimservice.go
More file actions
703 lines (607 loc) · 25.2 KB
/
nimservice.go
File metadata and controls
703 lines (607 loc) · 25.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
/*
Copyright 2024.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package standalone
import (
"context"
"fmt"
"github.com/NVIDIA/k8s-nim-operator/api/apps/v1alpha1"
appsv1alpha1 "github.com/NVIDIA/k8s-nim-operator/api/apps/v1alpha1"
"github.com/NVIDIA/k8s-nim-operator/internal/conditions"
"github.com/NVIDIA/k8s-nim-operator/internal/k8sutil"
"github.com/NVIDIA/k8s-nim-operator/internal/nimmodels"
"github.com/NVIDIA/k8s-nim-operator/internal/render"
rendertypes "github.com/NVIDIA/k8s-nim-operator/internal/render/types"
"github.com/NVIDIA/k8s-nim-operator/internal/shared"
"github.com/NVIDIA/k8s-nim-operator/internal/utils"
"github.com/go-logr/logr"
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
appsv1 "k8s.io/api/apps/v1"
autoscalingv2 "k8s.io/api/autoscaling/v2"
corev1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/api/errors"
apiResource "k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/log"
)
// GetScheme returns the scheme of the reconciler
func (r *NIMServiceReconciler) GetScheme() *runtime.Scheme {
return r.scheme
}
// GetLogger returns the logger of the reconciler
func (r *NIMServiceReconciler) GetLogger() logr.Logger {
return r.log
}
// GetClient returns the client instance
func (r *NIMServiceReconciler) GetClient() client.Client {
return r.Client
}
// GetUpdater returns the conditions updater instance
func (r *NIMServiceReconciler) GetUpdater() conditions.Updater {
return r.updater
}
// GetRenderer returns the renderer instance
func (r *NIMServiceReconciler) GetRenderer() render.Renderer {
return r.renderer
}
// GetEventRecorder returns the event recorder
func (r *NIMServiceReconciler) GetEventRecorder() record.EventRecorder {
return r.recorder
}
// GetOrchestratorType returns the container platform type
func (r *NIMServiceReconciler) GetOrchestratorType() k8sutil.OrchestratorType {
return r.orchestratorType
}
func (r *NIMServiceReconciler) cleanupNIMService(ctx context.Context, nimService *appsv1alpha1.NIMService) error {
// All dependent (owned) objects will be automatically garbage collected.
// TODO: Handle any custom cleanup logic for the NIM microservice
return nil
}
func (r *NIMServiceReconciler) reconcileNIMService(ctx context.Context, nimService *appsv1alpha1.NIMService) (ctrl.Result, error) {
logger := log.FromContext(ctx)
var err error
defer func() {
if err != nil {
r.GetEventRecorder().Eventf(nimService, corev1.EventTypeWarning, conditions.Failed,
"NIMService %s failed, msg: %s", nimService.Name, err.Error())
}
}()
// Generate annotation for the current operator-version and apply to all resources
// Get generic name for all resources
namespacedName := types.NamespacedName{Name: nimService.GetName(), Namespace: nimService.GetNamespace()}
renderer := r.GetRenderer()
// Sync serviceaccount
err = r.renderAndSyncResource(ctx, nimService, &renderer, &corev1.ServiceAccount{}, func() (client.Object, error) {
return renderer.ServiceAccount(nimService.GetServiceAccountParams())
}, "serviceaccount", conditions.ReasonServiceAccountFailed)
if err != nil {
return ctrl.Result{}, err
}
// Sync role
err = r.renderAndSyncResource(ctx, nimService, &renderer, &rbacv1.Role{}, func() (client.Object, error) {
return renderer.Role(nimService.GetRoleParams())
}, "role", conditions.ReasonRoleFailed)
if err != nil {
return ctrl.Result{}, err
}
// Sync rolebinding
err = r.renderAndSyncResource(ctx, nimService, &renderer, &rbacv1.RoleBinding{}, func() (client.Object, error) {
return renderer.RoleBinding(nimService.GetRoleBindingParams())
}, "rolebinding", conditions.ReasonRoleBindingFailed)
if err != nil {
return ctrl.Result{}, err
}
// Sync service
err = r.renderAndSyncResource(ctx, nimService, &renderer, &corev1.Service{}, func() (client.Object, error) {
return renderer.Service(nimService.GetServiceParams())
}, "service", conditions.ReasonServiceFailed)
if err != nil {
return ctrl.Result{}, err
}
// Sync ingress
if nimService.IsIngressEnabled() {
err = r.renderAndSyncResource(ctx, nimService, &renderer, &networkingv1.Ingress{}, func() (client.Object, error) {
return renderer.Ingress(nimService.GetIngressParams())
}, "ingress", conditions.ReasonIngressFailed)
if err != nil {
return ctrl.Result{}, err
}
} else {
err = r.cleanupResource(ctx, &networkingv1.Ingress{}, namespacedName)
if err != nil && !errors.IsNotFound(err) {
return ctrl.Result{}, err
}
}
// Sync HPA
if nimService.IsAutoScalingEnabled() {
err = r.renderAndSyncResource(ctx, nimService, &renderer, &autoscalingv2.HorizontalPodAutoscaler{}, func() (client.Object, error) {
return renderer.HPA(nimService.GetHPAParams())
}, "hpa", conditions.ReasonHPAFailed)
if err != nil {
return ctrl.Result{}, err
}
} else {
// If autoscaling is disabled, ensure the HPA is deleted
err = r.cleanupResource(ctx, &autoscalingv2.HorizontalPodAutoscaler{}, namespacedName)
if err != nil {
return ctrl.Result{}, err
}
}
// Sync Service Monitor
if nimService.IsServiceMonitorEnabled() {
err = r.renderAndSyncResource(ctx, nimService, &renderer, &monitoringv1.ServiceMonitor{}, func() (client.Object, error) {
return renderer.ServiceMonitor(nimService.GetServiceMonitorParams())
}, "servicemonitor", conditions.ReasonServiceMonitorFailed)
if err != nil {
return ctrl.Result{}, err
}
}
deploymentParams := nimService.GetDeploymentParams()
var modelPVC *appsv1alpha1.PersistentVolumeClaim
modelProfile := ""
deploymentParams.OrchestratorType = string(r.GetOrchestratorType())
// Select PVC for model store
if nimService.GetNIMCacheName() != "" {
// Fetch PVC for the associated NIMCache instance and mount it
nimCachePVC, err := r.getNIMCachePVC(ctx, nimService)
if err != nil {
logger.Error(err, "unable to obtain pvc backing the nimcache instance")
return ctrl.Result{}, err
}
logger.V(2).Info("obtained the backing pvc for nimcache instance", "pvc", nimCachePVC)
modelPVC = nimCachePVC
if profile := nimService.GetNIMCacheProfile(); profile != "" {
logger.Info("overriding model profile", "profile", profile)
modelProfile = profile
}
} else if nimService.Spec.Storage.PVC.Create != nil && *nimService.Spec.Storage.PVC.Create {
// Create a new PVC
modelPVC, err = r.reconcilePVC(ctx, nimService)
if err != nil {
logger.Error(err, "unable to create pvc")
return ctrl.Result{}, err
}
} else if nimService.Spec.Storage.PVC.Name != "" {
// Use an existing PVC
modelPVC = &nimService.Spec.Storage.PVC
} else {
err = fmt.Errorf("neither external PVC name or NIMCache volume is provided")
logger.Error(err, "failed to determine PVC for model-store")
return ctrl.Result{}, err
}
// Setup volume mounts with model store
deploymentParams.Volumes = nimService.GetVolumes(*modelPVC)
deploymentParams.VolumeMounts = nimService.GetVolumeMounts(*modelPVC)
// Setup env for explicit override profile is specified
if modelProfile != "" {
profileEnv := corev1.EnvVar{
Name: "NIM_MODEL_PROFILE",
Value: modelProfile,
}
deploymentParams.Env = append(deploymentParams.Env, profileEnv)
// Retrieve and set profile details from NIMCache
var profile *appsv1alpha1.NIMProfile
profile, err = r.getNIMCacheProfile(ctx, nimService, modelProfile)
if err != nil {
logger.Error(err, "Failed to get cached NIM profile")
return ctrl.Result{}, err
}
// Auto assign GPU resources in case of the optimized profile
if profile != nil {
if err = r.assignGPUResources(ctx, nimService, profile, deploymentParams); err != nil {
return ctrl.Result{}, err
}
}
// TODO: assign GPU resources and node selector that is required for the selected profile
}
// Sync deployment
err = r.renderAndSyncResource(ctx, nimService, &renderer, &appsv1.Deployment{}, func() (client.Object, error) {
return renderer.Deployment(deploymentParams)
}, "deployment", conditions.ReasonDeploymentFailed)
if err != nil {
return ctrl.Result{}, err
}
// Wait for deployment
msg, ready, err := r.isDeploymentReady(ctx, &namespacedName)
if err != nil {
return ctrl.Result{}, err
}
if !ready {
// Update status as NotReady
err = r.updater.SetConditionsNotReady(ctx, nimService, conditions.NotReady, msg)
r.GetEventRecorder().Eventf(nimService, corev1.EventTypeNormal, conditions.NotReady,
"NIMService %s not ready yet, msg: %s", nimService.Name, msg)
} else {
// Update NIMServiceStatus with model config.
err = r.updateModelStatus(ctx, nimService)
if err != nil {
return ctrl.Result{}, err
}
// Update status as ready
err = r.updater.SetConditionsReady(ctx, nimService, conditions.Ready, msg)
r.GetEventRecorder().Eventf(nimService, corev1.EventTypeNormal, conditions.Ready,
"NIMService %s ready, msg: %s", nimService.Name, msg)
}
if err != nil {
logger.Error(err, "Unable to update status")
return ctrl.Result{}, err
}
return ctrl.Result{}, nil
}
func (r *NIMServiceReconciler) updateModelStatus(ctx context.Context, nimService *v1alpha1.NIMService) error {
clusterEndpoint, externalEndpoint, err := r.getNIMModelEndpoints(ctx, nimService)
if err != nil {
return err
}
modelName, err := r.getNIMModelName(ctx, clusterEndpoint)
if err != nil {
return err
}
nimService.Status.Model = &v1alpha1.ModelStatus{
Name: modelName,
ClusterEndpoint: clusterEndpoint,
ExternalEndpoint: externalEndpoint,
}
return nil
}
func (r *NIMServiceReconciler) getNIMModelName(ctx context.Context, nimServiceEndpoint string) (string, error) {
modelsList, err := nimmodels.ListModelsV1(ctx, nimServiceEndpoint)
if err != nil {
return "", err
}
if modelsList.Object == nimmodels.ObjectTypeList {
for _, model := range modelsList.Data {
if model.Object != nimmodels.ObjectTypeModel {
continue
}
if model.Root == nil || *model.Root != model.Id {
continue
}
return model.Id, nil
}
}
return "", fmt.Errorf("failed to detect model name from nimservice endpoint '%s'", nimServiceEndpoint)
}
func (r *NIMServiceReconciler) getNIMModelEndpoints(ctx context.Context, nimService *appsv1alpha1.NIMService) (string, string, error) {
logger := log.FromContext(ctx)
// Lookup NIMCache instance in the same namespace as the NIMService instance
svc := &corev1.Service{}
if err := r.Get(ctx, types.NamespacedName{Name: nimService.GetName(), Namespace: nimService.GetNamespace()}, svc); err != nil {
logger.Error(err, "unable to fetch k8s service", "nimservice", nimService.GetName())
return "", "", err
}
var externalEndpoint string
port := nimService.GetServicePort()
if nimService.IsIngressEnabled() {
ingress := &networkingv1.Ingress{}
if err := r.Get(ctx, types.NamespacedName{Name: nimService.GetName(), Namespace: nimService.GetNamespace()}, ingress); err != nil {
logger.Error(err, "unable to fetch ingress", "nimservice", nimService.GetName())
return "", "", err
}
var found bool
for _, rule := range ingress.Spec.Rules {
if rule.HTTP == nil {
continue
}
for _, path := range rule.HTTP.Paths {
if path.Backend.Service != nil && path.Backend.Service.Name == nimService.GetName() {
externalEndpoint = rule.Host
found = true
break
}
}
if found {
break
}
}
} else if svc.Spec.Type == corev1.ServiceTypeLoadBalancer {
externalEndpoint = utils.FormatEndpoint(svc.Spec.LoadBalancerIP, port)
}
return utils.FormatEndpoint(svc.Spec.ClusterIP, port), externalEndpoint, nil
}
func (r *NIMServiceReconciler) renderAndSyncResource(ctx context.Context, nimService *appsv1alpha1.NIMService, renderer *render.Renderer, obj client.Object, renderFunc func() (client.Object, error), conditionType string, reason string) error {
logger := log.FromContext(ctx)
namespacedName := types.NamespacedName{Name: nimService.GetName(), Namespace: nimService.GetNamespace()}
resource, err := renderFunc()
if err != nil {
logger.Error(err, "failed to render", conditionType, namespacedName)
statusError := r.updater.SetConditionsFailed(ctx, nimService, reason, err.Error())
if statusError != nil {
logger.Error(statusError, "failed to update status", "nimservice", nimService.Name)
}
return err
}
// Check if the resource is nil
if resource == nil {
logger.V(2).Info("rendered nil resource")
return nil
}
metaAccessor, ok := resource.(metav1.Object)
if !ok {
logger.V(2).Info("rendered un-initialized resource")
return nil
}
if metaAccessor == nil || metaAccessor.GetName() == "" || metaAccessor.GetNamespace() == "" {
logger.V(2).Info("rendered un-initialized resource")
return nil
}
if err = controllerutil.SetControllerReference(nimService, resource, r.GetScheme()); err != nil {
logger.Error(err, "failed to set owner", conditionType, namespacedName)
statusError := r.updater.SetConditionsFailed(ctx, nimService, reason, err.Error())
if statusError != nil {
logger.Error(statusError, "failed to update status", "nimservice", nimService.Name)
}
return err
}
err = r.syncResource(ctx, obj, resource, namespacedName)
if err != nil {
logger.Error(err, "failed to sync", conditionType, namespacedName)
statusError := r.updater.SetConditionsFailed(ctx, nimService, reason, err.Error())
if statusError != nil {
logger.Error(statusError, "failed to update status", "nimservice", nimService.Name)
}
return err
}
return nil
}
// CheckDeploymentReadiness checks if the Deployment is ready
func (r *NIMServiceReconciler) isDeploymentReady(ctx context.Context, namespacedName *types.NamespacedName) (string, bool, error) {
deployment := &appsv1.Deployment{}
err := r.Get(ctx, client.ObjectKey{Name: namespacedName.Name, Namespace: namespacedName.Namespace}, deployment)
if err != nil {
if errors.IsNotFound(err) {
return "", false, nil
}
return "", false, err
}
cond := getDeploymentCondition(deployment.Status, appsv1.DeploymentProgressing)
if cond != nil && cond.Reason == "ProgressDeadlineExceeded" {
return fmt.Sprintf("deployment %q exceeded its progress deadline", deployment.Name), false, nil
}
if deployment.Spec.Replicas != nil && deployment.Status.UpdatedReplicas < *deployment.Spec.Replicas {
return fmt.Sprintf("Waiting for deployment %q rollout to finish: %d out of %d new replicas have been updated...\n", deployment.Name, deployment.Status.UpdatedReplicas, *deployment.Spec.Replicas), false, nil
}
if deployment.Status.Replicas > deployment.Status.UpdatedReplicas {
return fmt.Sprintf("Waiting for deployment %q rollout to finish: %d old replicas are pending termination...\n", deployment.Name, deployment.Status.Replicas-deployment.Status.UpdatedReplicas), false, nil
}
if deployment.Status.AvailableReplicas < deployment.Status.UpdatedReplicas {
return fmt.Sprintf("Waiting for deployment %q rollout to finish: %d of %d updated replicas are available...\n", deployment.Name, deployment.Status.AvailableReplicas, deployment.Status.UpdatedReplicas), false, nil
}
return fmt.Sprintf("deployment %q successfully rolled out\n", deployment.Name), true, nil
}
func getDeploymentCondition(status appsv1.DeploymentStatus, condType appsv1.DeploymentConditionType) *appsv1.DeploymentCondition {
for i := range status.Conditions {
c := status.Conditions[i]
if c.Type == condType {
return &c
}
}
return nil
}
func (r *NIMServiceReconciler) syncResource(ctx context.Context, obj client.Object, desired client.Object, namespacedName types.NamespacedName) error {
logger := log.FromContext(ctx)
err := r.Get(ctx, namespacedName, obj)
if err != nil && !errors.IsNotFound(err) {
return err
}
if !utils.IsSpecChanged(obj, desired) {
logger.V(2).Info("Object spec has not changed, skipping update", "obj", obj)
return nil
}
logger.V(2).Info("Object spec has changed, updating")
if errors.IsNotFound(err) {
err = r.Create(ctx, desired)
if err != nil {
return err
}
} else {
err = r.Update(ctx, desired)
if err != nil {
return err
}
}
return nil
}
// cleanupResource deletes the given Kubernetes resource if it exists.
// If the resource does not exist or an error occurs during deletion, the function returns nil or the error.
//
// Parameters:
// ctx (context.Context): The context for the operation.
// obj (client.Object): The Kubernetes resource to delete.
// namespacedName (types.NamespacedName): The namespaced name of the resource.
//
// Returns:
// error: An error if the resource deletion fails, or nil if the resource is not found or deletion is successful.
func (r *NIMServiceReconciler) cleanupResource(ctx context.Context, obj client.Object, namespacedName types.NamespacedName) error {
logger := log.FromContext(ctx)
err := r.Get(ctx, namespacedName, obj)
if err != nil && !errors.IsNotFound(err) {
return err
}
if errors.IsNotFound(err) {
return nil
}
err = r.Delete(ctx, obj)
if err != nil {
return err
}
logger.V(2).Info("NIM Service object changed, deleting ", "obj", obj)
return nil
}
// getNIMCachePVC returns PVC backing the NIM cache instance
func (r *NIMServiceReconciler) getNIMCachePVC(ctx context.Context, nimService *appsv1alpha1.NIMService) (*appsv1alpha1.PersistentVolumeClaim, error) {
logger := log.FromContext(ctx)
if nimService.GetNIMCacheName() == "" {
// NIM cache PVC is not used
return nil, nil
}
// Lookup NIMCache instance in the same namespace as the NIMService instance
nimCache := &appsv1alpha1.NIMCache{}
if err := r.Get(ctx, types.NamespacedName{Name: nimService.GetNIMCacheName(), Namespace: nimService.Namespace}, nimCache); err != nil {
logger.Error(err, "unable to fetch nimcache", "nimcache", nimService.GetNIMCacheName(), "nimservice", nimService.Name)
return nil, err
}
// Get the status of NIMCache
if nimCache.Status.State != appsv1alpha1.NimCacheStatusReady {
return nil, fmt.Errorf("nimcache %s is not ready, nimservice %s", nimCache.GetName(), nimService.GetName())
}
if nimCache.Status.PVC == "" {
return nil, fmt.Errorf("missing PVC for the nimcache instance %s, nimservice %s", nimCache.GetName(), nimService.GetName())
}
if nimCache.Spec.Storage.PVC.Name == "" {
nimCache.Spec.Storage.PVC.Name = nimCache.Status.PVC
}
// Get the underlying PVC for the NIMCache instance
return &nimCache.Spec.Storage.PVC, nil
}
func (r *NIMServiceReconciler) reconcilePVC(ctx context.Context, nimService *appsv1alpha1.NIMService) (*appsv1alpha1.PersistentVolumeClaim, error) {
logger := r.GetLogger()
pvcName := nimService.GetPVCName(nimService.Spec.Storage.PVC)
pvcNamespacedName := types.NamespacedName{Name: pvcName, Namespace: nimService.GetNamespace()}
pvc := &corev1.PersistentVolumeClaim{}
err := r.Get(ctx, pvcNamespacedName, pvc)
if err != nil && client.IgnoreNotFound(err) != nil {
return nil, err
}
// If PVC does not exist, create a new one if creation flag is enabled
if err != nil {
if nimService.Spec.Storage.PVC.Create != nil && *nimService.Spec.Storage.PVC.Create {
pvc, err = shared.ConstructPVC(nimService.Spec.Storage.PVC, metav1.ObjectMeta{Name: pvcName, Namespace: nimService.GetNamespace()})
if err != nil {
logger.Error(err, "Failed to construct pvc", "name", pvc.Name)
return nil, err
}
if err := controllerutil.SetControllerReference(nimService, pvc, r.GetScheme()); err != nil {
return nil, err
}
err = r.Create(ctx, pvc)
if err != nil {
logger.Error(err, "Failed to create pvc", "name", pvc.Name)
return nil, err
}
logger.Info("Created PVC for NIM Service", "pvc", pvcName)
conditions.UpdateCondition(&nimService.Status.Conditions, appsv1alpha1.NimCacheConditionPVCCreated, metav1.ConditionTrue, "PVCCreated", "The PVC has been created for storing NIM")
nimService.Status.State = appsv1alpha1.NimCacheStatusPVCCreated
if err := r.Status().Update(ctx, nimService); err != nil {
logger.Error(err, "Failed to update status", "NIMService", nimService.Name)
return nil, err
}
} else {
logger.Error(err, "PVC doesn't exist and auto-creation is not enabled", "name", pvcNamespacedName)
return nil, err
}
}
// If explicit name is not provided in the spec, update it with the one created
if nimService.Spec.Storage.PVC.Name == "" {
nimService.Spec.Storage.PVC.Name = pvc.Name
}
return &nimService.Spec.Storage.PVC, nil
}
// getNIMCacheProfile returns model profile info from the NIM cache instance
func (r *NIMServiceReconciler) getNIMCacheProfile(ctx context.Context, nimService *appsv1alpha1.NIMService, profile string) (*appsv1alpha1.NIMProfile, error) {
logger := log.FromContext(ctx)
if nimService.GetNIMCacheName() == "" {
// NIM cache is not used
return nil, nil
}
// Lookup NIMCache instance in the same namespace as the NIMService instance
nimCache := &appsv1alpha1.NIMCache{}
if err := r.Get(ctx, types.NamespacedName{Name: nimService.GetNIMCacheName(), Namespace: nimService.Namespace}, nimCache); err != nil {
logger.Error(err, "unable to fetch nimcache", "nimcache", nimService.GetNIMCacheName(), "nimservice", nimService.Name)
return nil, err
}
// Get the status of NIMCache
if nimCache.Status.State != appsv1alpha1.NimCacheStatusReady {
return nil, fmt.Errorf("nimcache %s is not ready, nimservice %s", nimCache.GetName(), nimService.GetName())
}
for _, cachedProfile := range nimCache.Status.Profiles {
if cachedProfile.Name == profile {
return &cachedProfile, nil
}
}
// If the specified profile is not cached, return nil
return nil, nil
}
// getTensorParallelismByProfile returns the value of tensor parallelism parameter in the given NIM profile
func (r *NIMServiceReconciler) getTensorParallelismByProfile(ctx context.Context, profile *appsv1alpha1.NIMProfile) (string, error) {
// List of possible keys for tensor parallelism
possibleKeys := []string{"tensorParallelism", "tp"}
tensorParallelism := ""
// Iterate through possible keys and return the first valid value
for _, key := range possibleKeys {
if value, exists := profile.Config[key]; exists {
tensorParallelism = value
break
}
}
return tensorParallelism, nil
}
// assignGPUResources automatically assigns GPU resources to the NIMService based on the provided profile,
// but retains any user-specified GPU resources if they are explicitly provided.
//
// This function retrieves the tensor parallelism (TP) value from the provided profile config to determine
// the number of GPUs to be allocated. If the TP value is defined and no GPU resources have been
// explicitly provided by the user, the function allocates GPUs according to the TP value.
// If the TP value is not present, the function defaults to allocating 1 GPU.
func (r *NIMServiceReconciler) assignGPUResources(ctx context.Context, nimService *appsv1alpha1.NIMService, profile *appsv1alpha1.NIMProfile, deploymentParams *rendertypes.DeploymentParams) error {
logger := log.FromContext(ctx)
// TODO: Make the resource name configurable
const gpuResourceName = corev1.ResourceName("nvidia.com/gpu")
// Check if the user has already provided a GPU resource quantity in the requests or limits
if deploymentParams.Resources != nil {
if _, gpuRequested := deploymentParams.Resources.Requests[gpuResourceName]; gpuRequested {
logger.V(2).Info("User has provided GPU resource requests, skipping auto-assignment", "gpuResource", gpuResourceName)
return nil
}
if _, gpuLimit := deploymentParams.Resources.Limits[gpuResourceName]; gpuLimit {
logger.V(2).Info("User has provided GPU resource limits, skipping auto-assignment", "gpuResource", gpuResourceName)
return nil
}
}
// If no user-provided GPU resource is found, proceed with auto-assignment
// Get tensorParallelism from the profile
tensorParallelism, err := r.getTensorParallelismByProfile(ctx, profile)
if err != nil {
logger.Error(err, "Failed to retrieve tensorParallelism")
return err
}
// Initialize the Resources field if not already initialized
if deploymentParams.Resources == nil {
deploymentParams.Resources = &corev1.ResourceRequirements{
Requests: corev1.ResourceList{},
Limits: corev1.ResourceList{},
}
}
// Assign GPU resources based on tensorParallelism, or default to 1 GPU if tensorParallelism is not available
gpuQuantity := apiResource.MustParse("1") // Default to 1 GPU
if tensorParallelism != "" {
gpuQuantity, err = apiResource.ParseQuantity(tensorParallelism)
if err != nil {
return fmt.Errorf("failed to parse tensorParallelism: %w", err)
}
logger.V(2).Info("Auto-assigning GPU resources based on tensorParallelism", "tensorParallelism", tensorParallelism, "gpuQuantity", gpuQuantity.String())
} else {
logger.V(2).Info("tensorParallelism not found, assigning 1 GPU by default", "Profile", profile.Name)
}
// Assign the GPU quantity for both requests and limits
deploymentParams.Resources.Requests[gpuResourceName] = gpuQuantity
deploymentParams.Resources.Limits[gpuResourceName] = gpuQuantity
return nil
}