Skip to content

Commit

Permalink
fix: output weight field of 0-weight backend (#397)
Browse files Browse the repository at this point in the history
* fix: output weight field of 0-weight backend

Signed-off-by: Lin Yang <[email protected]>

* refactor: weight of endpoint

Signed-off-by: Lin Yang <[email protected]>

---------

Signed-off-by: Lin Yang <[email protected]>
  • Loading branch information
reaver-flomesh authored Oct 21, 2024
1 parent 6ae4c12 commit ebbb8ea
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 25 deletions.
14 changes: 7 additions & 7 deletions pkg/gateway/fgw/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -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"`
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/gateway/processor/v2/grpcroute.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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(),
})
Expand Down
4 changes: 2 additions & 2 deletions pkg/gateway/processor/v2/httproute.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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(),
})
Expand Down
2 changes: 1 addition & 1 deletion pkg/gateway/processor/v2/tcproute.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pkg/gateway/processor/v2/tlsroute.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
{
Expand Down
2 changes: 1 addition & 1 deletion pkg/gateway/processor/v2/udproute.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 1 addition & 11 deletions pkg/gateway/processor/v2/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -37,22 +35,14 @@ 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
})
}
}

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
}

0 comments on commit ebbb8ea

Please sign in to comment.