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

Commit

Permalink
refactor: validate default cc supports default region
Browse files Browse the repository at this point in the history
Signed-off-by: Nick Hale <[email protected]>
  • Loading branch information
njhale committed Feb 16, 2024
1 parent eb9118b commit 48a4fe9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
26 changes: 21 additions & 5 deletions pkg/server/registry/apigroups/acorn/projects/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,34 @@ func (v *Validator) Validate(ctx context.Context, obj runtime.Object) field.Erro
if project.Spec.DefaultRegion != "" &&
!slices.Contains(project.Spec.SupportedRegions, project.Spec.DefaultRegion) &&
!slices.Contains(project.Spec.SupportedRegions, apiv1.AllRegions) {
result = append(result, field.Invalid(field.NewPath("spec", "defaultRegion"), project.Spec.DefaultRegion, "default region is not in the supported regions list"))
result = append(result, field.Invalid(
field.NewPath("spec", "defaultRegion"),
project.Spec.DefaultRegion,
"default region is not in the supported regions list",
))
}

if defaultComputeClass := project.Spec.DefaultComputeClass; defaultComputeClass != "" {
// Check if the default project compute class exists
// Check if the project specified default compute class exists
var cc apiv1.ComputeClass
if err := v.Client.Get(ctx, kclient.ObjectKey{Namespace: project.Name, Name: defaultComputeClass}, &cc); !apierrors.IsNotFound(err) {
switch err := v.Client.Get(ctx, kclient.ObjectKey{Namespace: project.Name, Name: defaultComputeClass}, &cc); {
case !apierrors.IsNotFound(err):
// The compute class does not exist, return an invalid error
result = append(result, field.Invalid(field.NewPath("spec", "defaultComputeClass"), defaultComputeClass, "default compute class does not exist"))
} else if err != nil {
result = append(result, field.Invalid(
field.NewPath("spec", "defaultComputeClass"),
defaultComputeClass,
"default compute class does not exist",
))
case err != nil:
// Some other error occurred while trying to get the compute class, return an internal error.
result = append(result, field.InternalError(field.NewPath("spec", "defaultComputeClass"), err))
case project.Spec.DefaultRegion != "" && !slices.Contains(cc.SupportedRegions, project.Spec.DefaultRegion):
// Compute class does not support the default region
result = append(result, field.Invalid(
field.NewPath("spec", "defaultComputeClass"),
defaultComputeClass,
"default compute class does not support the default region",
))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (s *Strategy) List(ctx context.Context, namespace string, _ storage.ListOpt

for _, ccc := range clusterComputeClasses.Items {
if _, ok := projectComputeClassesSeen[ccc.Name]; ok {
// A project compute class with the same name exists, skipping the project compute class
// A project compute class with the same name exists, skipping the cluster compute class
continue
}
if projectDefaultExists {
Expand Down

0 comments on commit 48a4fe9

Please sign in to comment.