Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BFF dev mode for local K8s testing #560

Merged
merged 1 commit into from
Nov 18, 2024
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
4 changes: 3 additions & 1 deletion clients/ui/bff/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down
7 changes: 5 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,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))
Expand Down
13 changes: 12 additions & 1 deletion clients/ui/bff/docs/dev-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}
```
```

#### 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
```
14 changes: 11 additions & 3 deletions clients/ui/bff/internal/api/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package api
import (
"context"
"fmt"
"net/http"

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

type contextKey string
Expand Down Expand Up @@ -41,7 +43,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.kubernetesClient, app.config)
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 +85,17 @@ func resolveBearerToken(k8s integrations.KubernetesClientInterface, header http.
return bearerToken, nil
}

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

if config.DevMode {
serviceDetails.ClusterIP = "localhost"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to bring the internal conversation here, it would be really interesting to parametrize the port too, that way we can get whatever port users might have used for port-forwarding.

serviceDetails.HTTPPort = int32(config.DevModePort)
}

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