@@ -62,6 +62,8 @@ func validateGatewayListeners(listeners []gatewayv1b1.Listener, path *field.Path
62
62
var errs field.ErrorList
63
63
errs = append (errs , validateListenerTLSConfig (listeners , path )... )
64
64
errs = append (errs , validateListenerHostname (listeners , path )... )
65
+ errs = append (errs , validateUniqueProtocol (listeners , path )... )
66
+ errs = append (errs , validateUniquePort (listeners , path )... )
65
67
return errs
66
68
}
67
69
@@ -91,3 +93,57 @@ func validateListenerHostname(listeners []gatewayv1b1.Listener, path *field.Path
91
93
}
92
94
return errs
93
95
}
96
+
97
+ // validateUniqueProtocol validates each listener hostname
98
+ // should not have the duplicate protocols
99
+ func validateUniqueProtocol (listeners []gatewayv1b1.Listener , path * field.Path ) field.ErrorList {
100
+ var errs field.ErrorList
101
+ hostnameProtocolUnique := make (map [gatewayv1b1.Hostname ]map [gatewayv1b1.ProtocolType ]int )
102
+ for i , h := range listeners {
103
+ if h .Hostname == nil {
104
+ continue
105
+ }
106
+ if len (hostnameProtocolUnique ) == 0 {
107
+ hostnameProtocolUnique [* h .Hostname ] = map [gatewayv1b1.ProtocolType ]int {h .Protocol : i }
108
+ continue
109
+ }
110
+ if _ , hostnameFound := hostnameProtocolUnique [* h .Hostname ]; hostnameFound {
111
+ if _ , protocolFound := hostnameProtocolUnique [* h.Hostname ][h.Protocol ]; protocolFound {
112
+ errs = append (errs , field .Forbidden (path .Index (i ).Child ("protocol" ), fmt .Sprintf ("should be unique in hostname: %v" , * h .Hostname )))
113
+ return errs
114
+ } else {
115
+ hostnameProtocolUnique [* h.Hostname ][h.Protocol ] = i
116
+ }
117
+ } else {
118
+ hostnameProtocolUnique [* h .Hostname ] = map [gatewayv1b1.ProtocolType ]int {h .Protocol : i }
119
+ }
120
+ }
121
+ return errs
122
+ }
123
+
124
+ // validateUniquePort validates each listener hostname
125
+ // should not have the duplicate ports
126
+ func validateUniquePort (listeners []gatewayv1b1.Listener , path * field.Path ) field.ErrorList {
127
+ var errs field.ErrorList
128
+ hostnamePortUnique := make (map [gatewayv1b1.Hostname ]map [gatewayv1b1.PortNumber ]int )
129
+ for i , h := range listeners {
130
+ if h .Hostname == nil {
131
+ continue
132
+ }
133
+ if len (hostnamePortUnique ) == 0 {
134
+ hostnamePortUnique [* h .Hostname ] = map [gatewayv1b1.PortNumber ]int {h .Port : i }
135
+ continue
136
+ }
137
+ if _ , hostnameFound := hostnamePortUnique [* h .Hostname ]; hostnameFound {
138
+ if _ , portFound := hostnamePortUnique [* h.Hostname ][h.Port ]; portFound {
139
+ errs = append (errs , field .Forbidden (path .Index (i ).Child ("port" ), fmt .Sprintf ("should be unique in hostname: %v" , * h .Hostname )))
140
+ return errs
141
+ } else {
142
+ hostnamePortUnique [* h.Hostname ][h.Port ] = i
143
+ }
144
+ } else {
145
+ hostnamePortUnique [* h .Hostname ] = map [gatewayv1b1.PortNumber ]int {h .Port : i }
146
+ }
147
+ }
148
+ return errs
149
+ }
0 commit comments