Skip to content

feat: Validate AuthProxyWorkload spec.selector field#209

Merged
hessjcg merged 6 commits intomainfrom
gh-36-validation
Feb 17, 2023
Merged

feat: Validate AuthProxyWorkload spec.selector field#209
hessjcg merged 6 commits intomainfrom
gh-36-validation

Conversation

@hessjcg
Copy link
Copy Markdown
Collaborator

@hessjcg hessjcg commented Feb 14, 2023

Ensure that the workload selector field is valid. Either name or selector must be set, but not both.

Related to #36

@hessjcg hessjcg requested a review from a team as a code owner February 14, 2023 22:10
wantUpdateValid bool
}

data := []*testcase{
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's fine to do it this way, but it's a little more common to just declare an anonymous struct inline like so:

data := []struct{
  desc string
  spec cloudsqlapi.AuthProxyWorkloadSpec
}{
  {
   // ... data here
   }
}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

Comment thread internal/api/v1alpha1/authproxyworkload_test.go Outdated
return nil
}

func (r *AuthProxyWorkload) Validate() error {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're testing through ValidateCreate and ValidateUpdate, does this need to be exported?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably not. I'll hide it.

allErrs = append(allErrs, validation.ValidateLabelName(r.Name, field.NewPath("metadata", "name"))...)
allErrs = append(allErrs, validateSpec(&r.Spec, field.NewPath("spec"))...)

if len(allErrs) == 0 {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be reversed. Indent the error flow.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.


allErrs = append(allErrs, validateWorkload(&spec.Workload, f.Child("workload"))...)

return allErrs
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can just inline allErrs here.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. I got rid of validateSpec() entirely.

@hessjcg hessjcg requested a review from enocom February 17, 2023 00:28
desc string
spec cloudsqlapi.AuthProxyWorkloadSpec
wantCreateValid bool
wantUpdateValid bool
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these used?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed.

{
desc: "happy path labels",
spec: cloudsqlapi.AuthProxyWorkloadSpec{
Workload: cloudsqlapi.WorkloadSelectorSpec{Kind: "Deployment", Selector: &v1.LabelSelector{MatchLabels: map[string]string{"app": "sample"}}},
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: let's try to wrap at 100 or even 80 characters.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

wantUpdateValid: true,
},
{
desc: "invalid labels and name both set",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's try to phrase this as "if name and levels are both set, error" or use some other kind of descriptive message that makes it easy to understand at a glance.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto below

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

wantUpdateValid: true,
},
{
desc: "happy path update",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this test case still belong here?

data := []struct {
desc string
spec cloudsqlapi.AuthProxyWorkloadSpec
wantCreateValid bool
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this just be wantErr?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I simplified to "wantValid", which makes more sense to me when I read the test code.

desc string
spec cloudsqlapi.AuthProxyWorkloadSpec
oldSpec cloudsqlapi.AuthProxyWorkloadSpec
wantCreateValid bool
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this used?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

},
wantCreateValid: true,
wantUpdateValid: true,
},
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add some failure cases here?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Failure cases are coming in future PRs.

Copy link
Copy Markdown
Collaborator Author

@hessjcg hessjcg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated naming semantics in authproxyworkload_test.go

data := []struct {
desc string
spec cloudsqlapi.AuthProxyWorkloadSpec
wantCreateValid bool
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I simplified to "wantValid", which makes more sense to me when I read the test code.

desc string
spec cloudsqlapi.AuthProxyWorkloadSpec
wantCreateValid bool
wantUpdateValid bool
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed.

{
desc: "happy path labels",
spec: cloudsqlapi.AuthProxyWorkloadSpec{
Workload: cloudsqlapi.WorkloadSelectorSpec{Kind: "Deployment", Selector: &v1.LabelSelector{MatchLabels: map[string]string{"app": "sample"}}},
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

},
wantCreateValid: true,
wantUpdateValid: true,
},
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Failure cases are coming in future PRs.

@hessjcg hessjcg requested a review from enocom February 17, 2023 22:42
@hessjcg hessjcg merged commit 98c460b into main Feb 17, 2023
@hessjcg hessjcg deleted the gh-36-validation branch February 17, 2023 22:55
This was referenced Feb 17, 2023
hessjcg pushed a commit that referenced this pull request Feb 21, 2023
Features
- Add new field RolloutStrategy control automatic rollout (#202) (090b88d)
- Add new terraform project for e2e test resources (#181) (0140592)
- Add script to run terraform with input validation. (#182) (857444a)
- Add support for Unix sockets. (#205) (8177a35), closes #47
- Add telemetry settings to configure health check port (#210) (3ede42d)
- Add the e2e test job for Cloud Build (#184) (dc2990c)
- Automatic changes to workloads when an AuthProxyWorload is deleted (#200) (e11caed)
- Automatically trigger pod rollout for appsv1 resources when AuthProxyWorkload changes. (#197) (3b0359b)
- Separate terraform for project setup and permissions (#179) (8f43657)
- Validate AuthProxyWorkload spec.selector field (#209) (98c460b)
- Validate AuthProxyWorkload updates to prevent changes to the workload selector. (#211) (4304283)

Co-authored-by: release-please[bot] <55107282+release-please[bot]@users.noreply.github.com>
Co-authored-by: Release PR Generate Bot action release-please[bot] <release-please[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants