Skip to content

feat(ws): add http service paths to WS get on backend #213

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

Merged
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
40 changes: 34 additions & 6 deletions workspaces/backend/internal/models/workspaces/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ func NewWorkspaceModelFromWorkspace(ws *kubefloworgv1beta1.Workspace, wsk *kubef
}
}

imageConfigModel, imageConfigValue := buildImageConfig(ws, wsk)
podConfigModel, _ := buildPodConfig(ws, wsk)

workspaceModel := Workspace{
Name: ws.Name,
Namespace: ws.Namespace,
Expand All @@ -113,8 +116,8 @@ func NewWorkspaceModelFromWorkspace(ws *kubefloworgv1beta1.Workspace, wsk *kubef
Data: dataVolumes,
},
Options: PodTemplateOptions{
ImageConfig: buildImageConfig(ws, wsk),
PodConfig: buildPodConfig(ws, wsk),
ImageConfig: imageConfigModel,
PodConfig: podConfigModel,
},
},
Activity: Activity{
Expand All @@ -124,6 +127,7 @@ func NewWorkspaceModelFromWorkspace(ws *kubefloworgv1beta1.Workspace, wsk *kubef
// https://github.com/kubeflow/notebooks/issues/38
LastProbe: nil,
},
Services: buildServices(ws, imageConfigValue),
}
return workspaceModel
}
Expand Down Expand Up @@ -151,7 +155,7 @@ func buildHomeVolume(ws *kubefloworgv1beta1.Workspace, wsk *kubefloworgv1beta1.W
}
}

func buildImageConfig(ws *kubefloworgv1beta1.Workspace, wsk *kubefloworgv1beta1.WorkspaceKind) ImageConfig {
func buildImageConfig(ws *kubefloworgv1beta1.Workspace, wsk *kubefloworgv1beta1.WorkspaceKind) (ImageConfig, *kubefloworgv1beta1.ImageConfigValue) {
// create a map of image configs from the WorkspaceKind for easy lookup by ID
// NOTE: we can only build this map if the WorkspaceKind exists, otherwise it will be empty
imageConfigMap := make(map[string]kubefloworgv1beta1.ImageConfigValue)
Expand All @@ -163,13 +167,15 @@ func buildImageConfig(ws *kubefloworgv1beta1.Workspace, wsk *kubefloworgv1beta1.
}

// get the current image config
var currentImageConfigValue *kubefloworgv1beta1.ImageConfigValue
currentImageConfig := OptionInfo{
Id: ws.Spec.PodTemplate.Options.ImageConfig,
DisplayName: UnknownImageConfig,
Description: UnknownImageConfig,
Labels: nil,
}
if cfg, ok := imageConfigMap[currentImageConfig.Id]; ok {
currentImageConfigValue = &cfg
currentImageConfig.DisplayName = cfg.Spawner.DisplayName
currentImageConfig.Description = ptr.Deref(cfg.Spawner.Description, "")
currentImageConfig.Labels = buildOptionLabels(cfg.Spawner.Labels)
Expand Down Expand Up @@ -218,10 +224,10 @@ func buildImageConfig(ws *kubefloworgv1beta1.Workspace, wsk *kubefloworgv1beta1.
Current: currentImageConfig,
Desired: desiredImageConfig,
RedirectChain: redirectChain,
}
}, currentImageConfigValue
}

func buildPodConfig(ws *kubefloworgv1beta1.Workspace, wsk *kubefloworgv1beta1.WorkspaceKind) PodConfig {
func buildPodConfig(ws *kubefloworgv1beta1.Workspace, wsk *kubefloworgv1beta1.WorkspaceKind) (PodConfig, *kubefloworgv1beta1.PodConfigValue) {
// create a map of pod configs from the WorkspaceKind for easy lookup by ID
// NOTE: we can only build this map if the WorkspaceKind exists, otherwise it will be empty
podConfigMap := make(map[string]kubefloworgv1beta1.PodConfigValue)
Expand All @@ -233,13 +239,15 @@ func buildPodConfig(ws *kubefloworgv1beta1.Workspace, wsk *kubefloworgv1beta1.Wo
}

// get the current pod config
var currentPodConfigValue *kubefloworgv1beta1.PodConfigValue
currentPodConfig := OptionInfo{
Id: ws.Spec.PodTemplate.Options.PodConfig,
DisplayName: UnknownPodConfig,
Description: UnknownPodConfig,
Labels: nil,
}
if cfg, ok := podConfigMap[currentPodConfig.Id]; ok {
currentPodConfigValue = &cfg
currentPodConfig.DisplayName = cfg.Spawner.DisplayName
currentPodConfig.Description = ptr.Deref(cfg.Spawner.Description, "")
currentPodConfig.Labels = buildOptionLabels(cfg.Spawner.Labels)
Expand Down Expand Up @@ -288,7 +296,7 @@ func buildPodConfig(ws *kubefloworgv1beta1.Workspace, wsk *kubefloworgv1beta1.Wo
Current: currentPodConfig,
Desired: desiredPodConfig,
RedirectChain: redirectChain,
}
}, currentPodConfigValue
}

func buildOptionLabels(labels []kubefloworgv1beta1.OptionSpawnerLabel) []OptionLabel {
Expand Down Expand Up @@ -322,3 +330,23 @@ func buildRedirectMessage(msg *kubefloworgv1beta1.RedirectMessage) *RedirectMess
Level: messageLevel,
}
}

func buildServices(ws *kubefloworgv1beta1.Workspace, imageConfigValue *kubefloworgv1beta1.ImageConfigValue) []Service {
if imageConfigValue == nil {
return nil
}

services := make([]Service, len(imageConfigValue.Spec.Ports))
for i := range imageConfigValue.Spec.Ports {
port := imageConfigValue.Spec.Ports[i]
switch port.Protocol { //nolint:gocritic
case kubefloworgv1beta1.ImagePortProtocolHTTP:
services[i].HttpService = &HttpService{
DisplayName: port.DisplayName,
HttpPath: fmt.Sprintf("/workspace/%s/%s/%s/", ws.Namespace, ws.Name, port.Id),
}
}
}

return services
}
10 changes: 10 additions & 0 deletions workspaces/backend/internal/models/workspaces/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Workspace struct {
StateMessage string `json:"stateMessage"`
PodTemplate PodTemplate `json:"podTemplate"`
Activity Activity `json:"activity"`
Services []Service `json:"services"`
}

type WorkspaceState string
Expand Down Expand Up @@ -143,3 +144,12 @@ const (
ProbeResultFailure ProbeResult = "Failure"
ProbeResultTimeout ProbeResult = "Timeout"
)

type Service struct {
HttpService *HttpService `json:"httpService,omitempty"`
}

type HttpService struct {
DisplayName string `json:"displayName"`
HttpPath string `json:"httpPath"`
}
Loading