From b6492d0beb47ec374936e2f9999da05c07162883 Mon Sep 17 00:00:00 2001 From: Ronnie Personal <76408835+Ronnie-personal@users.noreply.github.com> Date: Tue, 22 Aug 2023 21:45:43 -0400 Subject: [PATCH] draft coding Signed-off-by: Ronnie Personal <76408835+Ronnie-personal@users.noreply.github.com> --- internal/gatewayapi/filters.go | 23 ++++++++++++++--------- internal/ir/xds.go | 6 ++++-- internal/ir/zz_generated.deepcopy.go | 10 ++++++++-- internal/xds/translator/translator.go | 18 ++++++++++-------- 4 files changed, 36 insertions(+), 21 deletions(-) diff --git a/internal/gatewayapi/filters.go b/internal/gatewayapi/filters.go index 6901fcd79280..1ae3d0ee8631 100644 --- a/internal/gatewayapi/filters.go +++ b/internal/gatewayapi/filters.go @@ -56,7 +56,7 @@ type HTTPFilterIR struct { AddResponseHeaders []ir.AddHeader RemoveResponseHeaders []string - Mirror *ir.RouteDestination + Mirror []*ir.RouteDestination RequestAuthentication *ir.RequestAuthentication RateLimit *ir.RateLimit @@ -121,7 +121,7 @@ func (t *Translator) ProcessGRPCFilters(parentRef *RouteParentContext, HTTPFilterIR: &HTTPFilterIR{}, } - + filterIdx := 0 for i := range filters { filter := filters[i] // If an invalid filter type has been configured then skip processing any more filters @@ -139,7 +139,8 @@ func (t *Translator) ProcessGRPCFilters(parentRef *RouteParentContext, case v1alpha2.GRPCRouteFilterResponseHeaderModifier: t.processResponseHeaderModifierFilter(filter.ResponseHeaderModifier, httpFiltersContext) case v1alpha2.GRPCRouteFilterRequestMirror: - t.processRequestMirrorFilter(filter.RequestMirror, httpFiltersContext, resources) + t.processRequestMirrorFilter(filterIdx, filter.RequestMirror, httpFiltersContext, resources) + filterIdx++ case v1alpha2.GRPCRouteFilterExtensionRef: t.processExtensionRefHTTPFilter(filter.ExtensionRef, httpFiltersContext, resources) default: @@ -841,8 +842,8 @@ func (t *Translator) processRequestMirrorFilter( // Only add missing mirror destinations for _, mirrorEp := range mirrorEndpoints { var found bool - if filterContext.Mirror != nil { - for _, mirror := range filterContext.Mirror.Endpoints { + if filterContext.Mirror != nil && filterIdx < len(filterContext.Mirror) { + for _, mirror := range filterContext.Mirror[filterIdx].Endpoints { if mirror != nil { if mirror.Host == mirrorEp.Host && mirror.Port == mirrorEp.Port { found = true @@ -850,13 +851,17 @@ func (t *Translator) processRequestMirrorFilter( } } } + if !found { - if filterContext.Mirror == nil { - filterContext.Mirror = &ir.RouteDestination{ - Name: fmt.Sprintf("%s-mirror", irRouteDestinationName(filterContext.Route, filterContext.RuleIdx)), + if filterContext.Mirror == nil || filterIdx >= len(filterContext.Mirror) { + newMirror := &ir.RouteDestination{ + Name: fmt.Sprintf("%s-mirror-%d", irRouteDestinationName(filterContext.Route, filterContext.RuleIdx), filterIdx), } + newMirror.Endpoints = append(newMirror.Endpoints, mirrorEp) + filterContext.Mirror = append(filterContext.Mirror, newMirror) + } else { + filterContext.Mirror[filterIdx].Endpoints = append(filterContext.Mirror[filterIdx].Endpoints, mirrorEp) } - filterContext.Mirror.Endpoints = append(filterContext.Mirror.Endpoints, mirrorEp) } } } diff --git a/internal/ir/xds.go b/internal/ir/xds.go index 31cba8d9d365..34538481bf27 100644 --- a/internal/ir/xds.go +++ b/internal/ir/xds.go @@ -352,8 +352,10 @@ func (h HTTPRoute) Validate() error { } } if h.Mirror != nil { - if err := h.Mirror.Validate(); err != nil { - errs = multierror.Append(errs, err) + for _, mirror := range h.Mirror { + if err := mirror.Validate(); err != nil { + errs = multierror.Append(errs, err) + } } } if len(h.AddRequestHeaders) > 0 { diff --git a/internal/ir/zz_generated.deepcopy.go b/internal/ir/zz_generated.deepcopy.go index e9dacce71dd0..62e10d77daf1 100644 --- a/internal/ir/zz_generated.deepcopy.go +++ b/internal/ir/zz_generated.deepcopy.go @@ -321,8 +321,14 @@ func (in *HTTPRoute) DeepCopyInto(out *HTTPRoute) { } if in.Mirror != nil { in, out := &in.Mirror, &out.Mirror - *out = new(RouteDestination) - (*in).DeepCopyInto(*out) + *out = make([]*RouteDestination, len(*in)) + for i := range *in { + if (*in)[i] != nil { + in, out := &(*in)[i], &(*out)[i] + *out = new(RouteDestination) + (*in).DeepCopyInto(*out) + } + } } if in.Destination != nil { in, out := &in.Destination, &out.Destination diff --git a/internal/xds/translator/translator.go b/internal/xds/translator/translator.go index e15272e6af6d..5b5b4f544bd6 100644 --- a/internal/xds/translator/translator.go +++ b/internal/xds/translator/translator.go @@ -197,14 +197,16 @@ func (t *Translator) processHTTPListenerXdsTranslation(tCtx *types.ResourceVersi } if httpRoute.Mirror != nil { - if err := addXdsCluster(tCtx, addXdsClusterArgs{ - name: httpRoute.Mirror.Name, - endpoints: httpRoute.Mirror.Endpoints, - tSocket: nil, - protocol: protocol, - endpointType: Static, - }); err != nil && !errors.Is(err, ErrXdsClusterExists) { - return err + for _, mirrorDest := range httpRoute.Mirror { + if err := addXdsCluster(tCtx, addXdsClusterArgs{ + name: mirrorDest.Name, + endpoints: mirrorDest.Endpoints, + tSocket: nil, + protocol: protocol, + endpointType: Static, + }); err != nil && !errors.Is(err, ErrXdsClusterExists) { + return err + } } } }