Skip to content

Commit

Permalink
added gateway creation notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
Devaansh-Kumar committed Aug 20, 2024
1 parent 77a7ef3 commit 26a0cf0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
24 changes: 15 additions & 9 deletions pkg/i2gw/providers/common/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,28 @@ import (
"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"
)

// ToGateway converts the received ingresses to i2gw.GatewayResources,
// without taking into consideration any provider specific logic.
//
// If a provider wishes to recieve notifications from the common package,
// 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 seperated from the provider.
// ToGateway separated 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 {
switch len(notifyOpts) {
case 0:
notify = noNotifications
case 1:
notify = notifyOpts[0]
default:
return i2gw.GatewayResources{}, field.ErrorList{field.Invalid(field.NewPath(""), "", "number of notification callbacks exceeded. only 0 or 1 callbacks are currently supported")}
}

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

for _, rg := range a.ruleGroups {
listener := gatewayv1.Listener{}
Expand All @@ -200,6 +205,7 @@ func (a *ingressAggregator) toHTTPRoutesAndGateways(options i2gw.ProviderImpleme
}
gwKey := fmt.Sprintf("%s/%s", rg.namespace, rg.ingressClass)
listenersByNamespacedGateway[gwKey] = append(listenersByNamespacedGateway[gwKey], listener)
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 @@ -235,7 +241,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))
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,9 +291,9 @@ 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))
notify(notifications.InfoNotification, fmt.Sprintf("successfully created Gateway \"%v/%v\"", gw.Namespace, gw.Name), ingressByNamespacedGateway[gwKey]...)
}

return httpRoutes, gateways, errors
Expand Down Expand Up @@ -337,7 +343,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))
notify(notifications.InfoNotification, fmt.Sprintf("successfully converted to HTTPRoute \"%v/%v\"", httpRoute.Namespace, httpRoute.Name), buildIngressFromRuleGroup(rg))
return httpRoute, errors
}

Expand Down
9 changes: 5 additions & 4 deletions pkg/i2gw/providers/common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,11 @@ func removeBackendRefsDuplicates(backendRefs []gatewayv1.HTTPBackendRef) []gatew
return uniqueBackendRefs
}

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

func mockIngressFromRuleGroup(rg *ingressRuleGroup) client.Object {
func buildIngressFromRuleGroup(rg *ingressRuleGroup) client.Object {
return &networkingv1.Ingress{
TypeMeta: v1.TypeMeta{
Kind: "Ingress",
Expand All @@ -219,7 +220,7 @@ func mockIngressFromRuleGroup(rg *ingressRuleGroup) client.Object {
}
}

func mockIngressFromDefaultBackend(db ingressDefaultBackend) client.Object {
func buildIngressFromDefaultBackend(db ingressDefaultBackend) client.Object {
return &networkingv1.Ingress{
TypeMeta: v1.TypeMeta{
Kind: "Ingress",
Expand Down

0 comments on commit 26a0cf0

Please sign in to comment.