Skip to content

Commit 26a0cf0

Browse files
added gateway creation notifications
1 parent 77a7ef3 commit 26a0cf0

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

pkg/i2gw/providers/common/converter.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,28 @@ import (
2727
"k8s.io/apimachinery/pkg/runtime/schema"
2828
"k8s.io/apimachinery/pkg/types"
2929
"k8s.io/apimachinery/pkg/util/validation/field"
30+
"sigs.k8s.io/controller-runtime/pkg/client"
3031
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
3132
)
3233

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

4344
var notify notifications.NotificationCallback
44-
if len(notifyOpts) > 0 {
45-
notify = notifyOpts[0]
46-
} else {
45+
switch len(notifyOpts) {
46+
case 0:
4747
notify = noNotifications
48+
case 1:
49+
notify = notifyOpts[0]
50+
default:
51+
return i2gw.GatewayResources{}, field.ErrorList{field.Invalid(field.NewPath(""), "", "number of notification callbacks exceeded. only 0 or 1 callbacks are currently supported")}
4852
}
4953

5054
var errs field.ErrorList
@@ -183,6 +187,7 @@ func (a *ingressAggregator) toHTTPRoutesAndGateways(options i2gw.ProviderImpleme
183187
var httpRoutes []gatewayv1.HTTPRoute
184188
var errors field.ErrorList
185189
listenersByNamespacedGateway := map[string][]gatewayv1.Listener{}
190+
ingressByNamespacedGateway := map[string][]client.Object{}
186191

187192
for _, rg := range a.ruleGroups {
188193
listener := gatewayv1.Listener{}
@@ -200,6 +205,7 @@ func (a *ingressAggregator) toHTTPRoutesAndGateways(options i2gw.ProviderImpleme
200205
}
201206
gwKey := fmt.Sprintf("%s/%s", rg.namespace, rg.ingressClass)
202207
listenersByNamespacedGateway[gwKey] = append(listenersByNamespacedGateway[gwKey], listener)
208+
ingressByNamespacedGateway[gwKey] = append(ingressByNamespacedGateway[gwKey], buildIngressFromRuleGroup(rg))
203209
httpRoute, errs := rg.toHTTPRoute(options, notify)
204210
httpRoutes = append(httpRoutes, httpRoute)
205211
errors = append(errors, errs...)
@@ -235,7 +241,7 @@ func (a *ingressAggregator) toHTTPRoutesAndGateways(options i2gw.ProviderImpleme
235241
})
236242
}
237243

238-
notify(notifications.InfoNotification, fmt.Sprintf("successfully converted to HTTPRoute \"%v/%v\"", httpRoute.Namespace, httpRoute.Name), mockIngressFromDefaultBackend(db))
244+
notify(notifications.InfoNotification, fmt.Sprintf("successfully converted to HTTPRoute \"%v/%v\"", httpRoute.Namespace, httpRoute.Name), buildIngressFromDefaultBackend(db))
239245
httpRoutes = append(httpRoutes, httpRoute)
240246
}
241247

@@ -285,9 +291,9 @@ func (a *ingressAggregator) toHTTPRoutesAndGateways(options i2gw.ProviderImpleme
285291
}
286292

287293
var gateways []gatewayv1.Gateway
288-
for _, gw := range gatewaysByKey {
294+
for gwKey, gw := range gatewaysByKey {
289295
gateways = append(gateways, *gw)
290-
notify(notifications.InfoNotification, fmt.Sprintf("successfully created Gateway \"%v/%v\"", gw.Namespace, gw.Name))
296+
notify(notifications.InfoNotification, fmt.Sprintf("successfully created Gateway \"%v/%v\"", gw.Namespace, gw.Name), ingressByNamespacedGateway[gwKey]...)
291297
}
292298

293299
return httpRoutes, gateways, errors
@@ -337,7 +343,7 @@ func (rg *ingressRuleGroup) toHTTPRoute(options i2gw.ProviderImplementationSpeci
337343
httpRoute.Spec.Rules = append(httpRoute.Spec.Rules, hrRule)
338344
}
339345

340-
notify(notifications.InfoNotification, fmt.Sprintf("successfully converted to HTTPRoute \"%v/%v\"", httpRoute.Namespace, httpRoute.Name), mockIngressFromRuleGroup(rg))
346+
notify(notifications.InfoNotification, fmt.Sprintf("successfully converted to HTTPRoute \"%v/%v\"", httpRoute.Namespace, httpRoute.Name), buildIngressFromRuleGroup(rg))
341347
return httpRoute, errors
342348
}
343349

pkg/i2gw/providers/common/utils.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,11 @@ func removeBackendRefsDuplicates(backendRefs []gatewayv1.HTTPBackendRef) []gatew
204204
return uniqueBackendRefs
205205
}
206206

207-
func noNotifications(mType notifications.MessageType, message string, callingObject ...client.Object) {
208-
}
207+
// noNotifications is a callback function used when a provider does not want to use
208+
// notifications from the common package
209+
func noNotifications(_ notifications.MessageType, _ string, _ ...client.Object) {}
209210

210-
func mockIngressFromRuleGroup(rg *ingressRuleGroup) client.Object {
211+
func buildIngressFromRuleGroup(rg *ingressRuleGroup) client.Object {
211212
return &networkingv1.Ingress{
212213
TypeMeta: v1.TypeMeta{
213214
Kind: "Ingress",
@@ -219,7 +220,7 @@ func mockIngressFromRuleGroup(rg *ingressRuleGroup) client.Object {
219220
}
220221
}
221222

222-
func mockIngressFromDefaultBackend(db ingressDefaultBackend) client.Object {
223+
func buildIngressFromDefaultBackend(db ingressDefaultBackend) client.Object {
223224
return &networkingv1.Ingress{
224225
TypeMeta: v1.TypeMeta{
225226
Kind: "Ingress",

0 commit comments

Comments
 (0)