diff --git a/internal/controller/nemo_guardrail_controller.go b/internal/controller/nemo_guardrail_controller.go index f9ec522e..b1aed627 100644 --- a/internal/controller/nemo_guardrail_controller.go +++ b/internal/controller/nemo_guardrail_controller.go @@ -151,14 +151,12 @@ 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 orchestrator type, %v", err) - } - r.orchestratorType = orchestratorType + // Fetch container orchestrator type + orchestratorType, err := r.GetOrchestratorType() + if err != nil { + return ctrl.Result{}, fmt.Errorf("Unable to get container orchestrator type, %v", err) } + logger.Info("Container orchestrator is successfully set", "type", orchestratorType) // Handle platform-specific reconciliation if result, err := r.reconcileNemoGuardrail(ctx, NemoGuardrail); err != nil { @@ -211,8 +209,15 @@ func (r *NemoGuardrailReconciler) GetEventRecorder() record.EventRecorder { } // GetOrchestratorType returns the container platform type -func (r *NemoGuardrailReconciler) GetOrchestratorType() k8sutil.OrchestratorType { - return r.orchestratorType +func (r *NemoGuardrailReconciler) GetOrchestratorType() (k8sutil.OrchestratorType, error) { + if r.orchestratorType == "" { + orchestratorType, err := k8sutil.GetOrchestratorType(r.GetClient()) + if err != nil { + return k8sutil.Unknown, fmt.Errorf("Unable to get container orchestrator type, %v", err) + } + r.orchestratorType = orchestratorType + } + return r.orchestratorType, nil } // SetupWithManager sets up the controller with the Manager. diff --git a/internal/controller/nimcache_controller.go b/internal/controller/nimcache_controller.go index 11f6d3f8..0d368df5 100644 --- a/internal/controller/nimcache_controller.go +++ b/internal/controller/nimcache_controller.go @@ -177,14 +177,12 @@ func (r *NIMCacheReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c } } - // 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 orchestrator type, %v", err) - } - r.orchestratorType = orchestratorType + // Fetch container orchestrator type + orchestratorType, err := r.GetOrchestratorType() + if err != nil { + return ctrl.Result{}, fmt.Errorf("Unable to get container orchestrator type, %v", err) } + logger.Info("Container orchestrator is successfully set", "type", orchestratorType) // Handle nim-cache reconciliation result, err = r.reconcileNIMCache(ctx, nimCache) @@ -234,8 +232,15 @@ func (r *NIMCacheReconciler) GetEventRecorder() record.EventRecorder { } // GetOrchestratorType returns the container platform type -func (r *NIMCacheReconciler) GetOrchestratorType() k8sutil.OrchestratorType { - return r.orchestratorType +func (r *NIMCacheReconciler) GetOrchestratorType() (k8sutil.OrchestratorType, error) { + if r.orchestratorType == "" { + orchestratorType, err := k8sutil.GetOrchestratorType(r.GetClient()) + if err != nil { + return k8sutil.Unknown, fmt.Errorf("Unable to get container orchestrator type, %v", err) + } + r.orchestratorType = orchestratorType + } + return r.orchestratorType, nil } // SetupWithManager sets up the controller with the Manager. @@ -581,7 +586,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.GetOrchestratorType()) + pod := constructPodSpec(nimCache, r.orchestratorType) // Add nimCache as owner for watching on status change if err := controllerutil.SetControllerReference(nimCache, pod, r.GetScheme()); err != nil { return false, err @@ -700,7 +705,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.GetOrchestratorType()) + job, err := r.constructJob(ctx, nimCache, r.orchestratorType) if err != nil { logger.Error(err, "Failed to construct job") return err diff --git a/internal/controller/nimservice_controller.go b/internal/controller/nimservice_controller.go index d5337aba..e8d2e75c 100644 --- a/internal/controller/nimservice_controller.go +++ b/internal/controller/nimservice_controller.go @@ -148,14 +148,12 @@ 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 orchestrator type, %v", err) - } - r.orchestratorType = orchestratorType + // Fetch container orchestrator type + orchestratorType, err := r.GetOrchestratorType() + if err != nil { + return ctrl.Result{}, fmt.Errorf("Unable to get container orchestrator type, %v", err) } + logger.Info("Container orchestrator is successfully set", "type", orchestratorType) // Handle platform-specific reconciliation if result, err := r.Platform.Sync(ctx, r, nimService); err != nil { @@ -202,8 +200,15 @@ func (r *NIMServiceReconciler) GetEventRecorder() record.EventRecorder { } // GetOrchestratorType returns the container platform type -func (r *NIMServiceReconciler) GetOrchestratorType() k8sutil.OrchestratorType { - return r.orchestratorType +func (r *NIMServiceReconciler) GetOrchestratorType() (k8sutil.OrchestratorType, error) { + if r.orchestratorType == "" { + orchestratorType, err := k8sutil.GetOrchestratorType(r.GetClient()) + if err != nil { + return k8sutil.Unknown, fmt.Errorf("Unable to get container orchestrator type, %v", err) + } + r.orchestratorType = orchestratorType + } + return r.orchestratorType, nil } // SetupWithManager sets up the controller with the Manager. diff --git a/internal/controller/platform/standalone/standalone.go b/internal/controller/platform/standalone/standalone.go index cdab1735..b7148386 100644 --- a/internal/controller/platform/standalone/standalone.go +++ b/internal/controller/platform/standalone/standalone.go @@ -76,13 +76,15 @@ func NewNIMCacheReconciler(r shared.Reconciler) *NIMCacheReconciler { // NewNIMServiceReconciler returns NIMServiceReconciler for standalone mode func NewNIMServiceReconciler(r shared.Reconciler) *NIMServiceReconciler { + orchestratorType, _ := r.GetOrchestratorType() + return &NIMServiceReconciler{ Client: r.GetClient(), scheme: r.GetScheme(), log: r.GetLogger(), updater: r.GetUpdater(), recorder: r.GetEventRecorder(), - orchestratorType: r.GetOrchestratorType(), + orchestratorType: orchestratorType, } } diff --git a/internal/shared/reconciler.go b/internal/shared/reconciler.go index 6f09613b..96ff7a76 100644 --- a/internal/shared/reconciler.go +++ b/internal/shared/reconciler.go @@ -35,5 +35,5 @@ type Reconciler interface { GetUpdater() conditions.Updater GetRenderer() render.Renderer GetEventRecorder() record.EventRecorder - GetOrchestratorType() k8sutil.OrchestratorType + GetOrchestratorType() (k8sutil.OrchestratorType, error) }