@@ -190,73 +190,16 @@ func addPluginsToRoute(
190
190
ingress * k8snetv1.Ingress ,
191
191
kicContent * KICContent ,
192
192
) error {
193
+ ownerName := * service .Name + "-" + * route .Name
193
194
for _ , plugin := range route .Plugins {
194
195
if plugin .Name == nil {
195
196
log .Println ("Plugin name is empty. Please provide a name for the plugin." )
196
197
continue
197
198
}
198
- pluginName := * plugin .Name
199
- kongPlugin := configurationv1.KongPlugin {
200
- TypeMeta : metav1.TypeMeta {
201
- APIVersion : ConfigurationKongHQv1 ,
202
- Kind : KongPluginKind ,
203
- },
204
- ObjectMeta : metav1.ObjectMeta {
205
- Name : calculateSlug (* service .Name + "-" + * route .Name + "-" + pluginName ),
206
- Annotations : map [string ]string {IngressClass : ClassName },
207
- },
208
- PluginName : pluginName ,
209
- }
210
199
211
- // Populate plugin fields
212
- if plugin .Enabled != nil {
213
- kongPlugin .Disabled = ! * plugin .Enabled
214
- }
215
- if plugin .RunOn != nil {
216
- kongPlugin .RunOn = * plugin .RunOn
217
- }
218
- if plugin .Ordering != nil {
219
- kongPlugin .Ordering = & kong.PluginOrdering {
220
- Before : plugin .Ordering .Before ,
221
- After : plugin .Ordering .After ,
222
- }
223
- }
224
- if plugin .Protocols != nil {
225
- var protocols []string
226
- for _ , protocol := range plugin .Protocols {
227
- if protocol != nil {
228
- protocols = append (protocols , * protocol )
229
- }
230
- }
231
- kongPlugin .Protocols = configurationv1 .StringsToKongProtocols (protocols )
232
- }
233
- if plugin .Tags != nil {
234
- var tags []string
235
- for _ , tag := range plugin .Tags {
236
- if tag != nil {
237
- tags = append (tags , * tag )
238
- }
239
- }
240
- kongPlugin .ObjectMeta .Annotations [KongHQTags ] = strings .Join (tags , "," )
241
- }
242
-
243
- configJSON , err := json .Marshal (plugin .Config )
244
- if err != nil {
200
+ if err := processPlugin (plugin , ownerName , ingress .ObjectMeta .Annotations , kicContent ); err != nil {
245
201
return err
246
202
}
247
- kongPlugin .Config = apiextensionsv1.JSON {
248
- Raw : configJSON ,
249
- }
250
-
251
- // Add plugin reference to ingress annotations
252
- pluginsAnnotation := ingress .ObjectMeta .Annotations [KongHQPlugins ]
253
- if pluginsAnnotation == "" {
254
- ingress .ObjectMeta .Annotations [KongHQPlugins ] = kongPlugin .ObjectMeta .Name
255
- } else {
256
- ingress .ObjectMeta .Annotations [KongHQPlugins ] = pluginsAnnotation + "," + kongPlugin .ObjectMeta .Name
257
- }
258
-
259
- kicContent .KongPlugins = append (kicContent .KongPlugins , kongPlugin )
260
203
}
261
204
return nil
262
205
}
@@ -518,43 +461,103 @@ func addBackendRefs(httpRoute *k8sgwapiv1.HTTPRoute, service file.FService, rout
518
461
}
519
462
520
463
func addPluginsToGatewayAPIRoute (
521
- service file.FService , route * file.FRoute , httpRoute k8sgwapiv1.HTTPRoute , kicContent * KICContent ,
464
+ service file.FService ,
465
+ route * file.FRoute ,
466
+ httpRoute k8sgwapiv1.HTTPRoute ,
467
+ kicContent * KICContent ,
522
468
) error {
469
+ // Process route-level plugins
523
470
for _ , plugin := range route .Plugins {
524
- var kongPlugin configurationv1.KongPlugin
525
- kongPlugin .APIVersion = ConfigurationKongHQv1
526
- kongPlugin .Kind = KongPluginKind
527
- if plugin .Name != nil && route .Name != nil && service .Name != nil {
528
- kongPlugin .ObjectMeta .Name = calculateSlug (* service .Name + "-" + * route .Name + "-" + * plugin .Name )
529
- } else {
530
- log .Println ("Service name, route name or plugin name is empty. This is not recommended." +
531
- "Please, provide a name for the service, route and the plugin before generating Kong Ingress Controller manifests." )
532
- continue
471
+ if err := processGatewayAPIPlugin (plugin , service , route , & httpRoute , kicContent ); err != nil {
472
+ return err
533
473
}
534
- kongPlugin .ObjectMeta .Annotations = map [string ]string {IngressClass : ClassName }
535
- kongPlugin .PluginName = * plugin .Name
474
+ }
475
+ return nil
476
+ }
536
477
537
- var configJSON apiextensionsv1.JSON
538
- var err error
539
- configJSON .Raw , err = json .Marshal (plugin .Config )
540
- if err != nil {
541
- return err
478
+ // processGatewayAPIPlugin handles creating and configuring a KongPlugin for Gateway API routes
479
+ func processGatewayAPIPlugin (
480
+ plugin * file.FPlugin ,
481
+ service file.FService ,
482
+ route * file.FRoute ,
483
+ httpRoute * k8sgwapiv1.HTTPRoute ,
484
+ kicContent * KICContent ,
485
+ ) error {
486
+ // Skip plugins without a name
487
+ if plugin .Name == nil || route .Name == nil || service .Name == nil {
488
+ log .Println ("Service name, route name or plugin name is empty. This is not recommended. " +
489
+ "Please provide a name for the service, route and the plugin before generating Kong Ingress Controller manifests." )
490
+ return nil
491
+ }
492
+
493
+ // Create the KongPlugin
494
+ kongPlugin := configurationv1.KongPlugin {
495
+ TypeMeta : metav1.TypeMeta {
496
+ APIVersion : ConfigurationKongHQv1 ,
497
+ Kind : KongPluginKind ,
498
+ },
499
+ ObjectMeta : metav1.ObjectMeta {
500
+ Name : calculateSlug (* service .Name + "-" + * route .Name + "-" + * plugin .Name ),
501
+ Annotations : map [string ]string {IngressClass : ClassName },
502
+ },
503
+ PluginName : * plugin .Name ,
504
+ }
505
+
506
+ // Set plugin configuration
507
+ configJSON , err := json .Marshal (plugin .Config )
508
+ if err != nil {
509
+ return err
510
+ }
511
+ kongPlugin .Config = apiextensionsv1.JSON {Raw : configJSON }
512
+
513
+ // Populate additional plugin fields if they exist
514
+ if plugin .Enabled != nil {
515
+ kongPlugin .Disabled = ! * plugin .Enabled
516
+ }
517
+ if plugin .RunOn != nil {
518
+ kongPlugin .RunOn = * plugin .RunOn
519
+ }
520
+ if plugin .Ordering != nil {
521
+ kongPlugin .Ordering = & kong.PluginOrdering {
522
+ Before : plugin .Ordering .Before ,
523
+ After : plugin .Ordering .After ,
542
524
}
543
- kongPlugin .Config = configJSON
544
-
545
- // add plugins as extensionRef under filters for every rule
546
- for i := range httpRoute .Spec .Rules {
547
- httpRoute .Spec .Rules [i ].Filters = append (httpRoute .Spec .Rules [i ].Filters , k8sgwapiv1.HTTPRouteFilter {
548
- ExtensionRef : & k8sgwapiv1.LocalObjectReference {
549
- Name : k8sgwapiv1 .ObjectName (kongPlugin .ObjectMeta .Name ),
550
- Kind : KongPluginKind ,
551
- Group : ConfigurationKongHQ ,
552
- },
553
- Type : k8sgwapiv1 .HTTPRouteFilterExtensionRef ,
554
- })
525
+ }
526
+ if plugin .Protocols != nil {
527
+ var protocols []string
528
+ for _ , protocol := range plugin .Protocols {
529
+ if protocol != nil {
530
+ protocols = append (protocols , * protocol )
531
+ }
532
+ }
533
+ kongPlugin .Protocols = configurationv1 .StringsToKongProtocols (protocols )
534
+ }
535
+ if plugin .Tags != nil {
536
+ var tags []string
537
+ for _ , tag := range plugin .Tags {
538
+ if tag != nil {
539
+ tags = append (tags , * tag )
540
+ }
555
541
}
542
+ if kongPlugin .ObjectMeta .Annotations == nil {
543
+ kongPlugin .ObjectMeta .Annotations = make (map [string ]string )
544
+ }
545
+ kongPlugin .ObjectMeta .Annotations [KongHQTags ] = strings .Join (tags , "," )
546
+ }
556
547
557
- kicContent .KongPlugins = append (kicContent .KongPlugins , kongPlugin )
548
+ // Add plugins as extensionRef under filters for every rule
549
+ for i := range httpRoute .Spec .Rules {
550
+ httpRoute .Spec .Rules [i ].Filters = append (httpRoute .Spec .Rules [i ].Filters , k8sgwapiv1.HTTPRouteFilter {
551
+ ExtensionRef : & k8sgwapiv1.LocalObjectReference {
552
+ Name : k8sgwapiv1 .ObjectName (kongPlugin .ObjectMeta .Name ),
553
+ Kind : KongPluginKind ,
554
+ Group : ConfigurationKongHQ ,
555
+ },
556
+ Type : k8sgwapiv1 .HTTPRouteFilterExtensionRef ,
557
+ })
558
558
}
559
+
560
+ // Add the KongPlugin to the content
561
+ kicContent .KongPlugins = append (kicContent .KongPlugins , kongPlugin )
559
562
return nil
560
563
}
0 commit comments