Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion apis/v1beta1/validation/gateway.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2021 The Kubernetes Authors.
Copyright 2023 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -19,6 +19,7 @@ package validation
import (
"fmt"

"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/validation/field"

gatewayv1b1 "sigs.k8s.io/gateway-api/apis/v1beta1"
Expand Down Expand Up @@ -70,6 +71,7 @@ func validateGatewayListeners(listeners []gatewayv1b1.Listener, path *field.Path
errs = append(errs, validateListenerHostname(listeners, path)...)
errs = append(errs, ValidateTLSCertificateRefs(listeners, path)...)
errs = append(errs, ValidateListenerNames(listeners, path)...)
errs = append(errs, validateHostnameProtocolPort(listeners, path)...)
return errs
}

Expand Down Expand Up @@ -133,3 +135,24 @@ func ValidateListenerNames(listeners []gatewayv1b1.Listener, path *field.Path) f
}
return errs
}

// validateHostnameProtocolPort validates that the combination of port, protocol, and name are
// unique for each listener.
func validateHostnameProtocolPort(listeners []gatewayv1b1.Listener, path *field.Path) field.ErrorList {
var errs field.ErrorList
hostnameProtocolPortSets := sets.Set[string]{}
for i, listener := range listeners {
hostname := new(gatewayv1b1.Hostname)
if listener.Hostname != nil {
hostname = listener.Hostname
}
protocol := listener.Protocol
port := listener.Port
hostnameProtocolPort := fmt.Sprintf("%s:%s:%d", *hostname, protocol, port)
if hostnameProtocolPortSets.Has(hostnameProtocolPort) {
errs = append(errs, field.Forbidden(path.Index(i), fmt.Sprintln("combination of port, protocol, and name must be unique for each listener")))
}
hostnameProtocolPortSets.Insert(hostnameProtocolPort)
}
return errs
}
29 changes: 26 additions & 3 deletions apis/v1beta1/validation/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,37 @@ func TestValidateGateway(t *testing.T) {
},
"names are not unique within the Gateway": {
mutate: func(gw *gatewayv1b1.Gateway) {
hostnameFoo := gatewayv1b1.Hostname("foo.com")
hostnameBar := gatewayv1b1.Hostname("bar.com")
gw.Spec.Listeners[0].Name = "foo"
gw.Spec.Listeners = append(gw.Spec.Listeners, gatewayv1b1.Listener{
Name: "foo",
},
gw.Spec.Listeners[0].Hostname = &hostnameFoo
gw.Spec.Listeners = append(gw.Spec.Listeners,
gatewayv1b1.Listener{
Name: "foo",
Hostname: &hostnameBar,
},
)
},
expectErrsOnFields: []string{"spec.listeners[1].name"},
},
"combination of port, protocol, and name are not unique for each listener": {
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
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!

gw.Spec.Listeners[0].Protocol = gatewayv1b1.HTTPProtocolType
gw.Spec.Listeners[0].Port = 80
gw.Spec.Listeners = append(gw.Spec.Listeners,
gatewayv1b1.Listener{
Name: "bar",
Hostname: &hostnameFoo,
Protocol: gatewayv1b1.HTTPProtocolType,
Port: 80,
},
)
},
expectErrsOnFields: []string{"spec.listeners[1]"},
},
}

for name, tc := range testCases {
Expand Down