Skip to content

Commit

Permalink
feat: fetch MR endpoint (#128)
Browse files Browse the repository at this point in the history
* feat: fetch MR endpoint

In this PR:
- New kubernetes client (integrations/k8s.go)
- Fetch model registries handler (model_registry_handler.go)

Signed-off-by: Eder Ignatowicz <[email protected]>

* Small reminder on a TODO

Signed-off-by: Eder Ignatowicz <[email protected]>

---------

Signed-off-by: Eder Ignatowicz <[email protected]>
  • Loading branch information
ederign authored Jun 10, 2024
1 parent 0796353 commit f38a86e
Show file tree
Hide file tree
Showing 15 changed files with 411 additions and 32 deletions.
1 change: 1 addition & 0 deletions clients/ui/bff/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ COPY cmd/ cmd/
COPY api/ api/
COPY config/ config/
COPY data/ data/
COPY integrations/ integrations/

# Build the Go application
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o bff ./cmd/main.go
Expand Down
8 changes: 4 additions & 4 deletions clients/ui/bff/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ TBD

### Endpoints

| URL Pattern | Handler | Action |
|---------------------|--------------------|-------------------------------|
| GET /v1/healthcheck | HealthcheckHandler | Show application information. |

| URL Pattern | Handler | Action |
|-------------------------|----------------------|-------------------------------|
| GET /v1/healthcheck | HealthcheckHandler | Show application information. |
| GET /v1/model-registry/ | ModelRegistryHandler | Get all model registries, |

### Sample local calls
```
Expand Down
29 changes: 21 additions & 8 deletions clients/ui/bff/api/app.go
Original file line number Diff line number Diff line change
@@ -1,32 +1,41 @@
package api

import (
"fmt"
"github.com/kubeflow/model-registry/ui/bff/config"
"github.com/kubeflow/model-registry/ui/bff/data"
"github.com/kubeflow/model-registry/ui/bff/integrations"
"log/slog"
"net/http"

"github.com/julienschmidt/httprouter"
)

const (
// TODO(ederign) discuss versioning with the team
Version = "1.0.0"
HealthCheckPath = "/api/v1/healthcheck/"
ModelRegistry = "/api/v1/model-registry/"
)

type App struct {
config config.EnvConfig
logger *slog.Logger
models data.Models
config config.EnvConfig
logger *slog.Logger
models data.Models
kubernetesClient integrations.KubernetesClientInterface
}

func NewApp(cfg config.EnvConfig, logger *slog.Logger) *App {
func NewApp(cfg config.EnvConfig, logger *slog.Logger) (*App, error) {
k8sClient, err := integrations.NewKubernetesClient()
if err != nil {
return nil, fmt.Errorf("failed to create Kubernetes client: %w", err)
}

app := &App{
config: cfg,
logger: logger,
config: cfg,
logger: logger,
kubernetesClient: k8sClient,
}
return app
return app, nil
}

func (app *App) Routes() http.Handler {
Expand All @@ -35,7 +44,11 @@ func (app *App) Routes() http.Handler {
router.NotFound = http.HandlerFunc(app.notFoundResponse)
router.MethodNotAllowed = http.HandlerFunc(app.methodNotAllowedResponse)

// HTTP client routes
router.GET(HealthCheckPath, app.HealthcheckHandler)

// Kubernetes client routes
router.GET(ModelRegistry, app.ModelRegistryHandler)

return app.RecoverPanic(app.enableCORS(router))
}
14 changes: 5 additions & 9 deletions clients/ui/bff/api/healthcheck__handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,21 @@ func TestHealthCheckHandler(t *testing.T) {

rr := httptest.NewRecorder()
req, err := http.NewRequest(http.MethodGet, HealthCheckPath, nil)
if err != nil {
t.Fatal(err)
}
assert.NoError(t, err)

app.HealthcheckHandler(rr, req, nil)
rs := rr.Result()

defer rs.Body.Close()

body, err := io.ReadAll(rs.Body)
if err != nil {
t.Fatal("Failed to read response body")
}
assert.NoError(t, err)

var healthCheckRes data.HealthCheckModel
err = json.Unmarshal(body, &healthCheckRes)
if err != nil {
t.Fatalf("Error unmarshalling response JSON: %v", err)
}
assert.NoError(t, err)

assert.Equal(t, http.StatusOK, rr.Code)

expected := data.HealthCheckModel{
Status: "available",
Expand Down
2 changes: 1 addition & 1 deletion clients/ui/bff/api/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"strings"
)

type Envelope map[string]any
type Envelope map[string]interface{}

func (app *App) WriteJSON(w http.ResponseWriter, status int, data any, headers http.Header) error {

Expand Down
26 changes: 26 additions & 0 deletions clients/ui/bff/api/model_registry_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package api

import (
"github.com/julienschmidt/httprouter"
"net/http"
)

func (app *App) ModelRegistryHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {

registries, err := app.models.ModelRegistry.FetchAllModelRegistry(app.kubernetesClient)
if err != nil {
app.serverErrorResponse(w, r, err)
return
}

modelRegistryRes := Envelope{
"model_registry": registries,
}

err = app.WriteJSON(w, http.StatusOK, modelRegistryRes, nil)

if err != nil {
app.serverErrorResponse(w, r, err)
}

}
59 changes: 59 additions & 0 deletions clients/ui/bff/api/model_registry_handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package api

import (
"encoding/json"
"fmt"
"github.com/kubeflow/model-registry/ui/bff/data"
"github.com/kubeflow/model-registry/ui/bff/internals/mocks"
"github.com/stretchr/testify/assert"
"io"
"net/http"
"net/http/httptest"
"testing"
)

func TestModelRegistryHandler(t *testing.T) {
mockK8sClient := new(mocks.KubernetesClientMock)
mockK8sClient.On("FetchServiceNamesByComponent", "model-registry-server").Return([]string{"model-registry-dora", "model-registry-bella"}, nil)

testApp := App{
kubernetesClient: mockK8sClient,
}

req, err := http.NewRequest(http.MethodGet, ModelRegistry, nil)
assert.NoError(t, err)

rr := httptest.NewRecorder()

testApp.ModelRegistryHandler(rr, req, nil)
rs := rr.Result()

defer rs.Body.Close()
body, err := io.ReadAll(rs.Body)
assert.NoError(t, err)
fmt.Println(string(body))
var modelRegistryRes Envelope
err = json.Unmarshal(body, &modelRegistryRes)
assert.NoError(t, err)

assert.Equal(t, http.StatusOK, rr.Code)

// Convert the unmarshalled data to the expected type
actualModelRegistry := make([]data.ModelRegistryModel, 0)
for _, v := range modelRegistryRes["model_registry"].([]interface{}) {
model := v.(map[string]interface{})
actualModelRegistry = append(actualModelRegistry, data.ModelRegistryModel{Name: model["name"].(string)})
}
modelRegistryRes["model_registry"] = actualModelRegistry

var expected = Envelope{
"model_registry": []data.ModelRegistryModel{
{Name: "model-registry-dora"},
{Name: "model-registry-bella"},
},
}

assert.Equal(t, expected, modelRegistryRes)

mockK8sClient.AssertExpectations(t)
}
12 changes: 9 additions & 3 deletions clients/ui/bff/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ func main() {

logger := slog.New(slog.NewTextHandler(os.Stdout, nil))

app := api.NewApp(cfg, logger)
app, err := api.NewApp(cfg, logger)
if err != nil {
logger.Error(err.Error())
os.Exit(1)
}

srv := &http.Server{
Addr: fmt.Sprintf(":%d", cfg.Port),
Expand All @@ -32,8 +36,10 @@ func main() {

logger.Info("starting server", "addr", srv.Addr)

err := srv.ListenAndServe()
logger.Error(err.Error())
err = srv.ListenAndServe()
if err != nil {
logger.Error(err.Error())
}
os.Exit(1)
}

Expand Down
29 changes: 29 additions & 0 deletions clients/ui/bff/data/model_registry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package data

import (
"fmt"

k8s "github.com/kubeflow/model-registry/ui/bff/integrations"
)

type ModelRegistryModel struct {
Name string `json:"name"`
}

func (m ModelRegistryModel) FetchAllModelRegistry(client k8s.KubernetesClientInterface) ([]ModelRegistryModel, error) {

resources, err := client.FetchServiceNamesByComponent(k8s.ModelRegistryServiceComponentSelector)
if err != nil {
return nil, fmt.Errorf("error fetching model registries: %w", err)
}

var registries []ModelRegistryModel = []ModelRegistryModel{}
for _, item := range resources {
registry := ModelRegistryModel{
Name: item,
}
registries = append(registries, registry)
}

return registries, nil
}
9 changes: 2 additions & 7 deletions clients/ui/bff/data/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@ package data

// Models struct is a single convenient container to hold and represent all our data.
type Models struct {
HealthCheck HealthCheckModel
}

func NewModels() Models {
return Models{
HealthCheck: HealthCheckModel{},
}
HealthCheck HealthCheckModel
ModelRegistry ModelRegistryModel
}
38 changes: 38 additions & 0 deletions clients/ui/bff/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,48 @@ go 1.22.2
require (
github.com/julienschmidt/httprouter v1.3.0
github.com/stretchr/testify v1.8.4
k8s.io/apimachinery v0.30.1
k8s.io/client-go v0.30.1
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/imdario/mergo v0.3.6 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.5.0 // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/oauth2 v0.10.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/term v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.3.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.30.1 // indirect
k8s.io/klog/v2 v2.120.1 // indirect
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 // indirect
k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)
Loading

0 comments on commit f38a86e

Please sign in to comment.