Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions catalog/internal/server/openapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,25 @@ import (
model "github.com/kubeflow/model-registry/catalog/pkg/openapi"
)



// ModelCatalogServiceAPIRouter defines the required methods for binding the api requests to a responses for the ModelCatalogServiceAPI
// The ModelCatalogServiceAPIRouter implementation should parse necessary information from the http request,
// pass the data to a ModelCatalogServiceAPIServicer to perform the required actions, then write the service results to the http response.
type ModelCatalogServiceAPIRouter interface {
type ModelCatalogServiceAPIRouter interface {
FindModels(http.ResponseWriter, *http.Request)
FindModelsFilterOptions(http.ResponseWriter, *http.Request)
FindSources(http.ResponseWriter, *http.Request)
GetAllModelArtifacts(http.ResponseWriter, *http.Request)
GetModel(http.ResponseWriter, *http.Request)
}


// ModelCatalogServiceAPIServicer defines the api actions for the ModelCatalogServiceAPI service
// This interface intended to stay up to date with the openapi yaml used to generate it,
// while the service implementation can be ignored with the .openapi-generator-ignore file
// and updated with the logic required for the API.
type ModelCatalogServiceAPIServicer interface {
FindModels(context.Context, []string, string, string, string, string, model.OrderByField, model.SortOrder, string) (ImplResponse, error)
FindModelsFilterOptions(context.Context) (ImplResponse, error)
FindSources(context.Context, string, string, model.OrderByField, model.SortOrder, string) (ImplResponse, error)
GetAllModelArtifacts(context.Context, string, string, string, model.OrderByField, model.SortOrder, string, string) (ImplResponse, error)
GetModel(context.Context, string, string) (ImplResponse, error)
type ModelCatalogServiceAPIServicer interface {
FindModels(context.Context, []string, string, string, string, string, model.OrderByField, model.SortOrder, string) (ImplResponse, error)
FindModelsFilterOptions(context.Context) (ImplResponse, error)
FindSources(context.Context, string, string, model.OrderByField, model.SortOrder, string) (ImplResponse, error)
GetAllModelArtifacts(context.Context, string, string, string, model.OrderByField, model.SortOrder, string, string) (ImplResponse, error)
GetModel(context.Context, string, string) (ImplResponse, error)
}
20 changes: 17 additions & 3 deletions catalog/internal/server/openapi/api_model_catalog_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
package openapi

import (

"net/http"
"strings"

Expand Down Expand Up @@ -75,7 +74,7 @@ func (c *ModelCatalogServiceAPIController) Routes() Routes {
},
"GetModel": Route{
strings.ToUpper("Get"),
"/api/model_catalog/v1alpha1/sources/{source_id}/models/{model_name+}",
"/api/model_catalog/v1alpha1/sources/{source_id}/models/*",
c.GetModel,
},
}
Expand Down Expand Up @@ -155,7 +154,22 @@ func (c *ModelCatalogServiceAPIController) GetAllModelArtifacts(w http.ResponseW
// GetModel - Get a `CatalogModel`.
func (c *ModelCatalogServiceAPIController) GetModel(w http.ResponseWriter, r *http.Request) {
sourceIdParam := chi.URLParam(r, "source_id")
modelNameParam := chi.URLParam(r, "model_name+")
modelNameParam := chi.URLParam(r, "*")

// Special handling for getModel to delegate /artifacts requests to getAllModelArtifacts
// The wildcard /* pattern catches /artifacts requests, but we want those to go to GetAllModelArtifacts
if strings.HasSuffix(r.URL.Path, "/artifacts") {
// Extract the model name by removing the /artifacts suffix
modelName := strings.TrimSuffix(modelNameParam, "/artifacts")

// Add the model_name parameter to the route context so GetAllModelArtifacts can access it
chi.RouteContext(r.Context()).URLParams.Add("model_name", modelName)

// Call the GetAllModelArtifacts handler directly
c.GetAllModelArtifacts(w, r)
return
}

result, err := c.service.GetModel(r.Context(), sourceIdParam, modelNameParam)
// If an error occurred, encode the error with the status code
if err != nil {
Expand Down
18 changes: 9 additions & 9 deletions catalog/internal/server/openapi/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,28 @@
package openapi

import (
"net/http"
"reflect"
"net/http"

model "github.com/kubeflow/model-registry/pkg/openapi"
)

// Response return a ImplResponse struct filled
func Response(code int, body interface{}) ImplResponse {
return ImplResponse {
return ImplResponse{
Code: code,
Body: body,
}
}

func ErrorResponse(code int, err error) ImplResponse {
return ImplResponse{
Code: code,
Body: model.Error{
Code: http.StatusText(code),
Message: err.Error(),
},
}
return ImplResponse{
Code: code,
Body: model.Error{
Code: http.StatusText(code),
Message: err.Error(),
},
}
}

// IsZeroValue checks if the val is the zero-ed value.
Expand Down
8 changes: 4 additions & 4 deletions catalog/internal/server/openapi/routers.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import (

// A Route defines the parameters for an api endpoint
type Route struct {
Method string
Pattern string
Method string
Pattern string
HandlerFunc http.HandlerFunc
}

Expand Down Expand Up @@ -80,8 +80,8 @@ func EncodeJSONResponse(i interface{}, status *int, w http.ResponseWriter) {

if i != nil {
if err := json.NewEncoder(w).Encode(i); err != nil {
// FIXME: is it too late to inform the client of an error at this point??
glog.Errorf("error encoding JSON response: %v", err)
// FIXME: is it too late to inform the client of an error at this point??
glog.Errorf("error encoding JSON response: %v", err)
}
}
}
Expand Down
24 changes: 0 additions & 24 deletions catalog/internal/server/openapi/type_asserts.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,6 @@ func AssertErrorRequired(obj model.Error) error {
return nil
}

// AssertFilterOptionConstraints checks if the values respects the defined constraints
func AssertFilterOptionConstraints(obj model.FilterOption) error {
return nil
}

// AssertFilterOptionRangeConstraints checks if the values respects the defined constraints
func AssertFilterOptionRangeConstraints(obj model.FilterOptionRange) error {
return nil
Expand All @@ -272,25 +267,6 @@ func AssertFilterOptionRangeRequired(obj model.FilterOptionRange) error {
return nil
}

// AssertFilterOptionRequired checks if the required fields are not zero-ed
func AssertFilterOptionRequired(obj model.FilterOption) error {
elements := map[string]interface{}{
"type": obj.Type,
}
for name, el := range elements {
if isZero := IsZeroValue(el); isZero {
return &RequiredError{Field: name}
}
}

if obj.Range != nil {
if err := AssertFilterOptionRangeRequired(*obj.Range); err != nil {
return err
}
}
return nil
}

// AssertFilterOptionsListConstraints checks if the values respects the defined constraints
func AssertFilterOptionsListConstraints(obj model.FilterOptionsList) error {
return nil
Expand Down
19 changes: 19 additions & 0 deletions catalog/internal/server/openapi/type_asserts_overrides.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,22 @@ func AssertCatalogArtifactRequired(obj model.CatalogArtifact) error {
// checks the fields from CatalogModelArtifact, which doesn't compile.
return nil
}

// AssertFilterOptionRequired checks if the required fields are not zero-ed
func AssertFilterOptionRequired(obj model.FilterOption) error {
elements := map[string]interface{}{
"type": obj.Type,
}
for name, el := range elements {
if isZero := IsZeroValue(el); isZero {
return &RequiredError{Field: name}
}
}

if obj.Range != nil {
if err := AssertFilterOptionRangeRequired(*obj.Range); err != nil {
return err
}
}
return nil
}
4 changes: 3 additions & 1 deletion catalog/scripts/gen_type_asserts.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ ASSERT_FILE_PATH="$1/type_asserts.go"
PROJECT_ROOT=$(realpath "$(dirname "$0")"/..)

# These files generate with incorrect logic:
rm -f "$1/model_metadata_value.go" "$1/model_catalog_artifact.go"
rm -f "$1/model_metadata_value.go" \
"$1/model_catalog_artifact.go" \
"$1/model_filter_option.go"

python3 "${PROJECT_ROOT}/scripts/gen_type_asserts.py" $1 >"$ASSERT_FILE_PATH"

Expand Down
Loading