Skip to content

Commit 8575ed8

Browse files
authored
Add global setting for dns lookup family (#10755)
1 parent 62ac501 commit 8575ed8

13 files changed

+217
-24
lines changed

internal/kgateway/extensions2/settings/settings.go

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ import (
55
)
66

77
type Settings struct {
8+
// Controls the DnsLookupFamily for all static clusters created via Backend resources.
9+
// If not set, kgateway will default to "V4_PREFERRED". Note that this is different
10+
// from the Envoy default of "AUTO", which is effectively "V6_PREFERRED".
11+
// Supported values are: "ALL", "AUTO", "V4_PREFERRED", "V4_ONLY", "V6_ONLY"
12+
// Details on the behavior of these options are available on the Envoy documentation:
13+
// https://www.envoyproxy.io/docs/envoy/latest/api-v3/config/cluster/v3/cluster.proto#enum-config-cluster-v3-cluster-dnslookupfamily
14+
DnsLookupFamily string `split_words:"true" default:"V4_PREFERRED"`
15+
816
EnableIstioIntegration bool `split_words:"true"`
917
EnableAutoMtls bool `split_words:"true"`
1018
StsClusterName string `split_words:"true"`

internal/kgateway/extensions2/settings/settings_test.go

+14-9
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"testing"
66

77
"github.com/onsi/gomega"
8-
. "github.com/onsi/gomega"
98

109
"github.com/kgateway-dev/kgateway/v2/internal/kgateway/extensions2/settings"
1110
"github.com/kgateway-dev/kgateway/v2/internal/kgateway/wellknown"
@@ -26,9 +25,12 @@ func TestSettings(t *testing.T) {
2625
expectedErrorStr string
2726
}{
2827
{
28+
// TODO: this test case does not fail when a new field is added to Settings
29+
// but not updated here. should it?
2930
name: "defaults to empty or default values",
3031
envVars: map[string]string{},
3132
expectedSettings: &settings.Settings{
33+
DnsLookupFamily: "V4_PREFERRED",
3234
EnableIstioIntegration: false,
3335
EnableAutoMtls: false,
3436
StsClusterName: "",
@@ -40,6 +42,7 @@ func TestSettings(t *testing.T) {
4042
{
4143
name: "all values set",
4244
envVars: map[string]string{
45+
"KGW_DNS_LOOKUP_FAMILY": "V4_ONLY",
4346
"KGW_ENABLE_ISTIO_INTEGRATION": "true",
4447
"KGW_ENABLE_AUTO_MTLS": "true",
4548
"KGW_STS_CLUSTER_NAME": "my-cluster",
@@ -48,6 +51,7 @@ func TestSettings(t *testing.T) {
4851
"KGW_XDS_SERVICE_PORT": "1234",
4952
},
5053
expectedSettings: &settings.Settings{
54+
DnsLookupFamily: "V4_ONLY",
5155
EnableIstioIntegration: true,
5256
EnableAutoMtls: true,
5357
StsClusterName: "my-cluster",
@@ -78,9 +82,10 @@ func TestSettings(t *testing.T) {
7882
"KGW_ENABLE_AUTO_MTLS": "true",
7983
},
8084
expectedSettings: &settings.Settings{
81-
EnableAutoMtls: true,
82-
XdsServiceName: wellknown.DefaultXdsService,
83-
XdsServicePort: wellknown.DefaultXdsPort,
85+
DnsLookupFamily: "V4_PREFERRED",
86+
EnableAutoMtls: true,
87+
XdsServiceName: wellknown.DefaultXdsService,
88+
XdsServicePort: wellknown.DefaultXdsPort,
8489
},
8590
},
8691
}
@@ -92,21 +97,21 @@ func TestSettings(t *testing.T) {
9297
t.Cleanup(func() {
9398
for k := range tc.envVars {
9499
err := os.Unsetenv(k)
95-
g.Expect(err).NotTo(HaveOccurred())
100+
g.Expect(err).NotTo(gomega.HaveOccurred())
96101
}
97102
})
98103

99104
for k, v := range tc.envVars {
100105
err := os.Setenv(k, v)
101-
g.Expect(err).NotTo(HaveOccurred())
106+
g.Expect(err).NotTo(gomega.HaveOccurred())
102107
}
103108
s, err := settings.BuildSettings()
104109
if tc.expectedErrorStr != "" {
105-
g.Expect(err).To(HaveOccurred())
110+
g.Expect(err).To(gomega.HaveOccurred())
106111
g.Expect(err.Error()).To(gomega.ContainSubstring(tc.expectedErrorStr))
107112
} else {
108-
g.Expect(err).NotTo(HaveOccurred())
109-
g.Expect(s).To(Equal(tc.expectedSettings))
113+
g.Expect(err).NotTo(gomega.HaveOccurred())
114+
g.Expect(s).To(gomega.Equal(tc.expectedSettings))
110115
}
111116
})
112117
}

internal/kgateway/setup/ggv2setup_test.go

+21-7
Original file line numberDiff line numberDiff line change
@@ -107,18 +107,32 @@ func init() {
107107
grpclog.SetLoggerV2(grpclog.NewLoggerV2WithVerbosity(writer, writer, writer, 100))
108108
}
109109

110+
func TestWithAutoDns(t *testing.T) {
111+
os.Setenv("KGW_DNS_LOOKUP_FAMILY", "AUTO")
112+
t.Cleanup(func() {
113+
os.Unsetenv("KGW_DNS_LOOKUP_FAMILY")
114+
})
115+
runScenario(t, "testdata/autodns")
116+
}
117+
110118
func TestScenarios(t *testing.T) {
119+
// set global settings env vars; "default" ggv2setup_tests assume these are set to true
120+
os.Setenv("KGW_ENABLE_ISTIO_INTEGRATION", "true")
121+
os.Setenv("KGW_ENABLE_AUTO_MTLS", "true")
122+
t.Cleanup(func() {
123+
os.Unsetenv("KGW_ENABLE_ISTIO_INTEGRATION")
124+
os.Unsetenv("KGW_ENABLE_AUTO_MTLS")
125+
})
126+
runScenario(t, "testdata")
127+
}
128+
129+
func runScenario(t *testing.T, scenarioDir string) {
111130
proxy_syncer.UseDetailedUnmarshalling = true
112131
writer.set(t)
113132

114133
os.Setenv("POD_NAMESPACE", "gwtest") // TODO: is this still needed?
115-
// set global settings env vars; current ggv2setup_tests all assume these are set to true
116-
os.Setenv("KGW_ENABLE_ISTIO_INTEGRATION", "true")
117-
os.Setenv("KGW_ENABLE_AUTO_MTLS", "true")
118134
t.Cleanup(func() {
119135
os.Unsetenv("POD_NAMESPACE")
120-
os.Unsetenv("KGW_ENABLE_ISTIO_INTEGRATION")
121-
os.Unsetenv("KGW_ENABLE_AUTO_MTLS")
122136
})
123137

124138
testEnv := &envtest.Environment{
@@ -203,15 +217,15 @@ func TestScenarios(t *testing.T) {
203217
time.Sleep(time.Second)
204218

205219
// list all yamls in test data
206-
files, err := os.ReadDir("testdata")
220+
files, err := os.ReadDir(scenarioDir)
207221
if err != nil {
208222
t.Fatalf("failed to read dir: %v", err)
209223
}
210224
for _, f := range files {
211225
// run tests with the yaml files (but not -out.yaml files)/s
212226
parentT := t
213227
if strings.HasSuffix(f.Name(), ".yaml") && !strings.HasSuffix(f.Name(), "-out.yaml") {
214-
fullpath := filepath.Join("testdata", f.Name())
228+
fullpath := filepath.Join(scenarioDir, f.Name())
215229
t.Run(strings.TrimSuffix(f.Name(), ".yaml"), func(t *testing.T) {
216230
writer.set(t)
217231
t.Cleanup(func() {

internal/kgateway/setup/testdata/accesslog-filtercel-httplisteneropt-out.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ clusters:
9898
'@type': type.googleapis.com/envoy.extensions.transport_sockets.raw_buffer.v3.RawBuffer
9999
type: EDS
100100
- connectTimeout: 5s
101+
dnsLookupFamily: V4_PREFERRED
101102
loadAssignment:
102103
clusterName: backend_gwtest_log_0
103104
endpoints:

internal/kgateway/setup/testdata/ai-anthropic-passthrough-out.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ clusters:
6464
'@type': type.googleapis.com/envoy.extensions.transport_sockets.raw_buffer.v3.RawBuffer
6565
type: EDS
6666
- connectTimeout: 5s
67+
dnsLookupFamily: V4_PREFERRED
6768
loadAssignment:
6869
clusterName: backend_gwtest_anthropic_0
6970
endpoints:

internal/kgateway/setup/testdata/ai-deepseek-prompt-guard-out.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ clusters:
6464
'@type': type.googleapis.com/envoy.extensions.transport_sockets.raw_buffer.v3.RawBuffer
6565
type: EDS
6666
- connectTimeout: 5s
67+
dnsLookupFamily: V4_PREFERRED
6768
loadAssignment:
6869
clusterName: backend_gwtest_deepseek_0
6970
endpoints:

internal/kgateway/setup/testdata/ai-openai-moderation-promptguard-out.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ clusters:
6464
'@type': type.googleapis.com/envoy.extensions.transport_sockets.raw_buffer.v3.RawBuffer
6565
type: EDS
6666
- connectTimeout: 5s
67+
dnsLookupFamily: V4_PREFERRED
6768
loadAssignment:
6869
clusterName: backend_gwtest_openai_0
6970
endpoints:

internal/kgateway/setup/testdata/ai-vertex-ai-streaming-out.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ clusters:
6464
'@type': type.googleapis.com/envoy.extensions.transport_sockets.raw_buffer.v3.RawBuffer
6565
type: EDS
6666
- connectTimeout: 5s
67+
dnsLookupFamily: V4_PREFERRED
6768
loadAssignment:
6869
clusterName: backend_gwtest_vertexai_0
6970
endpoints:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
clusters:
2+
- connectTimeout: 5s
3+
loadAssignment:
4+
clusterName: backend_gwtest_static_0
5+
endpoints:
6+
- lbEndpoints:
7+
- endpoint:
8+
address:
9+
socketAddress:
10+
address: 1.2.3.4
11+
portValue: 8080
12+
healthCheckConfig:
13+
hostname: 1.2.3.4
14+
hostname: 1.2.3.4
15+
metadata: {}
16+
name: backend_gwtest_static_0
17+
type: STATIC
18+
- connectTimeout: 5s
19+
edsClusterConfig:
20+
edsConfig:
21+
ads: {}
22+
resourceApiVersion: V3
23+
metadata: {}
24+
name: kube_default_kubernetes_443
25+
type: EDS
26+
- connectTimeout: 5s
27+
edsClusterConfig:
28+
edsConfig:
29+
ads: {}
30+
resourceApiVersion: V3
31+
metadata: {}
32+
name: kube_gwtest_http-backend_8080
33+
type: EDS
34+
listeners:
35+
- address:
36+
socketAddress:
37+
address: '::'
38+
ipv4Compat: true
39+
portValue: 8080
40+
filterChains:
41+
- filters:
42+
- name: envoy.filters.network.http_connection_manager
43+
typedConfig:
44+
'@type': type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
45+
httpFilters:
46+
- name: envoy.filters.http.router
47+
typedConfig:
48+
'@type': type.googleapis.com/envoy.extensions.filters.http.router.v3.Router
49+
mergeSlashes: true
50+
normalizePath: true
51+
rds:
52+
configSource:
53+
ads: {}
54+
resourceApiVersion: V3
55+
routeConfigName: http
56+
statPrefix: http
57+
useRemoteAddress: true
58+
name: http
59+
name: http
60+
routes:
61+
- ignorePortInHostMatching: true
62+
name: http
63+
virtualHosts:
64+
- domains:
65+
- www.example.com
66+
name: http~www_example_com
67+
routes:
68+
- match:
69+
prefix: /
70+
name: http~www_example_com-route-0-httproute-route-to-upstream-gwtest-0-0-matcher-0
71+
route:
72+
cluster: backend_gwtest_static_0
73+
clusterNotFoundResponseCode: INTERNAL_SERVER_ERROR
74+
typedPerFilterConfig:
75+
ai.extproc.kgateway.io:
76+
'@type': type.googleapis.com/envoy.extensions.filters.http.ext_proc.v3.ExtProcPerRoute
77+
disabled: true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
kind: Gateway
2+
apiVersion: gateway.networking.k8s.io/v1
3+
metadata:
4+
name: http-gw-for-test
5+
namespace: gwtest
6+
spec:
7+
gatewayClassName: kgateway
8+
listeners:
9+
- protocol: HTTP
10+
port: 8080
11+
name: http
12+
allowedRoutes:
13+
namespaces:
14+
from: All
15+
---
16+
apiVersion: gateway.networking.k8s.io/v1beta1
17+
kind: HTTPRoute
18+
metadata:
19+
name: route-to-upstream
20+
namespace: gwtest
21+
spec:
22+
parentRefs:
23+
- name: http-gw-for-test
24+
hostnames:
25+
- "www.example.com"
26+
rules:
27+
- backendRefs:
28+
- name: static
29+
kind: Backend
30+
group: gateway.kgateway.dev
31+
---
32+
apiVersion: gateway.kgateway.dev/v1alpha1
33+
kind: Backend
34+
metadata:
35+
name: static
36+
namespace: gwtest
37+
spec:
38+
type: Static
39+
static:
40+
hosts:
41+
- host: 1.2.3.4
42+
port: 8080

internal/kgateway/setup/testdata/backend-out.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ clusters:
4949
'@type': type.googleapis.com/envoy.extensions.transport_sockets.raw_buffer.v3.RawBuffer
5050
type: EDS
5151
- connectTimeout: 5s
52+
dnsLookupFamily: V4_PREFERRED
5253
loadAssignment:
5354
clusterName: backend_gwtest_static_0
5455
endpoints:

0 commit comments

Comments
 (0)