Skip to content

fix(ws): honor PORT in swagger UI and clean up backend README #482

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

Closed
Closed
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
120 changes: 5 additions & 115 deletions workspaces/backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,124 +27,14 @@ make run
If you want to use a different port:

```shell
make run PORT=8000
make run PORT=8000
```

### Endpoints

| URL Pattern | Handler | Action |
|----------------------------------------------|------------------------|-----------------------------------------|
| GET /api/v1/healthcheck | healthcheck_handler | Show application information |
| GET /api/v1/namespaces | namespaces_handler | Get all Namespaces |
| GET /api/v1/swagger/ | swagger_handler | Swagger API documentation |
| GET /api/v1/workspaces | workspaces_handler | Get all Workspaces |
| GET /api/v1/workspaces/{namespace} | workspaces_handler | Get all Workspaces from a namespace |
| POST /api/v1/workspaces/{namespace} | workspaces_handler | Create a Workspace in a given namespace |
| GET /api/v1/workspaces/{namespace}/{name} | workspaces_handler | Get a Workspace entity |
| PATCH /api/v1/workspaces/{namespace}/{name} | TBD | Patch a Workspace entity |
| PUT /api/v1/workspaces/{namespace}/{name} | TBD | Update a Workspace entity |
| DELETE /api/v1/workspaces/{namespace}/{name} | workspaces_handler | Delete a Workspace entity |
| GET /api/v1/workspacekinds | workspacekinds_handler | Get all WorkspaceKind |
| POST /api/v1/workspacekinds | TBD | Create a WorkspaceKind |
| GET /api/v1/workspacekinds/{name} | workspacekinds_handler | Get a WorkspaceKind entity |
| PATCH /api/v1/workspacekinds/{name} | TBD | Patch a WorkspaceKind entity |
| PUT /api/v1/workspacekinds/{name} | TBD | Update a WorkspaceKind entity |
| DELETE /api/v1/workspacekinds/{name} | TBD | Delete a WorkspaceKind entity |
The `backend` server exposes a Swagger UI typically available at localhost:4000/api/v1/swagger/index.html.
- :warning: If providing a `PORT` override in `make run`, please ensure to use that port when composing the URL

### Sample local calls
Running the `backend` to serve the Swagger UI is the proper means to learning about and interacting with the available APIs.

Healthcheck:

```shell
# GET /api/v1/healthcheck
curl -i localhost:4000/api/v1/healthcheck
```

List all Namespaces:

```shell
# GET /api/v1/namespaces
curl -i localhost:4000/api/v1/namespaces
```

List all Workspaces:

```shell
# GET /api/v1/workspaces/
curl -i localhost:4000/api/v1/workspaces
```

List all Workspaces in a Namespace:

```shell
# GET /api/v1/workspaces/{namespace}
curl -i localhost:4000/api/v1/workspaces/default
```

Create a Workspace:

```shell
# POST /api/v1/workspaces/{namespace}
curl -X POST http://localhost:4000/api/v1/workspaces/default \
-H "Content-Type: application/json" \
-d '{
"data": {
"name": "dora",
"kind": "jupyterlab",
"paused": false,
"deferUpdates": false,
"podTemplate": {
"podMetadata": {
"labels": {
"app": "dora"
},
"annotations": {
"app": "dora"
}
},
"volumes": {
"home": "workspace-home-bella",
"data": [
{
"pvcName": "workspace-data-bella",
"mountPath": "/data/my-data",
"readOnly": false
}
]
},
"options": {
"imageConfig": "jupyterlab_scipy_190",
"podConfig": "tiny_cpu"
}
}
}
}'
```

Get a Workspace:

```shell
# GET /api/v1/workspaces/{namespace}/{name}
curl -i localhost:4000/api/v1/workspaces/default/dora
```

Delete a Workspace:

```shell
# DELETE /api/v1/workspaces/{namespace}/{name}
curl -X DELETE localhost:4000/api/v1/workspaces/default/dora
```

List all WorkspaceKinds:

```shell
# GET /api/v1/workspacekinds
curl -i localhost:4000/api/v1/workspacekinds
```

Get a WorkspaceKind:

```shell
# GET /api/v1/workspacekinds/{name}
curl -i localhost:4000/api/v1/workspacekinds/jupyterlab
```
If you wish to understand which handlers service the various endpoints, you can find the mapping in the `Routes()` function in [`app.go`](./api/app.go).
9 changes: 8 additions & 1 deletion workspaces/backend/api/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ package api
import (
"fmt"
"log/slog"
"net"
"net/http"
"strconv"

"github.com/julienschmidt/httprouter"
"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -30,7 +32,7 @@ import (

"github.com/kubeflow/notebooks/workspaces/backend/internal/config"
"github.com/kubeflow/notebooks/workspaces/backend/internal/repositories"
_ "github.com/kubeflow/notebooks/workspaces/backend/openapi"
"github.com/kubeflow/notebooks/workspaces/backend/openapi"
)

const (
Expand Down Expand Up @@ -78,6 +80,11 @@ func NewApp(cfg *config.EnvConfig, logger *slog.Logger, cl client.Client, scheme

// TODO: log the configuration on startup

// Override SwaggerInfo.Host with the correct port
if host, _, err := net.SplitHostPort(openapi.SwaggerInfo.Host); err == nil {
openapi.SwaggerInfo.Host = net.JoinHostPort(host, strconv.Itoa(cfg.Port))
}

// get a serializer for Kubernetes YAML
codecFactory := serializer.NewCodecFactory(scheme)
yamlSerializerInfo, found := runtime.SerializerInfoForMediaType(codecFactory.SupportedMediaTypes(), runtime.ContentTypeYAML)
Expand Down