Skip to content
Open
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
11 changes: 10 additions & 1 deletion workspaces/backend/internal/models/workspaces/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func NewWorkspaceListItemFromWorkspace(cfg *config.EnvConfig, ws *kubefloworgv1b
Paused: ptr.Deref(ws.Spec.Paused, false),
PausedTime: ws.Status.PauseTime,
PendingRestart: ws.Status.PendingRestart,
State: ws.Status.State,
State: defaultWorkspaceState(ws.Status.State),
StateMessage: ws.Status.StateMessage,
PodTemplate: PodTemplate{
PodMetadata: PodMetadata{
Expand Down Expand Up @@ -119,6 +119,15 @@ func wskExists(wsk *kubefloworgv1beta1.WorkspaceKind) bool {
return wsk != nil && wsk.UID != ""
}

// defaultWorkspaceState returns WorkspaceStateUnknown when the controller has not
// yet reconciled a workspace's status, so API consumers never see an empty state.
func defaultWorkspaceState(state kubefloworgv1beta1.WorkspaceState) kubefloworgv1beta1.WorkspaceState {
if state == "" {
return kubefloworgv1beta1.WorkspaceStateUnknown
}
return state
}

func buildHomeVolume(ws *kubefloworgv1beta1.Workspace, wsk *kubefloworgv1beta1.WorkspaceKind) *PodVolumeInfo {
if ws.Spec.PodTemplate.Volumes.Home == nil {
return nil
Expand Down
50 changes: 50 additions & 0 deletions workspaces/backend/internal/models/workspaces/funcs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
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 workspaces

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

kubefloworgv1beta1 "github.com/kubeflow/notebooks/workspaces/controller/api/v1beta1"
)

func TestWorkspacesModels(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Workspaces Models Suite")
}

var _ = Describe("defaultWorkspaceState", func() {
It("returns WorkspaceStateUnknown for the empty string", func() {
Expect(defaultWorkspaceState("")).To(Equal(kubefloworgv1beta1.WorkspaceStateUnknown))
})

It("returns the original state when it is already set", func() {
for _, state := range []kubefloworgv1beta1.WorkspaceState{
kubefloworgv1beta1.WorkspaceStateRunning,
kubefloworgv1beta1.WorkspaceStateTerminating,
kubefloworgv1beta1.WorkspaceStatePaused,
kubefloworgv1beta1.WorkspaceStatePending,
kubefloworgv1beta1.WorkspaceStateError,
kubefloworgv1beta1.WorkspaceStateUnknown,
} {
Expect(defaultWorkspaceState(state)).To(Equal(state))
}
})
})
Loading