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

Commit

Permalink
Export function for object existence in a project region
Browse files Browse the repository at this point in the history
The function is being exported so that it can be used in other packages
that need similar functionality. A test is also added for an untested
edge case.

Signed-off-by: Donnie Adams <[email protected]>
  • Loading branch information
thedadams committed Aug 14, 2023
1 parent a0d7930 commit 45dcb32
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
12 changes: 6 additions & 6 deletions pkg/server/registry/apigroups/acorn/projects/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ func (v *Validator) ValidateUpdate(ctx context.Context, newObj, _ runtime.Object
}

// If the user is removing a supported region, ensure that there are no apps in that region.
return v.ensureNoObjectsExistOutsideOfRegions(ctx, newProject.Name, newProject.Spec.SupportedRegions, &apiv1.AppList{}, &apiv1.VolumeList{})
return EnsureNoObjectsExistOutsideOfRegions(ctx, v.Client, newProject.Name, newProject.Spec.SupportedRegions, &apiv1.AppList{}, &apiv1.VolumeList{})
}

func (v *Validator) ensureNoObjectsExistOutsideOfRegions(ctx context.Context, namespace string, regions []string, objList ...kclient.ObjectList) field.ErrorList {
func EnsureNoObjectsExistOutsideOfRegions(ctx context.Context, client kclient.Client, namespace string, regions []string, objList ...kclient.ObjectList) field.ErrorList {
var result field.ErrorList
for _, obj := range objList {
var removedRegions, inRemovedRegion []string
if err := v.Client.List(ctx, obj, kclient.InNamespace(namespace)); err != nil {
if err := client.List(ctx, obj, kclient.InNamespace(namespace)); err != nil {
return field.ErrorList{field.InternalError(field.NewPath("spec", "supportedRegions"), err)}
}

Expand All @@ -76,7 +76,7 @@ func (v *Validator) ensureNoObjectsExistOutsideOfRegions(ctx context.Context, na
fmt.Sprintf(
"cannot remove regions %v while in use by the following %ss: %v",
removedRegions,
v.resource(obj),
resource(client, obj),
inRemovedRegion,
),
))
Expand All @@ -86,10 +86,10 @@ func (v *Validator) ensureNoObjectsExistOutsideOfRegions(ctx context.Context, na
return result
}

func (v *Validator) resource(obj runtime.Object) string {
func resource(client kclient.Client, obj runtime.Object) string {
kind := obj.GetObjectKind().GroupVersionKind().Kind
if kind == "" {
gvks, _, _ := v.Client.Scheme().ObjectKinds(obj)
gvks, _, _ := client.Scheme().ObjectKinds(obj)
if len(gvks) < 1 {
// Kind unknown
return "resource"
Expand Down
40 changes: 40 additions & 0 deletions pkg/server/registry/apigroups/acorn/projects/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,46 @@ func TestProjectUpdateValidation(t *testing.T) {
},
).Build(),
},
{
name: "Update project to remove supported region that was defaulted, but app exists in removed region",
wantError: true,
oldProject: apiv1.Project{
ObjectMeta: metav1.ObjectMeta{
Name: "my-project",
},
Status: v1.ProjectInstanceStatus{
DefaultRegion: "my-region",
SupportedRegions: []string{"my-region", "my-other-region"},
},
},
newProject: apiv1.Project{
ObjectMeta: metav1.ObjectMeta{
Name: "my-project",
},
Spec: v1.ProjectInstanceSpec{
SupportedRegions: []string{"my-region"},
},
Status: v1.ProjectInstanceStatus{
DefaultRegion: "my-region",
SupportedRegions: []string{"my-region", "my-other-region"},
},
},
client: fake.NewClientBuilder().WithScheme(scheme.Scheme).WithLists(
&apiv1.AppList{
Items: []apiv1.App{
{
ObjectMeta: metav1.ObjectMeta{
Name: "my-app",
Namespace: "my-project",
},
Spec: v1.AppInstanceSpec{
Region: "my-other-region",
},
},
},
},
).Build(),
},
}

for _, tt := range tests {
Expand Down

0 comments on commit 45dcb32

Please sign in to comment.