Skip to content

Commit

Permalink
Add BFF dev mode for local K8s testing
Browse files Browse the repository at this point in the history
Signed-off-by: Griffin-Sullivan <[email protected]>
  • Loading branch information
Griffin-Sullivan committed Nov 13, 2024
1 parent 30f7c26 commit 5a65ae9
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
3 changes: 2 additions & 1 deletion clients/ui/bff/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ IMG ?= model-registry-bff:latest
PORT ?= 4000
MOCK_K8S_CLIENT ?= false
MOCK_MR_CLIENT ?= false
DEV_MODE ?= false
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
ENVTEST_K8S_VERSION = 1.29.0

Expand Down Expand Up @@ -45,7 +46,7 @@ build: fmt vet test
.PHONY: run
run: fmt vet envtest
ENVTEST_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" \
go run ./cmd/main.go --port=$(PORT) --mock-k8s-client=$(MOCK_K8S_CLIENT) --mock-mr-client=$(MOCK_MR_CLIENT)
go run ./cmd/main.go --port=$(PORT) --mock-k8s-client=$(MOCK_K8S_CLIENT) --mock-mr-client=$(MOCK_MR_CLIENT) --dev-mode=$(DEV_MODE)

.PHONY: docker-build
docker-build:
Expand Down
6 changes: 4 additions & 2 deletions clients/ui/bff/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"context"
"flag"
"fmt"
"github.com/kubeflow/model-registry/ui/bff/internal/api"
"github.com/kubeflow/model-registry/ui/bff/internal/config"
"os/signal"
"syscall"

"github.com/kubeflow/model-registry/ui/bff/internal/api"
"github.com/kubeflow/model-registry/ui/bff/internal/config"

"log/slog"
"net/http"
"os"
Expand All @@ -21,6 +22,7 @@ func main() {
flag.IntVar(&cfg.Port, "port", getEnvAsInt("PORT", 4000), "API server port")
flag.BoolVar(&cfg.MockK8Client, "mock-k8s-client", false, "Use mock Kubernetes client")
flag.BoolVar(&cfg.MockMRClient, "mock-mr-client", false, "Use mock Model Registry client")
flag.BoolVar(&cfg.DevMode, "dev-mode", false, "Use development mode for access to local K8s cluster")
flag.Parse()

logger := slog.New(slog.NewTextHandler(os.Stdout, nil))
Expand Down
14 changes: 10 additions & 4 deletions clients/ui/bff/internal/api/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package api
import (
"context"
"fmt"
"net/http"

"github.com/julienschmidt/httprouter"
"github.com/kubeflow/model-registry/ui/bff/internal/integrations"
"net/http"
)

type contextKey string
Expand Down Expand Up @@ -41,7 +42,7 @@ func (app *App) AttachRESTClient(handler func(http.ResponseWriter, *http.Request

modelRegistryID := ps.ByName(ModelRegistryId)

modelRegistryBaseURL, err := resolveModelRegistryURL(modelRegistryID, app.kubernetesClient)
modelRegistryBaseURL, err := resolveModelRegistryURL(modelRegistryID, app)
if err != nil {
app.serverErrorResponse(w, r, fmt.Errorf("failed to resolve model registry base URL): %v", err))
return
Expand Down Expand Up @@ -83,11 +84,16 @@ func resolveBearerToken(k8s integrations.KubernetesClientInterface, header http.
return bearerToken, nil
}

func resolveModelRegistryURL(id string, client integrations.KubernetesClientInterface) (string, error) {
serviceDetails, err := client.GetServiceDetailsByName(id)
func resolveModelRegistryURL(id string, app *App) (string, error) {
serviceDetails, err := app.kubernetesClient.GetServiceDetailsByName(id)
if err != nil {
return "", err
}

if app.config.DevMode {
serviceDetails.ClusterIP = "localhost"
}

url := fmt.Sprintf("http://%s:%d/api/model_registry/v1alpha3", serviceDetails.ClusterIP, serviceDetails.HTTPPort)
return url, nil
}
1 change: 1 addition & 0 deletions clients/ui/bff/internal/config/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ type EnvConfig struct {
Port int
MockK8Client bool
MockMRClient bool
DevMode bool
}

0 comments on commit 5a65ae9

Please sign in to comment.