|
7 | 7 |
|
8 | 8 | apiv1 "k8s.io/api/core/v1"
|
9 | 9 | "k8s.io/apimachinery/pkg/types"
|
| 10 | + "sigs.k8s.io/controller-runtime/pkg/client" |
10 | 11 | v1 "sigs.k8s.io/gateway-api/apis/v1"
|
11 | 12 |
|
12 | 13 | "github.com/nginxinc/nginx-gateway-fabric/internal/mode/static/state/graph"
|
@@ -202,70 +203,80 @@ func (hpr *hostPathRules) upsertListener(l *graph.Listener) {
|
202 | 203 | hpr.httpsListeners = append(hpr.httpsListeners, l)
|
203 | 204 | }
|
204 | 205 |
|
205 |
| - for routeNsName, r := range l.Routes { |
206 |
| - var hostnames []string |
207 |
| - for _, p := range r.ParentRefs { |
208 |
| - if val, exist := p.Attachment.AcceptedHostnames[string(l.Source.Name)]; exist { |
209 |
| - hostnames = val |
210 |
| - } |
| 206 | + for _, r := range l.Routes { |
| 207 | + if !r.Valid { |
| 208 | + continue |
211 | 209 | }
|
212 | 210 |
|
213 |
| - for _, h := range hostnames { |
214 |
| - if prevListener, exists := hpr.listenersForHost[h]; exists { |
215 |
| - // override the previous listener if the new one has a more specific hostname |
216 |
| - if listenerHostnameMoreSpecific(l.Source.Hostname, prevListener.Source.Hostname) { |
217 |
| - hpr.listenersForHost[h] = l |
218 |
| - } |
219 |
| - } else { |
220 |
| - hpr.listenersForHost[h] = l |
221 |
| - } |
| 211 | + hpr.upsertRoute(r, l) |
| 212 | + } |
| 213 | +} |
222 | 214 |
|
223 |
| - if _, exist := hpr.rulesPerHost[h]; !exist { |
224 |
| - hpr.rulesPerHost[h] = make(map[pathAndType]PathRule) |
225 |
| - } |
| 215 | +func (hpr *hostPathRules) upsertRoute(route *graph.Route, listener *graph.Listener) { |
| 216 | + var hostnames []string |
| 217 | + for _, p := range route.ParentRefs { |
| 218 | + if val, exist := p.Attachment.AcceptedHostnames[string(listener.Source.Name)]; exist { |
| 219 | + hostnames = val |
226 | 220 | }
|
| 221 | + } |
227 | 222 |
|
228 |
| - for i, rule := range r.Source.Spec.Rules { |
229 |
| - if !r.Rules[i].ValidMatches { |
230 |
| - continue |
| 223 | + for _, h := range hostnames { |
| 224 | + if prevListener, exists := hpr.listenersForHost[h]; exists { |
| 225 | + // override the previous listener if the new one has a more specific hostname |
| 226 | + if listenerHostnameMoreSpecific(listener.Source.Hostname, prevListener.Source.Hostname) { |
| 227 | + hpr.listenersForHost[h] = listener |
231 | 228 | }
|
| 229 | + } else { |
| 230 | + hpr.listenersForHost[h] = listener |
| 231 | + } |
232 | 232 |
|
233 |
| - var filters HTTPFilters |
234 |
| - if r.Rules[i].ValidFilters { |
235 |
| - filters = createHTTPFilters(rule.Filters) |
236 |
| - } else { |
237 |
| - filters = HTTPFilters{ |
238 |
| - InvalidFilter: &InvalidHTTPFilter{}, |
239 |
| - } |
| 233 | + if _, exist := hpr.rulesPerHost[h]; !exist { |
| 234 | + hpr.rulesPerHost[h] = make(map[pathAndType]PathRule) |
| 235 | + } |
| 236 | + } |
| 237 | + |
| 238 | + for i, rule := range route.Source.Spec.Rules { |
| 239 | + if !route.Rules[i].ValidMatches { |
| 240 | + continue |
| 241 | + } |
| 242 | + |
| 243 | + var filters HTTPFilters |
| 244 | + if route.Rules[i].ValidFilters { |
| 245 | + filters = createHTTPFilters(rule.Filters) |
| 246 | + } else { |
| 247 | + filters = HTTPFilters{ |
| 248 | + InvalidFilter: &InvalidHTTPFilter{}, |
240 | 249 | }
|
| 250 | + } |
241 | 251 |
|
242 |
| - for _, h := range hostnames { |
243 |
| - for _, m := range rule.Matches { |
244 |
| - path := getPath(m.Path) |
| 252 | + for _, h := range hostnames { |
| 253 | + for _, m := range rule.Matches { |
| 254 | + path := getPath(m.Path) |
245 | 255 |
|
246 |
| - key := pathAndType{ |
247 |
| - path: path, |
248 |
| - pathType: *m.Path.Type, |
249 |
| - } |
| 256 | + key := pathAndType{ |
| 257 | + path: path, |
| 258 | + pathType: *m.Path.Type, |
| 259 | + } |
250 | 260 |
|
251 |
| - rule, exist := hpr.rulesPerHost[h][key] |
252 |
| - if !exist { |
253 |
| - rule.Path = path |
254 |
| - rule.PathType = convertPathType(*m.Path.Type) |
255 |
| - } |
| 261 | + rule, exist := hpr.rulesPerHost[h][key] |
| 262 | + if !exist { |
| 263 | + rule.Path = path |
| 264 | + rule.PathType = convertPathType(*m.Path.Type) |
| 265 | + } |
256 | 266 |
|
257 |
| - // create iteration variable inside the loop to fix implicit memory aliasing |
258 |
| - om := r.Source.ObjectMeta |
| 267 | + // create iteration variable inside the loop to fix implicit memory aliasing |
| 268 | + om := route.Source.ObjectMeta |
259 | 269 |
|
260 |
| - rule.MatchRules = append(rule.MatchRules, MatchRule{ |
261 |
| - Source: &om, |
262 |
| - BackendGroup: newBackendGroup(r.Rules[i].BackendRefs, routeNsName, i), |
263 |
| - Filters: filters, |
264 |
| - Match: convertMatch(m), |
265 |
| - }) |
| 270 | + routeNsName := client.ObjectKeyFromObject(route.Source) |
266 | 271 |
|
267 |
| - hpr.rulesPerHost[h][key] = rule |
268 |
| - } |
| 272 | + rule.MatchRules = append(rule.MatchRules, MatchRule{ |
| 273 | + Source: &om, |
| 274 | + BackendGroup: newBackendGroup(route.Rules[i].BackendRefs, routeNsName, i), |
| 275 | + Filters: filters, |
| 276 | + Match: convertMatch(m), |
| 277 | + }) |
| 278 | + |
| 279 | + hpr.rulesPerHost[h][key] = rule |
269 | 280 | }
|
270 | 281 | }
|
271 | 282 | }
|
@@ -371,6 +382,10 @@ func buildUpstreams(
|
371 | 382 | }
|
372 | 383 |
|
373 | 384 | for _, route := range l.Routes {
|
| 385 | + if !route.Valid { |
| 386 | + continue |
| 387 | + } |
| 388 | + |
374 | 389 | for _, rule := range route.Rules {
|
375 | 390 | if !rule.ValidMatches || !rule.ValidFilters {
|
376 | 391 | // don't generate upstreams for rules that have invalid matches or filters
|
|
0 commit comments