-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(konnect): make KongPlugin reconciler create KongPluginBindings f…
…or plugins attached to KongRoutes and KongServices (#644) * feat(konnect): make KongPlugin reconciler create KongPluginBindings for plugins attached to KongRoutes and KongServices * refactor: add annotations package with ExtractPlugins() for plugin names extraction based on konghq.com/plugins annotation * chore: add WithOwnerReference to KongPluginBuilder --------- Co-authored-by: Grzegorz Burzyński <czeslavo@gmail.com>
- Loading branch information
Showing
18 changed files
with
1,351 additions
and
180 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
config/samples/konnect-kongpluginbinding-kongservice-kongroute.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
--- | ||
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 | ||
--- | ||
kind: KongService | ||
apiVersion: configuration.konghq.com/v1alpha1 | ||
metadata: | ||
name: service-1 | ||
namespace: default | ||
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 | ||
--- | ||
apiVersion: configuration.konghq.com/v1 | ||
kind: KongPlugin | ||
metadata: | ||
name: rate-limit-5-min | ||
config: | ||
minute: 5 | ||
policy: local | ||
plugin: rate-limiting | ||
--- | ||
apiVersion: configuration.konghq.com/v1alpha1 | ||
kind: KongPluginBinding | ||
metadata: | ||
name: plugin-binding-kongservice-kongroute | ||
spec: | ||
controlPlaneRef: | ||
type: konnectNamespacedRef | ||
konnectNamespacedRef: | ||
name: demo-cp | ||
pluginRef: | ||
name: rate-limit-5-min | ||
targets: | ||
serviceRef: | ||
name: service-1 | ||
kind: KongService | ||
group: configuration.konghq.com | ||
routeRef: | ||
name: route-1 | ||
kind: KongRoute | ||
group: configuration.konghq.com |
69 changes: 69 additions & 0 deletions
69
config/samples/konnect-kongservice-and-kongroute-plugin-annotated.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
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 and KongRoute | ||
# hence it will create 1 KongPluginBinding with both of those set as targets. | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
||
configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1" | ||
) | ||
|
||
const ( | ||
// IndexFieldKongRouteOnReferencedPluginNames is the index field for KongRoute -> KongPlugin. | ||
IndexFieldKongRouteOnReferencedPluginNames = "kongRouteKongPluginRef" | ||
) | ||
|
||
// IndexOptionsForKongRoute returns required Index options for KongRoute reconciler. | ||
func IndexOptionsForKongRoute() []ReconciliationIndexOption { | ||
return []ReconciliationIndexOption{ | ||
{ | ||
IndexObject: &configurationv1alpha1.KongRoute{}, | ||
IndexField: IndexFieldKongRouteOnReferencedPluginNames, | ||
ExtractValue: kongRouteUsesPlugins, | ||
}, | ||
} | ||
} | ||
|
||
func kongRouteUsesPlugins(object client.Object) []string { | ||
route, ok := object.(*configurationv1alpha1.KongRoute) | ||
if !ok { | ||
return nil | ||
} | ||
return annotations.ExtractPlugins(route) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package konnect | ||
|
||
import ( | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
"github.com/kong/gateway-operator/pkg/annotations" | ||
|
||
configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1" | ||
) | ||
|
||
const ( | ||
// IndexFieldKongServiceOnReferencedPluginNames is the index field for KongService -> KongPlugin. | ||
IndexFieldKongServiceOnReferencedPluginNames = "kongServiceKongPluginRef" | ||
) | ||
|
||
// IndexOptionsForKongService returns required Index options for KongService reconciler. | ||
func IndexOptionsForKongService() []ReconciliationIndexOption { | ||
return []ReconciliationIndexOption{ | ||
{ | ||
IndexObject: &configurationv1alpha1.KongService{}, | ||
IndexField: IndexFieldKongServiceOnReferencedPluginNames, | ||
ExtractValue: kongServiceUsesPlugins, | ||
}, | ||
} | ||
} | ||
|
||
func kongServiceUsesPlugins(object client.Object) []string { | ||
svc, ok := object.(*configurationv1alpha1.KongService) | ||
if !ok { | ||
return nil | ||
} | ||
|
||
return annotations.ExtractPlugins(svc) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package konnect | ||
|
||
import ( | ||
"fmt" | ||
|
||
"k8s.io/apimachinery/pkg/runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" | ||
|
||
configurationv1alpha1 "github.com/kong/kubernetes-configuration/api/configuration/v1alpha1" | ||
) | ||
|
||
// KongPluginBindingBuilder helps to build KongPluginBinding objects. | ||
type KongPluginBindingBuilder struct { | ||
binding *configurationv1alpha1.KongPluginBinding | ||
} | ||
|
||
// NewKongPluginBindingBuilder creates a new KongPluginBindingBuilder. | ||
func NewKongPluginBindingBuilder() *KongPluginBindingBuilder { | ||
return &KongPluginBindingBuilder{ | ||
binding: &configurationv1alpha1.KongPluginBinding{}, | ||
} | ||
} | ||
|
||
// WithName sets the name of the KongPluginBinding. | ||
func (b *KongPluginBindingBuilder) WithName(name string) *KongPluginBindingBuilder { | ||
b.binding.Name = name | ||
return b | ||
} | ||
|
||
// WithGenerateName sets the generate name of the KongPluginBinding. | ||
func (b *KongPluginBindingBuilder) WithGenerateName(name string) *KongPluginBindingBuilder { | ||
b.binding.GenerateName = name | ||
return b | ||
} | ||
|
||
// WithNamespace sets the namespace of the KongPluginBinding. | ||
func (b *KongPluginBindingBuilder) WithNamespace(namespace string) *KongPluginBindingBuilder { | ||
b.binding.Namespace = namespace | ||
return b | ||
} | ||
|
||
// WithPluginRef sets the plugin reference of the KongPluginBinding. | ||
func (b *KongPluginBindingBuilder) WithPluginRef(pluginName string) *KongPluginBindingBuilder { | ||
b.binding.Spec.PluginReference.Name = pluginName | ||
return b | ||
} | ||
|
||
// WithControlPlaneRef 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) WithControlPlaneRef(ref *configurationv1alpha1.ControlPlaneRef) *KongPluginBindingBuilder { | ||
b.binding.Spec.ControlPlaneRef = ref | ||
return b | ||
} | ||
|
||
// WithServiceTarget sets the service target of the KongPluginBinding. | ||
func (b *KongPluginBindingBuilder) WithServiceTarget(serviceName string) *KongPluginBindingBuilder { | ||
b.binding.Spec.Targets.ServiceReference = &configurationv1alpha1.TargetRefWithGroupKind{ | ||
Group: configurationv1alpha1.GroupVersion.Group, | ||
Kind: "KongService", | ||
Name: serviceName, | ||
} | ||
return b | ||
} | ||
|
||
// WithRouteTarget sets the route target of the KongPluginBinding. | ||
func (b *KongPluginBindingBuilder) WithRouteTarget(routeName string) *KongPluginBindingBuilder { | ||
b.binding.Spec.Targets.RouteReference = &configurationv1alpha1.TargetRefWithGroupKind{ | ||
Group: configurationv1alpha1.GroupVersion.Group, | ||
Kind: "KongRoute", | ||
Name: routeName, | ||
} | ||
return b | ||
} | ||
|
||
// WithOwnerReference sets the owner reference of the KongPluginBinding. | ||
func (b *KongPluginBindingBuilder) WithOwnerReference(owner client.Object, scheme *runtime.Scheme) (*KongPluginBindingBuilder, error) { | ||
opts := []controllerutil.OwnerReferenceOption{ | ||
controllerutil.WithBlockOwnerDeletion(true), | ||
} | ||
if err := controllerutil.SetOwnerReference(owner, b.binding, scheme, opts...); err != nil { | ||
return nil, fmt.Errorf("failed to set owner reference: %w", err) | ||
} | ||
|
||
return b, nil | ||
} | ||
|
||
// Build returns the KongPluginBinding. | ||
func (b *KongPluginBindingBuilder) Build() *configurationv1alpha1.KongPluginBinding { | ||
return b.binding | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.