Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(konnect): add support for konghq.com/plugins annotation on KongConsumers #676

Merged
merged 2 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 KongConsumer
# 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
Loading