Skip to content

Commit

Permalink
mathew refactor 3
Browse files Browse the repository at this point in the history
Signed-off-by: Mathew Wicks <[email protected]>
  • Loading branch information
thesuperzapper committed Aug 28, 2024
1 parent f6ca24b commit 4487a2c
Show file tree
Hide file tree
Showing 4 changed files with 491 additions and 144 deletions.
5 changes: 5 additions & 0 deletions workspaces/controller/internal/controller/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ func NewExampleWorkspaceKind1(name string) *kubefloworgv1beta1.WorkspaceKind {
},
Values: []kubefloworgv1beta1.ImageConfigValue{
{
// WARNING: do not change the ID of this value or remove it, it is used in the tests
Id: "jupyterlab_scipy_180",
Spawner: kubefloworgv1beta1.OptionSpawnerInfo{
DisplayName: "jupyter-scipy:v1.8.0",
Expand Down Expand Up @@ -294,6 +295,7 @@ func NewExampleWorkspaceKind1(name string) *kubefloworgv1beta1.WorkspaceKind {
},
},
{
// WARNING: do not change the ID of this value or remove it, it is used in the tests
Id: "jupyterlab_scipy_190",
Spawner: kubefloworgv1beta1.OptionSpawnerInfo{
DisplayName: "jupyter-scipy:v1.9.0",
Expand Down Expand Up @@ -325,6 +327,7 @@ func NewExampleWorkspaceKind1(name string) *kubefloworgv1beta1.WorkspaceKind {
},
Values: []kubefloworgv1beta1.PodConfigValue{
{
// WARNING: do not change the ID of this value or remove it, it is used in the tests
Id: "tiny_cpu",
Spawner: kubefloworgv1beta1.OptionSpawnerInfo{
DisplayName: "Tiny CPU",
Expand All @@ -350,6 +353,7 @@ func NewExampleWorkspaceKind1(name string) *kubefloworgv1beta1.WorkspaceKind {
},
},
{
// WARNING: do not change the ID of this value or remove it, it is used in the tests
Id: "small_cpu",
Spawner: kubefloworgv1beta1.OptionSpawnerInfo{
DisplayName: "Small CPU",
Expand All @@ -375,6 +379,7 @@ func NewExampleWorkspaceKind1(name string) *kubefloworgv1beta1.WorkspaceKind {
},
},
{
// WARNING: do not change the ID of this value or remove it, it is used in the tests
Id: "big_gpu",
Spawner: kubefloworgv1beta1.OptionSpawnerInfo{
DisplayName: "Big GPU",
Expand Down
77 changes: 47 additions & 30 deletions workspaces/controller/internal/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,46 +104,63 @@ func CopyServiceFields(desired *corev1.Service, target *corev1.Service) bool {

// NormalizePodConfigSpec normalizes a PodConfigSpec so that it can be compared with reflect.DeepEqual
func NormalizePodConfigSpec(spec kubefloworgv1beta1.PodConfigSpec) error {
// Normalize Affinity
if spec.Affinity != nil && reflect.DeepEqual(spec.Affinity, corev1.Affinity{}) {
spec.Affinity = nil
}

// Normalize NodeSelector
if spec.NodeSelector != nil && len(spec.NodeSelector) == 0 {
spec.NodeSelector = nil
// normalize Affinity
if spec.Affinity != nil {

// set Affinity to nil if it is empty
if reflect.DeepEqual(spec.Affinity, corev1.Affinity{}) {
spec.Affinity = nil
}
}

// Normalize Tolerations
if spec.Tolerations != nil && len(spec.Tolerations) == 0 {
spec.Tolerations = nil
// normalize NodeSelector
if spec.NodeSelector != nil {

// set NodeSelector to nil if it is empty
if len(spec.NodeSelector) == 0 {
spec.NodeSelector = nil
}
}

// Normalize Resources.Requests
if reflect.DeepEqual(spec.Resources.Requests, corev1.ResourceList{}) {
spec.Resources.Requests = nil
// normalize Tolerations
if spec.Tolerations != nil {

// set Tolerations to nil if it is empty
if len(spec.Tolerations) == 0 {
spec.Tolerations = nil
}
}
if spec.Resources.Requests != nil {
for key, value := range spec.Resources.Requests {
q, err := resource.ParseQuantity(value.String())
if err != nil {
return err

// normalize Resources
if spec.Resources != nil {

// if Resources.Requests is empty, set it to nil
if len(spec.Resources.Requests) == 0 {
spec.Resources.Requests = nil
} else {
// otherwise, normalize the values in Resources.Requests
for key, value := range spec.Resources.Requests {
q, err := resource.ParseQuantity(value.String())
if err != nil {
return err
}
spec.Resources.Requests[key] = q
}
spec.Resources.Requests[key] = q
}
}

// Normalize Resources.Limits
if reflect.DeepEqual(spec.Resources.Limits, corev1.ResourceList{}) {
spec.Resources.Limits = nil
}
if spec.Resources.Limits != nil {
for key, value := range spec.Resources.Limits {
q, err := resource.ParseQuantity(value.String())
if err != nil {
return err
// if Resources.Limits is empty, set it to nil
if len(spec.Resources.Limits) == 0 {
spec.Resources.Limits = nil
} else {
// otherwise, normalize the values in Resources.Limits
for key, value := range spec.Resources.Limits {
q, err := resource.ParseQuantity(value.String())
if err != nil {
return err
}
spec.Resources.Limits[key] = q
}
spec.Resources.Limits[key] = q
}
}

Expand Down
95 changes: 95 additions & 0 deletions workspaces/controller/internal/webhook/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ func NewExampleWorkspaceKind(name string) *kubefloworgv1beta1.WorkspaceKind {
},
Values: []kubefloworgv1beta1.ImageConfigValue{
{
// WARNING: do not change the ID of this value or remove it, it is used in the tests
Id: "jupyterlab_scipy_180",
Spawner: kubefloworgv1beta1.OptionSpawnerInfo{
DisplayName: "jupyter-scipy:v1.8.0",
Expand Down Expand Up @@ -288,6 +289,7 @@ func NewExampleWorkspaceKind(name string) *kubefloworgv1beta1.WorkspaceKind {
},
},
{
// WARNING: do not change the ID of this value or remove it, it is used in the tests
Id: "jupyterlab_scipy_190",
Spawner: kubefloworgv1beta1.OptionSpawnerInfo{
DisplayName: "jupyter-scipy:v1.9.0",
Expand All @@ -311,6 +313,66 @@ func NewExampleWorkspaceKind(name string) *kubefloworgv1beta1.WorkspaceKind {
},
},
},
{
// WARNING: do not change the ID of this value or remove it, it is used in the tests
Id: "redirect_step_1",
Spawner: kubefloworgv1beta1.OptionSpawnerInfo{
DisplayName: "redirect_step_1",
},
Redirect: &kubefloworgv1beta1.OptionRedirect{
To: "redirect_step_2",
},
Spec: kubefloworgv1beta1.ImageConfigSpec{
Image: "redirect-test:step-1",
Ports: []kubefloworgv1beta1.ImagePort{
{
Id: "my_port",
DisplayName: "something",
Port: 1234,
Protocol: "HTTP",
},
},
},
},
{
// WARNING: do not change the ID of this value or remove it, it is used in the tests
Id: "redirect_step_2",
Spawner: kubefloworgv1beta1.OptionSpawnerInfo{
DisplayName: "redirect_step_2",
},
Redirect: &kubefloworgv1beta1.OptionRedirect{
To: "redirect_step_3",
},
Spec: kubefloworgv1beta1.ImageConfigSpec{
Image: "redirect-test:step-2",
Ports: []kubefloworgv1beta1.ImagePort{
{
Id: "my_port",
DisplayName: "something",
Port: 1234,
Protocol: "HTTP",
},
},
},
},
{
// WARNING: do not change the ID of this value or remove it, it is used in the tests
Id: "redirect_step_3",
Spawner: kubefloworgv1beta1.OptionSpawnerInfo{
DisplayName: "redirect_step_3",
},
Spec: kubefloworgv1beta1.ImageConfigSpec{
Image: "redirect-test:step-3",
Ports: []kubefloworgv1beta1.ImagePort{
{
Id: "my_port",
DisplayName: "something",
Port: 1234,
Protocol: "HTTP",
},
},
},
},
},
},
PodConfig: kubefloworgv1beta1.PodConfig{
Expand All @@ -319,6 +381,7 @@ func NewExampleWorkspaceKind(name string) *kubefloworgv1beta1.WorkspaceKind {
},
Values: []kubefloworgv1beta1.PodConfigValue{
{
// WARNING: do not change the ID of this value or remove it, it is used in the tests
Id: "tiny_cpu",
Spawner: kubefloworgv1beta1.OptionSpawnerInfo{
DisplayName: "Tiny CPU",
Expand All @@ -344,6 +407,7 @@ func NewExampleWorkspaceKind(name string) *kubefloworgv1beta1.WorkspaceKind {
},
},
{
// WARNING: do not change the ID of this value or remove it, it is used in the tests
Id: "small_cpu",
Spawner: kubefloworgv1beta1.OptionSpawnerInfo{
DisplayName: "Small CPU",
Expand All @@ -369,6 +433,7 @@ func NewExampleWorkspaceKind(name string) *kubefloworgv1beta1.WorkspaceKind {
},
},
{
// WARNING: do not change the ID of this value or remove it, it is used in the tests
Id: "big_gpu",
Spawner: kubefloworgv1beta1.OptionSpawnerInfo{
DisplayName: "Big GPU",
Expand Down Expand Up @@ -409,6 +474,36 @@ func NewExampleWorkspaceKind(name string) *kubefloworgv1beta1.WorkspaceKind {
},
},
},
{
// WARNING: do not change the ID of this value or remove it, it is used in the tests
Id: "redirect_step_1",
Spawner: kubefloworgv1beta1.OptionSpawnerInfo{
DisplayName: "redirect_step_1",
},
Redirect: &kubefloworgv1beta1.OptionRedirect{
To: "redirect_step_2",
},
Spec: kubefloworgv1beta1.PodConfigSpec{},
},
{
// WARNING: do not change the ID of this value or remove it, it is used in the tests
Id: "redirect_step_2",
Spawner: kubefloworgv1beta1.OptionSpawnerInfo{
DisplayName: "redirect_step_2",
},
Redirect: &kubefloworgv1beta1.OptionRedirect{
To: "redirect_step_3",
},
Spec: kubefloworgv1beta1.PodConfigSpec{},
},
{
// WARNING: do not change the ID of this value or remove it, it is used in the tests
Id: "redirect_step_3",
Spawner: kubefloworgv1beta1.OptionSpawnerInfo{
DisplayName: "redirect_step_3",
},
Spec: kubefloworgv1beta1.PodConfigSpec{},
},
},
},
},
Expand Down
Loading

0 comments on commit 4487a2c

Please sign in to comment.