From f620e41aeb92f4519985834a31e9e34f9e0aefcd Mon Sep 17 00:00:00 2001 From: "Juwon Hwang (Kevin)" Date: Wed, 27 Nov 2024 12:48:16 +0900 Subject: [PATCH] chore: fix ipFamily always nil Signed-off-by: Juwon Hwang (Kevin) --- internal/gatewayapi/helpers.go | 10 +++++----- internal/gatewayapi/route.go | 4 ++++ internal/ir/xds.go | 8 +++++--- internal/ir/zz_generated.deepcopy.go | 5 +++++ internal/xds/translator/cluster.go | 13 ++++++++----- internal/xds/translator/translator.go | 4 ++++ 6 files changed, 31 insertions(+), 13 deletions(-) diff --git a/internal/gatewayapi/helpers.go b/internal/gatewayapi/helpers.go index 366a24b827e..1c32df6c62c 100644 --- a/internal/gatewayapi/helpers.go +++ b/internal/gatewayapi/helpers.go @@ -610,20 +610,20 @@ func setIfNil[T any](target **T, value *T) { } } +// getIPFamily returns the IPFamily configuration from EnvoyProxy func getIPFamily(envoyProxy *egv1a1.EnvoyProxy) *ir.IPFamily { if envoyProxy == nil || envoyProxy.Spec.IPFamily == nil { return nil } - var result ir.IPFamily + switch *envoyProxy.Spec.IPFamily { case egv1a1.IPv4: - result = ir.IPv4 + return ptr.To(ir.IPv4) case egv1a1.IPv6: - result = ir.IPv6 + return ptr.To(ir.IPv6) case egv1a1.DualStack: - result = ir.Dualstack + return ptr.To(ir.Dualstack) default: return nil } - return &result } diff --git a/internal/gatewayapi/route.go b/internal/gatewayapi/route.go index 26627a07285..adf5b481ee1 100644 --- a/internal/gatewayapi/route.go +++ b/internal/gatewayapi/route.go @@ -1261,9 +1261,11 @@ func (t *Translator) processDestination(backendRefContext BackendRefContext, Protocol: protocol, Endpoints: endpoints, AddressType: addrType, + IPFamily: getIPFamily(envoyProxy), } case resource.KindService: ds = t.processServiceDestinationSetting(backendRef.BackendObjectReference, backendNamespace, protocol, resources, envoyProxy) + ds.IPFamily = getIPFamily(envoyProxy) ds.TLS = t.applyBackendTLSSetting( backendRef.BackendObjectReference, @@ -1282,6 +1284,7 @@ func (t *Translator) processDestination(backendRefContext BackendRefContext, ds.Filters = t.processDestinationFilters(routeType, backendRefContext, parentRef, route, resources) case egv1a1.KindBackend: ds = t.processBackendDestinationSetting(backendRef.BackendObjectReference, backendNamespace, resources) + ds.IPFamily = getIPFamily(envoyProxy) ds.TLS = t.applyBackendTLSSetting( backendRef.BackendObjectReference, @@ -1379,6 +1382,7 @@ func (t *Translator) processServiceDestinationSetting( Protocol: protocol, Endpoints: endpoints, AddressType: addrType, + IPFamily: getIPFamily(envoyProxy), } } diff --git a/internal/ir/xds.go b/internal/ir/xds.go index b0b9a1594b1..53c7d065910 100644 --- a/internal/ir/xds.go +++ b/internal/ir/xds.go @@ -1308,9 +1308,11 @@ type DestinationSetting struct { Endpoints []*DestinationEndpoint `json:"endpoints,omitempty" yaml:"endpoints,omitempty"` // AddressTypeState specifies the state of DestinationEndpoint address type. AddressType *DestinationAddressType `json:"addressType,omitempty" yaml:"addressType,omitempty"` - - TLS *TLSUpstreamConfig `json:"tls,omitempty" yaml:"tls,omitempty"` - Filters *DestinationFilters `json:"filters,omitempty" yaml:"filters,omitempty"` + // IPFamily specifies the IP family (IPv4 or IPv6) to use for this destination's endpoints. + // This is derived from the backend service and endpoint slice information. + IPFamily *IPFamily `json:"ipFamily,omitempty" yaml:"ipFamily,omitempty"` + TLS *TLSUpstreamConfig `json:"tls,omitempty" yaml:"tls,omitempty"` + Filters *DestinationFilters `json:"filters,omitempty" yaml:"filters,omitempty"` } // Validate the fields within the RouteDestination structure diff --git a/internal/ir/zz_generated.deepcopy.go b/internal/ir/zz_generated.deepcopy.go index de0be09ff0f..92294824a6c 100644 --- a/internal/ir/zz_generated.deepcopy.go +++ b/internal/ir/zz_generated.deepcopy.go @@ -772,6 +772,11 @@ func (in *DestinationSetting) DeepCopyInto(out *DestinationSetting) { *out = new(DestinationAddressType) **out = **in } + if in.IPFamily != nil { + in, out := &in.IPFamily, &out.IPFamily + *out = new(IPFamily) + **out = **in + } if in.TLS != nil { in, out := &in.TLS, &out.TLS *out = new(TLSUpstreamConfig) diff --git a/internal/xds/translator/cluster.go b/internal/xds/translator/cluster.go index c5064c29eef..9e4b512d1d0 100644 --- a/internal/xds/translator/cluster.go +++ b/internal/xds/translator/cluster.go @@ -28,7 +28,6 @@ import ( "google.golang.org/protobuf/types/known/wrapperspb" "k8s.io/utils/ptr" - egv1a1 "github.com/envoyproxy/gateway/api/v1alpha1" "github.com/envoyproxy/gateway/internal/ir" "github.com/envoyproxy/gateway/internal/utils/protocov" ) @@ -57,7 +56,7 @@ type xdsClusterArgs struct { backendConnection *ir.BackendConnection dns *ir.DNS useClientProtocol bool - ipFamily *egv1a1.IPFamily + ipFamily *ir.IPFamily } type EndpointType int @@ -87,11 +86,11 @@ func buildXdsCluster(args *xdsClusterArgs) *clusterv3.Cluster { dnsLookupFamily := clusterv3.Cluster_V4_PREFERRED if args.ipFamily != nil { switch *args.ipFamily { - case egv1a1.IPv4: + case ir.IPv4: dnsLookupFamily = clusterv3.Cluster_V4_ONLY - case egv1a1.IPv6: + case ir.IPv6: dnsLookupFamily = clusterv3.Cluster_V6_ONLY - case egv1a1.DualStack: + case ir.Dualstack: dnsLookupFamily = clusterv3.Cluster_ALL } } @@ -698,6 +697,7 @@ type ExtraArgs struct { metrics *ir.Metrics http1Settings *ir.HTTP1Settings http2Settings *ir.HTTP2Settings + ipFamily *ir.IPFamily } type clusterArgs interface { @@ -716,6 +716,7 @@ func (route *UDPRouteTranslator) asClusterArgs(extra *ExtraArgs) *xdsClusterArgs endpointType: buildEndpointType(route.Destination.Settings), metrics: extra.metrics, dns: route.DNS, + ipFamily: extra.ipFamily, } } @@ -737,6 +738,7 @@ func (route *TCPRouteTranslator) asClusterArgs(extra *ExtraArgs) *xdsClusterArgs metrics: extra.metrics, backendConnection: route.BackendConnection, dns: route.DNS, + ipFamily: extra.ipFamily, } } @@ -754,6 +756,7 @@ func (httpRoute *HTTPRouteTranslator) asClusterArgs(extra *ExtraArgs) *xdsCluste http1Settings: extra.http1Settings, http2Settings: extra.http2Settings, useClientProtocol: ptr.Deref(httpRoute.UseClientProtocol, false), + ipFamily: extra.ipFamily, } // Populate traffic features. diff --git a/internal/xds/translator/translator.go b/internal/xds/translator/translator.go index a76382dd569..e548a8d1ebd 100644 --- a/internal/xds/translator/translator.go +++ b/internal/xds/translator/translator.go @@ -465,6 +465,10 @@ func (t *Translator) addRouteToRouteConfig( http1Settings: httpListener.HTTP1, } + if len(httpRoute.Destination.Settings) > 0 { + ea.ipFamily = httpRoute.Destination.Settings[0].IPFamily + } + if httpRoute.Traffic != nil && httpRoute.Traffic.HTTP2 != nil { ea.http2Settings = httpRoute.Traffic.HTTP2 }