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

Kong plugins feature #72

Merged
merged 3 commits into from
Oct 23, 2023
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
3 changes: 3 additions & 0 deletions pkg/i2gw/providers/kong/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@ Current supported annotations:
in the annotation key after `.`, and the annotations value can contain multiple
header values separated by commas. All the header values for a specific header
name are intended to be ORed. Example: `konghq.com/headers.x-routing: "alpha,bravo"`.
- `konghq.com/plugins`: If specified, the values of this annotation are used to
configure plugins on the associated ingress rules. Multiple plugins can be specified
by separating values with commas. Example: `konghq.com/plugins: "plugin1,plugin2"`.

If you are reliant on any annotations not listed above, please open an issue.
1 change: 1 addition & 0 deletions pkg/i2gw/providers/kong/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const (

headersKey = "headers"
methodsKey = "methods"
pluginsKey = "plugins"
)

func kongAnnotation(suffix string) string {
Expand Down
1 change: 1 addition & 0 deletions pkg/i2gw/providers/kong/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func newConverter(conf *i2gw.ProviderConf) *converter {
featureParsers: []i2gw.FeatureParser{
headerMatchingFeature,
methodMatchingFeature,
pluginsFeature,
},
}
}
Expand Down
13 changes: 12 additions & 1 deletion pkg/i2gw/providers/kong/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func Test_ToGateway(t *testing.T) {
expectedErrors field.ErrorList
}{
{
name: "header matching, method matching, single ingress rule",
name: "header matching, method matching, plugin, single ingress rule",
ingresses: []networkingv1.Ingress{
{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -51,6 +51,7 @@ func Test_ToGateway(t *testing.T) {
Annotations: map[string]string{
"konghq.com/headers.key1": "val1",
"konghq.com/methods": "GET,POST",
"konghq.com/plugins": "plugin1",
},
},
Spec: networkingv1.IngressSpec{
Expand Down Expand Up @@ -131,6 +132,16 @@ func Test_ToGateway(t *testing.T) {
Method: ptrTo(gatewayv1beta1.HTTPMethodPost),
},
},
Filters: []gatewayv1beta1.HTTPRouteFilter{
{
Type: gatewayv1beta1.HTTPRouteFilterExtensionRef,
ExtensionRef: &gatewayv1beta1.LocalObjectReference{
Group: kongPluginGroup,
Kind: kongPluginKind,
Name: gatewayv1beta1.ObjectName("plugin1"),
},
},
},
BackendRefs: []gatewayv1beta1.HTTPBackendRef{
{
BackendRef: gatewayv1beta1.BackendRef{
Expand Down
2 changes: 1 addition & 1 deletion pkg/i2gw/providers/kong/method_matching_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
)

func TestPathMatchingFeature(t *testing.T) {
func TestMethodMatchingFeature(t *testing.T) {
iPrefix := networkingv1.PathTypePrefix

testCases := []struct {
Expand Down
87 changes: 87 additions & 0 deletions pkg/i2gw/providers/kong/plugins.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
Copyright 2023 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package kong

import (
"strings"

"github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw"
"github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/providers/common"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/validation/field"
gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
)

const (
kongPluginGroup gatewayv1beta1.Group = "configuration.konghq.com/v1"
kongPluginKind gatewayv1beta1.Kind = "KongPlugin"
)

// pluginsFeature parses the Kong Ingress Controller plugins annotation and converts it
// into HTTPRoutes rule's ExtensionRef filters.
// It's possible to define a list of plugins to attach to the same HTTPRoute by setting
// a comma-separated list.
//
// Example: konghq.com/plugins: "plugin1,plugin2"
func pluginsFeature(ingressResources i2gw.InputResources, gatewayResources *i2gw.GatewayResources) field.ErrorList {
ruleGroups := common.GetRuleGroups(ingressResources.Ingresses)
for _, rg := range ruleGroups {
for _, rule := range rg.Rules {
key := types.NamespacedName{Namespace: rule.Ingress.Namespace, Name: common.NameFromHost(rg.Host)}
httpRoute, ok := gatewayResources.HTTPRoutes[key]
if !ok {
panic("HTTPRoute does not exist - this should never happen")
}
filters := parsePluginsAnnotation(rule.Ingress.ObjectMeta.Namespace, rule.Ingress.ObjectMeta.Name, rule.Ingress.Annotations)
patchHTTPRoutePlugins(&httpRoute, filters)
}
}
return nil
}

func parsePluginsAnnotation(ingressNamespace, ingressName string, annotations map[string]string) []gatewayv1beta1.HTTPRouteFilter {
filters := make([]gatewayv1beta1.HTTPRouteFilter, 0)
mkey := kongAnnotation(pluginsKey)
for key, val := range annotations {
if key == mkey {
filtersValues := strings.Split(val, ",")
for _, v := range filtersValues {
if v == "" {
continue
}
filters = append(filters, gatewayv1beta1.HTTPRouteFilter{
Type: gatewayv1beta1.HTTPRouteFilterExtensionRef,
ExtensionRef: &gatewayv1beta1.LocalObjectReference{
Group: kongPluginGroup,
Kind: kongPluginKind,
Name: gatewayv1beta1.ObjectName(v),
},
})
}
}
}
return filters
}

func patchHTTPRoutePlugins(httpRoute *gatewayv1beta1.HTTPRoute, extensionRefs []gatewayv1beta1.HTTPRouteFilter) {
for i := range httpRoute.Spec.Rules {
if httpRoute.Spec.Rules[i].Filters == nil {
httpRoute.Spec.Rules[i].Filters = make([]gatewayv1beta1.HTTPRouteFilter, 0)
}
httpRoute.Spec.Rules[i].Filters = append(httpRoute.Spec.Rules[i].Filters, extensionRefs...)
}
}