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

feat: Support DNS resolution settings #3974

Merged
merged 16 commits into from
Aug 1, 2024
4 changes: 4 additions & 0 deletions api/v1alpha1/backendtrafficpolicy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ type BackendTrafficPolicySpec struct {
//
// +optional
Connection *BackendConnection `json:"connection,omitempty"`
// DNS includes dns resolution settings.
//
// +optional
DNS *DNS `json:"dns,omitempty"`
}

// +kubebuilder:object:root=true
Expand Down
18 changes: 18 additions & 0 deletions api/v1alpha1/dns_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright Envoy Gateway Authors
// SPDX-License-Identifier: Apache-2.0
// The full text of the Apache license is available in the LICENSE file at
// the root of the repo.

package v1alpha1

import metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

type DNS struct {
// DNSRefreshRate specifies the rate at which DNS records should be refreshed.
guydc marked this conversation as resolved.
Show resolved Hide resolved
// Defaults to 30 seconds.
DNSRefreshRate *metav1.Duration `json:"dnsRefreshRate,omitempty"`
// RespectDNSTTL indicates whether the DNS Time-To-Live (TTL) should be respected.
guydc marked this conversation as resolved.
Show resolved Hide resolved
// If the value is set to true, the DNS refresh rate will be set to the resource record’s TTL.
// Defaults to true.
RespectDNSTTL *bool `json:"respectDnsTtl,omitempty"`
}
30 changes: 30 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

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

Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,21 @@ spec:
rule: 'type(self) == string ? self.matches(r"^[1-9]+[0-9]*([EPTGMK]i|[EPTGMk])?$")
: type(self) == int'
type: object
dns:
description: DNS includes dns resolution settings.
properties:
dnsRefreshRate:
description: |-
DNSRefreshRate specifies the rate at which DNS records should be refreshed.
Defaults to 30 seconds.
type: string
respectDnsTtl:
description: |-
RespectDNSTTL indicates whether the DNS Time-To-Live (TTL) should be respected.
If the value is set to true, the DNS refresh rate will be set to the resource record’s TTL.
Defaults to true.
type: boolean
type: object
faultInjection:
description: |-
FaultInjection defines the fault injection policy to be applied. This configuration can be used to
Expand Down
36 changes: 36 additions & 0 deletions internal/gatewayapi/backendtrafficpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ func (t *Translator) translateBackendTrafficPolicyForRoute(policy *egv1a1.Backen
ka *ir.TCPKeepalive
rt *ir.Retry
bc *ir.BackendConnection
ds *ir.DNS
err, errs error
)

Expand Down Expand Up @@ -349,6 +350,10 @@ func (t *Translator) translateBackendTrafficPolicyForRoute(policy *egv1a1.Backen
}
}

if policy.Spec.DNS != nil {
ds = t.translateDNS(policy)
}

// Early return if got any errors
if errs != nil {
return errs
Expand All @@ -368,6 +373,7 @@ func (t *Translator) translateBackendTrafficPolicyForRoute(policy *egv1a1.Backen
r.TCPKeepalive = ka
r.Timeout = to
r.BackendConnection = bc
r.DNS = ds
}
}
}
Expand All @@ -380,6 +386,7 @@ func (t *Translator) translateBackendTrafficPolicyForRoute(policy *egv1a1.Backen
r.LoadBalancer = lb
r.Timeout = to
r.BackendConnection = bc
r.DNS = ds
}
}
}
Expand All @@ -400,6 +407,7 @@ func (t *Translator) translateBackendTrafficPolicyForRoute(policy *egv1a1.Backen
BackendConnection: bc,
}

r.DNS = ds
// Update the Host field in HealthCheck, now that we have access to the Route Hostname.
r.Traffic.HealthCheck.SetHTTPHostIfAbsent(r.Hostname)

Expand Down Expand Up @@ -432,6 +440,7 @@ func (t *Translator) translateBackendTrafficPolicyForGateway(policy *egv1a1.Back
ct *ir.Timeout
ka *ir.TCPKeepalive
rt *ir.Retry
ds *ir.DNS
err, errs error
)

Expand Down Expand Up @@ -479,6 +488,10 @@ func (t *Translator) translateBackendTrafficPolicyForGateway(policy *egv1a1.Back
}
}

if policy.Spec.DNS != nil {
ds = t.translateDNS(policy)
}

// Early return if got any errors
if errs != nil {
return errs
Expand Down Expand Up @@ -516,6 +529,10 @@ func (t *Translator) translateBackendTrafficPolicyForGateway(policy *egv1a1.Back
if r.Timeout == nil {
r.Timeout = ct
}

if r.DNS == nil {
r.DNS = ds
}
}
}

Expand All @@ -540,6 +557,10 @@ func (t *Translator) translateBackendTrafficPolicyForGateway(policy *egv1a1.Back
if route.Timeout == nil {
route.Timeout = ct
}

if route.DNS == nil {
route.DNS = ds
}
}

for _, http := range x.HTTP {
Expand Down Expand Up @@ -568,6 +589,10 @@ func (t *Translator) translateBackendTrafficPolicyForGateway(policy *egv1a1.Back
Retry: rt,
}

if r.DNS == nil {
r.DNS = ds
}

// Update the Host field in HealthCheck, now that we have access to the Route Hostname.
r.Traffic.HealthCheck.SetHTTPHostIfAbsent(r.Hostname)

Expand Down Expand Up @@ -847,6 +872,17 @@ func (t *Translator) buildConsistentHashLoadBalancer(policy *egv1a1.BackendTraff
return consistentHash, nil
}

func (t *Translator) translateDNS(policy *egv1a1.BackendTrafficPolicy) *ir.DNS {
ds := &ir.DNS{}
if policy.Spec.DNS.RespectDNSTTL != nil {
ds.RespectDNSTTL = policy.Spec.DNS.RespectDNSTTL
}
if policy.Spec.DNS.DNSRefreshRate != nil {
ds.DNSRefreshRate = policy.Spec.DNS.DNSRefreshRate
}
return ds
}

func (t *Translator) buildProxyProtocol(policy *egv1a1.BackendTrafficPolicy) *ir.ProxyProtocol {
var pp *ir.ProxyProtocol
switch policy.Spec.ProxyProtocol.Version {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
gateways:
- apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
namespace: envoy-gateway
name: gateway-1
spec:
gatewayClassName: envoy-gateway-class
listeners:
- name: http
protocol: HTTP
port: 80
allowedRoutes:
namespaces:
from: All
- apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
namespace: envoy-gateway
name: gateway-2
spec:
gatewayClassName: envoy-gateway-class
listeners:
- name: http
protocol: HTTP
port: 80
allowedRoutes:
namespaces:
from: All
httpRoutes:
- apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: default
name: httproute-1
spec:
hostnames:
- gateway.envoyproxy.io
parentRefs:
- namespace: envoy-gateway
name: gateway-2
sectionName: http
rules:
- matches:
- path:
value: "/"
backendRefs:
- name: service-1
port: 8080
- apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: default
name: httproute-2
spec:
hostnames:
- gateway.envoyproxy.io
parentRefs:
- namespace: envoy-gateway
name: gateway-2
sectionName: http
rules:
- matches:
- path:
value: "/v2"
backendRefs:
- name: service-2
port: 8080
- apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: default
name: httproute-3
spec:
hostnames:
- gateway.envoyproxy.io
parentRefs:
- namespace: envoy-gateway
name: gateway-1
sectionName: http
rules:
- matches:
- path:
value: "/v3"
backendRefs:
- name: service-3
port: 8080
backendTrafficPolicies:
- apiVersion: gateway.envoyproxy.io/v1alpha1
kind: BackendTrafficPolicy
metadata:
namespace: default
name: policy-for-route-1
spec:
targetRef:
group: gateway.networking.k8s.io
kind: HTTPRoute
name: httproute-1
dns:
dnsRefreshRate: "1s"
respectDnsTtl: true
- apiVersion: gateway.envoyproxy.io/v1alpha1
kind: BackendTrafficPolicy
metadata:
namespace: default
name: policy-for-route-2
spec:
targetRef:
group: gateway.networking.k8s.io
kind: HTTPRoute
name: httproute-2
dns:
dnsRefreshRate: "5s"
respectDnsTtl: false
- apiVersion: gateway.envoyproxy.io/v1alpha1
kind: BackendTrafficPolicy
metadata:
namespace: envoy-gateway
name: policy-for-all-routes-in-gateway-1
spec:
targetRef:
group: gateway.networking.k8s.io
kind: Gateway
name: gateway-1
dns:
dnsRefreshRate: "10s"
respectDnsTtl: true
Loading