Skip to content
This repository has been archived by the owner on Mar 16, 2024. It is now read-only.

Commit

Permalink
enhance: set project default cc status field
Browse files Browse the repository at this point in the history
Signed-off-by: Nick Hale <[email protected]>
  • Loading branch information
njhale committed Feb 20, 2024
1 parent d161899 commit f051f14
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 43 deletions.
20 changes: 15 additions & 5 deletions pkg/project/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/acorn-io/baaah/pkg/router"
apiv1 "github.com/acorn-io/runtime/pkg/apis/api.acorn.io/v1"
v1 "github.com/acorn-io/runtime/pkg/apis/internal.acorn.io/v1"
internaladminv1 "github.com/acorn-io/runtime/pkg/apis/internal.admin.acorn.io/v1"
"github.com/acorn-io/runtime/pkg/computeclasses"
"github.com/acorn-io/runtime/pkg/labels"
corev1 "k8s.io/api/core/v1"
Expand All @@ -18,26 +19,35 @@ import (

// SetDefaultComputeClass sets the default compute class status field of a [v1.ProjectInstance] to the value of its spec
// field if set.
func SetDefaultComputeClass(req router.Request, resp router.Response) error {
func SetDefaultComputeClass(req router.Request, _ router.Response) error {
project := req.Object.(*v1.ProjectInstance)
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.Name, project.Status.DefaultComputeClass); err != nil {
if computeClassName := project.Status.DefaultComputeClass; computeClassName != "" {
if _, err := computeclasses.GetAsProjectComputeClassInstance(req.Ctx, req.Client, project.Name, computeClassName); 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)
return fmt.Errorf("failed to check existence of default compute class [%s] specified by project [%s] status: %w", computeClassName, project.Name, err)
}

// The compute class does not exist, clear the status field.
project.Status.DefaultComputeClass = ""
}
}

resp.Objects(req.Object)
// Pick a default from the available compute classes
if project.Status.DefaultComputeClass == "" {
computeClassName, err := internaladminv1.GetDefaultComputeClassName(req.Ctx, req.Client, project.Name)
if kclient.IgnoreNotFound(err) != nil {
return fmt.Errorf("failed to get default compute class for project [%s]: %w", project.Name, err)
}

project.Status.DefaultComputeClass = computeClassName
}

return nil
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1 @@
`apiVersion: internal.acorn.io/v1
kind: ProjectInstance
metadata:
creationTimestamp: null
name: acorn
spec:
defaultComputeClass: compute-class-a
status: {}
`
""
Original file line number Diff line number Diff line change
@@ -1,10 +1 @@
`apiVersion: internal.acorn.io/v1
kind: ProjectInstance
metadata:
creationTimestamp: null
name: acorn
spec:
defaultComputeClass: cluster-compute-class-b
status:
defaultComputeClass: cluster-compute-class-b
`
""
Original file line number Diff line number Diff line change
@@ -1,10 +1 @@
`apiVersion: internal.acorn.io/v1
kind: ProjectInstance
metadata:
creationTimestamp: null
name: acorn
spec:
defaultComputeClass: compute-class-a
status:
defaultComputeClass: compute-class-a
`
""
Original file line number Diff line number Diff line change
@@ -1,9 +1 @@
`apiVersion: internal.acorn.io/v1
kind: ProjectInstance
metadata:
creationTimestamp: null
name: acorn
spec: {}
status:
defaultComputeClass: cluster-compute-class-b
`
""

0 comments on commit f051f14

Please sign in to comment.