Skip to content

Commit

Permalink
feat(konnect): allow binding KongConsumerGroup in KongPluginBinding (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
pmalek authored Sep 26, 2024
1 parent 3d3dbf6 commit 13e9586
Show file tree
Hide file tree
Showing 11 changed files with 200 additions and 107 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
- `KongService` [#550](https://github.com/Kong/gateway-operator/pull/550)
- `KongRoute` [#644](https://github.com/Kong/gateway-operator/pull/644)
- `KongConsumer` [#652](https://github.com/Kong/gateway-operator/pull/652)
- `KongConsumerGroup` [#654](https://github.com/Kong/gateway-operator/pull/654)
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
Expand Up @@ -7,7 +7,7 @@ metadata:
spec:
type: token
token: kpat_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
serverURL: eu.api.konghq.tech
serverURL: eu.api.konghq.tech
---
kind: KonnectGatewayControlPlane
apiVersion: konnect.konghq.com/v1alpha1
Expand Down Expand Up @@ -46,14 +46,13 @@ config:
policy: local
plugin: rate-limiting
---
kind: KongConsumer
apiVersion: configuration.konghq.com/v1
kind: KongConsumerGroup
apiVersion: configuration.konghq.com/v1beta1
metadata:
name: consumer-for-service-plugin-binding
name: consumer-group-for-service-pluginbinding
namespace: default
username: consumer-for-service-plugin-binding
custom_id: 08433C12-2B81-4738-B61D-3AA2136F0102
spec:
name: consumer-group-for-service-pluginbinding
controlPlaneRef:
type: konnectNamespacedRef
konnectNamespacedRef:
Expand All @@ -62,7 +61,7 @@ spec:
apiVersion: configuration.konghq.com/v1alpha1
kind: KongPluginBinding
metadata:
name: plugin-binding-kongservice-kongroute
name: plugin-binding-kongservice-kongconsumergroup
spec:
controlPlaneRef:
type: konnectNamespacedRef
Expand All @@ -75,5 +74,5 @@ spec:
name: service-1
kind: KongService
group: configuration.konghq.com
consumerRef:
name: consumer-for-service-plugin-binding
consumerGroupRef:
name: consumer-group-for-service-pluginbinding
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ metadata:
spec:
type: token
token: kpat_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
serverURL: eu.api.konghq.tech
serverURL: eu.api.konghq.tech
---
kind: KonnectGatewayControlPlane
apiVersion: konnect.konghq.com/v1alpha1
Expand Down
2 changes: 1 addition & 1 deletion config/samples/konnect-kongpluginbinding.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ metadata:
spec:
type: token
token: kpat_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
serverURL: eu.api.konghq.tech
serverURL: eu.api.konghq.tech
---
kind: KonnectGatewayControlPlane
apiVersion: konnect.konghq.com/v1alpha1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ metadata:
spec:
type: token
token: kpat_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
serverURL: eu.api.konghq.tech
serverURL: eu.api.konghq.tech
---
kind: KonnectGatewayControlPlane
apiVersion: konnect.konghq.com/v1alpha1
Expand Down
2 changes: 1 addition & 1 deletion config/samples/konnect-kongservice-plugin-annotated.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ metadata:
spec:
type: token
token: kpat_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
serverURL: eu.api.konghq.tech
serverURL: eu.api.konghq.tech
---
kind: KonnectGatewayControlPlane
apiVersion: konnect.konghq.com/v1alpha1
Expand Down
30 changes: 30 additions & 0 deletions controller/konnect/kongpluginbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,36 @@ func (b *KongPluginBindingBuilder) WithControlPlaneRef(ref *configurationv1alpha
return b
}

// WithControlPlaneRefKonnectNamespaced sets the control plane reference of the KongPluginBinding.
// NOTE: Users have to ensure that the ControlPlaneRef that's set here
// is the same across all the KongPluginBinding targets.
func (b *KongPluginBindingBuilder) WithControlPlaneRefKonnectNamespaced(name string) *KongPluginBindingBuilder {
b.binding.Spec.ControlPlaneRef = &configurationv1alpha1.ControlPlaneRef{
Type: configurationv1alpha1.ControlPlaneRefKonnectNamespacedRef,
KonnectNamespacedRef: &configurationv1alpha1.KonnectNamespacedRef{
Name: name,
},
}

return b
}

// WithConsumerTarget sets the consumer target of the KongPluginBinding.
func (b *KongPluginBindingBuilder) WithConsumerTarget(consumer string) *KongPluginBindingBuilder {
b.binding.Spec.Targets.ConsumerReference = &configurationv1alpha1.TargetRef{
Name: consumer,
}
return b
}

// WithConsumerGroupTarget sets the consumer group target of the KongPluginBinding.
func (b *KongPluginBindingBuilder) WithConsumerGroupTarget(cg string) *KongPluginBindingBuilder {
b.binding.Spec.Targets.ConsumerGroupReference = &configurationv1alpha1.TargetRef{
Name: cg,
}
return b
}

// WithServiceTarget sets the service target of the KongPluginBinding.
func (b *KongPluginBindingBuilder) WithServiceTarget(serviceName string) *KongPluginBindingBuilder {
b.binding.Spec.Targets.ServiceReference = &configurationv1alpha1.TargetRefWithGroupKind{
Expand Down
18 changes: 14 additions & 4 deletions controller/konnect/ops/ops_kongpluginbinding.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

configurationv1 "github.com/kong/kubernetes-configuration/api/configuration/v1"
configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1"
configurationv1beta1 "github.com/kong/kubernetes-configuration/api/configuration/v1beta1"
"github.com/kong/kubernetes-configuration/pkg/metadata"
)

Expand Down Expand Up @@ -199,7 +200,6 @@ func getPluginBindingTargets(
targetObjects = append(targetObjects, &kongRoute)
}
if ref := targets.ConsumerReference; ref != nil {

kongConsumer := configurationv1.KongConsumer{}
kongConsumer.SetName(ref.Name)
kongConsumer.SetNamespace(pluginBinding.GetNamespace())
Expand All @@ -208,8 +208,15 @@ func getPluginBindingTargets(
}
targetObjects = append(targetObjects, &kongConsumer)
}

// TODO: https://github.com/Kong/gateway-operator/issues/527 add support for KongConsumerGroup
if ref := targets.ConsumerGroupReference; ref != nil {
kongConsumerGroup := configurationv1beta1.KongConsumerGroup{}
kongConsumerGroup.SetName(ref.Name)
kongConsumerGroup.SetNamespace(pluginBinding.GetNamespace())
if err := cl.Get(ctx, client.ObjectKeyFromObject(&kongConsumerGroup), &kongConsumerGroup); err != nil {
return nil, err
}
targetObjects = append(targetObjects, &kongConsumerGroup)
}

return targetObjects, nil
}
Expand Down Expand Up @@ -281,7 +288,10 @@ func kongPluginWithTargetsToKongPluginInput(
pluginInput.Consumer = &sdkkonnectcomp.PluginConsumer{
ID: lo.ToPtr(id),
}
// TODO: https://github.com/Kong/gateway-operator/issues/527 add support for KongConsumerGroup
case *configurationv1beta1.KongConsumerGroup:
pluginInput.ConsumerGroup = &sdkkonnectcomp.PluginConsumerGroup{
ID: lo.ToPtr(id),
}
default:
return nil, fmt.Errorf("unsupported target type %T", t)
}
Expand Down
4 changes: 2 additions & 2 deletions test/envtest/deploy_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ func deployKongConsumerGroupAttachedToCP(
cl client.Client,
cgName string,
cp *konnectv1alpha1.KonnectGatewayControlPlane,
) configurationv1beta1.KongConsumerGroup {
) *configurationv1beta1.KongConsumerGroup {
t.Helper()

cg := configurationv1beta1.KongConsumerGroup{
Expand All @@ -409,7 +409,7 @@ func deployKongConsumerGroupAttachedToCP(
require.NoError(t, cl.Create(ctx, &cg))
t.Logf("deployed new KongConsumerGroup %s", client.ObjectKeyFromObject(&cg))

return cg
return &cg
}

func deployKongVaultAttachedToCP(
Expand Down
Loading

0 comments on commit 13e9586

Please sign in to comment.