Skip to content

Commit

Permalink
feat(ws): Notebooks 2.0 // Backend // List namespaces kubeflow#53
Browse files Browse the repository at this point in the history
Signed-off-by: Eder Ignatowicz <[email protected]>
  • Loading branch information
ederign committed Nov 12, 2024
1 parent 9d02acf commit fe475a8
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 1 deletion.
7 changes: 6 additions & 1 deletion workspaces/backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ make run PORT=8000

| URL Pattern | Handler | Action |
|----------------------------------------------|------------------------|-----------------------------------------|
| GET /api/v1/healthcheck | healthcheck_handler | Show application information. |
| GET /api/v1/healthcheck | healthcheck_handler | Show application information |
| GET /api/v1/namespaces | namespaces_handler | Get all Namespaces |
| 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 |
Expand All @@ -47,6 +48,10 @@ make run PORT=8000
curl -i localhost:4000/api/v1/healthcheck
```
```
# GET /api/v1/namespaces
curl -i localhost:4000/api/v1/namespaces
```
```
# GET /api/v1/workspaces/
curl -i localhost:4000/api/v1/workspaces
```
Expand Down
4 changes: 4 additions & 0 deletions workspaces/backend/api/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ const (
AllWorkspaceKindsPath = PathPrefix + "/workspacekinds"
WorkspaceKindNamePathParam = "name"
WorkspaceKindsByNamePath = AllWorkspaceKindsPath + "/:" + WorkspaceNamePathParam

//namespaces
AllNamespacesPath = PathPrefix + "/namespaces"
)

type App struct {
Expand Down Expand Up @@ -75,6 +78,7 @@ func (a *App) Routes() http.Handler {
router.MethodNotAllowed = http.HandlerFunc(a.methodNotAllowedResponse)

router.GET(HealthCheckPath, a.HealthcheckHandler)
router.GET(AllNamespacesPath, a.GetNamespacesHandler)

router.GET(AllWorkspacesPath, a.GetWorkspacesHandler)
router.GET(WorkspacesByNamespacePath, a.GetWorkspacesHandler)
Expand Down
45 changes: 45 additions & 0 deletions workspaces/backend/api/namespaces_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
*
* Copyright 2024.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* /
*/

package api

import (
"github.com/julienschmidt/httprouter"
"github.com/kubeflow/notebooks/workspaces/backend/internal/models"
"net/http"
)

type NamespacesEnvelope Envelope[[]models.NamespaceModel]

func (a *App) GetNamespacesHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {

namespaces, err := a.repositories.Namespace.GetNamespaces(r.Context())
if err != nil {
a.serverErrorResponse(w, r, err)
return
}

namespacesEnvelope := NamespacesEnvelope{
Data: namespaces,
}

err = a.WriteJSON(w, http.StatusOK, namespacesEnvelope, nil)
if err != nil {
a.serverErrorResponse(w, r, err)
}
}
29 changes: 29 additions & 0 deletions workspaces/backend/internal/models/namespaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
*
* Copyright 2024.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* /
*/

package models

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

func NewNamespaceModelFromNamespace(name string) NamespaceModel {
return NamespaceModel{
Name: name,
}
}
53 changes: 53 additions & 0 deletions workspaces/backend/internal/repositories/namespaces.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
*
* Copyright 2024.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* /
*/

package repositories

import (
"context"
"github.com/kubeflow/notebooks/workspaces/backend/internal/models"
v1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

type NamespaceRepository struct {
client client.Client
}

func NewNamespaceRepository(client client.Client) *NamespaceRepository {
return &NamespaceRepository{
client: client,
}
}

func (r *NamespaceRepository) GetNamespaces(ctx context.Context) ([]models.NamespaceModel, error) {

//TODO (ederign) Implement subject access review here to fetch only
// namespaces that "kubeflow-userid" has access
namespaceList := &v1.NamespaceList{}
err := r.client.List(ctx, namespaceList, &client.ListOptions{})
if err != nil {
return nil, err
}

namespaces := make([]models.NamespaceModel, len(namespaceList.Items))
for i, ns := range namespaceList.Items {
namespaces[i] = models.NewNamespaceModelFromNamespace(ns.Name)
}
return namespaces, nil
}
2 changes: 2 additions & 0 deletions workspaces/backend/internal/repositories/repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ type Repositories struct {
HealthCheck *HealthCheckRepository
Workspace *WorkspaceRepository
WorkspaceKind *WorkspaceKindRepository
Namespace *NamespaceRepository
}

func NewRepositories(client client.Client) *Repositories {
return &Repositories{
HealthCheck: NewHealthCheckRepository(),
Workspace: NewWorkspaceRepository(client),
WorkspaceKind: NewWorkspaceKindRepository(client),
Namespace: NewNamespaceRepository(client),
}
}

0 comments on commit fe475a8

Please sign in to comment.