Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions cmd/atenet/internal/dns/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ Cluster resources:

* Deployment `ate-system:dns`. Label: app=dns
* Service `ate-system:dns`.
* ConfigMap `ate-system:dns`.

These are defined in manifests/ate-install/atenet-dns.yaml.

Expand All @@ -20,16 +19,33 @@ These are defined in manifests/ate-install/atenet-dns.yaml.
* Deployment `ate-system:dns`.
* Service `ate-system:dns` pointing to the Deployment.

ConfigMap `ate-system:dns`:
Corefile, rendered by `corefile.go`:

```
# Match any 'A' query for an actor name + atespace pattern under actors.resources.substrate.ate.dev
# Answer any 'A' query for an actor name + atespace pattern under actors.resources.substrate.ate.dev
template IN A actors.resources.substrate.ate.dev {
match "^[a-z0-9]([-a-z0-9]*[a-z0-9])?\\.[a-z0-9]([-a-z0-9]*[a-z0-9])?\\.actors\\.resources\\.substrate\\.ate\\.dev\\.$"
answer "{{ .Name }} 60 IN A <router service address>"
fallthrough
}
# NODATA for a well-formed actor name on any other qtype (AAAA, HTTPS, SRV, ...).
template ANY ANY actors.resources.substrate.ate.dev {
match "^[a-z0-9]([-a-z0-9]*[a-z0-9])?\\.[a-z0-9]([-a-z0-9]*[a-z0-9])?\\.actors\\.resources\\.substrate\\.ate\\.dev\\.$"
rcode NOERROR
authority "{{ .Zone }} 60 IN SOA ns.dns.{{ .Zone }} hostmaster.{{ .Zone }} (1 60 60 60 60)"
fallthrough
}
# Terminal catch-all: NXDOMAIN for anything else in the zone.
template ANY ANY actors.resources.substrate.ate.dev {
rcode NXDOMAIN
authority "{{ .Zone }} 60 IN SOA ns.dns.{{ .Zone }} hostmaster.{{ .Zone }} (1 60 60 60 60)"
}
```

The last two blocks keep the zone from ever answering SERVFAIL, which musl libc
maps to `EAI_AGAIN` — sinking the paired A query with it — and which cannot be
cached negatively.

## Integration

* CoreDNS: Update CoreDNS ConfigMap to add the stub resolver.
Expand Down
25 changes: 24 additions & 1 deletion cmd/atenet/internal/dns/corefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ func init() {
}

func buildTemplate() string {
const (
fallthroughDirective = " fallthrough"
soaDirective = ` authority "{{ .Zone }} 60 IN SOA ns.dns.{{ .Zone }} hostmaster.{{ .Zone }} (1 60 60 60 60)"`
)

// Build up the corefileTemplate programmatically to make it easier to understand.
var directives []string
// Plugins to enable.
Expand All @@ -44,9 +49,27 @@ func buildTemplate() string {
directives = append(directives, fmt.Sprintf("template IN A %s {", resources.ActorDNSSuffix))
// Escape the suffix's dots so they match literally; the final \. matches the FQDN's trailing dot.
escapedSuffix := strings.ReplaceAll(resources.ActorDNSSuffix, ".", `\.`)
directives = append(directives, fmt.Sprintf(` match "^%s\.%s\.%s\.$"`, resources.ResourceNameRegexPattern, resources.ResourceNameRegexPattern, escapedSuffix))
actorMatch := fmt.Sprintf(` match "^%s\.%s\.%s\.$"`, resources.ResourceNameRegexPattern, resources.ResourceNameRegexPattern, escapedSuffix)
directives = append(directives, actorMatch)
// Note the %s -- this will be filled with the router IP.
directives = append(directives, ` answer "{{ .Name }} 60 IN A %s"`)
directives = append(directives, fallthroughDirective)
directives = append(directives, "}")

// Valid actor names return NOERROR (NODATA) for non-A queries.
directives = append(directives, fmt.Sprintf("template ANY ANY %s {", resources.ActorDNSSuffix))
directives = append(directives, actorMatch)
directives = append(directives, " rcode NOERROR")
directives = append(directives, soaDirective)
directives = append(directives, fallthroughDirective)
directives = append(directives, "}")

// Returns rcode NXDOMAIN (Non-Existent Domain) for any query that did not
// match the valid actor regex in the previous blocks.
// TODO(#922): answer empty non-terminals with NODATA.
directives = append(directives, fmt.Sprintf("template ANY ANY %s {", resources.ActorDNSSuffix))
directives = append(directives, " rcode NXDOMAIN")
directives = append(directives, soaDirective)
directives = append(directives, "}")

// Generate the template.
Expand Down
75 changes: 44 additions & 31 deletions cmd/atenet/internal/dns/corefile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,50 +15,63 @@
package dns

import (
"fmt"
"strings"
"testing"

"github.com/agent-substrate/substrate/internal/resources"
)

// Spelled out rather than built from resources.ResourceNameRegexPattern and
// ActorDNSSuffix: the rendered zone is a wire contract, so a change to either
// constant should fail here instead of being tracked silently.
const wantCorefileFmt = `actors.resources.substrate.ate.dev:53 {
log
errors
health :8080
ready :8181
reload
template IN A actors.resources.substrate.ate.dev {
match "^[a-z0-9]([-a-z0-9]*[a-z0-9])?\.[a-z0-9]([-a-z0-9]*[a-z0-9])?\.actors\.resources\.substrate\.ate\.dev\.$"
answer "{{ .Name }} 60 IN A %s"
fallthrough
}
template ANY ANY actors.resources.substrate.ate.dev {
match "^[a-z0-9]([-a-z0-9]*[a-z0-9])?\.[a-z0-9]([-a-z0-9]*[a-z0-9])?\.actors\.resources\.substrate\.ate\.dev\.$"
rcode NOERROR
authority "{{ .Zone }} 60 IN SOA ns.dns.{{ .Zone }} hostmaster.{{ .Zone }} (1 60 60 60 60)"
fallthrough
}
template ANY ANY actors.resources.substrate.ate.dev {
rcode NXDOMAIN
authority "{{ .Zone }} 60 IN SOA ns.dns.{{ .Zone }} hostmaster.{{ .Zone }} (1 60 60 60 60)"
}
}
`

// zoneBody strips the "# Generated at <timestamp>" header.
func zoneBody(t *testing.T, corefile string) string {
t.Helper()
header, body, ok := strings.Cut(corefile, "\n")
if !ok || !strings.HasPrefix(header, "# Generated at ") {
t.Fatalf("makeCoreFile() has no generated-at header, got first line %q", header)
}
return body
}

func TestMakeCoreFile(t *testing.T) {
tests := []struct {
name string
routerIP string
expected []string
}{
{
name: "standard local IP",
routerIP: "10.240.0.10",
expected: []string{
"actors.resources.substrate.ate.dev:53 {",
"log",
"errors",
"health :8080",
"ready :8181",
"reload",
"template IN A actors.resources.substrate.ate.dev {",
`match "^` + resources.ResourceNameRegexPattern + `\.` + resources.ResourceNameRegexPattern + `\.actors\.resources\.substrate\.ate\.dev\.$"`,
`answer "{{ .Name }} 60 IN A 10.240.0.10"`,
},
},
{
name: "different IP",
routerIP: "192.168.1.1",
expected: []string{
"actors.resources.substrate.ate.dev:53 {",
`answer "{{ .Name }} 60 IN A 192.168.1.1"`,
},
},
{name: "cluster IP", routerIP: "10.240.0.10"},
{name: "different cluster IP", routerIP: "192.168.1.1"},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got := makeCoreFile(tc.routerIP)
for _, exp := range tc.expected {
if !strings.Contains(got, exp) {
t.Errorf("makeCoreFile(%q) missing expected substring %q\nGot:\n%s", tc.routerIP, exp, got)
}
got := zoneBody(t, makeCoreFile(tc.routerIP))
want := fmt.Sprintf(wantCorefileFmt, tc.routerIP)
if got != want {
t.Errorf("makeCoreFile(%q) rendered an unexpected Corefile\nGot:\n%s\nWant:\n%s", tc.routerIP, got, want)
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/atenet/internal/router/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func NewRouterCmd() *cobra.Command {
// must propagate to the Service endpoints before the drain starts.
cmd.Flags().DurationVar(&cfg.DrainDelay, "drain-delay", 13*time.Second, "How long to keep serving after SIGTERM before starting the drain, covering readiness-probe detection and Service endpoint propagation")
cmd.Flags().DurationVar(&cfg.DrainTimeout, "drain-timeout", 0, "Deadline for the ext_proc drain on shutdown; streams still open past it (parked requests included) are forcefully cancelled. 0 (the default) derives --parked-request-budget + the actor route timeout + margin so parked requests always finish normally. Explicit values must be >= --parked-request-budget")
cmd.Flags().StringVar(&cfg.EnvoyAdminAddr, "envoy-admin-address", "127.0.0.1:9901", "Envoy admin interface the shutdown sequence drives to drain the sidecar (healthcheck/fail, drain_listeners, stats polling). Ignored with --atenet-router=agentgateway")
cmd.Flags().StringVar(&cfg.EnvoyAdminAddr, "envoy-admin-address", "localhost:9901", "Envoy admin interface the shutdown sequence drives to drain the sidecar (healthcheck/fail, drain_listeners, stats polling). Ignored with --atenet-router=agentgateway")
cmd.Flags().StringVar(&cfg.DrainCompleteFile, "drain-complete-file", defaultDrainCompleteFile, "Marker file created (on a pod-shared emptyDir) once the shutdown drain completes; the dataplane container's preStop hook polls for it so the proxy exits as soon as — and no sooner than — the drain is done. Removed at startup to defuse stale markers. Empty disables the handshake")

return cmd
Expand Down
4 changes: 3 additions & 1 deletion cmd/atenet/internal/router/dataplane.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ type dataplaneHealthCheck struct {
func (r atenetRouter) healthCheck() dataplaneHealthCheck {
switch r {
case atenetRouterEnvoy:
return dataplaneHealthCheck{url: "http://127.0.0.1:9901/ready", expectedBody: "LIVE"}
// localhost, not 127.0.0.1: the admin socket binds `::`, so the dial
// has to be able to fall through to the IPv6 loopback.
return dataplaneHealthCheck{url: "http://localhost:9901/ready", expectedBody: "LIVE"}
case atenetRouterAgentgateway:
return dataplaneHealthCheck{url: "http://127.0.0.1:15021/healthz/ready", expectedBody: "ready"}
default:
Expand Down
34 changes: 34 additions & 0 deletions cmd/atenet/internal/router/drain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package router

import (
"context"
"net"
"net/http"
"net/http/httptest"
"os"
Expand Down Expand Up @@ -280,6 +281,39 @@ func TestEnvoyDrainerDrainsToZero(t *testing.T) {
}
}

// TestEnvoyDrainerReachesIPv6OnlyAdmin guards the loopback coupling behind
// --envoy-admin-address: the gateway admin sockets bind "::", and a drainer
// pinned to the IPv4 loopback would find nothing listening, read that as an
// exited Envoy, and report a drain it never performed -- silently, since that
// path returns nil. Hence the assertion on the POSTs rather than on the error.
func TestEnvoyDrainerReachesIPv6OnlyAdmin(t *testing.T) {
ln, err := net.Listen("tcp6", "[::1]:0")
if err != nil {
t.Skipf("no IPv6 loopback on this host: %v", err)
}
admin := &fakeEnvoyAdmin{activeSeries: []int{0}}
srv := httptest.NewUnstartedServer(admin.handler())
srv.Listener.Close()
srv.Listener = ln
srv.Start()
defer srv.Close()

d := newEnvoyDrainer(net.JoinHostPort("localhost", itoa(ln.Addr().(*net.TCPAddr).Port)))
d.pollInterval = 5 * time.Millisecond

ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
if err := d.Drain(ctx); err != nil {
t.Fatalf("Drain: %v", err)
}

admin.mu.Lock()
defer admin.mu.Unlock()
if len(admin.posts) != 2 {
t.Errorf("admin POSTs = %v, want the drain to have reached the IPv6 loopback", admin.posts)
}
}

// TestEnvoyDrainerAdminGone asserts an unreachable admin interface (Envoy
// already exited) is treated as a completed drain, quickly.
func TestEnvoyDrainerAdminGone(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/atenet/internal/router/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestCheckDataplane(t *testing.T) {
{
name: "envoy",
router: atenetRouterEnvoy,
wantURL: "http://127.0.0.1:9901/ready",
wantURL: "http://localhost:9901/ready",
response: "LIVE",
wantMessage: "LIVE",
},
Expand Down
25 changes: 25 additions & 0 deletions cmd/atenet/internal/router/xds.go
Original file line number Diff line number Diff line change
Expand Up @@ -1108,6 +1108,27 @@ func (x *XdsServer) buildTracing() *hcmv3.HttpConnectionManager_Tracing {
}
}

// dualStackAdditionalAddresses returns the IPv6 half of a dual-stack ingress
// listener, to pair with a primary 0.0.0.0 socket on the same port. Ipv4Compat
// stays false: clearing IPV6_V6ONLY would collide with that primary.
func dualStackAdditionalAddresses(port int) []*listenerv3.AdditionalAddress {
return []*listenerv3.AdditionalAddress{
{
Address: &corev3.Address{
Address: &corev3.Address_SocketAddress{
SocketAddress: &corev3.SocketAddress{
Address: "::",
Ipv4Compat: false,
PortSpecifier: &corev3.SocketAddress_PortValue{
PortValue: uint32(port),
},
},
},
},
},
}
}

func (x *XdsServer) buildListener() *listenerv3.Listener {
hcm := x.buildHcm("ingress_http", true)

Expand All @@ -1123,6 +1144,7 @@ func (x *XdsServer) buildListener() *listenerv3.Listener {
},
},
},
AdditionalAddresses: dualStackAdditionalAddresses(x.ingressPort),
FilterChains: []*listenerv3.FilterChain{
{
Filters: []*listenerv3.Filter{
Expand Down Expand Up @@ -1182,6 +1204,7 @@ func (x *XdsServer) buildHttpsListener() *listenerv3.Listener {
},
},
},
AdditionalAddresses: dualStackAdditionalAddresses(x.httpsPort),
FilterChains: []*listenerv3.FilterChain{
{
Filters: []*listenerv3.Filter{
Expand Down Expand Up @@ -1213,6 +1236,7 @@ func (x *XdsServer) buildConnectTerminateListener() *listenerv3.Listener {
},
},
},
AdditionalAddresses: dualStackAdditionalAddresses(x.connectPlainTextPort),
FilterChains: []*listenerv3.FilterChain{
{
Filters: []*listenerv3.Filter{
Expand Down Expand Up @@ -1246,6 +1270,7 @@ func (x *XdsServer) buildConnectTerminateTLSListener() *listenerv3.Listener {
},
},
},
AdditionalAddresses: dualStackAdditionalAddresses(x.connectTLSPort),
FilterChains: []*listenerv3.FilterChain{
{
Filters: []*listenerv3.Filter{
Expand Down
Loading
Loading