-
Notifications
You must be signed in to change notification settings - Fork 33
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
feat(ws): add GET WS yaml to backend #209
Open
mohamedch7
wants to merge
4
commits into
kubeflow:notebooks-v2
Choose a base branch
from
mohamedch7:workspace-details
base: notebooks-v2
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
bed4aff
feat(ws): Add GET workspace yaml endpoint
mohamedch7 ad49f17
feat(ws): Add GET workspace yaml endpoint to README.md
mohamedch7 5e7c92c
fix(ws): Fix lint errors and typo
mohamedch7 d5dda0a
fix(ws): Update license header formatting in workspace_yaml_handler
mohamedch7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
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 ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/julienschmidt/httprouter" | ||
|
||
"errors" | ||
|
||
"github.com/kubeflow/notebooks/workspaces/backend/internal/repositories/workspaces" | ||
|
||
"sigs.k8s.io/yaml" | ||
) | ||
|
||
type WorkspaceYAMLEnvelope struct { | ||
Data string `json:"data"` | ||
} | ||
|
||
func (a *App) GetWorkspaceYAMLHandler(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { | ||
namespace := ps.ByName(NamespacePathParam) | ||
workspaceName := ps.ByName(ResourceNamePathParam) | ||
|
||
if namespace == "" || workspaceName == "" { | ||
a.serverErrorResponse(w, r, fmt.Errorf("namespace or workspace name is empty")) | ||
return | ||
} | ||
|
||
workspace, err := a.repositories.Workspace.GetWorkspace(r.Context(), namespace, workspaceName) | ||
if err != nil { | ||
if errors.Is(err, workspaces.ErrWorkspaceNotFound) { | ||
a.notFoundResponse(w, r) | ||
return | ||
} | ||
a.serverErrorResponse(w, r, err) | ||
return | ||
} | ||
|
||
yamlBytes, err := yaml.Marshal(workspace) | ||
if err != nil { | ||
a.serverErrorResponse(w, r, err) | ||
return | ||
} | ||
|
||
response := WorkspaceYAMLEnvelope{ | ||
Data: string(yamlBytes), | ||
} | ||
|
||
err = a.WriteJSON(w, http.StatusOK, response, nil) | ||
if err != nil { | ||
a.serverErrorResponse(w, r, err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
// 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 ( | ||
"encoding/json" | ||
"fmt" | ||
"log/slog" | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
|
||
"github.com/julienschmidt/httprouter" | ||
kubefloworgv1beta1 "github.com/kubeflow/notebooks/workspaces/controller/api/v1beta1" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
"github.com/kubeflow/notebooks/workspaces/backend/internal/config" | ||
"github.com/kubeflow/notebooks/workspaces/backend/internal/repositories" | ||
) | ||
|
||
var _ = Describe("Workspace YAML Handler", Ordered, func() { | ||
const namespaceName = "namespace-yaml" | ||
|
||
var ( | ||
a *App | ||
workspace *kubefloworgv1beta1.Workspace | ||
workspaceKey types.NamespacedName | ||
workspaceKindName string | ||
) | ||
|
||
BeforeAll(func() { | ||
uniqueName := "wsk-yaml-test" | ||
workspaceName := fmt.Sprintf("workspace-%s", uniqueName) | ||
workspaceKindName = fmt.Sprintf("workspacekind-%s", uniqueName) | ||
|
||
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil)) | ||
repos := repositories.NewRepositories(k8sClient) | ||
a = &App{ | ||
Config: &config.EnvConfig{ | ||
Port: 4000, | ||
}, | ||
repositories: repos, | ||
logger: logger, | ||
} | ||
|
||
By("creating namespace") | ||
namespace := &corev1.Namespace{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: namespaceName, | ||
}, | ||
} | ||
Expect(k8sClient.Create(ctx, namespace)).To(Succeed()) | ||
|
||
By("creating a WorkspaceKind") | ||
workspaceKind := NewExampleWorkspaceKind(workspaceKindName) | ||
Expect(k8sClient.Create(ctx, workspaceKind)).To(Succeed()) | ||
|
||
By("creating the Workspace") | ||
workspace = NewExampleWorkspace(workspaceName, namespaceName, workspaceKindName) | ||
Expect(k8sClient.Create(ctx, workspace)).To(Succeed()) | ||
workspaceKey = types.NamespacedName{Name: workspaceName, Namespace: namespaceName} | ||
}) | ||
|
||
AfterAll(func() { | ||
By("cleaning up resources") | ||
workspace := &kubefloworgv1beta1.Workspace{} | ||
if err := k8sClient.Get(ctx, workspaceKey, workspace); err == nil { | ||
Expect(k8sClient.Delete(ctx, workspace)).To(Succeed()) | ||
} | ||
|
||
workspaceKind := &kubefloworgv1beta1.WorkspaceKind{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: workspaceKindName, | ||
}, | ||
} | ||
if err := k8sClient.Get(ctx, client.ObjectKeyFromObject(workspaceKind), workspaceKind); err == nil { | ||
Expect(k8sClient.Delete(ctx, workspaceKind)).To(Succeed()) | ||
} | ||
|
||
namespace := &corev1.Namespace{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: namespaceName, | ||
}, | ||
} | ||
if err := k8sClient.Get(ctx, client.ObjectKeyFromObject(namespace), namespace); err == nil { | ||
Expect(k8sClient.Delete(ctx, namespace)).To(Succeed()) | ||
} | ||
}) | ||
|
||
It("should retrieve the workspace YAML successfully", func() { | ||
req := httptest.NewRequest(http.MethodGet, fmt.Sprintf("/api/v1/workspaces/%s/%s/details/yaml", namespaceName, workspaceKey.Name), http.NoBody) | ||
rr := httptest.NewRecorder() | ||
|
||
ps := httprouter.Params{ | ||
{Key: "namespace", Value: namespaceName}, | ||
{Key: "name", Value: workspaceKey.Name}, | ||
} | ||
|
||
a.GetWorkspaceYAMLHandler(rr, req, ps) | ||
|
||
Expect(rr.Code).To(Equal(http.StatusOK)) | ||
|
||
var response WorkspaceYAMLEnvelope | ||
Expect(json.NewDecoder(rr.Body).Decode(&response)).To(Succeed()) | ||
|
||
Expect(response.Data).To(ContainSubstring(fmt.Sprintf("name: %s", workspaceKey.Name))) | ||
Expect(response.Data).To(ContainSubstring(fmt.Sprintf("namespace: %s", namespaceName))) | ||
}) | ||
|
||
It("should return 404 when workspace doesn't exist", func() { | ||
req := httptest.NewRequest(http.MethodGet, fmt.Sprintf("/api/v1/workspaces/%s/non-existent/details/yaml", namespaceName), http.NoBody) | ||
rr := httptest.NewRecorder() | ||
|
||
ps := httprouter.Params{ | ||
{Key: "namespace", Value: namespaceName}, | ||
{Key: "name", Value: "non-existent"}, | ||
} | ||
|
||
a.GetWorkspaceYAMLHandler(rr, req, ps) | ||
|
||
Expect(rr.Code).To(Equal(http.StatusNotFound)) | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mohamedch7 I would like to confirm with @paulovmr what we are doing here.
As far as I understand, what we are planning for the UI is having a "RAW" YAML representation of the workspace and not the models.Workspace object (that we translate)
@paulovmr can you please confirm that what we want is a way to see the RAW Workspace YAML there?