Skip to content

Commit

Permalink
Initialize orchestrator type during reconciliation after the cache is…
Browse files Browse the repository at this point in the history
… setup

Signed-off-by: Shiva Krishna, Merla <[email protected]>
  • Loading branch information
shivamerla committed Oct 31, 2024
1 parent 4855e39 commit 18f029c
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 72 deletions.
36 changes: 19 additions & 17 deletions internal/controller/nemo_guardrail_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,33 +55,26 @@ const NemoGuardrailFinalizer = "finalizer.NemoGuardrail.apps.nvidia.com"
// NemoGuardrailReconciler reconciles a NemoGuardrail object
type NemoGuardrailReconciler struct {
client.Client
scheme *runtime.Scheme
log logr.Logger
updater conditions.Updater
renderer render.Renderer
Config *rest.Config
recorder record.EventRecorder
k8sType k8sutil.OrchestratorType
scheme *runtime.Scheme
log logr.Logger
updater conditions.Updater
renderer render.Renderer
Config *rest.Config
recorder record.EventRecorder
orchestratorType k8sutil.OrchestratorType
}

// Ensure NemoGuardrailReconciler implements the Reconciler interface
var _ shared.Reconciler = &NemoGuardrailReconciler{}

// NewNemoGuardrailReconciler creates a new reconciler for NemoGuardrail with the given platform
func NewNemoGuardrailReconciler(client client.Client, scheme *runtime.Scheme, updater conditions.Updater, renderer render.Renderer, log logr.Logger) *NemoGuardrailReconciler {
// Set container platform type
k8sType, err := k8sutil.GetOrchestratorType(client)
if err != nil {
return nil
}

return &NemoGuardrailReconciler{
Client: client,
scheme: scheme,
updater: updater,
renderer: renderer,
log: log,
k8sType: k8sType,
}
}

Expand Down Expand Up @@ -158,6 +151,15 @@ func (r *NemoGuardrailReconciler) Reconcile(ctx context.Context, req ctrl.Reques
}
}

// Set container orchestrator type
if r.GetOrchestratorType() != "" {
orchestratorType, err := k8sutil.GetOrchestratorType(r.GetClient())
if err != nil {
return ctrl.Result{}, fmt.Errorf("Unable to get container orhestrator type, %v", err)
}
r.orchestratorType = orchestratorType
}

// Handle platform-specific reconciliation
if result, err := r.reconcileNemoGuardrail(ctx, NemoGuardrail); err != nil {
logger.Error(err, "error reconciling NemoGuardrail", "name", NemoGuardrail.Name)
Expand Down Expand Up @@ -208,9 +210,9 @@ func (r *NemoGuardrailReconciler) GetEventRecorder() record.EventRecorder {
return r.recorder
}

// GetK8sType returns the container platform type
func (r *NemoGuardrailReconciler) GetK8sType() k8sutil.OrchestratorType {
return r.k8sType
// GetOrchestratorType returns the container platform type
func (r *NemoGuardrailReconciler) GetOrchestratorType() k8sutil.OrchestratorType {
return r.orchestratorType
}

// SetupWithManager sets up the controller with the Manager.
Expand Down
40 changes: 21 additions & 19 deletions internal/controller/nimcache_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,32 +84,24 @@ const (
// NIMCacheReconciler reconciles a NIMCache object
type NIMCacheReconciler struct {
client.Client
scheme *runtime.Scheme
log logr.Logger
Platform platform.Platform
k8sType k8sutil.OrchestratorType
updater conditions.Updater
recorder record.EventRecorder
scheme *runtime.Scheme
log logr.Logger
Platform platform.Platform
orchestratorType k8sutil.OrchestratorType
updater conditions.Updater
recorder record.EventRecorder
}

// Ensure NIMCacheReconciler implements the Reconciler interface
var _ shared.Reconciler = &NIMCacheReconciler{}

// NewNIMCacheReconciler creates a new reconciler for NIMCache with the given platform
func NewNIMCacheReconciler(client client.Client, scheme *runtime.Scheme, log logr.Logger, platform platform.Platform) *NIMCacheReconciler {
// Set container orchestrator type
k8sType, err := k8sutil.GetOrchestratorType(client)
if err != nil {
log.Error(err, "Unable to get container orhestrator type")
return nil
}

return &NIMCacheReconciler{
Client: client,
scheme: scheme,
log: log,
Platform: platform,
k8sType: k8sType,
}
}

Expand Down Expand Up @@ -184,6 +176,16 @@ func (r *NIMCacheReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
return ctrl.Result{}, nil
}
}

// Set container orchestrator type
if r.GetOrchestratorType() != "" {
orchestratorType, err := k8sutil.GetOrchestratorType(r.GetClient())
if err != nil {
return ctrl.Result{}, fmt.Errorf("Unable to get container orhestrator type, %v", err)
}
r.orchestratorType = orchestratorType
}

// Handle nim-cache reconciliation
result, err = r.reconcileNIMCache(ctx, nimCache)
if err != nil {
Expand Down Expand Up @@ -231,9 +233,9 @@ func (r *NIMCacheReconciler) GetEventRecorder() record.EventRecorder {
return r.recorder
}

// GetK8sType returns the container platform type
func (r *NIMCacheReconciler) GetK8sType() k8sutil.OrchestratorType {
return r.k8sType
// GetOrchestratorType returns the container platform type
func (r *NIMCacheReconciler) GetOrchestratorType() k8sutil.OrchestratorType {
return r.orchestratorType
}

// SetupWithManager sets up the controller with the Manager.
Expand Down Expand Up @@ -579,7 +581,7 @@ func (r *NIMCacheReconciler) reconcileModelManifest(ctx context.Context, nimCach

// Create a configmap by extracting the model manifest
// Create a temporary pod for parsing model manifest
pod := constructPodSpec(nimCache, r.GetK8sType())
pod := constructPodSpec(nimCache, r.GetOrchestratorType())
// Add nimCache as owner for watching on status change
if err := controllerutil.SetControllerReference(nimCache, pod, r.GetScheme()); err != nil {
return false, err
Expand Down Expand Up @@ -698,7 +700,7 @@ func (r *NIMCacheReconciler) reconcileJob(ctx context.Context, nimCache *appsv1a

// If Job does not exist and caching is not complete, create a new one
if err != nil && nimCache.Status.State != appsv1alpha1.NimCacheStatusReady {
job, err := r.constructJob(ctx, nimCache, r.GetK8sType())
job, err := r.constructJob(ctx, nimCache, r.GetOrchestratorType())
if err != nil {
logger.Error(err, "Failed to construct job")
return err
Expand Down
40 changes: 21 additions & 19 deletions internal/controller/nimservice_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package controller

import (
"context"
"fmt"
"reflect"

appsv1alpha1 "github.com/NVIDIA/k8s-nim-operator/api/apps/v1alpha1"
Expand Down Expand Up @@ -49,36 +50,28 @@ const NIMServiceFinalizer = "finalizer.nimservice.apps.nvidia.com"
// NIMServiceReconciler reconciles a NIMService object
type NIMServiceReconciler struct {
client.Client
scheme *runtime.Scheme
log logr.Logger
updater conditions.Updater
renderer render.Renderer
Config *rest.Config
Platform platform.Platform
k8sType k8sutil.OrchestratorType
recorder record.EventRecorder
scheme *runtime.Scheme
log logr.Logger
updater conditions.Updater
renderer render.Renderer
Config *rest.Config
Platform platform.Platform
orchestratorType k8sutil.OrchestratorType
recorder record.EventRecorder
}

// Ensure NIMServiceReconciler implements the Reconciler interface
var _ shared.Reconciler = &NIMServiceReconciler{}

// NewNIMServiceReconciler creates a new reconciler for NIMService with the given platform
func NewNIMServiceReconciler(client client.Client, scheme *runtime.Scheme, updater conditions.Updater, renderer render.Renderer, log logr.Logger, platform platform.Platform) *NIMServiceReconciler {
// Set container orchestrator type
k8sType, err := k8sutil.GetOrchestratorType(client)
if err != nil {
log.Error(err, "Unable to get container orhestrator type")
return nil
}

return &NIMServiceReconciler{
Client: client,
scheme: scheme,
updater: updater,
renderer: renderer,
log: log,
Platform: platform,
k8sType: k8sType,
}
}

Expand Down Expand Up @@ -155,6 +148,15 @@ func (r *NIMServiceReconciler) Reconcile(ctx context.Context, req ctrl.Request)
}
}

// Set container orchestrator type
if r.GetOrchestratorType() != "" {
orchestratorType, err := k8sutil.GetOrchestratorType(r.GetClient())
if err != nil {
return ctrl.Result{}, fmt.Errorf("Unable to get container orhestrator type, %v", err)
}
r.orchestratorType = orchestratorType
}

// Handle platform-specific reconciliation
if result, err := r.Platform.Sync(ctx, r, nimService); err != nil {
logger.Error(err, "error reconciling NIMService", "name", nimService.Name)
Expand Down Expand Up @@ -199,9 +201,9 @@ func (r *NIMServiceReconciler) GetEventRecorder() record.EventRecorder {
return r.recorder
}

// GetK8sType returns the container platform type
func (r *NIMServiceReconciler) GetK8sType() k8sutil.OrchestratorType {
return r.k8sType
// GetOrchestratorType returns the container platform type
func (r *NIMServiceReconciler) GetOrchestratorType() k8sutil.OrchestratorType {
return r.orchestratorType
}

// SetupWithManager sets up the controller with the Manager.
Expand Down
8 changes: 4 additions & 4 deletions internal/controller/platform/standalone/nimservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ func (r *NIMServiceReconciler) GetEventRecorder() record.EventRecorder {
return r.recorder
}

// GetK8sType returns the container platform type
func (r *NIMServiceReconciler) GetK8sType() k8sutil.OrchestratorType {
return r.k8sType
// 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 {
Expand Down Expand Up @@ -179,7 +179,7 @@ func (r *NIMServiceReconciler) reconcileNIMService(ctx context.Context, nimServi
var modelPVC *appsv1alpha1.PersistentVolumeClaim
modelProfile := ""

deploymentParams.OrchestratorType = string(r.GetK8sType())
deploymentParams.OrchestratorType = string(r.GetOrchestratorType())

// Select PVC for model store
if nimService.GetNIMCacheName() != "" {
Expand Down
24 changes: 12 additions & 12 deletions internal/controller/platform/standalone/standalone.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ type NIMCacheReconciler struct {
// NIMServiceReconciler represents the NIMService reconciler instance for standalone mode
type NIMServiceReconciler struct {
client.Client
scheme *runtime.Scheme
log logr.Logger
updater conditions.Updater
renderer render.Renderer
recorder record.EventRecorder
k8sType k8sutil.OrchestratorType
scheme *runtime.Scheme
log logr.Logger
updater conditions.Updater
renderer render.Renderer
recorder record.EventRecorder
orchestratorType k8sutil.OrchestratorType
}

// NewNIMCacheReconciler returns NIMCacheReconciler for standalone mode
Expand All @@ -77,12 +77,12 @@ func NewNIMCacheReconciler(r shared.Reconciler) *NIMCacheReconciler {
// NewNIMServiceReconciler returns NIMServiceReconciler for standalone mode
func NewNIMServiceReconciler(r shared.Reconciler) *NIMServiceReconciler {
return &NIMServiceReconciler{
Client: r.GetClient(),
scheme: r.GetScheme(),
log: r.GetLogger(),
updater: r.GetUpdater(),
recorder: r.GetEventRecorder(),
k8sType: r.GetK8sType(),
Client: r.GetClient(),
scheme: r.GetScheme(),
log: r.GetLogger(),
updater: r.GetUpdater(),
recorder: r.GetEventRecorder(),
orchestratorType: r.GetOrchestratorType(),
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/shared/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ type Reconciler interface {
GetUpdater() conditions.Updater
GetRenderer() render.Renderer
GetEventRecorder() record.EventRecorder
GetK8sType() k8sutil.OrchestratorType
GetOrchestratorType() k8sutil.OrchestratorType
}

0 comments on commit 18f029c

Please sign in to comment.