From ebbb8ea73bfaea526bd06675f8afd8abf7495634 Mon Sep 17 00:00:00 2001 From: Lin Yang Date: Mon, 21 Oct 2024 15:05:42 +0800 Subject: [PATCH] fix: output weight field of 0-weight backend (#397) * fix: output weight field of 0-weight backend Signed-off-by: Lin Yang * refactor: weight of endpoint Signed-off-by: Lin Yang --------- Signed-off-by: Lin Yang --- pkg/gateway/fgw/config.go | 14 +++++++------- pkg/gateway/processor/v2/grpcroute.go | 4 ++-- pkg/gateway/processor/v2/httproute.go | 4 ++-- pkg/gateway/processor/v2/tcproute.go | 2 +- pkg/gateway/processor/v2/tlsroute.go | 2 +- pkg/gateway/processor/v2/udproute.go | 2 +- pkg/gateway/processor/v2/utils.go | 12 +----------- 7 files changed, 15 insertions(+), 25 deletions(-) diff --git a/pkg/gateway/fgw/config.go b/pkg/gateway/fgw/config.go index 107d2c7f..782ad623 100644 --- a/pkg/gateway/fgw/config.go +++ b/pkg/gateway/fgw/config.go @@ -230,11 +230,11 @@ type UDPRouteRule struct { type HTTPBackendRef struct { Kind string `json:"kind"` Name string `json:"name"` - Weight int32 `json:"weight,omitempty"` + Weight *int32 `json:"weight,omitempty"` Filters []HTTPRouteFilter `json:"filters,omitempty" hash:"set"` } -func NewHTTPBackendRef(name string, weight int32) HTTPBackendRef { +func NewHTTPBackendRef(name string, weight *int32) HTTPBackendRef { return HTTPBackendRef{ Kind: "Backend", Name: name, @@ -281,11 +281,11 @@ type HTTPRequestMirrorFilter struct { type GRPCBackendRef struct { Kind string `json:"kind"` Name string `json:"name"` - Weight int32 `json:"weight,omitempty"` + Weight *int32 `json:"weight,omitempty"` Filters []GRPCRouteFilter `json:"filters,omitempty" hash:"set"` } -func NewGRPCBackendRef(name string, weight int32) GRPCBackendRef { +func NewGRPCBackendRef(name string, weight *int32) GRPCBackendRef { return GRPCBackendRef{ Kind: "Backend", Name: name, @@ -334,11 +334,11 @@ func NewBackendRef(name string) BackendRef { } } -func NewBackendRefWithWeight(name string, weight int32) BackendRef { +func NewBackendRefWithWeight(name string, weight *int32) BackendRef { return BackendRef{ Kind: "Backend", Name: name, - Weight: ptr.To(weight), + Weight: weight, } } @@ -369,7 +369,7 @@ type BackendSpec struct { type BackendTarget struct { Address string `json:"address"` Port *int32 `json:"port"` - Weight int32 `json:"weight,omitempty"` + Weight *int32 `json:"weight,omitempty"` Tags map[string]string `json:"tags,omitempty"` } diff --git a/pkg/gateway/processor/v2/grpcroute.go b/pkg/gateway/processor/v2/grpcroute.go index 4ab6aa84..a4f8bd7b 100644 --- a/pkg/gateway/processor/v2/grpcroute.go +++ b/pkg/gateway/processor/v2/grpcroute.go @@ -105,7 +105,7 @@ func (c *ConfigGenerator) toV2GRPCBackendRefs(grpcRoute *gwv1.GRPCRoute, rule *g backendRefs := make([]fgwv2.GRPCBackendRef, 0) for _, bk := range rule.BackendRefs { if svcPort := c.backendRefToServicePortName(grpcRoute, bk.BackendRef.BackendObjectReference, holder); svcPort != nil { - b2 := fgwv2.NewGRPCBackendRef(svcPort.String(), backendWeight(bk.BackendRef)) + b2 := fgwv2.NewGRPCBackendRef(svcPort.String(), bk.BackendRef.Weight) if len(bk.Filters) > 0 { b2.Filters = c.toV2GRPCRouteFilters(grpcRoute, bk.Filters, holder) @@ -136,7 +136,7 @@ func (c *ConfigGenerator) toV2GRPCRouteFilters(grpcRoute *gwv1.GRPCRoute, routeF filters = append(filters, fgwv2.GRPCRouteFilter{ Type: gwv1.GRPCRouteFilterRequestMirror, RequestMirror: &fgwv2.HTTPRequestMirrorFilter{ - BackendRef: fgwv2.NewBackendRefWithWeight(svcPort.String(), 1), + BackendRef: fgwv2.NewBackendRef(svcPort.String()), }, Key: uuid.NewString(), }) diff --git a/pkg/gateway/processor/v2/httproute.go b/pkg/gateway/processor/v2/httproute.go index 145cdee0..0df74df4 100644 --- a/pkg/gateway/processor/v2/httproute.go +++ b/pkg/gateway/processor/v2/httproute.go @@ -105,7 +105,7 @@ func (c *ConfigGenerator) toV2HTTPBackendRefs(httpRoute *gwv1.HTTPRoute, rule *g backendRefs := make([]fgwv2.HTTPBackendRef, 0) for _, bk := range rule.BackendRefs { if svcPort := c.backendRefToServicePortName(httpRoute, bk.BackendRef.BackendObjectReference, holder); svcPort != nil { - b2 := fgwv2.NewHTTPBackendRef(svcPort.String(), backendWeight(bk.BackendRef)) + b2 := fgwv2.NewHTTPBackendRef(svcPort.String(), bk.BackendRef.Weight) if len(bk.Filters) > 0 { b2.Filters = c.toV2HTTPRouteFilters(httpRoute, bk.Filters, holder) @@ -136,7 +136,7 @@ func (c *ConfigGenerator) toV2HTTPRouteFilters(httpRoute *gwv1.HTTPRoute, routeF filters = append(filters, fgwv2.HTTPRouteFilter{ Type: gwv1.HTTPRouteFilterRequestMirror, RequestMirror: &fgwv2.HTTPRequestMirrorFilter{ - BackendRef: fgwv2.NewBackendRefWithWeight(svcPort.String(), 1), + BackendRef: fgwv2.NewBackendRef(svcPort.String()), }, Key: uuid.NewString(), }) diff --git a/pkg/gateway/processor/v2/tcproute.go b/pkg/gateway/processor/v2/tcproute.go index 1a52f6d5..fd3090d4 100644 --- a/pkg/gateway/processor/v2/tcproute.go +++ b/pkg/gateway/processor/v2/tcproute.go @@ -99,7 +99,7 @@ func (c *ConfigGenerator) toV2TCPBackendRefs(tcpRoute *gwv1alpha2.TCPRoute, rule for _, backend := range rule.BackendRefs { backend := backend if svcPort := c.backendRefToServicePortName(tcpRoute, backend.BackendObjectReference, holder); svcPort != nil { - backendRefs = append(backendRefs, fgwv2.NewBackendRefWithWeight(svcPort.String(), backendWeight(backend))) + backendRefs = append(backendRefs, fgwv2.NewBackendRefWithWeight(svcPort.String(), backend.Weight)) for _, processor := range c.getBackendPolicyProcessors(tcpRoute) { processor.Process(tcpRoute, holder.GetParentRef(), rule, backend.BackendObjectReference, svcPort) diff --git a/pkg/gateway/processor/v2/tlsroute.go b/pkg/gateway/processor/v2/tlsroute.go index 2afb1334..c61697ec 100644 --- a/pkg/gateway/processor/v2/tlsroute.go +++ b/pkg/gateway/processor/v2/tlsroute.go @@ -107,7 +107,7 @@ func (c *ConfigGenerator) toV2TLSBackendRefs(_ *gwv1alpha2.TLSRoute, rule gwv1al for _, backend := range rule.BackendRefs { name := fmt.Sprintf("%s%s", backend.Name, formatTLSPort(backend.Port)) - backendRefs = append(backendRefs, fgwv2.NewBackendRefWithWeight(name, backendWeight(backend))) + backendRefs = append(backendRefs, fgwv2.NewBackendRefWithWeight(name, backend.Weight)) backends = append(backends, fgwv2.NewBackend(name, []fgwv2.BackendTarget{ { diff --git a/pkg/gateway/processor/v2/udproute.go b/pkg/gateway/processor/v2/udproute.go index b06a1ea3..d0fc2de8 100644 --- a/pkg/gateway/processor/v2/udproute.go +++ b/pkg/gateway/processor/v2/udproute.go @@ -100,7 +100,7 @@ func (c *ConfigGenerator) toV2UDPBackendRefs(udpRoute *gwv1alpha2.UDPRoute, rule for _, backend := range rule.BackendRefs { backend := backend if svcPort := c.backendRefToServicePortName(udpRoute, backend.BackendObjectReference, holder); svcPort != nil { - backendRefs = append(backendRefs, fgwv2.NewBackendRefWithWeight(svcPort.String(), backendWeight(backend))) + backendRefs = append(backendRefs, fgwv2.NewBackendRefWithWeight(svcPort.String(), backend.Weight)) c.services[svcPort.String()] = serviceContext{ svcPortName: *svcPort, diff --git a/pkg/gateway/processor/v2/utils.go b/pkg/gateway/processor/v2/utils.go index 7c252524..c2ca9fc5 100644 --- a/pkg/gateway/processor/v2/utils.go +++ b/pkg/gateway/processor/v2/utils.go @@ -11,8 +11,6 @@ import ( "github.com/flomesh-io/fsm/pkg/utils/cidr" fgwv2 "github.com/flomesh-io/fsm/pkg/gateway/fgw" - - gwv1 "sigs.k8s.io/gateway-api/apis/v1" ) func toFGWBackendTargets(endpointSet map[endpointContext]struct{}) []fgwv2.BackendTarget { @@ -37,7 +35,7 @@ func toFGWBackendTargets(endpointSet map[endpointContext]struct{}) []fgwv2.Backe targets = append(targets, fgwv2.BackendTarget{ Address: ep.address, Port: ptr.To(ep.port), - Weight: 1, + Weight: ptr.To(int32(1)), // TODO: support setting weight of endpoint in the future }) } } @@ -45,14 +43,6 @@ func toFGWBackendTargets(endpointSet map[endpointContext]struct{}) []fgwv2.Backe return targets } -func backendWeight(bk gwv1.BackendRef) int32 { - if bk.Weight != nil { - return *bk.Weight - } - - return 1 -} - func isHeadlessServiceWithoutSelector(service *corev1.Service) bool { return k8s.IsHeadlessService(service) && len(service.Spec.Selector) == 0 }