Skip to content

Commit

Permalink
added notifications in the common package
Browse files Browse the repository at this point in the history
  • Loading branch information
Devaansh-Kumar committed Aug 15, 2024
1 parent 95d5e4d commit f0bf4ab
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 12 deletions.
5 changes: 5 additions & 0 deletions pkg/i2gw/notifications/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ type NotificationAggregator struct {

var NotificationAggr NotificationAggregator

// NotificationCallback is a callback function used to send notifications from within the common
// package without the common package having knowledge about which provider is making a call it
type NotificationCallback func(mType MessageType, message string, callingObject ...client.Object)

// DispatchNotification is used to send a notification to the NotificationAggregator
func (na *NotificationAggregator) DispatchNotification(notification Notification, ProviderName string) {
na.mutex.Lock()
Expand Down Expand Up @@ -84,6 +88,7 @@ func (na *NotificationAggregator) CreateNotificationTables() map[string]string {
return notificationTablesMap
}

// convertObjectsToStr takes a slice of client.Object as input and extracts the Kind and Namespaced Name
func convertObjectsToStr(ob []client.Object) string {
var sb strings.Builder

Expand Down
2 changes: 1 addition & 1 deletion pkg/i2gw/providers/apisix/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (c *converter) convert(storage *storage) (i2gw.GatewayResources, field.Erro
}
// Convert plain ingress resources to gateway resources, ignoring all
// provider-specific features.
gatewayResources, errs := common.ToGateway(ingressList, c.implementationSpecificOptions)
gatewayResources, errs := common.ToGateway(ingressList, c.implementationSpecificOptions, notify)
if len(errs) > 0 {
return i2gw.GatewayResources{}, errs
}
Expand Down
26 changes: 21 additions & 5 deletions pkg/i2gw/providers/common/converter.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"
networkingv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand All @@ -31,9 +32,21 @@ import (

// ToGateway converts the received ingresses to i2gw.GatewayResources,
// without taking into consideration any provider specific logic.
func ToGateway(ingresses []networkingv1.Ingress, options i2gw.ProviderImplementationSpecificOptions) (i2gw.GatewayResources, field.ErrorList) {
//
// If a provider wishes to recieve notifications from the common package,
// it can pass a notifications.NotificationCallback function which can be used
// to send notifications on behalf of the provider while keeping the logic of
// ToGateway seperated from the provider.
func ToGateway(ingresses []networkingv1.Ingress, options i2gw.ProviderImplementationSpecificOptions, notifyOpts ...notifications.NotificationCallback) (i2gw.GatewayResources, field.ErrorList) {
aggregator := ingressAggregator{ruleGroups: map[ruleGroupKey]*ingressRuleGroup{}}

var notify notifications.NotificationCallback
if len(notifyOpts) > 0 {
notify = notifyOpts[0]
} else {
notify = noNotifications
}

var errs field.ErrorList
for _, ingress := range ingresses {
aggregator.addIngress(ingress)
Expand All @@ -42,7 +55,7 @@ func ToGateway(ingresses []networkingv1.Ingress, options i2gw.ProviderImplementa
return i2gw.GatewayResources{}, errs
}

routes, gateways, errs := aggregator.toHTTPRoutesAndGateways(options)
routes, gateways, errs := aggregator.toHTTPRoutesAndGateways(options, notify)
if len(errs) > 0 {
return i2gw.GatewayResources{}, errs
}
Expand Down Expand Up @@ -166,7 +179,7 @@ func (a *ingressAggregator) addIngressRule(namespace, name, ingressClass string,
rg.rules = append(rg.rules, ingressRule{rule: rule})
}

func (a *ingressAggregator) toHTTPRoutesAndGateways(options i2gw.ProviderImplementationSpecificOptions) ([]gatewayv1.HTTPRoute, []gatewayv1.Gateway, field.ErrorList) {
func (a *ingressAggregator) toHTTPRoutesAndGateways(options i2gw.ProviderImplementationSpecificOptions, notify notifications.NotificationCallback) ([]gatewayv1.HTTPRoute, []gatewayv1.Gateway, field.ErrorList) {
var httpRoutes []gatewayv1.HTTPRoute
var errors field.ErrorList
listenersByNamespacedGateway := map[string][]gatewayv1.Listener{}
Expand All @@ -187,7 +200,7 @@ func (a *ingressAggregator) toHTTPRoutesAndGateways(options i2gw.ProviderImpleme
}
gwKey := fmt.Sprintf("%s/%s", rg.namespace, rg.ingressClass)
listenersByNamespacedGateway[gwKey] = append(listenersByNamespacedGateway[gwKey], listener)
httpRoute, errs := rg.toHTTPRoute(options)
httpRoute, errs := rg.toHTTPRoute(options, notify)
httpRoutes = append(httpRoutes, httpRoute)
errors = append(errors, errs...)
}
Expand Down Expand Up @@ -222,6 +235,7 @@ func (a *ingressAggregator) toHTTPRoutesAndGateways(options i2gw.ProviderImpleme
})
}

notify(notifications.InfoNotification, fmt.Sprintf("successfully converted to HTTPRoute \"%v/%v\"", httpRoute.Namespace, httpRoute.Name), mockIngressFromDefaultBackend(db))
httpRoutes = append(httpRoutes, httpRoute)
}

Expand Down Expand Up @@ -273,12 +287,13 @@ func (a *ingressAggregator) toHTTPRoutesAndGateways(options i2gw.ProviderImpleme
var gateways []gatewayv1.Gateway
for _, gw := range gatewaysByKey {
gateways = append(gateways, *gw)
notify(notifications.InfoNotification, fmt.Sprintf("successfully created Gateway \"%v/%v\"", gw.Namespace, gw.Name))
}

return httpRoutes, gateways, errors
}

func (rg *ingressRuleGroup) toHTTPRoute(options i2gw.ProviderImplementationSpecificOptions) (gatewayv1.HTTPRoute, field.ErrorList) {
func (rg *ingressRuleGroup) toHTTPRoute(options i2gw.ProviderImplementationSpecificOptions, notify notifications.NotificationCallback) (gatewayv1.HTTPRoute, field.ErrorList) {
ingressPathsByMatchKey := groupIngressPathsByMatchKey(rg.rules)
httpRoute := gatewayv1.HTTPRoute{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -322,6 +337,7 @@ func (rg *ingressRuleGroup) toHTTPRoute(options i2gw.ProviderImplementationSpeci
httpRoute.Spec.Rules = append(httpRoute.Spec.Rules, hrRule)
}

notify(notifications.InfoNotification, fmt.Sprintf("successfully converted to HTTPRoute \"%v/%v\"", httpRoute.Namespace, httpRoute.Name), mockIngressFromRuleGroup(rg))
return httpRoute, errors
}

Expand Down
3 changes: 1 addition & 2 deletions pkg/i2gw/providers/common/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,8 +416,7 @@ func Test_ingresses2GatewaysAndHttpRoutes(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {

gatewayResources, errs := ToGateway(tc.ingresses, i2gw.ProviderImplementationSpecificOptions{})

gatewayResources, errs := ToGateway(tc.ingresses, i2gw.ProviderImplementationSpecificOptions{}, noNotifications)
if len(gatewayResources.HTTPRoutes) != len(tc.expectedGatewayResources.HTTPRoutes) {
t.Errorf("Expected %d HTTPRoutes, got %d: %+v",
len(tc.expectedGatewayResources.HTTPRoutes), len(gatewayResources.HTTPRoutes), gatewayResources.HTTPRoutes)
Expand Down
30 changes: 30 additions & 0 deletions pkg/i2gw/providers/common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ import (
"fmt"
"regexp"

"github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/notifications"
networkingv1 "k8s.io/api/networking/v1"
networkingv1beta1 "k8s.io/api/networking/v1beta1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/validation/field"
"sigs.k8s.io/controller-runtime/pkg/client"
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
)

Expand Down Expand Up @@ -200,3 +203,30 @@ func removeBackendRefsDuplicates(backendRefs []gatewayv1.HTTPBackendRef) []gatew
}
return uniqueBackendRefs
}

func noNotifications(mType notifications.MessageType, message string, callingObject ...client.Object) {
}

func mockIngressFromRuleGroup(rg *ingressRuleGroup) client.Object {
return &networkingv1.Ingress{
TypeMeta: v1.TypeMeta{
Kind: "Ingress",
},
ObjectMeta: v1.ObjectMeta{
Name: rg.name,
Namespace: rg.namespace,
},
}
}

func mockIngressFromDefaultBackend(db ingressDefaultBackend) client.Object {
return &networkingv1.Ingress{
TypeMeta: v1.TypeMeta{
Kind: "Ingress",
},
ObjectMeta: v1.ObjectMeta{
Name: db.name,
Namespace: db.namespace,
},
}
}
2 changes: 1 addition & 1 deletion pkg/i2gw/providers/gce/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (c *converter) convert(storage *storage) (i2gw.GatewayResources, field.Erro

// Convert plain ingress resources to gateway resources, ignoring all
// provider-specific features.
gatewayResources, errs := common.ToGateway(ingressList, c.implementationSpecificOptions)
gatewayResources, errs := common.ToGateway(ingressList, c.implementationSpecificOptions, notify)
if len(errs) > 0 {
return i2gw.GatewayResources{}, errs
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/i2gw/providers/gce/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))
}
2 changes: 1 addition & 1 deletion pkg/i2gw/providers/ingressnginx/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (c *converter) convert(storage *storage) (i2gw.GatewayResources, field.Erro

// Convert plain ingress resources to gateway resources, ignoring all
// provider-specific features.
gatewayResources, errs := common.ToGateway(ingressList, i2gw.ProviderImplementationSpecificOptions{})
gatewayResources, errs := common.ToGateway(ingressList, i2gw.ProviderImplementationSpecificOptions{}, notify)
if len(errs) > 0 {
return i2gw.GatewayResources{}, errs
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/i2gw/providers/kong/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (c *converter) convert(storage *storage) (i2gw.GatewayResources, field.Erro

// Convert plain ingress resources to gateway resources, ignoring all
// provider-specific features.
gatewayResources, errs := common.ToGateway(ingressList, c.implementationSpecificOptions)
gatewayResources, errs := common.ToGateway(ingressList, c.implementationSpecificOptions, notify)
if len(errs) > 0 {
errorList = append(errorList, errs...)
}
Expand Down

0 comments on commit f0bf4ab

Please sign in to comment.