Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
Signed-off-by: Shiva Krishna, Merla <[email protected]>
  • Loading branch information
shivamerla committed Nov 4, 2024
1 parent 43fd6f3 commit bbd16db
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 31 deletions.
23 changes: 14 additions & 9 deletions internal/controller/nemo_guardrail_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand Down
27 changes: 16 additions & 11 deletions internal/controller/nimcache_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
23 changes: 14 additions & 9 deletions internal/controller/nimservice_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion internal/controller/platform/standalone/standalone.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

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
GetOrchestratorType() k8sutil.OrchestratorType
GetOrchestratorType() (k8sutil.OrchestratorType, error)
}

0 comments on commit bbd16db

Please sign in to comment.