Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: unify the way to create RouteDestination #1104

Merged
merged 6 commits into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 19 additions & 18 deletions internal/gatewayapi/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,11 +547,11 @@ func (t *Translator) processTLSRouteParentRefs(tlsRoute *TLSRouteContext, resour
weight = uint32(*backendRef.Weight)
}

routeDestinations = append(routeDestinations, &ir.RouteDestination{
Host: service.Spec.ClusterIP,
Port: uint32(*backendRef.Port),
Weight: weight,
})
routeDestinations = append(routeDestinations, ir.NewRouteDestWithWeight(
service.Spec.ClusterIP,
uint32(*backendRef.Port),
weight,
))
}

// TODO handle:
Expand Down Expand Up @@ -684,10 +684,10 @@ func (t *Translator) processUDPRouteParentRefs(udpRoute *UDPRouteContext, resour
}

// weight is not used in udp route destinations
routeDestinations = append(routeDestinations, &ir.RouteDestination{
Host: service.Spec.ClusterIP,
Port: uint32(*backendRef.Port),
})
routeDestinations = append(routeDestinations, ir.NewRouteDest(
service.Spec.ClusterIP,
uint32(*backendRef.Port),
))

accepted := false
for _, listener := range parentRef.listeners {
Expand Down Expand Up @@ -809,10 +809,10 @@ func (t *Translator) processTCPRouteParentRefs(tcpRoute *TCPRouteContext, resour
}

// weight is not used in tcp route destinations
routeDestinations = append(routeDestinations, &ir.RouteDestination{
Host: service.Spec.ClusterIP,
Port: uint32(*backendRef.Port),
})
routeDestinations = append(routeDestinations, ir.NewRouteDest(
service.Spec.ClusterIP,
uint32(*backendRef.Port),
))

accepted := false
for _, listener := range parentRef.listeners {
Expand Down Expand Up @@ -886,11 +886,12 @@ func (t *Translator) processRouteDestination(backendRef v1beta1.BackendRef,
return nil, weight
}

return &ir.RouteDestination{
Host: service.Spec.ClusterIP,
Port: uint32(*backendRef.Port),
Weight: weight,
}, weight
// we need to validate backendRef first before using its data to create routeDestination
return ir.NewRouteDestWithWeight(
service.Spec.ClusterIP,
uint32(*backendRef.Port),
weight,
), weight

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ xdsIR:
destinations:
- host: "7.7.7.7"
port: 8163
weight: 0

infraIR:
envoy-gateway-gateway-1:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ xdsIR:
destinations:
- host: "7.7.7.7"
port: 8162
weight: 0

infraIR:
envoy-gateway-gateway-1:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ xdsIR:
destinations:
- host: "7.7.7.7"
port: 8163
weight: 0
infraIR:
envoy-gateway-gateway-1:
proxy:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ xdsIR:
destinations:
- host: "7.7.7.7"
port: 8162
weight: 0
infraIR:
envoy-gateway-gateway-1:
proxy:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ xdsIR:
destinations:
- host: "7.7.7.7"
port: 8163
weight: 0

infraIR:
envoy-gateway-gateway-1:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ xdsIR:
destinations:
- host: "7.7.7.7"
port: 8162
weight: 0

infraIR:
envoy-gateway-gateway-1:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,12 @@ xdsIR:
destinations:
- host: "7.7.7.7"
port: 8163
weight: 0
- name: "envoy-gateway-gateway-1-tcp2-tcproute-2"
address: "0.0.0.0"
port: 10163
destinations:
- host: "7.7.7.7"
port: 8163
weight: 0

infraIR:
envoy-gateway-gateway-1:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,12 @@ xdsIR:
destinations:
- host: "7.7.7.7"
port: 8163
weight: 0
- name: "envoy-gateway-gateway-1-tcp2-tcproute-1"
address: "0.0.0.0"
port: 10162
destinations:
- host: "7.7.7.7"
port: 8163
weight: 0

infraIR:
envoy-gateway-gateway-1:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,12 @@ xdsIR:
destinations:
- host: "7.7.7.7"
port: 8162
weight: 0
- name: "envoy-gateway-gateway-1-udp2-udproute-2"
address: "0.0.0.0"
port: 10163
destinations:
- host: "7.7.7.7"
port: 8162
weight: 0

infraIR:
envoy-gateway-gateway-1:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,12 @@ xdsIR:
destinations:
- host: "7.7.7.7"
port: 8162
weight: 0
- name: "envoy-gateway-gateway-1-udp2-udproute-1"
address: "0.0.0.0"
port: 10162
destinations:
- host: "7.7.7.7"
port: 8162
weight: 0

infraIR:
envoy-gateway-gateway-1:
Expand Down
16 changes: 12 additions & 4 deletions internal/ir/xds.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,14 +368,15 @@ func (j *JwtRequestAuthentication) Validate() error {
}

// RouteDestination holds the destination details associated with the route
// +kubebuilder:object:generate=true
type RouteDestination struct {
// Host refers to the FQDN or IP address of the backend service.
Host string
// Port on the service to forward the request to.
Port uint32
// Weight associated with this destination.
// Note: Weight is not used in UDP route.
Weight uint32
// Note: Weight is not used in TCP/UDP route.
Weight *uint32
}

// Validate the fields within the RouteDestination structure
Expand All @@ -393,11 +394,18 @@ func (r RouteDestination) Validate() error {
}

// NewRouteDest creates a new RouteDestination.
func NewRouteDest(host string, port uint32, weight uint32) *RouteDestination {
func NewRouteDest(host string, port uint32) *RouteDestination {
return &RouteDestination{
Host: host,
Port: port,
}
}

func NewRouteDestWithWeight(host string, port uint32, weight uint32) *RouteDestination {
return &RouteDestination{
Host: host,
Port: port,
Weight: weight,
Weight: &weight,
}
}

Expand Down
28 changes: 24 additions & 4 deletions internal/ir/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/xds/translator/authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ func createJwksClusters(tCtx *types.ResourceVersionTable, routes []*ir.HTTPRoute
return err
}
if existingCluster := findXdsCluster(tCtx, jwks.name); existingCluster == nil {
routeDestinations := []*ir.RouteDestination{ir.NewRouteDest(jwks.hostname, jwks.port, 0)}
routeDestinations := []*ir.RouteDestination{ir.NewRouteDest(jwks.hostname, jwks.port)}
jwksServerCluster := buildXdsCluster(jwks.name, routeDestinations, false /*isHTTP2 */, jwks.isStatic)
tSocket, err := buildXdsUpstreamTLSSocket()
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/xds/translator/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ func buildXdsEndpoints(destinations []*ir.RouteDestination) []*endpointv3.LbEndp
},
},
}
if destination.Weight != 0 {
lbEndpoint.LoadBalancingWeight = &wrapperspb.UInt32Value{Value: destination.Weight}
if destination.Weight != nil {
lbEndpoint.LoadBalancingWeight = &wrapperspb.UInt32Value{Value: *destination.Weight}
}
endpoints = append(endpoints, lbEndpoint)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/xds/translator/ratelimit.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ func (t *Translator) buildRateLimitServiceCluster(irListener *ir.HTTPListener) *

clusterName := getRateLimitServiceClusterName()
host, port := t.getRateLimitServiceGrpcHostPort()
routeDestinations := []*ir.RouteDestination{ir.NewRouteDest(host, uint32(port), 0)}
routeDestinations := []*ir.RouteDestination{ir.NewRouteDest(host, uint32(port))}
rateLimitServerCluster := buildXdsCluster(clusterName, routeDestinations, true /*isHTTP2 */, false /*isStatic */)

return rateLimitServerCluster
Expand Down