Skip to content

Commit

Permalink
chore: fix ipFamily always nil
Browse files Browse the repository at this point in the history
Signed-off-by: Juwon Hwang (Kevin) <[email protected]>
  • Loading branch information
juwon8891 committed Nov 29, 2024
1 parent da265aa commit f356ebf
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 41 deletions.
23 changes: 10 additions & 13 deletions internal/xds/translator/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@
package translator

import (
"log"

egv1a1 "github.com/envoyproxy/gateway/api/v1alpha1"
"github.com/envoyproxy/gateway/internal/ir"
"k8s.io/utils/ptr"

"errors"
"fmt"
"net/netip"
Expand All @@ -23,7 +17,10 @@ import (
routev3 "github.com/envoyproxy/go-control-plane/envoy/config/route/v3"
hcmv3 "github.com/envoyproxy/go-control-plane/envoy/extensions/filters/network/http_connection_manager/v3"
"google.golang.org/protobuf/types/known/anypb"
"k8s.io/utils/ptr"

egv1a1 "github.com/envoyproxy/gateway/api/v1alpha1"
"github.com/envoyproxy/gateway/internal/ir"
"github.com/envoyproxy/gateway/internal/xds/types"
)

Expand Down Expand Up @@ -221,16 +218,16 @@ func determineIPFamily(settings []*ir.DestinationSetting) *egv1a1.IPFamily {
}
}

if hasDualStack {
switch {
case hasDualStack:
return ptr.To(egv1a1.DualStack)
} else if hasIPv4 && hasIPv6 {
case hasIPv4 && hasIPv6:
return ptr.To(egv1a1.DualStack)
} else if hasIPv4 {
case hasIPv4:
return ptr.To(egv1a1.IPv4)
} else if hasIPv6 {
log.Printf("Using IPv6")
case hasIPv6:
return ptr.To(egv1a1.IPv6)
default:
return nil
}

return nil
}
55 changes: 28 additions & 27 deletions internal/xds/translator/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@ package translator
import (
"testing"

"github.com/envoyproxy/gateway/api/v1alpha1"
"github.com/envoyproxy/gateway/internal/ir"
"github.com/stretchr/testify/assert"
"k8s.io/utils/ptr"

egv1a1 "github.com/envoyproxy/gateway/api/v1alpha1"
"github.com/envoyproxy/gateway/internal/ir"
)

func Test_determineIPFamily(t *testing.T) {
tests := []struct {
name string
settings []*ir.DestinationSetting
want *v1alpha1.IPFamily
want *egv1a1.IPFamily
}{
{
name: "nil settings should return nil",
Expand All @@ -33,72 +34,72 @@ func Test_determineIPFamily(t *testing.T) {
{
name: "single IPv4 setting",
settings: []*ir.DestinationSetting{
{IPFamily: ptr.To(v1alpha1.IPv4)},
{IPFamily: ptr.To(egv1a1.IPv4)},
},
want: ptr.To(v1alpha1.IPv4),
want: ptr.To(egv1a1.IPv4),
},
{
name: "single IPv6 setting",
settings: []*ir.DestinationSetting{
{IPFamily: ptr.To(v1alpha1.IPv6)},
{IPFamily: ptr.To(egv1a1.IPv6)},
},
want: ptr.To(v1alpha1.IPv6),
want: ptr.To(egv1a1.IPv6),
},
{
name: "single DualStack setting",
settings: []*ir.DestinationSetting{
{IPFamily: ptr.To(v1alpha1.DualStack)},
{IPFamily: ptr.To(egv1a1.DualStack)},
},
want: ptr.To(v1alpha1.DualStack),
want: ptr.To(egv1a1.DualStack),
},
{
name: "mixed IPv4 and IPv6 should return DualStack",
settings: []*ir.DestinationSetting{
{IPFamily: ptr.To(v1alpha1.IPv4)},
{IPFamily: ptr.To(v1alpha1.IPv6)},
{IPFamily: ptr.To(egv1a1.IPv4)},
{IPFamily: ptr.To(egv1a1.IPv6)},
},
want: ptr.To(v1alpha1.DualStack),
want: ptr.To(egv1a1.DualStack),
},
{
name: "DualStack with IPv4 should return DualStack",
settings: []*ir.DestinationSetting{
{IPFamily: ptr.To(v1alpha1.DualStack)},
{IPFamily: ptr.To(v1alpha1.IPv4)},
{IPFamily: ptr.To(egv1a1.DualStack)},
{IPFamily: ptr.To(egv1a1.IPv4)},
},
want: ptr.To(v1alpha1.DualStack),
want: ptr.To(egv1a1.DualStack),
},
{
name: "DualStack with IPv6 should return DualStack",
settings: []*ir.DestinationSetting{
{IPFamily: ptr.To(v1alpha1.DualStack)},
{IPFamily: ptr.To(v1alpha1.IPv6)},
{IPFamily: ptr.To(egv1a1.DualStack)},
{IPFamily: ptr.To(egv1a1.IPv6)},
},
want: ptr.To(v1alpha1.DualStack),
want: ptr.To(egv1a1.DualStack),
},
{
name: "mixed with nil IPFamily should be ignored",
settings: []*ir.DestinationSetting{
{IPFamily: ptr.To(v1alpha1.IPv4)},
{IPFamily: ptr.To(egv1a1.IPv4)},
{IPFamily: nil},
{IPFamily: ptr.To(v1alpha1.IPv6)},
{IPFamily: ptr.To(egv1a1.IPv6)},
},
want: ptr.To(v1alpha1.DualStack),
want: ptr.To(egv1a1.DualStack),
},
{
name: "multiple IPv4 settings should return IPv4",
settings: []*ir.DestinationSetting{
{IPFamily: ptr.To(v1alpha1.IPv4)},
{IPFamily: ptr.To(v1alpha1.IPv4)},
{IPFamily: ptr.To(egv1a1.IPv4)},
{IPFamily: ptr.To(egv1a1.IPv4)},
},
want: ptr.To(v1alpha1.IPv4),
want: ptr.To(egv1a1.IPv4),
},
{
name: "multiple IPv6 settings should return IPv6",
settings: []*ir.DestinationSetting{
{IPFamily: ptr.To(v1alpha1.IPv6)},
{IPFamily: ptr.To(v1alpha1.IPv6)},
{IPFamily: ptr.To(egv1a1.IPv6)},
{IPFamily: ptr.To(egv1a1.IPv6)},
},
want: ptr.To(v1alpha1.IPv6),
want: ptr.To(egv1a1.IPv6),
},
}

Expand Down
2 changes: 1 addition & 1 deletion test/e2e/testdata/httproute-dualstack.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,4 @@ metadata:
name: envoy-gateway
namespace: gateway-conformance-infra
spec:
controllerName: gateway.envoyproxy.io/gatewayclass-controller
controllerName: gateway.envoyproxy.io/gatewayclass-controller

0 comments on commit f356ebf

Please sign in to comment.