@@ -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
0 commit comments