Skip to content

Commit

Permalink
Fix MR InferenceService reconciler
Browse files Browse the repository at this point in the history
Signed-off-by: Andrea Lamparelli <[email protected]>
  • Loading branch information
lampajr committed Jan 11, 2024
1 parent 7be4141 commit 15935b0
Showing 1 changed file with 34 additions and 18 deletions.
52 changes: 34 additions & 18 deletions controllers/mr_inferenceservice_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,24 +85,27 @@ func (r *ModelRegistryInferenceServiceReconciler) Reconcile(ctx context.Context,
modelRegistryNamespace = req.Namespace
}

mr, err := r.initModelRegistryService(ctx, log, modelRegistryNamespace)
// setup grpc connection to ml-metadata
ctxTimeout, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer func() {
log.Info("Cancelling ctx")
cancel()
}()

mr, err := r.initModelRegistryService(ctx, ctxTimeout, log, modelRegistryNamespace)
if err != nil {
log.Error(err, "Stop ModelRegistry InferenceService reconciliation")
return ctrl.Result{Requeue: true, RequeueAfter: time.Second * 10}, err
} else if mr == nil {
// There is no model registry installed, do not requeue
log.Info("Cannot find ModelRegistry in given namespace, stopping reconciliation")
return ctrl.Result{}, nil
}

// Retrieve or create the ServingEnvironment associated to the current namespace
servingEnvironment, err := mr.GetServingEnvironmentByParams(&req.Namespace, nil)
if err != nil || servingEnvironment.Id == nil {
log.Info("ServingEnvironment not found, creating it..")
servingEnvironment, err = mr.UpsertServingEnvironment(&openapi.ServingEnvironment{
Name: &req.Namespace,
ExternalID: &req.Namespace,
})
if err != nil || servingEnvironment.Id == nil {
err = fmt.Errorf("unable to create ServingEnvironment: %w", err)
return ctrl.Result{Requeue: true}, err
}
servingEnvironment, err := r.getOrCreateServingEnvironment(log, mr, req.Namespace)
if err != nil {
return ctrl.Result{Requeue: true}, err
}

// Let's add a finalizer. Then, we can define some operations which should
Expand Down Expand Up @@ -282,6 +285,20 @@ func (r *ModelRegistryInferenceServiceReconciler) processDelta(ctx context.Conte
return nil
}

func (r *ModelRegistryInferenceServiceReconciler) getOrCreateServingEnvironment(log logr.Logger, mr api.ModelRegistryApi, namespace string) (*openapi.ServingEnvironment, error) {
servingEnvironment, err := mr.GetServingEnvironmentByParams(&namespace, nil)
if err != nil {
log.Info("ServingEnvironment not found, creating it..")
servingEnvironment, err = mr.UpsertServingEnvironment(&openapi.ServingEnvironment{
Name: &namespace,
})
if err != nil {
return nil, fmt.Errorf("unable to create ServingEnvironment: %w", err)
}
}
return servingEnvironment, nil
}

// createMRInferenceService create a new model registry InferenceService resource based on provided input
func (r *ModelRegistryInferenceServiceReconciler) createMRInferenceService(
log logr.Logger,
Expand Down Expand Up @@ -321,11 +338,12 @@ func (r *ModelRegistryInferenceServiceReconciler) onDeletion(mr api.ModelRegistr
}

// initModelRegistryService setup a gRPC connection with MLMD server and initialize the model registry service
func (r *ModelRegistryInferenceServiceReconciler) initModelRegistryService(ctx context.Context, log logr.Logger, namespace string) (api.ModelRegistryApi, error) {
func (r *ModelRegistryInferenceServiceReconciler) initModelRegistryService(ctx context.Context, ctxTimeout context.Context, log logr.Logger, namespace string) (api.ModelRegistryApi, error) {
log1 := log.WithValues("mr-namespace", namespace)
mlmdAddr, ok := os.LookupEnv(constants.MLMDAddressEnv)

if !ok || mlmdAddr == "" {
log.Info("Retrieving mlmd address from deployed model registry service..")
log1.Info("Retrieving mlmd address from deployed model registry service")
// Env variable not set, look for existing model registry service
opts := []client.ListOption{client.InNamespace(namespace), client.MatchingLabels{
"component": "model-registry",
Expand Down Expand Up @@ -360,11 +378,9 @@ func (r *ModelRegistryInferenceServiceReconciler) initModelRegistryService(ctx c
}

if mlmdAddr == "" {
return nil, fmt.Errorf("cannot connect to the model registry: empty mlmd address")
log1.Info("Cannot connect to the model registry: empty mlmd address")
return nil, nil
}
// setup grpc connection to ml-metadata
ctxTimeout, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()

// Setup model registry service
log.Info("Connecting to " + mlmdAddr)
Expand Down

0 comments on commit 15935b0

Please sign in to comment.