@@ -18,7 +18,6 @@ package workspaces
1818
1919import (
2020 "fmt"
21- "path"
2221
2322 kubefloworgv1beta1 "github.com/kubeflow/notebooks/workspaces/controller/api/v1beta1"
2423 "k8s.io/utils/ptr"
@@ -90,6 +89,9 @@ func NewWorkspaceModelFromWorkspace(ws *kubefloworgv1beta1.Workspace, wsk *kubef
9089 }
9190 }
9291
92+ imageConfigModel , imageConfigValue := buildImageConfig (ws , wsk )
93+ podConfigModel , _ := buildPodConfig (ws , wsk )
94+
9395 workspaceModel := Workspace {
9496 Name : ws .Name ,
9597 Namespace : ws .Namespace ,
@@ -114,8 +116,8 @@ func NewWorkspaceModelFromWorkspace(ws *kubefloworgv1beta1.Workspace, wsk *kubef
114116 Data : dataVolumes ,
115117 },
116118 Options : PodTemplateOptions {
117- ImageConfig : buildImageConfig ( ws , wsk ) ,
118- PodConfig : buildPodConfig ( ws , wsk ) ,
119+ ImageConfig : imageConfigModel ,
120+ PodConfig : podConfigModel ,
119121 },
120122 },
121123 Activity : Activity {
@@ -125,51 +127,11 @@ func NewWorkspaceModelFromWorkspace(ws *kubefloworgv1beta1.Workspace, wsk *kubef
125127 // https://github.com/kubeflow/notebooks/issues/38
126128 LastProbe : nil ,
127129 },
128- Services : buildServicesList (ws , wsk ),
130+ Services : buildServices (ws , imageConfigValue ),
129131 }
130132 return workspaceModel
131133}
132134
133- func buildServicesList (ws * kubefloworgv1beta1.Workspace , wsk * kubefloworgv1beta1.WorkspaceKind ) []Service {
134- if ! wskExists (wsk ) {
135- return nil
136- }
137-
138- imageConfig := wsk .Spec .PodTemplate .Options .ImageConfig
139- basePath := "/workspace"
140- namespacePath := path .Join (basePath , ws .Namespace , ws .Name )
141-
142- services := make ([]Service , 0 , len (imageConfig .Values ))
143-
144- for _ , val := range imageConfig .Values {
145- services = append (services , extractServices (& val , namespacePath )... )
146- }
147- return services
148- }
149-
150- func extractServices (val * kubefloworgv1beta1.ImageConfigValue , namespacePath string ) []Service {
151- if len (val .Spec .Ports ) == 0 {
152- return []Service {}
153- }
154-
155- services := make ([]Service , 0 , len (val .Spec .Ports ))
156-
157- for _ , port := range val .Spec .Ports {
158- displayName := port .DisplayName
159- if displayName == "" {
160- displayName = val .Id
161- }
162- service := Service {
163- HttpService : & HttpService {
164- DisplayName : displayName ,
165- HttpPath : path .Join (namespacePath , port .Id ),
166- },
167- }
168- services = append (services , service )
169- }
170- return services
171- }
172-
173135func wskExists (wsk * kubefloworgv1beta1.WorkspaceKind ) bool {
174136 return wsk != nil && wsk .UID != ""
175137}
@@ -193,7 +155,7 @@ func buildHomeVolume(ws *kubefloworgv1beta1.Workspace, wsk *kubefloworgv1beta1.W
193155 }
194156}
195157
196- func buildImageConfig (ws * kubefloworgv1beta1.Workspace , wsk * kubefloworgv1beta1.WorkspaceKind ) ImageConfig {
158+ func buildImageConfig (ws * kubefloworgv1beta1.Workspace , wsk * kubefloworgv1beta1.WorkspaceKind ) ( ImageConfig , * kubefloworgv1beta1. ImageConfigValue ) {
197159 // create a map of image configs from the WorkspaceKind for easy lookup by ID
198160 // NOTE: we can only build this map if the WorkspaceKind exists, otherwise it will be empty
199161 imageConfigMap := make (map [string ]kubefloworgv1beta1.ImageConfigValue )
@@ -205,13 +167,15 @@ func buildImageConfig(ws *kubefloworgv1beta1.Workspace, wsk *kubefloworgv1beta1.
205167 }
206168
207169 // get the current image config
170+ var currentImageConfigValue * kubefloworgv1beta1.ImageConfigValue
208171 currentImageConfig := OptionInfo {
209172 Id : ws .Spec .PodTemplate .Options .ImageConfig ,
210173 DisplayName : UnknownImageConfig ,
211174 Description : UnknownImageConfig ,
212175 Labels : nil ,
213176 }
214177 if cfg , ok := imageConfigMap [currentImageConfig .Id ]; ok {
178+ currentImageConfigValue = & cfg
215179 currentImageConfig .DisplayName = cfg .Spawner .DisplayName
216180 currentImageConfig .Description = ptr .Deref (cfg .Spawner .Description , "" )
217181 currentImageConfig .Labels = buildOptionLabels (cfg .Spawner .Labels )
@@ -260,10 +224,10 @@ func buildImageConfig(ws *kubefloworgv1beta1.Workspace, wsk *kubefloworgv1beta1.
260224 Current : currentImageConfig ,
261225 Desired : desiredImageConfig ,
262226 RedirectChain : redirectChain ,
263- }
227+ }, currentImageConfigValue
264228}
265229
266- func buildPodConfig (ws * kubefloworgv1beta1.Workspace , wsk * kubefloworgv1beta1.WorkspaceKind ) PodConfig {
230+ func buildPodConfig (ws * kubefloworgv1beta1.Workspace , wsk * kubefloworgv1beta1.WorkspaceKind ) ( PodConfig , * kubefloworgv1beta1. PodConfigValue ) {
267231 // create a map of pod configs from the WorkspaceKind for easy lookup by ID
268232 // NOTE: we can only build this map if the WorkspaceKind exists, otherwise it will be empty
269233 podConfigMap := make (map [string ]kubefloworgv1beta1.PodConfigValue )
@@ -275,13 +239,15 @@ func buildPodConfig(ws *kubefloworgv1beta1.Workspace, wsk *kubefloworgv1beta1.Wo
275239 }
276240
277241 // get the current pod config
242+ var currentPodConfigValue * kubefloworgv1beta1.PodConfigValue
278243 currentPodConfig := OptionInfo {
279244 Id : ws .Spec .PodTemplate .Options .PodConfig ,
280245 DisplayName : UnknownPodConfig ,
281246 Description : UnknownPodConfig ,
282247 Labels : nil ,
283248 }
284249 if cfg , ok := podConfigMap [currentPodConfig .Id ]; ok {
250+ currentPodConfigValue = & cfg
285251 currentPodConfig .DisplayName = cfg .Spawner .DisplayName
286252 currentPodConfig .Description = ptr .Deref (cfg .Spawner .Description , "" )
287253 currentPodConfig .Labels = buildOptionLabels (cfg .Spawner .Labels )
@@ -330,7 +296,7 @@ func buildPodConfig(ws *kubefloworgv1beta1.Workspace, wsk *kubefloworgv1beta1.Wo
330296 Current : currentPodConfig ,
331297 Desired : desiredPodConfig ,
332298 RedirectChain : redirectChain ,
333- }
299+ }, currentPodConfigValue
334300}
335301
336302func buildOptionLabels (labels []kubefloworgv1beta1.OptionSpawnerLabel ) []OptionLabel {
@@ -364,3 +330,23 @@ func buildRedirectMessage(msg *kubefloworgv1beta1.RedirectMessage) *RedirectMess
364330 Level : messageLevel ,
365331 }
366332}
333+
334+ func buildServices (ws * kubefloworgv1beta1.Workspace , imageConfigValue * kubefloworgv1beta1.ImageConfigValue ) []Service {
335+ if imageConfigValue == nil {
336+ return nil
337+ }
338+
339+ services := make ([]Service , len (imageConfigValue .Spec .Ports ))
340+ for i := range imageConfigValue .Spec .Ports {
341+ port := imageConfigValue .Spec .Ports [i ]
342+ switch port .Protocol { //nolint:gocritic
343+ case kubefloworgv1beta1 .ImagePortProtocolHTTP :
344+ services [i ].HttpService = & HttpService {
345+ DisplayName : port .DisplayName ,
346+ HttpPath : fmt .Sprintf ("/workspace/%s/%s/%s/" , ws .Namespace , ws .Name , port .Id ),
347+ }
348+ }
349+ }
350+
351+ return services
352+ }
0 commit comments