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

Added notifications in the common package #180

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions PROVIDER.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@ package examplegateway

import "github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/notifications"

func notify(mType notifications.MessageType, message string) {
newNotification := notifications.Notification{Type: mType, Message: message}
notifications.CommonNotification.DispatchNotication(newNotification, string(ProviderName))
func notify(mType notifications.MessageType, message string, callingObjects ...client.Object){
newNotification := notifications.NewNotification(mType, message, callingObjects...)
notifications.NotificationAggr.DispatchNotification(newNotification, string(ProviderName))
}
```
7. Import the new package at `cmd/print`.
Expand Down
16 changes: 13 additions & 3 deletions pkg/i2gw/notifications/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
)

func init() {
NotificationAggr = NotificationAggregator{Notifications: map[string][]Notification{}}
NotificationAggr = BuildNotificationAggregator()
}

const (
Expand All @@ -51,6 +51,15 @@ 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, CallingObjects ...client.Object)

// BuildNotificationAggregator returns an instance of initialized NotificationAggregator
func BuildNotificationAggregator() NotificationAggregator {
return NotificationAggregator{Notifications: map[string][]Notification{}}
}

// 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 +93,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 All @@ -98,6 +108,6 @@ 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}
func NewNotification(mType MessageType, message string, callingObjects ...client.Object) Notification {
return Notification{Type: mType, Message: message, CallingObjects: callingObjects}
}
2 changes: 1 addition & 1 deletion pkg/i2gw/providers/apisix/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (c *resourcesToIRConverter) convertToIR(storage *storage) (intermediate.IR,
}
// Convert plain ingress resources to gateway resources, ignoring all
// provider-specific features.
ir, errs := common.ToIR(ingressList, c.implementationSpecificOptions)
ir, errs := common.ToIR(ingressList, c.implementationSpecificOptions, notify)
if len(errs) > 0 {
return intermediate.IR{}, errs
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/i2gw/providers/apisix/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
)

func notify(mType notifications.MessageType, message string, callingObject ...client.Object) {
newNotification := notifications.NewNotification(mType, message, callingObject...)
func notify(mType notifications.MessageType, message string, callingObjects ...client.Object) {
newNotification := notifications.NewNotification(mType, message, callingObjects...)
notifications.NotificationAggr.DispatchNotification(newNotification, string(Name))
}
34 changes: 28 additions & 6 deletions pkg/i2gw/providers/common/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,36 @@ import (

"github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw"
"github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/intermediate"
"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"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/validation/field"
"sigs.k8s.io/controller-runtime/pkg/client"
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
)

// ToIR converts the received ingresses to intermediate.IR without taking into
// consideration any provider specific logic.
func ToIR(ingresses []networkingv1.Ingress, options i2gw.ProviderImplementationSpecificOptions) (intermediate.IR, field.ErrorList) {
//
// If a provider wishes to receive 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 separated from the provider.
func ToIR(ingresses []networkingv1.Ingress, options i2gw.ProviderImplementationSpecificOptions, notifyOpts ...notifications.NotificationCallback) (intermediate.IR, field.ErrorList) {
aggregator := ingressAggregator{ruleGroups: map[ruleGroupKey]*ingressRuleGroup{}}

var notify notifications.NotificationCallback
switch len(notifyOpts) {
case 0:
notify = noNotifications
case 1:
notify = notifyOpts[0]
default:
return intermediate.IR{}, field.ErrorList{field.Invalid(field.NewPath(""), "", "number of notification callbacks exceeded. only 0 or 1 callbacks are currently supported")}
}

var errs field.ErrorList
for _, ingress := range ingresses {
aggregator.addIngress(ingress)
Expand All @@ -45,7 +62,7 @@ func ToIR(ingresses []networkingv1.Ingress, options i2gw.ProviderImplementationS
return intermediate.IR{}, errs
}

routes, gateways, errs := aggregator.toHTTPRoutesAndGateways(options)
routes, gateways, errs := aggregator.toHTTPRoutesAndGateways(options, notify)
if len(errs) > 0 {
return intermediate.IR{}, errs
}
Expand Down Expand Up @@ -169,10 +186,11 @@ 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{}
ingressByNamespacedGateway := map[string][]client.Object{}

// Sort the rulegroups to iterate the map in a sorted order.
ruleGroupsKeys := make([]ruleGroupKey, 0, len(a.ruleGroups))
Expand Down Expand Up @@ -201,7 +219,8 @@ 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)
ingressByNamespacedGateway[gwKey] = append(ingressByNamespacedGateway[gwKey], buildIngressFromRuleGroup(rg))
httpRoute, errs := rg.toHTTPRoute(options, notify)
httpRoutes = append(httpRoutes, httpRoute)
errors = append(errors, errs...)
}
Expand Down Expand Up @@ -236,6 +255,7 @@ func (a *ingressAggregator) toHTTPRoutesAndGateways(options i2gw.ProviderImpleme
})
}

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

Expand Down Expand Up @@ -285,14 +305,15 @@ func (a *ingressAggregator) toHTTPRoutesAndGateways(options i2gw.ProviderImpleme
}

var gateways []gatewayv1.Gateway
for _, gw := range gatewaysByKey {
for gwKey, gw := range gatewaysByKey {
gateways = append(gateways, *gw)
notify(notifications.InfoNotification, fmt.Sprintf("successfully created Gateway \"%v/%v\"", gw.Namespace, gw.Name), ingressByNamespacedGateway[gwKey]...)
}

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 @@ -336,6 +357,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), buildIngressFromRuleGroup(rg))
return httpRoute, errors
}

Expand Down
Loading