Skip to content

Commit 77a7ef3

Browse files
added notifications in the common package
1 parent c661417 commit 77a7ef3

File tree

9 files changed

+62
-12
lines changed

9 files changed

+62
-12
lines changed

pkg/i2gw/notifications/notifications.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ type NotificationAggregator struct {
5151

5252
var NotificationAggr NotificationAggregator
5353

54+
// NotificationCallback is a callback function used to send notifications from within the common
55+
// package without the common package having knowledge about which provider is making a call it
56+
type NotificationCallback func(mType MessageType, message string, callingObject ...client.Object)
57+
5458
// DispatchNotification is used to send a notification to the NotificationAggregator
5559
func (na *NotificationAggregator) DispatchNotification(notification Notification, ProviderName string) {
5660
na.mutex.Lock()
@@ -84,6 +88,7 @@ func (na *NotificationAggregator) CreateNotificationTables() map[string]string {
8488
return notificationTablesMap
8589
}
8690

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

pkg/i2gw/providers/apisix/converter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func (c *converter) convert(storage *storage) (i2gw.GatewayResources, field.Erro
4848
}
4949
// Convert plain ingress resources to gateway resources, ignoring all
5050
// provider-specific features.
51-
gatewayResources, errs := common.ToGateway(ingressList, c.implementationSpecificOptions)
51+
gatewayResources, errs := common.ToGateway(ingressList, c.implementationSpecificOptions, notify)
5252
if len(errs) > 0 {
5353
return i2gw.GatewayResources{}, errs
5454
}

pkg/i2gw/providers/common/converter.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"strings"
2222

2323
"github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw"
24+
"github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/notifications"
2425
networkingv1 "k8s.io/api/networking/v1"
2526
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2627
"k8s.io/apimachinery/pkg/runtime/schema"
@@ -31,9 +32,21 @@ import (
3132

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

43+
var notify notifications.NotificationCallback
44+
if len(notifyOpts) > 0 {
45+
notify = notifyOpts[0]
46+
} else {
47+
notify = noNotifications
48+
}
49+
3750
var errs field.ErrorList
3851
for _, ingress := range ingresses {
3952
aggregator.addIngress(ingress)
@@ -42,7 +55,7 @@ func ToGateway(ingresses []networkingv1.Ingress, options i2gw.ProviderImplementa
4255
return i2gw.GatewayResources{}, errs
4356
}
4457

45-
routes, gateways, errs := aggregator.toHTTPRoutesAndGateways(options)
58+
routes, gateways, errs := aggregator.toHTTPRoutesAndGateways(options, notify)
4659
if len(errs) > 0 {
4760
return i2gw.GatewayResources{}, errs
4861
}
@@ -166,7 +179,7 @@ func (a *ingressAggregator) addIngressRule(namespace, name, ingressClass string,
166179
rg.rules = append(rg.rules, ingressRule{rule: rule})
167180
}
168181

169-
func (a *ingressAggregator) toHTTPRoutesAndGateways(options i2gw.ProviderImplementationSpecificOptions) ([]gatewayv1.HTTPRoute, []gatewayv1.Gateway, field.ErrorList) {
182+
func (a *ingressAggregator) toHTTPRoutesAndGateways(options i2gw.ProviderImplementationSpecificOptions, notify notifications.NotificationCallback) ([]gatewayv1.HTTPRoute, []gatewayv1.Gateway, field.ErrorList) {
170183
var httpRoutes []gatewayv1.HTTPRoute
171184
var errors field.ErrorList
172185
listenersByNamespacedGateway := map[string][]gatewayv1.Listener{}
@@ -187,7 +200,7 @@ func (a *ingressAggregator) toHTTPRoutesAndGateways(options i2gw.ProviderImpleme
187200
}
188201
gwKey := fmt.Sprintf("%s/%s", rg.namespace, rg.ingressClass)
189202
listenersByNamespacedGateway[gwKey] = append(listenersByNamespacedGateway[gwKey], listener)
190-
httpRoute, errs := rg.toHTTPRoute(options)
203+
httpRoute, errs := rg.toHTTPRoute(options, notify)
191204
httpRoutes = append(httpRoutes, httpRoute)
192205
errors = append(errors, errs...)
193206
}
@@ -222,6 +235,7 @@ func (a *ingressAggregator) toHTTPRoutesAndGateways(options i2gw.ProviderImpleme
222235
})
223236
}
224237

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

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

278293
return httpRoutes, gateways, errors
279294
}
280295

281-
func (rg *ingressRuleGroup) toHTTPRoute(options i2gw.ProviderImplementationSpecificOptions) (gatewayv1.HTTPRoute, field.ErrorList) {
296+
func (rg *ingressRuleGroup) toHTTPRoute(options i2gw.ProviderImplementationSpecificOptions, notify notifications.NotificationCallback) (gatewayv1.HTTPRoute, field.ErrorList) {
282297
ingressPathsByMatchKey := groupIngressPathsByMatchKey(rg.rules)
283298
httpRoute := gatewayv1.HTTPRoute{
284299
ObjectMeta: metav1.ObjectMeta{
@@ -322,6 +337,7 @@ func (rg *ingressRuleGroup) toHTTPRoute(options i2gw.ProviderImplementationSpeci
322337
httpRoute.Spec.Rules = append(httpRoute.Spec.Rules, hrRule)
323338
}
324339

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

pkg/i2gw/providers/common/converter_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -416,8 +416,7 @@ func Test_ingresses2GatewaysAndHttpRoutes(t *testing.T) {
416416
for _, tc := range testCases {
417417
t.Run(tc.name, func(t *testing.T) {
418418

419-
gatewayResources, errs := ToGateway(tc.ingresses, i2gw.ProviderImplementationSpecificOptions{})
420-
419+
gatewayResources, errs := ToGateway(tc.ingresses, i2gw.ProviderImplementationSpecificOptions{}, noNotifications)
421420
if len(gatewayResources.HTTPRoutes) != len(tc.expectedGatewayResources.HTTPRoutes) {
422421
t.Errorf("Expected %d HTTPRoutes, got %d: %+v",
423422
len(tc.expectedGatewayResources.HTTPRoutes), len(gatewayResources.HTTPRoutes), gatewayResources.HTTPRoutes)

pkg/i2gw/providers/common/utils.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@ import (
2020
"fmt"
2121
"regexp"
2222

23+
"github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/notifications"
2324
networkingv1 "k8s.io/api/networking/v1"
2425
networkingv1beta1 "k8s.io/api/networking/v1beta1"
26+
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2527
"k8s.io/apimachinery/pkg/util/validation/field"
28+
"sigs.k8s.io/controller-runtime/pkg/client"
2629
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
2730
)
2831

@@ -200,3 +203,30 @@ func removeBackendRefsDuplicates(backendRefs []gatewayv1.HTTPBackendRef) []gatew
200203
}
201204
return uniqueBackendRefs
202205
}
206+
207+
func noNotifications(mType notifications.MessageType, message string, callingObject ...client.Object) {
208+
}
209+
210+
func mockIngressFromRuleGroup(rg *ingressRuleGroup) client.Object {
211+
return &networkingv1.Ingress{
212+
TypeMeta: v1.TypeMeta{
213+
Kind: "Ingress",
214+
},
215+
ObjectMeta: v1.ObjectMeta{
216+
Name: rg.name,
217+
Namespace: rg.namespace,
218+
},
219+
}
220+
}
221+
222+
func mockIngressFromDefaultBackend(db ingressDefaultBackend) client.Object {
223+
return &networkingv1.Ingress{
224+
TypeMeta: v1.TypeMeta{
225+
Kind: "Ingress",
226+
},
227+
ObjectMeta: v1.ObjectMeta{
228+
Name: db.name,
229+
Namespace: db.namespace,
230+
},
231+
}
232+
}

pkg/i2gw/providers/gce/converter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func (c *converter) convert(storage *storage) (i2gw.GatewayResources, field.Erro
5757

5858
// Convert plain ingress resources to gateway resources, ignoring all
5959
// provider-specific features.
60-
gatewayResources, errs := common.ToGateway(ingressList, c.implementationSpecificOptions)
60+
gatewayResources, errs := common.ToGateway(ingressList, c.implementationSpecificOptions, notify)
6161
if len(errs) > 0 {
6262
return i2gw.GatewayResources{}, errs
6363
}

pkg/i2gw/providers/gce/notification.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ import (
2222
)
2323

2424
func notify(mType notifications.MessageType, message string, callingObject ...client.Object) {
25-
newNotification := notifications.Notification{Type: mType, Message: message, CallingObjects: callingObject}
25+
newNotification := notifications.NewNotification(mType, message, callingObject...)
2626
notifications.NotificationAggr.DispatchNotification(newNotification, string(ProviderName))
2727
}

pkg/i2gw/providers/ingressnginx/converter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func (c *converter) convert(storage *storage) (i2gw.GatewayResources, field.Erro
4343

4444
// Convert plain ingress resources to gateway resources, ignoring all
4545
// provider-specific features.
46-
gatewayResources, errs := common.ToGateway(ingressList, i2gw.ProviderImplementationSpecificOptions{})
46+
gatewayResources, errs := common.ToGateway(ingressList, i2gw.ProviderImplementationSpecificOptions{}, notify)
4747
if len(errs) > 0 {
4848
return i2gw.GatewayResources{}, errs
4949
}

pkg/i2gw/providers/kong/converter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func (c *converter) convert(storage *storage) (i2gw.GatewayResources, field.Erro
5555

5656
// Convert plain ingress resources to gateway resources, ignoring all
5757
// provider-specific features.
58-
gatewayResources, errs := common.ToGateway(ingressList, c.implementationSpecificOptions)
58+
gatewayResources, errs := common.ToGateway(ingressList, c.implementationSpecificOptions, notify)
5959
if len(errs) > 0 {
6060
errorList = append(errorList, errs...)
6161
}

0 commit comments

Comments
 (0)