@@ -14,10 +14,10 @@ import (
1414 commonv1alpha1 "github.com/kong/kong-operator/api/common/v1alpha1"
1515 configurationv1 "github.com/kong/kong-operator/api/configuration/v1"
1616 configurationv1alpha1 "github.com/kong/kong-operator/api/configuration/v1alpha1"
17- "github.com/kong/kong-operator/controller/hybridgateway/builder"
1817 hybridgatewayerrors "github.com/kong/kong-operator/controller/hybridgateway/errors"
1918 "github.com/kong/kong-operator/controller/hybridgateway/kongroute"
20- "github.com/kong/kong-operator/controller/hybridgateway/namegen"
19+ "github.com/kong/kong-operator/controller/hybridgateway/plugin"
20+ "github.com/kong/kong-operator/controller/hybridgateway/pluginbinding"
2121 "github.com/kong/kong-operator/controller/hybridgateway/refs"
2222 "github.com/kong/kong-operator/controller/hybridgateway/route"
2323 "github.com/kong/kong-operator/controller/hybridgateway/service"
@@ -404,60 +404,44 @@ func (c *httpRouteConverter) translate(ctx context.Context, logger logr.Logger)
404404 log .Debug (logger , "Successfully translated KongRoute resource" ,
405405 "route" , routeName )
406406
407- // Build the kong plugin and kong plugin binding resources.
407+ // Build the KongPlugin and KongPluginBinding resources.
408408 log .Debug (logger , "Processing filters for rule" ,
409409 "kongRoute" , routeName ,
410410 "filterCount" , len (rule .Filters ))
411411
412412 for _ , filter := range rule .Filters {
413- pluginName := namegen .NewKongPluginName (filter )
414-
415- log .Trace (logger , "Building KongPlugin resource" ,
416- "plugin" , pluginName ,
417- "filterType" , filter .Type )
418-
419- plugin , err := builder .NewKongPlugin ().
420- WithName (pluginName ).
421- WithNamespace (c .route .Namespace ).
422- WithLabels (c .route , & pRef ).
423- WithAnnotations (c .route , & pRef ).
424- WithFilter (filter ).Build ()
413+ pluginPtr , err := plugin .PluginForFilter (ctx , logger , c .Client , c .route , filter , & pRef )
425414 if err != nil {
426- log .Error (logger , err , "Failed to build KongPlugin resource, skipping filter" ,
427- "plugin" , pluginName ,
428- "filterType" , filter .Type )
429- translationErrors = append (translationErrors , fmt .Errorf ("failed to build KongPlugin %s: %w" , pluginName , err ))
415+ log .Error (logger , err , "Failed to translate KongPlugin resource, skipping filter" ,
416+ "filter" , filter .Type )
417+ translationErrors = append (translationErrors , fmt .Errorf ("failed to translate KongPlugin for filter: %w" , err ))
430418 continue
431419 }
432- c .outputStore = append (c .outputStore , & plugin )
433-
434- // Create a KongPluginBinding to bind the KongPlugin to each rule match.
435- bindingName := namegen .NewKongPluginBindingName (routeName , pluginName )
436- log .Trace (logger , "Building KongPluginBinding resource" ,
437- "binding" , bindingName ,
438- "plugin" , pluginName ,
439- "kongRoute" , routeName )
440-
441- binding , err := builder .NewKongPluginBinding ().
442- WithName (bindingName ).
443- WithNamespace (c .route .Namespace ).
444- WithLabels (c .route , & pRef ).
445- WithAnnotations (c .route , & pRef ).
446- WithPluginRef (pluginName ).
447- WithControlPlaneRef (* cp ).
448- WithOwner (c .route ).
449- WithRouteRef (routeName ).Build ()
420+ pluginName := pluginPtr .Name
421+ c .outputStore = append (c .outputStore , pluginPtr )
422+
423+ // Create a KongPluginBinding to bind the KongPlugin to each KongRoute.
424+ bindingPtr , err := pluginbinding .BindingForPluginAndRoute (
425+ ctx ,
426+ logger ,
427+ c .Client ,
428+ c .route ,
429+ & pRef ,
430+ cp ,
431+ pluginName ,
432+ routeName ,
433+ )
450434 if err != nil {
451435 log .Error (logger , err , "Failed to build KongPluginBinding resource, skipping binding" ,
452- "binding" , bindingName ,
453436 "plugin" , pluginName ,
454437 "kongRoute" , routeName )
455- translationErrors = append (translationErrors , fmt .Errorf ("failed to build KongPluginBinding %s: %w" , bindingName , err ))
438+ translationErrors = append (translationErrors , fmt .Errorf ("failed to build KongPluginBinding for plugin %s: %w" , pluginName , err ))
456439 continue
457440 }
458- c .outputStore = append (c .outputStore , & binding )
441+ bindingName := bindingPtr .Name
442+ c .outputStore = append (c .outputStore , bindingPtr )
459443
460- log .Debug (logger , "Successfully built KongPlugin and KongPluginBinding resources" ,
444+ log .Debug (logger , "Successfully translated KongPlugin and KongPluginBinding resources" ,
461445 "plugin" , pluginName ,
462446 "binding" , bindingName )
463447 }
0 commit comments