Skip to content

added notifications for kong provider #173

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

Merged
merged 3 commits into from
Aug 12, 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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ go.work

# GolangCI-Lint cache
/cache

# VSCode files for debugging
.vscode/
4 changes: 4 additions & 0 deletions pkg/i2gw/notifications/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,7 @@ func convertObjectsToStr(ob []client.Object) string {

return sb.String()
}

func NewNotification(mType MessageType, message string, callingObject ...client.Object) Notification {
return Notification{Type: mType, Message: message, CallingObjects: callingObject}
}
2 changes: 1 addition & 1 deletion pkg/i2gw/providers/istio/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ import (
)

func notify(mType notifications.MessageType, message string, callingObject ...client.Object) {
newNotification := notifications.Notification{Type: mType, Message: message, CallingObjects: callingObject}
newNotification := notifications.NewNotification(mType, message, callingObject...)
notifications.NotificationAggr.DispatchNotification(newNotification, string(ProviderName))
}
4 changes: 3 additions & 1 deletion pkg/i2gw/providers/kong/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,13 @@ func (c *converter) convert(storage *storage) (i2gw.GatewayResources, field.Erro
errorList = append(errorList, errs...)
}

tcpGatewayResources, errs := crds.TCPIngressToGatewayAPI(storage.TCPIngresses)
tcpGatewayResources, notificationsAggregator, errs := crds.TCPIngressToGatewayAPI(storage.TCPIngresses)
if len(errs) > 0 {
errorList = append(errorList, errs...)
}

dispatchNotification(notificationsAggregator)

if len(errorList) > 0 {
return i2gw.GatewayResources{}, errorList
}
Expand Down
20 changes: 13 additions & 7 deletions pkg/i2gw/providers/kong/crds/tcpingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"strings"

"github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw"
"github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/notifications"
"github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/providers/common"
networkingv1beta1 "k8s.io/api/networking/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -34,20 +35,21 @@ import (
)

// TCPIngressToGatewayAPI converts the received TCPingresses to i2gw.GatewayResources,
func TCPIngressToGatewayAPI(ingresses []kongv1beta1.TCPIngress) (i2gw.GatewayResources, field.ErrorList) {
func TCPIngressToGatewayAPI(ingresses []kongv1beta1.TCPIngress) (i2gw.GatewayResources, []notifications.Notification, field.ErrorList) {
aggregator := tcpIngressAggregator{ruleGroups: map[ruleGroupKey]*tcpIngressRuleGroup{}}
var notificationsAggregator []notifications.Notification

var errs field.ErrorList
for _, ingress := range ingresses {
aggregator.addIngress(ingress)
aggregator.addIngress(ingress, &notificationsAggregator)
}
if len(errs) > 0 {
return i2gw.GatewayResources{}, errs
return i2gw.GatewayResources{}, notificationsAggregator, errs
}

tcpRoutes, tlsRoutes, gateways, errs := aggregator.toRoutesAndGateways()
if len(errs) > 0 {
return i2gw.GatewayResources{}, errs
return i2gw.GatewayResources{}, notificationsAggregator, errs
}

tcpRouteByKey := make(map[types.NamespacedName]gatewayv1alpha2.TCPRoute)
Expand All @@ -72,15 +74,19 @@ func TCPIngressToGatewayAPI(ingresses []kongv1beta1.TCPIngress) (i2gw.GatewayRes
Gateways: gatewayByKey,
TCPRoutes: tcpRouteByKey,
TLSRoutes: tlsRouteByKey,
}, nil
}, notificationsAggregator, nil
}

func (a *tcpIngressAggregator) addIngress(tcpIngress kongv1beta1.TCPIngress) {
func (a *tcpIngressAggregator) addIngress(tcpIngress kongv1beta1.TCPIngress, notificationsAggregator *[]notifications.Notification) {
var ingressClass string
if _, ok := tcpIngress.Annotations[networkingv1beta1.AnnotationIngressClass]; ok {
if ingressClassAnnotation, ok := tcpIngress.Annotations[networkingv1beta1.AnnotationIngressClass]; ok {
ingressClass = tcpIngress.Annotations[networkingv1beta1.AnnotationIngressClass]
n := notifications.NewNotification(notifications.InfoNotification, fmt.Sprintf("ingress class \"%v\" taken from %v annotation", ingressClassAnnotation, networkingv1beta1.AnnotationIngressClass), &tcpIngress)
*notificationsAggregator = append(*notificationsAggregator, n)
} else {
ingressClass = tcpIngress.Name
n := notifications.NewNotification(notifications.InfoNotification, "ingress class taken from name of TCPIngress", &tcpIngress)
*notificationsAggregator = append(*notificationsAggregator, n)
}
for _, rule := range tcpIngress.Spec.Rules {
a.addIngressRule(tcpIngress.Namespace, tcpIngress.Name, ingressClass, rule, tcpIngress.Spec)
Expand Down
2 changes: 1 addition & 1 deletion pkg/i2gw/providers/kong/crds/tcpingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func TestTCPIngressToGatewayAPI(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
gatewayResources, errs := TCPIngressToGatewayAPI(tc.tcpIngresses)
gatewayResources, _, errs := TCPIngressToGatewayAPI(tc.tcpIngresses)

if len(gatewayResources.Gateways) != len(tc.expectedGatewayResources.Gateways) {
t.Errorf("Expected %d Gateways, got %d: %+v",
Expand Down
4 changes: 4 additions & 0 deletions pkg/i2gw/providers/kong/header_matching.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"strings"

"github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw"
"github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/notifications"
"github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/providers/common"
networkingv1 "k8s.io/api/networking/v1"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -81,6 +82,9 @@ func patchHTTPRouteHeaderMatching(httpRoute *gatewayv1.HTTPRoute, headerNames []
}
}
httpRoute.Spec.Rules[i].Matches = newMatches
if len(newMatches) > 0 {
notify(notifications.InfoNotification, fmt.Sprintf("parsed \"%v\" annotation of ingress and patched %v fields", kongAnnotation(headersKey), field.NewPath("httproute", "spec", "rules").Key("").Child("matches")), httpRoute)
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion pkg/i2gw/providers/kong/method_matching.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"strings"

"github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw"
"github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/notifications"
"github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/providers/common"
networkingv1 "k8s.io/api/networking/v1"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -68,12 +69,13 @@ func patchHTTPRouteMethodMatching(httpRoute *gatewayv1.HTTPRoute, methods []gate
}
if len(matches) > 0 {
httpRoute.Spec.Rules[i].Matches = matches
notify(notifications.InfoNotification, fmt.Sprintf("parsed \"%v\" annotation of ingress and patched %v fields", kongAnnotation(methodsKey), field.NewPath("httproute", "spec", "rules").Key("").Child("matches").Key("").Child("method")), httpRoute)
}
}
}

func parseMethodsAnnotation(ingressNamespace, ingressName string, annotations map[string]string) ([]gatewayv1.HTTPMethod, field.ErrorList) {
fieldPath := field.NewPath(fmt.Sprintf("%s/%s", ingressNamespace, ingressName)).Child("metadata").Child("annotations").Child("konghq.com/methods")
fieldPath := field.NewPath(fmt.Sprintf("%s/%s", ingressNamespace, ingressName)).Child("metadata").Child("annotations").Child(kongAnnotation(methodsKey))
errs := field.ErrorList{}
methods := make([]gatewayv1.HTTPMethod, 0)
mkey := kongAnnotation(methodsKey)
Expand Down
33 changes: 33 additions & 0 deletions pkg/i2gw/providers/kong/notification.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
Copyright 2024 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 (
"github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/notifications"
"sigs.k8s.io/controller-runtime/pkg/client"
)

func notify(mType notifications.MessageType, message string, callingObject ...client.Object) {
newNotification := notifications.NewNotification(mType, message, callingObject...)
notifications.NotificationAggr.DispatchNotification(newNotification, string(Name))
}

func dispatchNotification(n []notifications.Notification) {
for _, v := range n {
notify(v.Type, v.Message, v.CallingObjects...)
}
}
5 changes: 5 additions & 0 deletions pkg/i2gw/providers/kong/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ package kong

import (
"errors"
"fmt"
"strings"

"github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw"
"github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/notifications"
"github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/providers/common"
networkingv1 "k8s.io/api/networking/v1"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -81,4 +83,7 @@ func patchHTTPRoutePlugins(httpRoute *gatewayv1.HTTPRoute, extensionRefs []gatew
}
httpRoute.Spec.Rules[i].Filters = append(httpRoute.Spec.Rules[i].Filters, extensionRefs...)
}
if len(extensionRefs) != 0 {
notify(notifications.InfoNotification, fmt.Sprintf("parsed \"%v\" annotation of ingress and patched %v fields", kongAnnotation(pluginsKey), field.NewPath("httproute", "spec", "rules").Key("").Child("filters")), httpRoute)
}
}