Skip to content

Commit 0db1fd7

Browse files
roee1313rafriat
andauthored
feat(ws): add ws counts to backend wsk model (#368)
Signed-off-by: rafriat <[email protected]> Co-authored-by: rafriat <[email protected]>
1 parent f3fc2e2 commit 0db1fd7

File tree

5 files changed

+117
-25
lines changed

5 files changed

+117
-25
lines changed

workspaces/backend/api/suite_test.go

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

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

Lines changed: 26 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,10 @@ func NewWorkspaceKindModelFromWorkspaceKind(wsk *kubefloworgv1beta1.WorkspaceKin
6062
Hidden: ptr.Deref(wsk.Spec.Spawner.Hidden, false),
6163
Icon: iconRef,
6264
Logo: logoRef,
65+
// TODO: in the future will need to support including exactly one of clusterMetrics or namespaceMetrics based on request context
66+
ClusterMetrics: clusterMetrics{
67+
Workspaces: wsk.Status.Workspaces,
68+
},
6369
PodTemplate: PodTemplate{
6470
PodMetadata: PodMetadata{
6571
Labels: podLabels,
@@ -71,18 +77,26 @@ func NewWorkspaceKindModelFromWorkspaceKind(wsk *kubefloworgv1beta1.WorkspaceKin
7177
Options: PodTemplateOptions{
7278
ImageConfig: ImageConfig{
7379
Default: wsk.Spec.PodTemplate.Options.ImageConfig.Spawner.Default,
74-
Values: buildImageConfigValues(wsk.Spec.PodTemplate.Options.ImageConfig),
80+
Values: buildImageConfigValues(wsk.Spec.PodTemplate.Options.ImageConfig, statusImageConfigMap),
7581
},
7682
PodConfig: PodConfig{
7783
Default: wsk.Spec.PodTemplate.Options.PodConfig.Spawner.Default,
78-
Values: buildPodConfigValues(wsk.Spec.PodTemplate.Options.PodConfig),
84+
Values: buildPodConfigValues(wsk.Spec.PodTemplate.Options.PodConfig, statusPodConfigMap),
7985
},
8086
},
8187
},
8288
}
8389
}
8490

85-
func buildImageConfigValues(imageConfig kubefloworgv1beta1.ImageConfig) []ImageConfigValue {
91+
func buildOptionMetricsMap(metrics []kubefloworgv1beta1.OptionMetric) map[string]int32 {
92+
resultMap := make(map[string]int32)
93+
for _, metric := range metrics {
94+
resultMap[metric.Id] = metric.Workspaces
95+
}
96+
return resultMap
97+
}
98+
99+
func buildImageConfigValues(imageConfig kubefloworgv1beta1.ImageConfig, statusImageConfigMap map[string]int32) []ImageConfigValue {
86100
imageConfigValues := make([]ImageConfigValue, len(imageConfig.Values))
87101
for i := range imageConfig.Values {
88102
option := imageConfig.Values[i]
@@ -93,12 +107,16 @@ func buildImageConfigValues(imageConfig kubefloworgv1beta1.ImageConfig) []ImageC
93107
Labels: buildOptionLabels(option.Spawner.Labels),
94108
Hidden: ptr.Deref(option.Spawner.Hidden, false),
95109
Redirect: buildOptionRedirect(option.Redirect),
110+
// TODO: in the future will need to support including exactly one of clusterMetrics or namespaceMetrics based on request context
111+
ClusterMetrics: clusterMetrics{
112+
Workspaces: statusImageConfigMap[option.Id],
113+
},
96114
}
97115
}
98116
return imageConfigValues
99117
}
100118

101-
func buildPodConfigValues(podConfig kubefloworgv1beta1.PodConfig) []PodConfigValue {
119+
func buildPodConfigValues(podConfig kubefloworgv1beta1.PodConfig, statusPodConfigMap map[string]int32) []PodConfigValue {
102120
podConfigValues := make([]PodConfigValue, len(podConfig.Values))
103121
for i := range podConfig.Values {
104122
option := podConfig.Values[i]
@@ -109,6 +127,10 @@ func buildPodConfigValues(podConfig kubefloworgv1beta1.PodConfig) []PodConfigVal
109127
Labels: buildOptionLabels(option.Spawner.Labels),
110128
Hidden: ptr.Deref(option.Spawner.Hidden, false),
111129
Redirect: buildOptionRedirect(option.Redirect),
130+
// TODO: in the future will need to support including exactly one of clusterMetrics or namespaceMetrics based on request context
131+
ClusterMetrics: clusterMetrics{
132+
Workspaces: statusPodConfigMap[option.Id],
133+
},
112134
}
113135
}
114136
return podConfigValues

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

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,20 @@ limitations under the License.
1717
package workspacekinds
1818

1919
type WorkspaceKind struct {
20-
Name string `json:"name"`
21-
DisplayName string `json:"displayName"`
22-
Description string `json:"description"`
23-
Deprecated bool `json:"deprecated"`
24-
DeprecationMessage string `json:"deprecationMessage"`
25-
Hidden bool `json:"hidden"`
26-
Icon ImageRef `json:"icon"`
27-
Logo ImageRef `json:"logo"`
28-
PodTemplate PodTemplate `json:"podTemplate"`
20+
Name string `json:"name"`
21+
DisplayName string `json:"displayName"`
22+
Description string `json:"description"`
23+
Deprecated bool `json:"deprecated"`
24+
DeprecationMessage string `json:"deprecationMessage"`
25+
Hidden bool `json:"hidden"`
26+
Icon ImageRef `json:"icon"`
27+
Logo ImageRef `json:"logo"`
28+
ClusterMetrics clusterMetrics `json:"clusterMetrics,omitempty"`
29+
PodTemplate PodTemplate `json:"podTemplate"`
30+
}
31+
32+
type clusterMetrics struct {
33+
Workspaces int32 `json:"workspacesCount"`
2934
}
3035

3136
type ImageRef struct {
@@ -58,12 +63,13 @@ type ImageConfig struct {
5863
}
5964

6065
type ImageConfigValue struct {
61-
Id string `json:"id"`
62-
DisplayName string `json:"displayName"`
63-
Description string `json:"description"`
64-
Labels []OptionLabel `json:"labels"`
65-
Hidden bool `json:"hidden"`
66-
Redirect *OptionRedirect `json:"redirect,omitempty"`
66+
Id string `json:"id"`
67+
DisplayName string `json:"displayName"`
68+
Description string `json:"description"`
69+
Labels []OptionLabel `json:"labels"`
70+
Hidden bool `json:"hidden"`
71+
Redirect *OptionRedirect `json:"redirect,omitempty"`
72+
ClusterMetrics clusterMetrics `json:"clusterMetrics,omitempty"`
6773
}
6874

6975
type PodConfig struct {
@@ -72,12 +78,13 @@ type PodConfig struct {
7278
}
7379

7480
type PodConfigValue struct {
75-
Id string `json:"id"`
76-
DisplayName string `json:"displayName"`
77-
Description string `json:"description"`
78-
Labels []OptionLabel `json:"labels"`
79-
Hidden bool `json:"hidden"`
80-
Redirect *OptionRedirect `json:"redirect,omitempty"`
81+
Id string `json:"id"`
82+
DisplayName string `json:"displayName"`
83+
Description string `json:"description"`
84+
Labels []OptionLabel `json:"labels"`
85+
Hidden bool `json:"hidden"`
86+
Redirect *OptionRedirect `json:"redirect,omitempty"`
87+
ClusterMetrics clusterMetrics `json:"clusterMetrics,omitempty"`
8188
}
8289

8390
type OptionLabel struct {

workspaces/backend/openapi/docs.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,9 @@ const docTemplate = `{
778778
"workspacekinds.ImageConfigValue": {
779779
"type": "object",
780780
"properties": {
781+
"clusterMetrics": {
782+
"$ref": "#/definitions/workspacekinds.clusterMetrics"
783+
},
781784
"description": {
782785
"type": "string"
783786
},
@@ -848,6 +851,9 @@ const docTemplate = `{
848851
"workspacekinds.PodConfigValue": {
849852
"type": "object",
850853
"properties": {
854+
"clusterMetrics": {
855+
"$ref": "#/definitions/workspacekinds.clusterMetrics"
856+
},
851857
"description": {
852858
"type": "string"
853859
},
@@ -948,6 +954,9 @@ const docTemplate = `{
948954
"workspacekinds.WorkspaceKind": {
949955
"type": "object",
950956
"properties": {
957+
"clusterMetrics": {
958+
"$ref": "#/definitions/workspacekinds.clusterMetrics"
959+
},
951960
"deprecated": {
952961
"type": "boolean"
953962
},
@@ -977,6 +986,14 @@ const docTemplate = `{
977986
}
978987
}
979988
},
989+
"workspacekinds.clusterMetrics": {
990+
"type": "object",
991+
"properties": {
992+
"workspacesCount": {
993+
"type": "integer"
994+
}
995+
}
996+
},
980997
"workspaces.Activity": {
981998
"type": "object",
982999
"properties": {

workspaces/backend/openapi/swagger.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,9 @@
776776
"workspacekinds.ImageConfigValue": {
777777
"type": "object",
778778
"properties": {
779+
"clusterMetrics": {
780+
"$ref": "#/definitions/workspacekinds.clusterMetrics"
781+
},
779782
"description": {
780783
"type": "string"
781784
},
@@ -846,6 +849,9 @@
846849
"workspacekinds.PodConfigValue": {
847850
"type": "object",
848851
"properties": {
852+
"clusterMetrics": {
853+
"$ref": "#/definitions/workspacekinds.clusterMetrics"
854+
},
849855
"description": {
850856
"type": "string"
851857
},
@@ -946,6 +952,9 @@
946952
"workspacekinds.WorkspaceKind": {
947953
"type": "object",
948954
"properties": {
955+
"clusterMetrics": {
956+
"$ref": "#/definitions/workspacekinds.clusterMetrics"
957+
},
949958
"deprecated": {
950959
"type": "boolean"
951960
},
@@ -975,6 +984,14 @@
975984
}
976985
}
977986
},
987+
"workspacekinds.clusterMetrics": {
988+
"type": "object",
989+
"properties": {
990+
"workspacesCount": {
991+
"type": "integer"
992+
}
993+
}
994+
},
978995
"workspaces.Activity": {
979996
"type": "object",
980997
"properties": {

0 commit comments

Comments
 (0)