From a6d3962abfdc036b8f8db93e529860492b3d3ab9 Mon Sep 17 00:00:00 2001 From: Griffin-Sullivan Date: Tue, 12 Nov 2024 15:30:38 -0500 Subject: [PATCH] Add BFF dev mode for local K8s testing Signed-off-by: Griffin-Sullivan --- clients/ui/bff/Makefile | 4 +++- clients/ui/bff/cmd/main.go | 7 +++++-- clients/ui/bff/docs/dev-guide.md | 13 ++++++++++++- clients/ui/bff/internal/api/middleware.go | 15 +++++++++++---- clients/ui/bff/internal/config/environment.go | 2 ++ 5 files changed, 33 insertions(+), 8 deletions(-) diff --git a/clients/ui/bff/Makefile b/clients/ui/bff/Makefile index dec0984d3..b8e6ccdd3 100644 --- a/clients/ui/bff/Makefile +++ b/clients/ui/bff/Makefile @@ -3,6 +3,8 @@ IMG ?= model-registry-bff:latest PORT ?= 4000 MOCK_K8S_CLIENT ?= false MOCK_MR_CLIENT ?= false +DEV_MODE ?= false +DEV_MODE_PORT ?= 8080 # ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary. ENVTEST_K8S_VERSION = 1.29.0 @@ -45,7 +47,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) --dev-mode-port=$(DEV_MODE_PORT) .PHONY: docker-build docker-build: diff --git a/clients/ui/bff/cmd/main.go b/clients/ui/bff/cmd/main.go index 25c367c3f..eb719229d 100644 --- a/clients/ui/bff/cmd/main.go +++ b/clients/ui/bff/cmd/main.go @@ -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" @@ -21,6 +22,8 @@ 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.IntVar(&cfg.DevModePort, "dev-mode-port", getEnvAsInt("DEV_MODE_PORT", 8080), "Use port when in development mode") flag.Parse() logger := slog.New(slog.NewTextHandler(os.Stdout, nil)) diff --git a/clients/ui/bff/docs/dev-guide.md b/clients/ui/bff/docs/dev-guide.md index 01e5fab92..582c72896 100644 --- a/clients/ui/bff/docs/dev-guide.md +++ b/clients/ui/bff/docs/dev-guide.md @@ -82,4 +82,15 @@ curl http://localhost:8080/api/model_registry/v1alpha3/registered_models You should receive a 200 response if everything is working correctly, the body should look like: ```json {"items":[],"nextPageToken":"","pageSize":0,"size":0} -``` \ No newline at end of file +``` + +#### 6. Run BFF locally in Dev Mode +To access your local kind cluster when running the BFF locally, you can use the `DEV_MODE` option. This is useful for when +you want to test live changes on real cluster. To do so, simply run: +```shell +make run DEV_MODE=true +``` +You can also specify the port you are forwarding to if it is something other than 8080: +```shell +make run DEV_MODE=true DEV_MODE_PORT=8081 +``` diff --git a/clients/ui/bff/internal/api/middleware.go b/clients/ui/bff/internal/api/middleware.go index 743cd2567..4042ae527 100644 --- a/clients/ui/bff/internal/api/middleware.go +++ b/clients/ui/bff/internal/api/middleware.go @@ -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 @@ -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 @@ -83,11 +84,17 @@ 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" + serviceDetails.HTTPPort = int32(app.config.DevModePort) + } + url := fmt.Sprintf("http://%s:%d/api/model_registry/v1alpha3", serviceDetails.ClusterIP, serviceDetails.HTTPPort) return url, nil } diff --git a/clients/ui/bff/internal/config/environment.go b/clients/ui/bff/internal/config/environment.go index f7b10bf63..f63dcce83 100644 --- a/clients/ui/bff/internal/config/environment.go +++ b/clients/ui/bff/internal/config/environment.go @@ -4,4 +4,6 @@ type EnvConfig struct { Port int MockK8Client bool MockMRClient bool + DevMode bool + DevModePort int }