@@ -194,31 +194,62 @@ func (g ParentInfo) Equals(other ParentInfo) bool {
194194 slices .Equal (g .Hostnames , other .Hostnames )
195195}
196196
197+ type GatewayTransformationFunction func (GatewayCollectionConfig ) func (ctx krt.HandlerContext , obj * gwv1.Gateway ) (* gwv1.GatewayStatus , []* GatewayListener )
198+
199+ type GatewayCollectionConfigOption func (o * GatewayCollectionConfig )
200+
201+ func WithGatewayTransformationFunc (f GatewayTransformationFunction ) GatewayCollectionConfigOption {
202+ return func (o * GatewayCollectionConfig ) {
203+ o .transformationFunc = f
204+ }
205+ }
206+
207+ type GatewayCollectionConfig struct {
208+ ControllerName string
209+ Gateways krt.Collection [* gwv1.Gateway ]
210+ ListenerSets krt.Collection [ListenerSet ]
211+ GatewayClasses krt.Collection [GatewayClass ]
212+ Namespaces krt.Collection [* corev1.Namespace ]
213+ Grants ReferenceGrants
214+ Secrets krt.Collection [* corev1.Secret ]
215+ ConfigMaps krt.Collection [* corev1.ConfigMap ]
216+ KrtOpts krtutil.KrtOptions
217+
218+ listenerIndex krt.Index [types.NamespacedName , ListenerSet ]
219+ transformationFunc GatewayTransformationFunction
220+ }
221+
197222// GatewayCollection returns a collection of the internal representations GatewayListeners for the given gateway.
198223func GatewayCollection (
199- controllerName string ,
200- gateways krt.Collection [* gwv1.Gateway ],
201- listenerSets krt.Collection [ListenerSet ],
202- gatewayClasses krt.Collection [GatewayClass ],
203- namespaces krt.Collection [* corev1.Namespace ],
204- grants ReferenceGrants ,
205- secrets krt.Collection [* corev1.Secret ],
206- configMaps krt.Collection [* corev1.ConfigMap ],
207- krtopts krtutil.KrtOptions ,
224+ cfg GatewayCollectionConfig ,
225+ opts ... GatewayCollectionConfigOption ,
208226) (
209227 krt.StatusCollection [* gwv1.Gateway , gwv1.GatewayStatus ],
210228 krt.Collection [* GatewayListener ],
211229) {
212- listenerIndex := krt .NewIndex (listenerSets , "gatewayParent" , func (o ListenerSet ) []types.NamespacedName {
230+ for _ , fn := range opts {
231+ fn (& cfg )
232+ }
233+ cfg .listenerIndex = krt .NewIndex (cfg .ListenerSets , "gatewayParent" , func (o ListenerSet ) []types.NamespacedName {
213234 return []types.NamespacedName {o .GatewayParent }
214235 })
215- statusCol , gw := krt .NewStatusManyCollection (gateways , func (ctx krt.HandlerContext , obj * gwv1.Gateway ) (* gwv1.GatewayStatus , []* GatewayListener ) {
216- class := krt .FetchOne (ctx , gatewayClasses , krt .FilterKey (string (obj .Spec .GatewayClassName )))
236+ if cfg .transformationFunc == nil {
237+ cfg .transformationFunc = GatewaysTransformationFunc
238+ }
239+
240+ statusCol , gw := krt .NewStatusManyCollection (cfg .Gateways , cfg .transformationFunc (cfg ), cfg .KrtOpts .ToOptions ("KubernetesGateway" )... )
241+
242+ return statusCol , gw
243+ }
244+
245+ func GatewaysTransformationFunc (cfg GatewayCollectionConfig ) func (ctx krt.HandlerContext , obj * gwv1.Gateway ) (* gwv1.GatewayStatus , []* GatewayListener ) {
246+ return func (ctx krt.HandlerContext , obj * gwv1.Gateway ) (* gwv1.GatewayStatus , []* GatewayListener ) {
247+ class := krt .FetchOne (ctx , cfg .GatewayClasses , krt .FilterKey (string (obj .Spec .GatewayClassName )))
217248 if class == nil {
218249 logger .Debug ("gateway class not found, skipping" , "gw_name" , obj .GetName (), "gatewayClassName" , obj .Spec .GatewayClassName )
219250 return nil , nil
220251 }
221- if string (class .Controller ) != controllerName {
252+ if string (class .Controller ) != cfg . ControllerName {
222253 logger .Debug ("skipping gateway not managed by our controller" , "gw_name" , obj .GetName (), "gatewayClassName" , obj .Spec .GatewayClassName , "controllerName" , class .Controller )
223254 return nil , nil // ignore gateways not managed by our controller
224255 }
@@ -249,7 +280,7 @@ func GatewayCollection(
249280 // Attached Routes count starts at 0 and gets updated later in the status syncer
250281 // when the real count is available after route processing
251282
252- hostnames , tlsInfo , updatedStatus , programmed := BuildListener (ctx , secrets , configMaps , grants , namespaces , obj , status .Listeners , kgw , l , i , nil )
283+ hostnames , tlsInfo , updatedStatus , programmed := BuildListener (ctx , cfg . Secrets , cfg . ConfigMaps , cfg . Grants , cfg . Namespaces , obj , status .Listeners , kgw , l , i , nil )
253284 status .Listeners = updatedStatus
254285
255286 lstatus := status .Listeners [i ]
@@ -303,7 +334,7 @@ func GatewayCollection(
303334 })
304335 result = append (result , res )
305336 }
306- listenersFromSets := krt .Fetch (ctx , listenerSets , krt .FilterIndex (listenerIndex , config .NamespacedName (obj )))
337+ listenersFromSets := krt .Fetch (ctx , cfg . ListenerSets , krt .FilterIndex (cfg . listenerIndex , config .NamespacedName (obj )))
307338 for _ , ls := range listenersFromSets {
308339 result = append (result , & GatewayListener {
309340 Name : ls .Name ,
@@ -320,9 +351,7 @@ func GatewayCollection(
320351 }
321352 gws := rm .BuildGWStatus (context .Background (), * obj , nil )
322353 return gws , result
323- }, krtopts .ToOptions ("KubernetesGateway" )... )
324-
325- return statusCol , gw
354+ }
326355}
327356
328357type ListenerSet struct {
0 commit comments