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

Webhook: validate the combination of port, protocol, and hostname are unique for each listener. #1457

Merged
merged 6 commits into from
Apr 6, 2023

Conversation

gyohuangxin
Copy link
Member

@gyohuangxin gyohuangxin commented Oct 14, 2022

What type of PR is this?

/kind feature

What this PR does / why we need it:

Add validation to validate the combination of port, protocol, and hostname are unique for each listener.

Which issue(s) this PR fixes:

Fixes #847

Does this PR introduce a user-facing change?:

NONE

@k8s-ci-robot k8s-ci-robot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. kind/feature Categorizes issue or PR as related to a new feature. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Oct 14, 2022
@k8s-ci-robot k8s-ci-robot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Oct 14, 2022
@k8s-ci-robot
Copy link
Contributor

Hi @gyohuangxin. 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 the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Oct 14, 2022
@gyohuangxin
Copy link
Member Author

@robscott I make it a draft PR because I have some question:

  1. As you mentioned in Webhook: Validate unique port, protocol, and hostname per listener #847:

The validating webhook should ensure that port, protocol, and hostname are unique among Gateway listeners

but then you give an example:

"2 listeners for foo.com would be valid if they were on different ports or protocols, but not if port and protocol were also identical."

They are a bit inconsistent, but my understanding is the webhook should separately validate unique port, protocol per hostname for gateway listeners. Is it correct? Is it needed to verify unique port, protocol across hostnames? Is it needed to verify unique hostname per listener?

  1. I add validations in v1beta1 apis, should I add them in v1alpha1 apis too?

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Oct 20, 2022
@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Nov 15, 2022
@shaneutt
Copy link
Member

@gyohuangxin where are we at with this one? Is there anything we can do to help the progression here? Looks like maybe we're still waiting on some feedback requested from @robscott?

@gyohuangxin
Copy link
Member Author

Yes, it's on hold because I'm still confused with the first question, I'm not sure my thought is close to the reality.

@robscott I make it a draft PR because I have some question:

  1. As you mentioned in Webhook: Validate unique port, protocol, and hostname per listener #847:

The validating webhook should ensure that port, protocol, and hostname are unique among Gateway listeners

but then you give an example:

"2 listeners for foo.com would be valid if they were on different ports or protocols, but not if port and protocol were also identical."

They are a bit inconsistent, but my understanding is the webhook should separately validate unique port, protocol per hostname for gateway listeners. Is it correct? Is it needed to verify unique port, protocol across hostnames? Is it needed to verify unique hostname per listener?

@shaneutt shaneutt added this to the v0.7.0 milestone Mar 14, 2023
Comment on lines 128 to 119
hostnameProtocolMap := make(map[gatewayv1b1.Hostname][]gatewayv1b1.ProtocolType)
hostnamePortMap := make(map[gatewayv1b1.Hostname][]gatewayv1b1.PortNumber)
Copy link
Member

Choose a reason for hiding this comment

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

Hey @gyohuangxin, I think there's a simpler way to do this. All that we really want to do is ensure that the combination of port, protocol, and name are unique. So for example, the following would be valid:

- port: 80
  hostname: foo.com
  protocol: HTTP
- port: 443
  hostname: foo.com
  protocol: HTTP

But the following would not be valid:

- port: 80
  hostname: foo.com
  protocol: HTTP
- port: 80
  hostname: foo.com
  protocol: HTTP

I think what you have is slightly different than that, requiring that for the same hostname (foo.com), both port and protocol must be different for each listener. The spec as it's written today only requires that one of them is different.

A simple way to accomplish this would be to use something like fmt.Sprintf("%s:%s:%d", protocol, hostname, port) for each listener and then use k8s sets util to see if the value for that listener duplicated any listeners you'd already looked at within the same Gateway. Hopefully that makes sense, sorry for the delay on this!

Copy link
Member Author

Choose a reason for hiding this comment

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

@robscott Thanks for your clear comments, don't worry about the delay. I also like the simple implementation, thanks!

Copy link
Contributor

Choose a reason for hiding this comment

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

should this webhook attempt to reject one of these tuples (80, http, *.com) and (80, http, *.foo.com) :) ?

Copy link
Member

Choose a reason for hiding this comment

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

@arkodg I think it will very hard to accomplish. How would you decide if regexA (*.com in this case) is overlapping with regexB(*foo.com).

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah its hard, but I wanted to highlight the case.
Since the API calls out any regex match to be implementation specific, even adding language in the API for this might be tricky
cc @robscott

@robscott
Copy link
Member

Thanks for the work on this @gyohuangxin! Sorry I lost track of this, don't hesitate to ping me if I can help explain anything better.

@@ -0,0 +1,41 @@
/*
Copyright 2022 The Kubernetes Authors.
Copy link
Member

Choose a reason for hiding this comment

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

2023


// ContainsInPortSlice checks whether the provided Port
// is in the target Port slice.
func ContainsInPortSlice(items []gatewayv1b1.PortNumber, item *gatewayv1b1.PortNumber) bool {
Copy link
Member

Choose a reason for hiding this comment

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

pass value type of PortNumber


// ContainsInProtocolSlice checks whether the provided Protocol
// is in the target Protocol slice.
func ContainsInProtocolSlice(items []gatewayv1b1.ProtocolType, item *gatewayv1b1.ProtocolType) bool {
Copy link
Member

Choose a reason for hiding this comment

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

value type instead of pointer

}
if listener.Port != 0 {
if portSlice, ok := hostnamePortMap[targetHostname]; ok {
if utils.ContainsInPortSlice(portSlice, &listener.Port) {
Copy link
Member

Choose a reason for hiding this comment

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

can use Slices.Contains from golang.org/x/exp/slices

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Mar 24, 2023
@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Mar 28, 2023
…port, protocol, and name are unique for each listener.

Signed-off-by: Huang Xin <xin1.huang@intel.com>
@gyohuangxin
Copy link
Member Author

@robscott @shaneutt Can you review this PR? Thanks

Signed-off-by: Huang Xin <xin1.huang@intel.com>
@LiorLieberman
Copy link
Member

Thanks @gyohuangxin ! This Looks good to me.
We probably want to change the PR description to match the new logic - ensuring Listener's port-protocol-hostname combinations are unique within an individual Gateway.

over to @robscott for a final review and approval.

@gyohuangxin
Copy link
Member Author

Thanks @gyohuangxin ! This Looks good to me. We probably want to change the PR description to match the new logic - ensuring Listener's port-protocol-hostname combinations are unique within an individual Gateway.

over to @robscott for a final review and approval.

Sorry for missing the PR description, updated. Thanks for reminding 💯

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 !

@dprotaso
Copy link
Contributor

dprotaso commented Apr 4, 2023

Based on this comment it seems like we should take into consideration the tls properties attached to a listener when determing uniqueness

#1842 (comment)

@gyohuangxin gyohuangxin changed the title Webhook: validate the combination of port, protocol, and name are unique for each listener. Webhook: validate the combination of port, protocol, and hostname are unique for each listener. Apr 5, 2023
@gyohuangxin
Copy link
Member Author

Thanks for your comments @dprotaso . As said in @robscott 's #1842 (comment), the "Hostname" can be different for shared Port and Protocol, like the TLS example on the website: https://gateway-api.sigs.k8s.io/guides/tls/#wildcard-tls-listeners

So, I think it can cover the use cases. Please let me know if there are some use cases that use the same combination of port, protocol and hostname but use distinct tls fields.

Copy link
Member

@robscott robscott left a comment

Choose a reason for hiding this comment

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

Thanks @gyohuangxin! Would like a few more test cases but otherwise LGTM.

mutate: func(gw *gatewayv1b1.Gateway) {
hostnameFoo := gatewayv1b1.Hostname("foo.com")
gw.Spec.Listeners[0].Name = "foo"
gw.Spec.Listeners[0].Hostname = &hostnameFoo
Copy link
Member

Choose a reason for hiding this comment

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

Since hostname is optional can you add one more test case that shows that the same setup fails when both listeners have separate hostnames?

Copy link
Member

Choose a reason for hiding this comment

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

Similarly, it would be helpful to have additional test cases where everything was the same except for one of the fields (port, protocol, or hostname) to ensure those are all still valid.

Copy link
Member Author

Choose a reason for hiding this comment

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

Since hostname is optional can you add one more test case that shows that the same setup fails when both listeners have separate hostnames?

Do you mean to test the combination of port and protocol are unique when hostnames not set? If yes, I've add one: d75ab56#diff-e3c0a139e14037364db162e955588e8153e795f4cbe84b6fce32149d62ca2a73R170-R184

Similarly, it would be helpful to have additional test cases where everything was the same except for one of the fields (port, protocol, or hostname) to ensure those are all still valid.

Updated, thank you!

Signed-off-by: Huang Xin <xin1.huang@intel.com>
@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Apr 6, 2023
Copy link
Member

@robscott robscott left a comment

Choose a reason for hiding this comment

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

Thanks @gyohuangxin! Just a couple tiny nits but otherwise LGTM.

apis/v1beta1/validation/gateway_test.go Outdated Show resolved Hide resolved
apis/v1beta1/validation/gateway.go Outdated Show resolved Hide resolved
Signed-off-by: Huang Xin <xin1.huang@intel.com>
@robscott
Copy link
Member

robscott commented Apr 6, 2023

Thanks @gyohuangxin!

/approve
/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. approved Indicates a PR has been approved by an approver from all required OWNERS files. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Apr 6, 2023
@robscott robscott added tide/merge-method-squash Denotes a PR that should be squashed by tide when it merges. and removed approved Indicates a PR has been approved by an approver from all required OWNERS files. labels Apr 6, 2023
@robscott
Copy link
Member

robscott commented Apr 6, 2023

/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 Apr 6, 2023
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

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

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 Apr 6, 2023
@k8s-ci-robot k8s-ci-robot merged commit bb88d58 into kubernetes-sigs:main Apr 6, 2023
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/feature Categorizes issue or PR as related to a new feature. 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. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. tide/merge-method-squash Denotes a PR that should be squashed by tide when it merges.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Webhook: Validate unique port, protocol, and hostname per listener
8 participants