-
Notifications
You must be signed in to change notification settings - Fork 690
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
validate Gateway listener protocols/ports/hostnames #4462
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c8bcabe
validate Gateway HTTP listener ports/hostnames
skriss c9808ec
extend listener validation to cover HTTPS/TLS listeners
skriss e9fc9a6
move unsupported listener protocol handling
skriss b93d006
tidy
skriss 70e6fbc
provision Envoy service ports only from listener specs
skriss 7ad54d5
changelog
skriss f63a772
update ports in tests
skriss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Gateway API: set appropriate conditions on Listeners if they don't specify the same port as other Listeners for their protocol group (i.e. HTTP, or HTTPS/TLS) or don't have a unique hostname within their group. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
// Copyright Project Contour Authors | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package gatewayapi | ||
|
||
import ( | ||
"fmt" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
gatewayapi_v1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2" | ||
) | ||
|
||
type ValidateListenersResult struct { | ||
InsecurePort int | ||
SecurePort int | ||
|
||
InvalidListenerConditions map[gatewayapi_v1alpha2.SectionName]metav1.Condition | ||
} | ||
|
||
// ValidateListeners validates protocols, ports and hostnames on a set of listeners. | ||
// It ensures that: | ||
// - all protocols are supported | ||
// - each listener group (grouped by protocol, with HTTPS & TLS going together) uses a single port | ||
// - hostnames within each listener group are unique | ||
// It returns the insecure & secure ports to use, as well as conditions for all invalid listeners. | ||
// If a listener is not in the "InvalidListenerConditions" map, it is assumed to be valid according | ||
// to the above rules. | ||
func ValidateListeners(listeners []gatewayapi_v1alpha2.Listener) ValidateListenersResult { | ||
result := ValidateListenersResult{ | ||
InvalidListenerConditions: map[gatewayapi_v1alpha2.SectionName]metav1.Condition{}, | ||
} | ||
|
||
// All listeners with a protocol of "HTTP" must use the same port number | ||
// Heuristic: the first port number encountered is allowed, any other listeners with a different port number are marked "Detached" with "PortUnavailable" | ||
// All listeners with a protocol of "HTTP" using the one allowed port must have a unique hostname | ||
// Any listener with a duplicate hostname is marked "Conflicted" with "HostnameConflict" | ||
|
||
var ( | ||
insecureHostnames = map[string]int{} | ||
secureHostnames = map[string]int{} | ||
) | ||
|
||
for _, listener := range listeners { | ||
switch listener.Protocol { | ||
case gatewayapi_v1alpha2.HTTPProtocolType: | ||
// Keep the first insecure listener port we see | ||
if result.InsecurePort == 0 { | ||
result.InsecurePort = int(listener.Port) | ||
} | ||
|
||
// Count hostnames among insecure listeners with the "valid" port. | ||
// For other insecure listeners with an "invalid" port, the | ||
// "PortUnavailable" reason will take precedence. | ||
if int(listener.Port) == result.InsecurePort { | ||
insecureHostnames[listenerHostname(listener)]++ | ||
} | ||
case gatewayapi_v1alpha2.HTTPSProtocolType, gatewayapi_v1alpha2.TLSProtocolType: | ||
// Keep the first secure listener port we see | ||
if result.SecurePort == 0 { | ||
result.SecurePort = int(listener.Port) | ||
} | ||
|
||
// Count hostnames among secure listeners with the "valid" port. | ||
// For other secure listeners with an "invalid" port, the | ||
// "PortUnavailable" reason will take precedence. | ||
if int(listener.Port) == result.SecurePort { | ||
secureHostnames[listenerHostname(listener)]++ | ||
} | ||
} | ||
} | ||
|
||
for _, listener := range listeners { | ||
switch listener.Protocol { | ||
case gatewayapi_v1alpha2.HTTPProtocolType: | ||
switch { | ||
case int(listener.Port) != result.InsecurePort: | ||
result.InvalidListenerConditions[listener.Name] = metav1.Condition{ | ||
Type: string(gatewayapi_v1alpha2.ListenerConditionDetached), | ||
Status: metav1.ConditionTrue, | ||
Reason: string(gatewayapi_v1alpha2.ListenerReasonPortUnavailable), | ||
Message: "Only one HTTP port is supported", | ||
} | ||
case insecureHostnames[listenerHostname(listener)] > 1: | ||
result.InvalidListenerConditions[listener.Name] = metav1.Condition{ | ||
Type: string(gatewayapi_v1alpha2.ListenerConditionConflicted), | ||
Status: metav1.ConditionTrue, | ||
Reason: string(gatewayapi_v1alpha2.ListenerReasonHostnameConflict), | ||
Message: "Hostname must be unique among HTTP listeners", | ||
} | ||
} | ||
case gatewayapi_v1alpha2.HTTPSProtocolType, gatewayapi_v1alpha2.TLSProtocolType: | ||
switch { | ||
case int(listener.Port) != result.SecurePort: | ||
result.InvalidListenerConditions[listener.Name] = metav1.Condition{ | ||
Type: string(gatewayapi_v1alpha2.ListenerConditionDetached), | ||
Status: metav1.ConditionTrue, | ||
Reason: string(gatewayapi_v1alpha2.ListenerReasonPortUnavailable), | ||
Message: "Only one HTTPS/TLS port is supported", | ||
} | ||
case secureHostnames[listenerHostname(listener)] > 1: | ||
result.InvalidListenerConditions[listener.Name] = metav1.Condition{ | ||
Type: string(gatewayapi_v1alpha2.ListenerConditionConflicted), | ||
Status: metav1.ConditionTrue, | ||
Reason: string(gatewayapi_v1alpha2.ListenerReasonHostnameConflict), | ||
Message: "Hostname must be unique among HTTPS/TLS listeners", | ||
} | ||
} | ||
default: | ||
result.InvalidListenerConditions[listener.Name] = metav1.Condition{ | ||
Type: string(gatewayapi_v1alpha2.ListenerConditionDetached), | ||
Status: metav1.ConditionTrue, | ||
Reason: string(gatewayapi_v1alpha2.ListenerReasonUnsupportedProtocol), | ||
Message: fmt.Sprintf("Listener protocol %q is unsupported, must be one of HTTP, HTTPS or TLS", listener.Protocol), | ||
} | ||
} | ||
} | ||
|
||
return result | ||
} | ||
|
||
func listenerHostname(listener gatewayapi_v1alpha2.Listener) string { | ||
if listener.Hostname != nil { | ||
return string(*listener.Hostname) | ||
} | ||
return "" | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this may be something to call out in a doc somewhere, i dont think this specific grouping/heuristic is in the gateway docs themselves (looking at the
Listeners
field documentation here https://gateway-api.sigs.k8s.io/v1alpha2/references/spec/#gateway.networking.k8s.io/v1alpha2.Gateway)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, this is really a Contour-specific thing since we only support 2 ports total right now. Agree we should call this out somewhere - need to figure out where to put docs for all this, but let me add this to a new issue.