Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Validate requestRedirect with backendrefs #2161

Merged

Conversation

slayer321
Copy link
Contributor

@slayer321 slayer321 commented Jul 2, 2023

What type of PR is this?

/kind bug
/kind test
What this PR does / why we need it:
Validate and Prevent Redirect Filters and BackendRefs in same HTTPRoute Rules.
Which issue(s) this PR fixes:

Fixes #2148

Does this PR introduce a user-facing change?:

Webhook validation now ensures that BackendRefs can not be specified in the same HTTPRoute rule as a Redirect filter

@k8s-ci-robot k8s-ci-robot added kind/bug Categorizes issue or PR as related to a bug. do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. kind/test labels Jul 2, 2023
@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Jul 2, 2023
@k8s-ci-robot
Copy link
Contributor

Welcome @slayer321!

It looks like this is your first PR to kubernetes-sigs/gateway-api 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-sigs/gateway-api has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot
Copy link
Contributor

Hi @slayer321. Thanks for your PR.

I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@k8s-ci-robot k8s-ci-robot added needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Jul 2, 2023
@gyohuangxin
Copy link
Member

/ok-to-test

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Jul 3, 2023
@gyohuangxin
Copy link
Member

gyohuangxin commented Jul 3, 2023

@slayer321 The job failed due to a error:

./hack/invalid-examples/v1beta1/httproute/invalid-request-redirect-with-backendref.yaml
  16:19     error    no new line character at the end of file  (new-line-at-end-of-file)
[0;31mTest FAILED: hack/../hack/verify-yamllint.sh 
make: *** [Makefile:114: verify] Error 1

Copy link
Member

@gyohuangxin gyohuangxin left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@@ -868,7 +868,7 @@ func TestValidateHTTPRouteTypeMatchesField(t *testing.T) {
StatusCode: new(int),
},
},
errCount: 0,
errCount: 1,
Copy link
Contributor

Choose a reason for hiding this comment

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

why has this changed ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The same reason that I mentioned for v1beta1 changes.

Copy link
Member

Choose a reason for hiding this comment

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

@slayer321 do you happen to have a link handy to that comment or can you just copy it here? Not finding the context easily and also have the same question.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was taking about this comment #2161 (comment)

@@ -1049,7 +1075,7 @@ func TestValidateHTTPRouteTypeMatchesField(t *testing.T) {
StatusCode: new(int),
},
},
errCount: 0,
errCount: 1,
Copy link
Contributor

Choose a reason for hiding this comment

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

why has this changed ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So, as in this issue we need to validate that BackendRefs is not used when Redirect Filters are used in httpRoute.
In this example as we can see that we are using Redirect Filters and then in the for loops we have BackendRefs as well.

After I added a check of it inside validation/httproute.go. The test started failing so I made the errCount as 1 .

Copy link
Contributor

Choose a reason for hiding this comment

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

ah thanks for the clarification, didnt go through at the entire file earlier 🙈
would be good to add a positive test case, maybe as a new standalone function that ensures thats when the Redirect filter exists and no backendRefs are set, validation is successful

Copy link
Contributor Author

Choose a reason for hiding this comment

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

maybe I didn't get it. so for new we already have a standalone function which checks if Redirect filter exists then no backendRefs should be set.

Copy link
Contributor

Choose a reason for hiding this comment

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

was referring to a standalone alone test TestValidateRequestRedirectFiltersWithNoBackendRef
anyways this is a good to have and is non blocking

Copy link
Contributor Author

Choose a reason for hiding this comment

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

make sense. I have added standalone test as you mentioned for both the cases where backendref is added and not added.

var errs field.ErrorList
for _, filter := range rule.Filters {
if filter.RequestRedirect != nil && len(rule.BackendRefs) > 0 {
errs = append(errs, field.Invalid(path.Child("filters"), filter, "RequestRedirect filter is not allowed with backendRefs"))
Copy link
Contributor

Choose a reason for hiding this comment

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

is filter, needed ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If you are asking if filter needed inside the field.Invalid function. So yeah I think we can change to gatewayv1b1.HTTPRouteFilterRequestRedirect .. as it will be more specific.

errs = append(errs, field.Invalid(path.Child("filters"), gatewayv1b1.HTTPRouteFilterRequestRedirect, "RequestRedirect filter is not allowed with backendRefs"))

Copy link
Contributor

Choose a reason for hiding this comment

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

thanks, yeah +1 to this change

Copy link
Contributor

@arkodg arkodg left a comment

Choose a reason for hiding this comment

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

LGTM thanks !

@slayer321
Copy link
Contributor Author

Hi, as api-verify CI is failing so I tried running the golangci-lint run command .. but it is just showing Killed

sachinmaurya@sachin:~/go/src/github.com/gateway-api (test-request-route-w-backend-ref)$ golangci-lint run 
WARN [runner] The linter 'structcheck' is deprecated (since v1.49.0) due to: The owner seems to have abandoned the linter.  Replaced by unused. 
WARN [runner] The linter 'deadcode' is deprecated (since v1.49.0) due to: The owner seems to have abandoned the linter.  Replaced by unused. 
WARN [runner] The linter 'varcheck' is deprecated (since v1.49.0) due to: The owner seems to have abandoned the linter.  Replaced by unused. 
Killed

What can be the reason for that 🤔

@arkodg
Copy link
Contributor

arkodg commented Jul 6, 2023

golangci-lint run

@slayer321 can you rebase to main and try ./hack/verify-golint.sh ?

Signed-off-by: slayer321 <sachin.maurya7666@gmail.com>
Signed-off-by: slayer321 <sachin.maurya7666@gmail.com>
Signed-off-by: slayer321 <sachin.maurya7666@gmail.com>
Signed-off-by: slayer321 <sachin.maurya7666@gmail.com>
@slayer321
Copy link
Contributor Author

I tried it but still got below error

sachinmaurya@sachin:~/go/src/github.com/gateway-api (test-request-route-w-backend-ref)$ ./hack/verify-golint.sh
level=warning msg="[runner] Can't run linter goanalysis_metalinter: musttag: running `go list all`: exit status 1"
level=error msg="Running error: 1 error occurred:\n\t* can't run linter goanalysis_metalinter: musttag: running `go list all`: exit status 1\n\n"

After looking for it , I found that there where many similar errors but nothing same as this one. most of them says that we need to run go build.
has anyone else faced the similar error while working on this project or is it some issue with my local setup.

@gyohuangxin
Copy link
Member

@slayer321 I don't have idea about it. Even if you can't run the ./hack/verify-golint.sh in your environment, you can find the details from the CI job logs: https://prow.k8s.io/view/gs/kubernetes-jenkins/pr-logs/pull/kubernetes-sigs_gateway-api/2161/pull-gateway-api-verify/1676963399549849600

Signed-off-by: slayer321 <sachin.maurya7666@gmail.com>
@robscott
Copy link
Member

/release-note "Webhook validation now ensures that BackendRefs can not be specified in the same HTTPRoute rule as a Redirect filter"

@robscott
Copy link
Member

/release-note-edit Webhook validation now ensures that BackendRefs can not be specified in the same HTTPRoute rule as a Redirect filter

@k8s-ci-robot k8s-ci-robot added release-note Denotes a PR that will be considered when it comes time to generate release notes. and removed do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. labels Jul 17, 2023
@robscott
Copy link
Member

Thanks @slayer321!

/lgtm
/approve

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jul 17, 2023
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: arkodg, gyohuangxin, robscott, slayer321

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jul 17, 2023
@k8s-ci-robot k8s-ci-robot merged commit d708851 into kubernetes-sigs:main Jul 17, 2023
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/bug Categorizes issue or PR as related to a bug. kind/test lgtm "Looks good to me", indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Validation Should Prevent Redirect Filters and BackendRefs in same HTTPRoute Rules
5 participants