Skip to content

Commit

Permalink
feat(konnect): add support for konghq.com/plugins annotation on KongC…
Browse files Browse the repository at this point in the history
…onsumerGroups (#684)
  • Loading branch information
pmalek authored Oct 3, 2024
1 parent 4ecf237 commit f189f2d
Show file tree
Hide file tree
Showing 8 changed files with 977 additions and 63 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
- `KongService` [#550](https://github.com/Kong/gateway-operator/pull/550)
- `KongRoute` [#644](https://github.com/Kong/gateway-operator/pull/644)
- `KongConsumer` [#676](https://github.com/Kong/gateway-operator/pull/676)
- `KongConsumerGroup` [#684](https://github.com/Kong/gateway-operator/pull/684)
These `KongPluginBinding`s are taken by the `KongPluginBinding` reconciler
to create the corresponding plugin objects in Konnect.
- `KongConsumer` associated with `ConsumerGroups` is now reconciled in Konnect by removing/adding
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
kind: KonnectAPIAuthConfiguration
apiVersion: konnect.konghq.com/v1alpha1
metadata:
name: demo-auth
namespace: default
spec:
type: token
token: kpat_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
serverURL: eu.api.konghq.tech
---
kind: KonnectGatewayControlPlane
apiVersion: konnect.konghq.com/v1alpha1
metadata:
name: demo-cp
namespace: default
spec:
name: demo-cp
labels:
app: demo-cp
key1: demo-cp
konnect:
authRef:
name: demo-auth
# namespace not required if APIAuthConfiguration is in the same namespace
---
# This KongPlugin is bound to both the KongService, KongRoute and KongConsumerGroup
# hence it will create 2 KongPluginBinding with the following targets:
# - KongService and KongConsumerGroup
# - KongRoute and KongConsumerGroup
apiVersion: configuration.konghq.com/v1
kind: KongPlugin
metadata:
name: rate-limit-5-min
namespace: default
config:
minute: 5
policy: local
plugin: rate-limiting
---
kind: KongService
apiVersion: configuration.konghq.com/v1alpha1
metadata:
name: service-1
namespace: default
annotations:
konghq.com/plugins: rate-limit-5-min
spec:
name: service-1
host: example.com
controlPlaneRef:
type: konnectNamespacedRef
konnectNamespacedRef:
name: demo-cp
---
kind: KongRoute
apiVersion: configuration.konghq.com/v1alpha1
metadata:
name: route-1
namespace: default
annotations:
konghq.com/plugins: rate-limit-5-min
spec:
name: route-1
protocols:
- http
hosts:
- example.com
serviceRef:
type: namespacedRef
namespacedRef:
name: service-1
---
kind: KongConsumerGroup
apiVersion: configuration.konghq.com/v1beta1
metadata:
name: consumer-group-1
namespace: default
annotations:
konghq.com/plugins: rate-limit-5-min
spec:
name: consumer-group-1
controlPlaneRef:
type: konnectNamespacedRef
konnectNamespacedRef:
name: test1
33 changes: 33 additions & 0 deletions controller/konnect/index_kongconsumergroup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package konnect

import (
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/kong/gateway-operator/pkg/annotations"

configurationv1beta1 "github.com/kong/kubernetes-configuration/api/configuration/v1beta1"
)

const (
// IndexFieldKongConsumerGroupOnPlugin is the index field for KongConsumerGroup -> KongPlugin.
IndexFieldKongConsumerGroupOnPlugin = "consumerGroupPluginRef"
)

// IndexOptionsForKongConsumerGroup returns required Index options for KongConsumerGroup reconciler.
func IndexOptionsForKongConsumerGroup() []ReconciliationIndexOption {
return []ReconciliationIndexOption{
{
IndexObject: &configurationv1beta1.KongConsumerGroup{},
IndexField: IndexFieldKongConsumerGroupOnPlugin,
ExtractValue: kongConsumerGroupReferencesKongPluginsViaAnnotation,
},
}
}

func kongConsumerGroupReferencesKongPluginsViaAnnotation(object client.Object) []string {
consumerGroup, ok := object.(*configurationv1beta1.KongConsumerGroup)
if !ok {
return nil
}
return annotations.ExtractPluginsWithNamespaces(consumerGroup)
}
Loading

0 comments on commit f189f2d

Please sign in to comment.