@@ -277,14 +277,23 @@ func buildSectionNameRefs(
277
277
routeNamespace string ,
278
278
gws map [types.NamespacedName ]* Gateway ,
279
279
) ([]ParentRef , error ) {
280
- sectionNameRefs := make ([]ParentRef , 0 , len (parentRefs ))
281
-
282
280
type key struct {
283
281
gwNsName types.NamespacedName
284
282
sectionName string
285
283
}
286
284
uniqueSectionsPerGateway := make (map [key ]struct {})
287
285
286
+ sectionNameRefs := make ([]ParentRef , 0 , len (parentRefs ))
287
+
288
+ checkUniqueSections := func (key key ) error {
289
+ if _ , exist := uniqueSectionsPerGateway [key ]; exist {
290
+ return fmt .Errorf ("duplicate section name %q for Gateway %s" , key .sectionName , key .gwNsName .String ())
291
+ }
292
+
293
+ uniqueSectionsPerGateway [key ] = struct {}{}
294
+ return nil
295
+ }
296
+
288
297
parentRefIndex := 0
289
298
290
299
for _ , p := range parentRefs {
@@ -301,13 +310,11 @@ func buildSectionNameRefs(
301
310
// If there is no section name, we create ParentRefs for each listener in the gateway
302
311
if p .SectionName == nil {
303
312
for _ , l := range gw .Listeners {
304
- sectionName := string (l .Source .Name )
305
- k .sectionName = sectionName
313
+ k .sectionName = string (l .Source .Name )
306
314
307
- if _ , exist := uniqueSectionsPerGateway [ k ]; exist {
308
- return nil , fmt . Errorf ( "duplicate section name %q for Gateway %s" , sectionName , gwNsName . String ())
315
+ if err := checkUniqueSections ( k ); err != nil {
316
+ return nil , err
309
317
}
310
- uniqueSectionsPerGateway [k ] = struct {}{}
311
318
312
319
sectionNameRefs = append (sectionNameRefs , ParentRef {
313
320
Idx : parentRefIndex ,
@@ -320,14 +327,10 @@ func buildSectionNameRefs(
320
327
continue
321
328
}
322
329
323
- sectionName := string (* p .SectionName )
324
-
325
- k .sectionName = sectionName
326
-
327
- if _ , exist := uniqueSectionsPerGateway [k ]; exist {
328
- return nil , fmt .Errorf ("duplicate section name %q for Gateway %s" , sectionName , gwNsName .String ())
330
+ k .sectionName = string (* p .SectionName )
331
+ if err := checkUniqueSections (k ); err != nil {
332
+ return nil , err
329
333
}
330
- uniqueSectionsPerGateway [k ] = struct {}{}
331
334
332
335
sectionNameRefs = append (sectionNameRefs , ParentRef {
333
336
Idx : parentRefIndex ,
0 commit comments