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
…onsumers
  • Loading branch information
pmalek committed Oct 1, 2024
1 parent 3f2109f commit 3c32a9e
Show file tree
Hide file tree
Showing 13 changed files with 743 additions and 61 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
the creation of a managed `KongPluginBinding` resource:
- `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)
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 KongCon
# hence it will create 2 KongPluginBinding with the following targets:
# - KongService and KongConsumer
# - KongRoute and KongConsumer
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: KongConsumer
apiVersion: configuration.konghq.com/v1
metadata:
name: consumer-api-key-1
namespace: default
annotations:
konghq.com/plugins: rate-limit-5-min
username: consumer1
spec:
controlPlaneRef:
type: konnectNamespacedRef
konnectNamespacedRef:
name: demo-cp
17 changes: 17 additions & 0 deletions controller/konnect/index_kongconsumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ package konnect
import (
"sigs.k8s.io/controller-runtime/pkg/client"

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

configurationv1 "github.com/kong/kubernetes-configuration/api/configuration/v1"
)

const (
// IndexFieldKongConsumerOnKongConsumerGroup is the index field for KongConsumer -> KongConsumerGroup.
IndexFieldKongConsumerOnKongConsumerGroup = "consumerGroupRef"
// IndexFieldKongConsumerOnPlugin is the index field for KongConsumer -> KongPlugin.
IndexFieldKongConsumerOnPlugin = "consumerPluginRef"
)

// IndexOptionsForKongConsumer returns required Index options for KongConsumer reconciler.
Expand All @@ -19,6 +23,11 @@ func IndexOptionsForKongConsumer() []ReconciliationIndexOption {
IndexField: IndexFieldKongConsumerOnKongConsumerGroup,
ExtractValue: kongConsumerReferencesFromKongConsumerGroup,
},
{
IndexObject: &configurationv1.KongConsumer{},
IndexField: IndexFieldKongConsumerOnPlugin,
ExtractValue: kongConsumerReferencesKongPluginsViaAnnotation,
},
}
}

Expand All @@ -29,3 +38,11 @@ func kongConsumerReferencesFromKongConsumerGroup(object client.Object) []string
}
return consumer.ConsumerGroups
}

func kongConsumerReferencesKongPluginsViaAnnotation(object client.Object) []string {
consumer, ok := object.(*configurationv1.KongConsumer)
if !ok {
return nil
}
return annotations.ExtractPluginsWithNamespaces(consumer)
}
Loading

0 comments on commit 3c32a9e

Please sign in to comment.