diff --git a/internal/gatewayapi/route.go b/internal/gatewayapi/route.go index 87255a9bb62..55506e9d5d9 100644 --- a/internal/gatewayapi/route.go +++ b/internal/gatewayapi/route.go @@ -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: @@ -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 { @@ -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 { @@ -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 } diff --git a/internal/gatewayapi/testdata/gateway-with-single-listener-with-multiple-tcproutes.out.yaml b/internal/gatewayapi/testdata/gateway-with-single-listener-with-multiple-tcproutes.out.yaml index 8348d196a2d..80b1c46ac47 100644 --- a/internal/gatewayapi/testdata/gateway-with-single-listener-with-multiple-tcproutes.out.yaml +++ b/internal/gatewayapi/testdata/gateway-with-single-listener-with-multiple-tcproutes.out.yaml @@ -83,7 +83,6 @@ xdsIR: destinations: - host: "7.7.7.7" port: 8163 - weight: 0 infraIR: envoy-gateway-gateway-1: diff --git a/internal/gatewayapi/testdata/gateway-with-single-listener-with-multiple-udproutes.out.yaml b/internal/gatewayapi/testdata/gateway-with-single-listener-with-multiple-udproutes.out.yaml index 4a6e97d8c09..7bb01533847 100644 --- a/internal/gatewayapi/testdata/gateway-with-single-listener-with-multiple-udproutes.out.yaml +++ b/internal/gatewayapi/testdata/gateway-with-single-listener-with-multiple-udproutes.out.yaml @@ -83,7 +83,6 @@ xdsIR: destinations: - host: "7.7.7.7" port: 8162 - weight: 0 infraIR: envoy-gateway-gateway-1: diff --git a/internal/gatewayapi/testdata/gateway-with-two-listeners-on-same-tcp-port.out.yaml b/internal/gatewayapi/testdata/gateway-with-two-listeners-on-same-tcp-port.out.yaml index 676829906c5..2771a86dcee 100644 --- a/internal/gatewayapi/testdata/gateway-with-two-listeners-on-same-tcp-port.out.yaml +++ b/internal/gatewayapi/testdata/gateway-with-two-listeners-on-same-tcp-port.out.yaml @@ -79,7 +79,6 @@ xdsIR: destinations: - host: "7.7.7.7" port: 8163 - weight: 0 infraIR: envoy-gateway-gateway-1: proxy: diff --git a/internal/gatewayapi/testdata/gateway-with-two-listeners-on-same-udp-port.out.yaml b/internal/gatewayapi/testdata/gateway-with-two-listeners-on-same-udp-port.out.yaml index a439967e68f..931f2d02621 100644 --- a/internal/gatewayapi/testdata/gateway-with-two-listeners-on-same-udp-port.out.yaml +++ b/internal/gatewayapi/testdata/gateway-with-two-listeners-on-same-udp-port.out.yaml @@ -79,7 +79,6 @@ xdsIR: destinations: - host: "7.7.7.7" port: 8162 - weight: 0 infraIR: envoy-gateway-gateway-1: proxy: diff --git a/internal/gatewayapi/testdata/gateway-with-two-listeners-with-same-port-http-tcp-protocol.out.yaml b/internal/gatewayapi/testdata/gateway-with-two-listeners-with-same-port-http-tcp-protocol.out.yaml index ea329c1c2d6..2497a4328b8 100644 --- a/internal/gatewayapi/testdata/gateway-with-two-listeners-with-same-port-http-tcp-protocol.out.yaml +++ b/internal/gatewayapi/testdata/gateway-with-two-listeners-with-same-port-http-tcp-protocol.out.yaml @@ -120,7 +120,6 @@ xdsIR: destinations: - host: "7.7.7.7" port: 8163 - weight: 0 infraIR: envoy-gateway-gateway-1: diff --git a/internal/gatewayapi/testdata/gateway-with-two-listeners-with-same-port-http-udp-protocol.out.yaml b/internal/gatewayapi/testdata/gateway-with-two-listeners-with-same-port-http-udp-protocol.out.yaml index 4dc246cd4a8..02f59f1c303 100644 --- a/internal/gatewayapi/testdata/gateway-with-two-listeners-with-same-port-http-udp-protocol.out.yaml +++ b/internal/gatewayapi/testdata/gateway-with-two-listeners-with-same-port-http-udp-protocol.out.yaml @@ -120,7 +120,6 @@ xdsIR: destinations: - host: "7.7.7.7" port: 8162 - weight: 0 infraIR: envoy-gateway-gateway-1: diff --git a/internal/gatewayapi/testdata/gateway-with-two-listeners-with-tcproutes-with-sectionname.out.yaml b/internal/gatewayapi/testdata/gateway-with-two-listeners-with-tcproutes-with-sectionname.out.yaml index 7974ccc6115..7fe9875491c 100644 --- a/internal/gatewayapi/testdata/gateway-with-two-listeners-with-tcproutes-with-sectionname.out.yaml +++ b/internal/gatewayapi/testdata/gateway-with-two-listeners-with-tcproutes-with-sectionname.out.yaml @@ -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: diff --git a/internal/gatewayapi/testdata/gateway-with-two-listeners-with-tcproutes-without-sectionname.out.yaml b/internal/gatewayapi/testdata/gateway-with-two-listeners-with-tcproutes-without-sectionname.out.yaml index 9e933fadc9a..01e7190db6c 100644 --- a/internal/gatewayapi/testdata/gateway-with-two-listeners-with-tcproutes-without-sectionname.out.yaml +++ b/internal/gatewayapi/testdata/gateway-with-two-listeners-with-tcproutes-without-sectionname.out.yaml @@ -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: diff --git a/internal/gatewayapi/testdata/gateway-with-two-listeners-with-udproutes-with-sectionname.out.yaml b/internal/gatewayapi/testdata/gateway-with-two-listeners-with-udproutes-with-sectionname.out.yaml index 3f04fdb93dd..341197e005e 100644 --- a/internal/gatewayapi/testdata/gateway-with-two-listeners-with-udproutes-with-sectionname.out.yaml +++ b/internal/gatewayapi/testdata/gateway-with-two-listeners-with-udproutes-with-sectionname.out.yaml @@ -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: diff --git a/internal/gatewayapi/testdata/gateway-with-two-listeners-with-udproutes-without-sectionname.out.yaml b/internal/gatewayapi/testdata/gateway-with-two-listeners-with-udproutes-without-sectionname.out.yaml index 7a3ae8d8324..08ce2701165 100644 --- a/internal/gatewayapi/testdata/gateway-with-two-listeners-with-udproutes-without-sectionname.out.yaml +++ b/internal/gatewayapi/testdata/gateway-with-two-listeners-with-udproutes-without-sectionname.out.yaml @@ -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: diff --git a/internal/ir/xds.go b/internal/ir/xds.go index 59dcedd5d31..922f4d99df5 100644 --- a/internal/ir/xds.go +++ b/internal/ir/xds.go @@ -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 @@ -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, } } diff --git a/internal/ir/zz_generated.deepcopy.go b/internal/ir/zz_generated.deepcopy.go index 64778f94aa0..bac7102c88c 100644 --- a/internal/ir/zz_generated.deepcopy.go +++ b/internal/ir/zz_generated.deepcopy.go @@ -205,7 +205,7 @@ func (in *HTTPRoute) DeepCopyInto(out *HTTPRoute) { if (*in)[i] != nil { in, out := &(*in)[i], &(*out)[i] *out = new(RouteDestination) - **out = **in + (*in).DeepCopyInto(*out) } } } @@ -216,7 +216,7 @@ func (in *HTTPRoute) DeepCopyInto(out *HTTPRoute) { if (*in)[i] != nil { in, out := &(*in)[i], &(*out)[i] *out = new(RouteDestination) - **out = **in + (*in).DeepCopyInto(*out) } } } @@ -585,6 +585,26 @@ func (in *RequestAuthentication) DeepCopy() *RequestAuthentication { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *RouteDestination) DeepCopyInto(out *RouteDestination) { + *out = *in + if in.Weight != nil { + in, out := &in.Weight, &out.Weight + *out = new(uint32) + **out = **in + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new RouteDestination. +func (in *RouteDestination) DeepCopy() *RouteDestination { + if in == nil { + return nil + } + out := new(RouteDestination) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *StringMatch) DeepCopyInto(out *StringMatch) { *out = *in @@ -635,7 +655,7 @@ func (in *TCPListener) DeepCopyInto(out *TCPListener) { if (*in)[i] != nil { in, out := &(*in)[i], &(*out)[i] *out = new(RouteDestination) - **out = **in + (*in).DeepCopyInto(*out) } } } @@ -706,7 +726,7 @@ func (in *UDPListener) DeepCopyInto(out *UDPListener) { if (*in)[i] != nil { in, out := &(*in)[i], &(*out)[i] *out = new(RouteDestination) - **out = **in + (*in).DeepCopyInto(*out) } } } diff --git a/internal/xds/translator/authentication.go b/internal/xds/translator/authentication.go index 862f2f602a0..7ca981452b5 100644 --- a/internal/xds/translator/authentication.go +++ b/internal/xds/translator/authentication.go @@ -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 { diff --git a/internal/xds/translator/cluster.go b/internal/xds/translator/cluster.go index 6f547f0e7bc..6a342fb899b 100644 --- a/internal/xds/translator/cluster.go +++ b/internal/xds/translator/cluster.go @@ -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) } diff --git a/internal/xds/translator/ratelimit.go b/internal/xds/translator/ratelimit.go index 2f673485d49..53ffdd5bc59 100644 --- a/internal/xds/translator/ratelimit.go +++ b/internal/xds/translator/ratelimit.go @@ -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