Skip to content

Commit 7421ad0

Browse files
author
rafriat
committed
feat(ws): Notebooks 2.0 // Backend // add count to workspacekind schema response
Signed-off-by: rafriat <[email protected]>
1 parent f23af69 commit 7421ad0

File tree

6 files changed

+73
-4
lines changed

6 files changed

+73
-4
lines changed

workspaces/backend/api/suite_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,5 +454,34 @@ func NewExampleWorkspaceKind(name string) *kubefloworgv1beta1.WorkspaceKind {
454454
},
455455
},
456456
},
457+
Status: kubefloworgv1beta1.WorkspaceKindStatus{
458+
Workspaces: 1,
459+
PodTemplateOptions: kubefloworgv1beta1.PodTemplateOptionsMetrics{
460+
ImageConfig: []kubefloworgv1beta1.OptionMetric{
461+
{
462+
Id: "jupyterlab_scipy_180",
463+
Workspaces: 1,
464+
},
465+
{
466+
Id: "jupyterlab_scipy_190",
467+
Workspaces: 0,
468+
},
469+
},
470+
PodConfig: []kubefloworgv1beta1.OptionMetric{
471+
{
472+
Id: "tiny_cpu",
473+
Workspaces: 1,
474+
},
475+
{
476+
Id: "small_cpu",
477+
Workspaces: 0,
478+
},
479+
{
480+
Id: "big_gpu",
481+
Workspaces: 0,
482+
},
483+
},
484+
},
485+
},
457486
}
458487
}

workspaces/backend/internal/models/workspacekinds/funcs.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ func NewWorkspaceKindModelFromWorkspaceKind(wsk *kubefloworgv1beta1.WorkspaceKin
3636
podAnnotations[k] = v
3737
}
3838
}
39+
statusImageConfigMap := buildOptionMetricsMap(wsk.Status.PodTemplateOptions.ImageConfig)
40+
statusPodConfigMap := buildOptionMetricsMap(wsk.Status.PodTemplateOptions.PodConfig)
3941

4042
// TODO: icons can either be a remote URL or read from a ConfigMap.
4143
// in BOTH cases, we should cache and serve the image under a path on the backend API:
@@ -60,6 +62,7 @@ func NewWorkspaceKindModelFromWorkspaceKind(wsk *kubefloworgv1beta1.WorkspaceKin
6062
Hidden: ptr.Deref(wsk.Spec.Spawner.Hidden, false),
6163
Icon: iconRef,
6264
Logo: logoRef,
65+
Workspaces: wsk.Status.Workspaces,
6366
PodTemplate: PodTemplate{
6467
PodMetadata: PodMetadata{
6568
Labels: podLabels,
@@ -71,18 +74,26 @@ func NewWorkspaceKindModelFromWorkspaceKind(wsk *kubefloworgv1beta1.WorkspaceKin
7174
Options: PodTemplateOptions{
7275
ImageConfig: ImageConfig{
7376
Default: wsk.Spec.PodTemplate.Options.ImageConfig.Spawner.Default,
74-
Values: buildImageConfigValues(wsk.Spec.PodTemplate.Options.ImageConfig),
77+
Values: buildImageConfigValues(wsk.Spec.PodTemplate.Options.ImageConfig, statusImageConfigMap),
7578
},
7679
PodConfig: PodConfig{
7780
Default: wsk.Spec.PodTemplate.Options.PodConfig.Spawner.Default,
78-
Values: buildPodConfigValues(wsk.Spec.PodTemplate.Options.PodConfig),
81+
Values: buildPodConfigValues(wsk.Spec.PodTemplate.Options.PodConfig, statusPodConfigMap),
7982
},
8083
},
8184
},
8285
}
8386
}
8487

85-
func buildImageConfigValues(imageConfig kubefloworgv1beta1.ImageConfig) []ImageConfigValue {
88+
func buildOptionMetricsMap(metrics []kubefloworgv1beta1.OptionMetric) map[string]int32 {
89+
resultMap := make(map[string]int32)
90+
for _, metric := range metrics {
91+
resultMap[metric.Id] = metric.Workspaces
92+
}
93+
return resultMap
94+
}
95+
96+
func buildImageConfigValues(imageConfig kubefloworgv1beta1.ImageConfig, statusImageConfigMap map[string]int32) []ImageConfigValue {
8697
imageConfigValues := make([]ImageConfigValue, len(imageConfig.Values))
8798
for i := range imageConfig.Values {
8899
option := imageConfig.Values[i]
@@ -93,12 +104,13 @@ func buildImageConfigValues(imageConfig kubefloworgv1beta1.ImageConfig) []ImageC
93104
Labels: buildOptionLabels(option.Spawner.Labels),
94105
Hidden: ptr.Deref(option.Spawner.Hidden, false),
95106
Redirect: buildOptionRedirect(option.Redirect),
107+
Workspaces: statusImageConfigMap[option.Id],
96108
}
97109
}
98110
return imageConfigValues
99111
}
100112

101-
func buildPodConfigValues(podConfig kubefloworgv1beta1.PodConfig) []PodConfigValue {
113+
func buildPodConfigValues(podConfig kubefloworgv1beta1.PodConfig, statusPodConfigMap map[string]int32) []PodConfigValue {
102114
podConfigValues := make([]PodConfigValue, len(podConfig.Values))
103115
for i := range podConfig.Values {
104116
option := podConfig.Values[i]
@@ -109,6 +121,7 @@ func buildPodConfigValues(podConfig kubefloworgv1beta1.PodConfig) []PodConfigVal
109121
Labels: buildOptionLabels(option.Spawner.Labels),
110122
Hidden: ptr.Deref(option.Spawner.Hidden, false),
111123
Redirect: buildOptionRedirect(option.Redirect),
124+
Workspaces: statusPodConfigMap[option.Id],
112125
}
113126
}
114127
return podConfigValues

workspaces/backend/internal/models/workspacekinds/types.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type WorkspaceKind struct {
2525
Hidden bool `json:"hidden"`
2626
Icon ImageRef `json:"icon"`
2727
Logo ImageRef `json:"logo"`
28+
Workspaces int32 `json:"workspacesCount"`
2829
PodTemplate PodTemplate `json:"podTemplate"`
2930
}
3031

@@ -64,6 +65,7 @@ type ImageConfigValue struct {
6465
Labels []OptionLabel `json:"labels"`
6566
Hidden bool `json:"hidden"`
6667
Redirect *OptionRedirect `json:"redirect,omitempty"`
68+
Workspaces int32 `json:"workspacesCount"`
6769
}
6870

6971
type PodConfig struct {
@@ -78,6 +80,7 @@ type PodConfigValue struct {
7880
Labels []OptionLabel `json:"labels"`
7981
Hidden bool `json:"hidden"`
8082
Redirect *OptionRedirect `json:"redirect,omitempty"`
83+
Workspaces int32 `json:"workspacesCount"`
8184
}
8285

8386
type OptionLabel struct {

workspaces/backend/openapi/docs.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,9 @@ const docTemplate = `{
740740
},
741741
"redirect": {
742742
"$ref": "#/definitions/workspacekinds.OptionRedirect"
743+
},
744+
"workspacesCount": {
745+
"type": "integer"
743746
}
744747
}
745748
},
@@ -810,6 +813,9 @@ const docTemplate = `{
810813
},
811814
"redirect": {
812815
"$ref": "#/definitions/workspacekinds.OptionRedirect"
816+
},
817+
"workspacesCount": {
818+
"type": "integer"
813819
}
814820
}
815821
},
@@ -916,6 +922,9 @@ const docTemplate = `{
916922
},
917923
"podTemplate": {
918924
"$ref": "#/definitions/workspacekinds.PodTemplate"
925+
},
926+
"workspacesCount": {
927+
"type": "integer"
919928
}
920929
}
921930
},

workspaces/backend/openapi/swagger.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,9 @@
738738
},
739739
"redirect": {
740740
"$ref": "#/definitions/workspacekinds.OptionRedirect"
741+
},
742+
"workspacesCount": {
743+
"type": "integer"
741744
}
742745
}
743746
},
@@ -808,6 +811,9 @@
808811
},
809812
"redirect": {
810813
"$ref": "#/definitions/workspacekinds.OptionRedirect"
814+
},
815+
"workspacesCount": {
816+
"type": "integer"
811817
}
812818
}
813819
},
@@ -914,6 +920,9 @@
914920
},
915921
"podTemplate": {
916922
"$ref": "#/definitions/workspacekinds.PodTemplate"
923+
},
924+
"workspacesCount": {
925+
"type": "integer"
917926
}
918927
}
919928
},

workspaces/backend/openapi/swagger.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ definitions:
140140
type: array
141141
redirect:
142142
$ref: '#/definitions/workspacekinds.OptionRedirect'
143+
workspacesCount:
144+
type: integer
143145
type: object
144146
workspacekinds.ImageRef:
145147
properties:
@@ -185,6 +187,8 @@ definitions:
185187
type: array
186188
redirect:
187189
$ref: '#/definitions/workspacekinds.OptionRedirect'
190+
workspacesCount:
191+
type: integer
188192
type: object
189193
workspacekinds.PodMetadata:
190194
properties:
@@ -255,6 +259,8 @@ definitions:
255259
type: string
256260
podTemplate:
257261
$ref: '#/definitions/workspacekinds.PodTemplate'
262+
workspacesCount:
263+
type: integer
258264
type: object
259265
workspaces.Activity:
260266
properties:

0 commit comments

Comments
 (0)