1
1
/*
2
- Copyright 2021 The Kubernetes Authors.
2
+ Copyright 2023 The Kubernetes Authors.
3
3
4
4
Licensed under the Apache License, Version 2.0 (the "License");
5
5
you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@ package validation
19
19
import (
20
20
"fmt"
21
21
22
+ "k8s.io/apimachinery/pkg/util/sets"
22
23
"k8s.io/apimachinery/pkg/util/validation/field"
23
24
24
25
gatewayv1b1 "sigs.k8s.io/gateway-api/apis/v1beta1"
@@ -70,6 +71,7 @@ func validateGatewayListeners(listeners []gatewayv1b1.Listener, path *field.Path
70
71
errs = append (errs , validateListenerHostname (listeners , path )... )
71
72
errs = append (errs , ValidateTLSCertificateRefs (listeners , path )... )
72
73
errs = append (errs , ValidateListenerNames (listeners , path )... )
74
+ errs = append (errs , validateHostnameProtocolPort (listeners , path )... )
73
75
return errs
74
76
}
75
77
@@ -133,3 +135,25 @@ func ValidateListenerNames(listeners []gatewayv1b1.Listener, path *field.Path) f
133
135
}
134
136
return errs
135
137
}
138
+
139
+ // validateHostnameProtocolPort validates that the combination of port, protocol, and name are
140
+ // unique for each listener.
141
+ func validateHostnameProtocolPort (listeners []gatewayv1b1.Listener , path * field.Path ) field.ErrorList {
142
+ var errs field.ErrorList
143
+ hostnameProtocolPortSets := sets.Set [string ]{}
144
+ for i , listener := range listeners {
145
+ hostname := new (gatewayv1b1.Hostname )
146
+ if listener .Hostname != nil {
147
+ hostname = listener .Hostname
148
+ }
149
+ protocol := listener .Protocol
150
+ port := listener .Port
151
+
152
+ hostnameProtocolPort := fmt .Sprintf ("%s:%s:%d" , * hostname , protocol , port )
153
+ if hostnameProtocolPortSets .Has (hostnameProtocolPort ) {
154
+ errs = append (errs , field .Forbidden (path .Index (i ), fmt .Sprintln ("combination of port, protocol, and name must be unique for each listener" )))
155
+ }
156
+ hostnameProtocolPortSets .Insert (hostnameProtocolPort )
157
+ }
158
+ return errs
159
+ }
0 commit comments