diff --git a/pkg/apis/internal.admin.acorn.io/v1/default.go b/pkg/apis/internal.admin.acorn.io/v1/default.go index afc27a444..2ac2c5bdb 100644 --- a/pkg/apis/internal.admin.acorn.io/v1/default.go +++ b/pkg/apis/internal.admin.acorn.io/v1/default.go @@ -33,7 +33,7 @@ func getCurrentClusterComputeClassDefault(ctx context.Context, c client.Client, defaultCCC = z.Pointer(clusterComputeClass) } - if clusterComputeClass.Name == projectDefault { + if projectDefault != "" && clusterComputeClass.Name == projectDefault { projectDefaultCCC = z.Pointer(clusterComputeClass) } } @@ -68,7 +68,7 @@ func getCurrentProjectComputeClassDefault(ctx context.Context, c client.Client, defaultPCC = z.Pointer(projectComputeClass) } - if projectDefault projectComputeClass.Name == projectDefault { + if projectDefault != "" && projectComputeClass.Name == projectDefault { projectDefaultPCC = z.Pointer(projectComputeClass) } } diff --git a/pkg/controller/resolvedofferings/testdata/computeclass/acornfile-override-compute-class/expected.golden b/pkg/controller/resolvedofferings/testdata/computeclass/acornfile-override-compute-class/expected.golden index 8a9683ce7..40315968a 100644 --- a/pkg/controller/resolvedofferings/testdata/computeclass/acornfile-override-compute-class/expected.golden +++ b/pkg/controller/resolvedofferings/testdata/computeclass/acornfile-override-compute-class/expected.golden @@ -49,6 +49,7 @@ status: resolvedOfferings: containers: "": + class: sample-compute-class memory: 0 left: class: sample-compute-class diff --git a/pkg/controller/resolvedofferings/testdata/computeclass/compute-class-default/expected.golden b/pkg/controller/resolvedofferings/testdata/computeclass/compute-class-default/expected.golden index cfa85853d..3b46bd188 100644 --- a/pkg/controller/resolvedofferings/testdata/computeclass/compute-class-default/expected.golden +++ b/pkg/controller/resolvedofferings/testdata/computeclass/compute-class-default/expected.golden @@ -48,6 +48,7 @@ status: resolvedOfferings: containers: "": + class: sample-compute-class memory: 0 left: class: sample-compute-class diff --git a/pkg/controller/resolvedofferings/testdata/computeclass/user-override-compute-class/expected.golden b/pkg/controller/resolvedofferings/testdata/computeclass/user-override-compute-class/expected.golden index 389cdb743..bcb2ab7b6 100644 --- a/pkg/controller/resolvedofferings/testdata/computeclass/user-override-compute-class/expected.golden +++ b/pkg/controller/resolvedofferings/testdata/computeclass/user-override-compute-class/expected.golden @@ -51,6 +51,7 @@ status: resolvedOfferings: containers: "": + class: sample-compute-class memory: 0 left: class: sample-compute-class diff --git a/pkg/controller/scheduling/testdata/computeclass/invalid-priority-class-should-error/existing.yaml b/pkg/controller/scheduling/testdata/computeclass/invalid-priority-class-should-error/existing.yaml index 5ab6e1dbc..be82320b7 100644 --- a/pkg/controller/scheduling/testdata/computeclass/invalid-priority-class-should-error/existing.yaml +++ b/pkg/controller/scheduling/testdata/computeclass/invalid-priority-class-should-error/existing.yaml @@ -7,3 +7,8 @@ description: Simple description for a simple ComputeClass cpuScaler: 0.25 default: true priorityClassName: does-not-exist +--- +kind: ProjectInstance +apiVersion: internal.acorn.io/v1 +metadata: + name: app-namespace diff --git a/pkg/controller/scheduling/testdata/computeclass/priority-class/existing.yaml b/pkg/controller/scheduling/testdata/computeclass/priority-class/existing.yaml index a3935b547..a19cd9a1d 100644 --- a/pkg/controller/scheduling/testdata/computeclass/priority-class/existing.yaml +++ b/pkg/controller/scheduling/testdata/computeclass/priority-class/existing.yaml @@ -13,3 +13,8 @@ description: Simple description for a simple ComputeClass cpuScaler: 0.25 default: true priorityClassName: exists +--- +kind: ProjectInstance +apiVersion: internal.acorn.io/v1 +metadata: + name: app-namespace diff --git a/pkg/controller/scheduling/testdata/computeclass/two-ccc-defaults-should-error/existing.yaml b/pkg/controller/scheduling/testdata/computeclass/two-ccc-defaults-should-error/existing.yaml index 01356068e..71a186d0b 100644 --- a/pkg/controller/scheduling/testdata/computeclass/two-ccc-defaults-should-error/existing.yaml +++ b/pkg/controller/scheduling/testdata/computeclass/two-ccc-defaults-should-error/existing.yaml @@ -39,4 +39,9 @@ affinity: - key: foo operator: In values: - - bar \ No newline at end of file + - bar +--- +kind: ProjectInstance +apiVersion: internal.acorn.io/v1 +metadata: + name: app-namespace diff --git a/pkg/controller/scheduling/testdata/computeclass/two-containers/existing.yaml b/pkg/controller/scheduling/testdata/computeclass/two-containers/existing.yaml index a99056da4..5a96f3370 100644 --- a/pkg/controller/scheduling/testdata/computeclass/two-containers/existing.yaml +++ b/pkg/controller/scheduling/testdata/computeclass/two-containers/existing.yaml @@ -17,4 +17,9 @@ affinity: - key: foo operator: In values: - - bar \ No newline at end of file + - bar +--- +kind: ProjectInstance +apiVersion: internal.acorn.io/v1 +metadata: + name: app-namespace diff --git a/pkg/controller/scheduling/testdata/computeclass/two-pcc-defaults-should-error/existing.yaml b/pkg/controller/scheduling/testdata/computeclass/two-pcc-defaults-should-error/existing.yaml index 7c0c87d2b..264a3f943 100644 --- a/pkg/controller/scheduling/testdata/computeclass/two-pcc-defaults-should-error/existing.yaml +++ b/pkg/controller/scheduling/testdata/computeclass/two-pcc-defaults-should-error/existing.yaml @@ -41,4 +41,9 @@ affinity: - key: foo operator: In values: - - bar \ No newline at end of file + - bar +--- +kind: ProjectInstance +apiVersion: internal.acorn.io/v1 +metadata: + name: app-namespace diff --git a/pkg/controller/scheduling/testdata/memory/all-set-overwrite/existing.yaml b/pkg/controller/scheduling/testdata/memory/all-set-overwrite/existing.yaml new file mode 100644 index 000000000..95a9b5ad5 --- /dev/null +++ b/pkg/controller/scheduling/testdata/memory/all-set-overwrite/existing.yaml @@ -0,0 +1,5 @@ +--- +kind: ProjectInstance +apiVersion: internal.acorn.io/v1 +metadata: + name: app-namespace diff --git a/pkg/controller/scheduling/testdata/memory/all-set/existing.yaml b/pkg/controller/scheduling/testdata/memory/all-set/existing.yaml new file mode 100644 index 000000000..95a9b5ad5 --- /dev/null +++ b/pkg/controller/scheduling/testdata/memory/all-set/existing.yaml @@ -0,0 +1,5 @@ +--- +kind: ProjectInstance +apiVersion: internal.acorn.io/v1 +metadata: + name: app-namespace diff --git a/pkg/controller/scheduling/testdata/memory/container/existing.yaml b/pkg/controller/scheduling/testdata/memory/container/existing.yaml new file mode 100644 index 000000000..95a9b5ad5 --- /dev/null +++ b/pkg/controller/scheduling/testdata/memory/container/existing.yaml @@ -0,0 +1,5 @@ +--- +kind: ProjectInstance +apiVersion: internal.acorn.io/v1 +metadata: + name: app-namespace diff --git a/pkg/controller/scheduling/testdata/memory/job/existing.yaml b/pkg/controller/scheduling/testdata/memory/job/existing.yaml new file mode 100644 index 000000000..95a9b5ad5 --- /dev/null +++ b/pkg/controller/scheduling/testdata/memory/job/existing.yaml @@ -0,0 +1,5 @@ +--- +kind: ProjectInstance +apiVersion: internal.acorn.io/v1 +metadata: + name: app-namespace diff --git a/pkg/controller/scheduling/testdata/memory/overwrite-acornfile-memory/existing.yaml b/pkg/controller/scheduling/testdata/memory/overwrite-acornfile-memory/existing.yaml new file mode 100644 index 000000000..95a9b5ad5 --- /dev/null +++ b/pkg/controller/scheduling/testdata/memory/overwrite-acornfile-memory/existing.yaml @@ -0,0 +1,5 @@ +--- +kind: ProjectInstance +apiVersion: internal.acorn.io/v1 +metadata: + name: app-namespace diff --git a/pkg/controller/scheduling/testdata/memory/removed-container/existing.yaml b/pkg/controller/scheduling/testdata/memory/removed-container/existing.yaml new file mode 100644 index 000000000..95a9b5ad5 --- /dev/null +++ b/pkg/controller/scheduling/testdata/memory/removed-container/existing.yaml @@ -0,0 +1,5 @@ +--- +kind: ProjectInstance +apiVersion: internal.acorn.io/v1 +metadata: + name: app-namespace diff --git a/pkg/controller/scheduling/testdata/memory/sidecar/existing.yaml b/pkg/controller/scheduling/testdata/memory/sidecar/existing.yaml new file mode 100644 index 000000000..95a9b5ad5 --- /dev/null +++ b/pkg/controller/scheduling/testdata/memory/sidecar/existing.yaml @@ -0,0 +1,5 @@ +--- +kind: ProjectInstance +apiVersion: internal.acorn.io/v1 +metadata: + name: app-namespace diff --git a/pkg/controller/scheduling/testdata/memory/two-containers/existing.yaml b/pkg/controller/scheduling/testdata/memory/two-containers/existing.yaml new file mode 100644 index 000000000..95a9b5ad5 --- /dev/null +++ b/pkg/controller/scheduling/testdata/memory/two-containers/existing.yaml @@ -0,0 +1,5 @@ +--- +kind: ProjectInstance +apiVersion: internal.acorn.io/v1 +metadata: + name: app-namespace diff --git a/pkg/controller/scheduling/testdata/memory/with-acornfile-memory/existing.yaml b/pkg/controller/scheduling/testdata/memory/with-acornfile-memory/existing.yaml new file mode 100644 index 000000000..95a9b5ad5 --- /dev/null +++ b/pkg/controller/scheduling/testdata/memory/with-acornfile-memory/existing.yaml @@ -0,0 +1,5 @@ +--- +kind: ProjectInstance +apiVersion: internal.acorn.io/v1 +metadata: + name: app-namespace diff --git a/pkg/controller/scheduling/testdata/tolerations/container/existing.yaml b/pkg/controller/scheduling/testdata/tolerations/container/existing.yaml new file mode 100644 index 000000000..95a9b5ad5 --- /dev/null +++ b/pkg/controller/scheduling/testdata/tolerations/container/existing.yaml @@ -0,0 +1,5 @@ +--- +kind: ProjectInstance +apiVersion: internal.acorn.io/v1 +metadata: + name: app-namespace diff --git a/pkg/controller/scheduling/testdata/tolerations/job/existing.yaml b/pkg/controller/scheduling/testdata/tolerations/job/existing.yaml new file mode 100644 index 000000000..95a9b5ad5 --- /dev/null +++ b/pkg/controller/scheduling/testdata/tolerations/job/existing.yaml @@ -0,0 +1,5 @@ +--- +kind: ProjectInstance +apiVersion: internal.acorn.io/v1 +metadata: + name: app-namespace diff --git a/pkg/controller/scheduling/testdata/tolerations/removed-container/existing.yaml b/pkg/controller/scheduling/testdata/tolerations/removed-container/existing.yaml new file mode 100644 index 000000000..95a9b5ad5 --- /dev/null +++ b/pkg/controller/scheduling/testdata/tolerations/removed-container/existing.yaml @@ -0,0 +1,5 @@ +--- +kind: ProjectInstance +apiVersion: internal.acorn.io/v1 +metadata: + name: app-namespace diff --git a/pkg/project/handlers.go b/pkg/project/handlers.go index 8a4edcebe..59e5a6a00 100644 --- a/pkg/project/handlers.go +++ b/pkg/project/handlers.go @@ -20,15 +20,14 @@ import ( // field if set. func SetDefaultComputeClass(req router.Request, resp router.Response) error { project := req.Object.(*v1.ProjectInstance) - if cc := project.Spec.DefaultComputeClass; cc != "" && - project.Status.DefaultComputeClass != cc { + if cc := project.Spec.DefaultComputeClass; cc != "" && project.Status.DefaultComputeClass != cc { // The spec has been changed, update the status field to match. project.Status.DefaultComputeClass = cc } // Check if the given compute class exists if project.Status.DefaultComputeClass != "" { - if _, err := computeclasses.GetAsProjectComputeClassInstance(req.Ctx, req.Client, project.Status.Namespace, project.Status.DefaultComputeClass); err != nil { + if _, err := computeclasses.GetAsProjectComputeClassInstance(req.Ctx, req.Client, project.Name, project.Status.DefaultComputeClass); err != nil { if !apierrors.IsNotFound(err) { return fmt.Errorf("failed to check existence of default compute class on project [%s] status: %w", project.Name, err) } diff --git a/pkg/project/handlers_test.go b/pkg/project/handlers_test.go index 390008eb9..ca00d4eb8 100644 --- a/pkg/project/handlers_test.go +++ b/pkg/project/handlers_test.go @@ -14,6 +14,13 @@ import ( kclient "sigs.k8s.io/controller-runtime/pkg/client" ) +func TestDefaultComputeClass(t *testing.T) { + tester.DefaultTest(t, scheme.Scheme, "testdata/setdefaultcomputeclass/cc-missing", SetDefaultComputeClass) + tester.DefaultTest(t, scheme.Scheme, "testdata/setdefaultcomputeclass/spec-only", SetDefaultComputeClass) + tester.DefaultTest(t, scheme.Scheme, "testdata/setdefaultcomputeclass/status-only", SetDefaultComputeClass) + tester.DefaultTest(t, scheme.Scheme, "testdata/setdefaultcomputeclass/spec-override", SetDefaultComputeClass) +} + func TestSetProjectSupportedRegions(t *testing.T) { tester.DefaultTest(t, scheme.Scheme, "testdata/setsupportedregions/no-default", SetSupportedRegions) tester.DefaultTest(t, scheme.Scheme, "testdata/setsupportedregions/with-supported-regions", SetSupportedRegions) diff --git a/pkg/project/testdata/setdefaultcomputeclass/cc-missing/expected.golden b/pkg/project/testdata/setdefaultcomputeclass/cc-missing/expected.golden new file mode 100644 index 000000000..7e6e9f223 --- /dev/null +++ b/pkg/project/testdata/setdefaultcomputeclass/cc-missing/expected.golden @@ -0,0 +1,9 @@ +`apiVersion: internal.acorn.io/v1 +kind: ProjectInstance +metadata: + creationTimestamp: null + name: acorn +spec: + defaultComputeClass: compute-class-a +status: {} +` diff --git a/pkg/project/testdata/setdefaultcomputeclass/cc-missing/input.yaml b/pkg/project/testdata/setdefaultcomputeclass/cc-missing/input.yaml new file mode 100644 index 000000000..5ee4c7923 --- /dev/null +++ b/pkg/project/testdata/setdefaultcomputeclass/cc-missing/input.yaml @@ -0,0 +1,8 @@ +kind: ProjectInstance +apiVersion: internal.acorn.io/v1 +metadata: + name: acorn +spec: + defaultComputeClass: compute-class-a +status: + defaultComputeClass: compute-class-a diff --git a/pkg/project/testdata/setdefaultcomputeclass/spec-only/existing.yaml b/pkg/project/testdata/setdefaultcomputeclass/spec-only/existing.yaml new file mode 100644 index 000000000..4235c9a1d --- /dev/null +++ b/pkg/project/testdata/setdefaultcomputeclass/spec-only/existing.yaml @@ -0,0 +1,44 @@ +--- +kind: ProjectComputeClassInstance +apiVersion: internal.admin.acorn.io/v1 +metadata: + name: compute-class-a + namespace: acorn +cpuScaler: 0.25 +memory: + min: 1Mi # 1Mi + max: 2Mi # 2Mi + default: 1Mi # 1Mi +affinity: + nodeAffinity: + requiredDuringSchedulingIgnoredDuringExecution: + nodeSelectorTerms: + - matchExpressions: + - key: foo + operator: In + values: + - bar +--- +kind: ClusterComputeClassInstance +apiVersion: internal.admin.acorn.io/v1 +metadata: + name: cluster-compute-class-b +cpuScaler: 0.25 +memory: + min: 1Mi # 1Mi + max: 2Mi # 2Mi + default: 1Mi # 1Mi +affinity: + nodeAffinity: + requiredDuringSchedulingIgnoredDuringExecution: + nodeSelectorTerms: + - matchExpressions: + - key: foo + operator: In + values: + - bar +--- +kind: ProjectInstance +apiVersion: internal.acorn.io/v1 +metadata: + name: acorn diff --git a/pkg/project/testdata/setdefaultcomputeclass/spec-only/expected.golden b/pkg/project/testdata/setdefaultcomputeclass/spec-only/expected.golden new file mode 100644 index 000000000..3f68667c2 --- /dev/null +++ b/pkg/project/testdata/setdefaultcomputeclass/spec-only/expected.golden @@ -0,0 +1,10 @@ +`apiVersion: internal.acorn.io/v1 +kind: ProjectInstance +metadata: + creationTimestamp: null + name: acorn +spec: + defaultComputeClass: cluster-compute-class-b +status: + defaultComputeClass: cluster-compute-class-b +` diff --git a/pkg/project/testdata/setdefaultcomputeclass/spec-only/input.yaml b/pkg/project/testdata/setdefaultcomputeclass/spec-only/input.yaml new file mode 100644 index 000000000..c8710faa4 --- /dev/null +++ b/pkg/project/testdata/setdefaultcomputeclass/spec-only/input.yaml @@ -0,0 +1,6 @@ +kind: ProjectInstance +apiVersion: internal.acorn.io/v1 +metadata: + name: acorn +spec: + defaultComputeClass: cluster-compute-class-b \ No newline at end of file diff --git a/pkg/project/testdata/setdefaultcomputeclass/spec-override/existing.yaml b/pkg/project/testdata/setdefaultcomputeclass/spec-override/existing.yaml new file mode 100644 index 000000000..d133b89df --- /dev/null +++ b/pkg/project/testdata/setdefaultcomputeclass/spec-override/existing.yaml @@ -0,0 +1,46 @@ +--- +kind: ProjectComputeClassInstance +apiVersion: internal.admin.acorn.io/v1 +metadata: + name: compute-class-a + namespace: acorn +cpuScaler: 0.25 +memory: + min: 1Mi # 1Mi + max: 2Mi # 2Mi + default: 1Mi # 1Mi +affinity: + nodeAffinity: + requiredDuringSchedulingIgnoredDuringExecution: + nodeSelectorTerms: + - matchExpressions: + - key: foo + operator: In + values: + - bar +--- +kind: ClusterComputeClassInstance +apiVersion: internal.admin.acorn.io/v1 +metadata: + name: cluster-compute-class-b +cpuScaler: 0.25 +memory: + min: 1Mi # 1Mi + max: 2Mi # 2Mi + default: 1Mi # 1Mi +affinity: + nodeAffinity: + requiredDuringSchedulingIgnoredDuringExecution: + nodeSelectorTerms: + - matchExpressions: + - key: foo + operator: In + values: + - bar +--- +kind: ProjectInstance +apiVersion: internal.acorn.io/v1 +metadata: + name: acorn +status: + defaultComputeClass: cluster-compute-class-b diff --git a/pkg/project/testdata/setdefaultcomputeclass/spec-override/expected.golden b/pkg/project/testdata/setdefaultcomputeclass/spec-override/expected.golden new file mode 100644 index 000000000..6f21e24c7 --- /dev/null +++ b/pkg/project/testdata/setdefaultcomputeclass/spec-override/expected.golden @@ -0,0 +1,10 @@ +`apiVersion: internal.acorn.io/v1 +kind: ProjectInstance +metadata: + creationTimestamp: null + name: acorn +spec: + defaultComputeClass: compute-class-a +status: + defaultComputeClass: compute-class-a +` diff --git a/pkg/project/testdata/setdefaultcomputeclass/spec-override/input.yaml b/pkg/project/testdata/setdefaultcomputeclass/spec-override/input.yaml new file mode 100644 index 000000000..dd099e2cb --- /dev/null +++ b/pkg/project/testdata/setdefaultcomputeclass/spec-override/input.yaml @@ -0,0 +1,6 @@ +kind: ProjectInstance +apiVersion: internal.acorn.io/v1 +metadata: + name: acorn +spec: + defaultComputeClass: compute-class-a \ No newline at end of file diff --git a/pkg/project/testdata/setdefaultcomputeclass/status-only/existing.yaml b/pkg/project/testdata/setdefaultcomputeclass/status-only/existing.yaml new file mode 100644 index 000000000..d133b89df --- /dev/null +++ b/pkg/project/testdata/setdefaultcomputeclass/status-only/existing.yaml @@ -0,0 +1,46 @@ +--- +kind: ProjectComputeClassInstance +apiVersion: internal.admin.acorn.io/v1 +metadata: + name: compute-class-a + namespace: acorn +cpuScaler: 0.25 +memory: + min: 1Mi # 1Mi + max: 2Mi # 2Mi + default: 1Mi # 1Mi +affinity: + nodeAffinity: + requiredDuringSchedulingIgnoredDuringExecution: + nodeSelectorTerms: + - matchExpressions: + - key: foo + operator: In + values: + - bar +--- +kind: ClusterComputeClassInstance +apiVersion: internal.admin.acorn.io/v1 +metadata: + name: cluster-compute-class-b +cpuScaler: 0.25 +memory: + min: 1Mi # 1Mi + max: 2Mi # 2Mi + default: 1Mi # 1Mi +affinity: + nodeAffinity: + requiredDuringSchedulingIgnoredDuringExecution: + nodeSelectorTerms: + - matchExpressions: + - key: foo + operator: In + values: + - bar +--- +kind: ProjectInstance +apiVersion: internal.acorn.io/v1 +metadata: + name: acorn +status: + defaultComputeClass: cluster-compute-class-b diff --git a/pkg/project/testdata/setdefaultcomputeclass/status-only/expected.golden b/pkg/project/testdata/setdefaultcomputeclass/status-only/expected.golden new file mode 100644 index 000000000..fa1c251fb --- /dev/null +++ b/pkg/project/testdata/setdefaultcomputeclass/status-only/expected.golden @@ -0,0 +1,9 @@ +`apiVersion: internal.acorn.io/v1 +kind: ProjectInstance +metadata: + creationTimestamp: null + name: acorn +spec: {} +status: + defaultComputeClass: cluster-compute-class-b +` diff --git a/pkg/project/testdata/setdefaultcomputeclass/status-only/input.yaml b/pkg/project/testdata/setdefaultcomputeclass/status-only/input.yaml new file mode 100644 index 000000000..ac18a5ecf --- /dev/null +++ b/pkg/project/testdata/setdefaultcomputeclass/status-only/input.yaml @@ -0,0 +1,6 @@ +kind: ProjectInstance +apiVersion: internal.acorn.io/v1 +metadata: + name: acorn +status: + defaultComputeClass: cluster-compute-class-b \ No newline at end of file