@@ -14,13 +14,15 @@ 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"
19- "github.com/kong/kong-operator/controller/hybridgateway/metadata"
20- "github.com/kong/kong-operator/controller/hybridgateway/namegen"
18+ "github.com/kong/kong-operator/controller/hybridgateway/kongroute"
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"
23+ "github.com/kong/kong-operator/controller/hybridgateway/service"
2324 "github.com/kong/kong-operator/controller/hybridgateway/target"
25+ "github.com/kong/kong-operator/controller/hybridgateway/upstream"
2426 "github.com/kong/kong-operator/controller/hybridgateway/utils"
2527 "github.com/kong/kong-operator/controller/pkg/log"
2628 gwtypes "github.com/kong/kong-operator/internal/types"
@@ -334,31 +336,19 @@ func (c *httpRouteConverter) translate(ctx context.Context, logger logr.Logger)
334336 "filterCount" , len (rule .Filters ))
335337
336338 // Build the KongUpstream resource.
337- upstreamName := namegen .NewKongUpstreamName (cp , rule )
338- log .Trace (logger , "Building KongUpstream resource" ,
339- "upstream" , upstreamName ,
340- "controlPlane" , cp .KonnectNamespacedRef )
341-
342- upstream , err := builder .NewKongUpstream ().
343- WithName (upstreamName ).
344- WithNamespace (c .route .Namespace ).
345- WithLabels (c .route , & pRef ).
346- WithAnnotations (c .route , & pRef ).
347- WithSpecName (upstreamName ).
348- WithControlPlaneRef (* cp ).
349- WithOwner (c .route ).Build ()
339+ upstreamPtr , err := upstream .UpstreamForRule (ctx , logger , c .Client , c .route , rule , & pRef , cp )
350340 if err != nil {
351- log .Error (logger , err , "Failed to build KongUpstream resource, skipping rule" ,
352- "upstream" , upstreamName ,
341+ log .Error (logger , err , "Failed to translate KongUpstream resource for rule, skipping rule" ,
353342 "controlPlane" , cp .KonnectNamespacedRef )
354- translationErrors = append (translationErrors , fmt .Errorf ("failed to build KongUpstream %s : %w" , upstreamName , err ))
343+ translationErrors = append (translationErrors , fmt .Errorf ("failed to translate KongUpstream resource : %w" , err ))
355344 continue
356345 }
357- c .outputStore = append (c .outputStore , & upstream )
358- log .Debug (logger , "Successfully built KongUpstream resource" ,
346+ upstreamName := upstreamPtr .Name
347+ c .outputStore = append (c .outputStore , upstreamPtr )
348+ log .Debug (logger , "Successfully translated KongUpstream resource" ,
359349 "upstream" , upstreamName )
360350
361- // Build the KongTarget resources using the new rule-based approach .
351+ // Build the KongTarget resources.
362352 targets , err := target .TargetsForBackendRefs (
363353 ctx ,
364354 logger .WithValues ("upstream" , upstreamName ),
@@ -372,135 +362,86 @@ func (c *httpRouteConverter) translate(ctx context.Context, logger logr.Logger)
372362 c .clusterDomain ,
373363 )
374364 if err != nil {
375- log .Error (logger , err , "Failed to create KongTarget resources for rule, skipping rule" ,
365+ log .Error (logger , err , "Failed to translate KongTarget resources for rule, skipping rule" ,
376366 "upstream" , upstreamName ,
377367 "backendRefs" , rule .BackendRefs ,
378368 "parentRef" , pRef )
379- translationErrors = append (translationErrors , fmt .Errorf ("failed to create KongTarget resources for upstream %s: %w" , upstreamName , err ))
369+ translationErrors = append (translationErrors , fmt .Errorf ("failed to translate KongTarget resources for upstream %s: %w" , upstreamName , err ))
380370 continue
381371 }
382- log .Debug (logger , "Successfully created KongTarget resources" ,
372+ log .Debug (logger , "Successfully translated KongTarget resources" ,
383373 "upstream" , upstreamName ,
384374 "targetCount" , len (targets ))
385375 for _ , tgt := range targets {
386376 c .outputStore = append (c .outputStore , & tgt )
387377 }
388378
389379 // Build the KongService resource.
390- serviceName := namegen .NewKongServiceName (cp , rule )
391- log .Trace (logger , "Building KongService resource" ,
392- "service" , serviceName ,
393- "upstream" , upstreamName )
394-
395- service , err := builder .NewKongService ().
396- WithName (serviceName ).
397- WithNamespace (c .route .Namespace ).
398- WithLabels (c .route , & pRef ).
399- WithAnnotations (c .route , & pRef ).
400- WithSpecName (serviceName ).
401- WithSpecHost (upstreamName ).
402- WithControlPlaneRef (* cp ).
403- WithOwner (c .route ).Build ()
380+ servicePtr , err := service .ServiceForRule (ctx , logger , c .Client , c .route , rule , & pRef , cp , upstreamName )
404381 if err != nil {
405- log .Error (logger , err , "Failed to build KongService resource, skipping rule" ,
406- "service " , serviceName ,
382+ log .Error (logger , err , "Failed to translate KongService resource, skipping rule" ,
383+ "controlPlane " , cp . KonnectNamespacedRef ,
407384 "upstream" , upstreamName )
408- translationErrors = append (translationErrors , fmt .Errorf ("failed to build KongService %s : %w" , serviceName , err ))
385+ translationErrors = append (translationErrors , fmt .Errorf ("failed to translate KongService for rule : %w" , err ))
409386 continue
410387 }
411- c .outputStore = append (c .outputStore , & service )
412- log .Debug (logger , "Successfully built KongService resource" ,
388+ serviceName := servicePtr .Name
389+ c .outputStore = append (c .outputStore , servicePtr )
390+ log .Debug (logger , "Successfully translated KongService resource" ,
413391 "service" , serviceName )
414392
415- // Build the kong route resource.
416- routeName := namegen .NewKongRouteName (c .route , cp , rule )
417- log .Trace (logger , "Building KongRoute resource" ,
418- "kongRoute" , routeName ,
419- "service" , serviceName ,
420- "hostnames" , hostnames ,
421- "matchCount" , len (rule .Matches ))
422-
423- routeBuilder := builder .NewKongRoute ().
424- WithName (routeName ).
425- WithNamespace (c .route .Namespace ).
426- WithLabels (c .route , & pRef ).
427- WithAnnotations (c .route , & pRef ).
428- WithSpecName (routeName ).
429- WithHosts (hostnames ).
430- WithStripPath (metadata .ExtractStripPath (c .route .Annotations )).
431- WithKongService (serviceName ).
432- WithOwner (c .route )
433- for _ , match := range rule .Matches {
434- routeBuilder = routeBuilder .WithHTTPRouteMatch (match )
435- }
436- route , err := routeBuilder .Build ()
393+ // Build the KongRoute resource.
394+ routePtr , err := kongroute .RouteForRule (ctx , logger , c .Client , c .route , rule , & pRef , cp , serviceName , hostnames )
437395 if err != nil {
438- log .Error (logger , err , "Failed to build KongRoute resource, skipping rule" ,
439- "kongRoute" , routeName ,
396+ log .Error (logger , err , "Failed to translate KongRoute resource, skipping rule" ,
440397 "service" , serviceName ,
441398 "hostnames" , hostnames )
442- translationErrors = append (translationErrors , fmt .Errorf ("failed to build KongRoute %s : %w" , routeName , err ))
399+ translationErrors = append (translationErrors , fmt .Errorf ("failed to translate KongRoute for rule : %w" , err ))
443400 continue
444401 }
445- c .outputStore = append (c .outputStore , & route )
446- log .Debug (logger , "Successfully built KongRoute resource" ,
447- "kongRoute" , routeName )
402+ routeName := routePtr .Name
403+ c .outputStore = append (c .outputStore , routePtr )
404+ log .Debug (logger , "Successfully translated KongRoute resource" ,
405+ "route" , routeName )
448406
449- // Build the kong plugin and kong plugin binding resources.
407+ // Build the KongPlugin and KongPluginBinding resources.
450408 log .Debug (logger , "Processing filters for rule" ,
451409 "kongRoute" , routeName ,
452410 "filterCount" , len (rule .Filters ))
453411
454412 for _ , filter := range rule .Filters {
455- pluginName := namegen .NewKongPluginName (filter )
456-
457- log .Trace (logger , "Building KongPlugin resource" ,
458- "plugin" , pluginName ,
459- "filterType" , filter .Type )
460-
461- plugin , err := builder .NewKongPlugin ().
462- WithName (pluginName ).
463- WithNamespace (c .route .Namespace ).
464- WithLabels (c .route , & pRef ).
465- WithAnnotations (c .route , & pRef ).
466- WithFilter (filter ).
467- WithOwner (c .route ).Build ()
413+ pluginPtr , err := plugin .PluginForFilter (ctx , logger , c .Client , c .route , filter , & pRef )
468414 if err != nil {
469- log .Error (logger , err , "Failed to build KongPlugin resource, skipping filter" ,
470- "plugin" , pluginName ,
471- "filterType" , filter .Type )
472- 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 ))
473418 continue
474419 }
475- c .outputStore = append (c .outputStore , & plugin )
476-
477- // Create a KongPluginBinding to bind the KongPlugin to each rule match.
478- bindingName := namegen .NewKongPluginBindingName (routeName , pluginName )
479- log .Trace (logger , "Building KongPluginBinding resource" ,
480- "binding" , bindingName ,
481- "plugin" , pluginName ,
482- "kongRoute" , routeName )
483-
484- binding , err := builder .NewKongPluginBinding ().
485- WithName (bindingName ).
486- WithNamespace (c .route .Namespace ).
487- WithLabels (c .route , & pRef ).
488- WithAnnotations (c .route , & pRef ).
489- WithPluginRef (pluginName ).
490- WithControlPlaneRef (* cp ).
491- WithOwner (c .route ).
492- 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+ )
493434 if err != nil {
494435 log .Error (logger , err , "Failed to build KongPluginBinding resource, skipping binding" ,
495- "binding" , bindingName ,
496436 "plugin" , pluginName ,
497437 "kongRoute" , routeName )
498- 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 ))
499439 continue
500440 }
501- c .outputStore = append (c .outputStore , & binding )
441+ bindingName := bindingPtr .Name
442+ c .outputStore = append (c .outputStore , bindingPtr )
502443
503- log .Debug (logger , "Successfully built KongPlugin and KongPluginBinding resources" ,
444+ log .Debug (logger , "Successfully translated KongPlugin and KongPluginBinding resources" ,
504445 "plugin" , pluginName ,
505446 "binding" , bindingName )
506447 }
0 commit comments