Skip to content
Open
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
26 changes: 23 additions & 3 deletions cmd/atenet/internal/dns/corefile.go
Comment thread
ygao-g marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ func init() {
}

func buildTemplate() string {
// Build up the corefileTemplate programmatically to make it easier to understand.
const soaDirective = ` authority "{{ .Zone }} 60 IN SOA ns.dns.{{ .Zone }} hostmaster.{{ .Zone }} (1 60 60 60 60)"`

var directives []string
// Plugins to enable.
directives = append(directives, "log")
Expand All @@ -44,12 +45,31 @@ 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)
Comment thread
ygao-g marked this conversation as resolved.
// Note the %s -- this will be filled with the router IP.
directives = append(directives, ` answer "{{ .Name }} 60 IN A %s"`)
// Without fallthrough a regex miss answers SERVFAIL instead of reaching the
// next block.
directives = append(directives, " fallthrough")
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, " fallthrough")
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.
b := strings.Builder{}
fmt.Fprintf(&b, "# Generated at %s\n", time.Now())
fmt.Fprintf(&b, "%s:53 {\n ", resources.ActorDNSSuffix)
Expand Down
73 changes: 42 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,61 @@
package dns

import (
"fmt"
"strings"
"testing"

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

// Spelled out literally so a change to the name regex or DNS suffix fails this test.
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
11 changes: 3 additions & 8 deletions cmd/atenet/internal/dns/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,14 @@ import (
"syscall"
"time"

"github.com/agent-substrate/substrate/internal/atenetconsts"
"github.com/agent-substrate/substrate/internal/resources"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)

const (
// serviceName is the name of the CoreDNS service.
serviceName = "dns"
systemNamespace = "ate-system"
)

// Controller manages the DNS configuration for the ATE.
type Controller struct {
Client client.Client
Expand Down Expand Up @@ -73,7 +68,7 @@ func (c *Controller) reconcile(ctx context.Context) error {

// 1. Get the ClusterIP of atenet-router in ate-system namespace
routerSvc := &corev1.Service{}
if err := c.Client.Get(ctx, types.NamespacedName{Name: "atenet-router", Namespace: systemNamespace}, routerSvc); err != nil {
if err := c.Client.Get(ctx, types.NamespacedName{Name: atenetconsts.RouterService, Namespace: atenetconsts.NamespaceATESystem}, routerSvc); err != nil {
if errors.IsNotFound(err) {
slog.WarnContext(ctx, "atenet-router service not found, skipping until it is available")
return nil
Expand All @@ -89,7 +84,7 @@ func (c *Controller) reconcile(ctx context.Context) error {

// 2. Get the ClusterIP of dns service in ate-system namespace
dnsSvc := &corev1.Service{}
if err := c.Client.Get(ctx, types.NamespacedName{Name: serviceName, Namespace: systemNamespace}, dnsSvc); err != nil {
if err := c.Client.Get(ctx, types.NamespacedName{Name: atenetconsts.DNSService, Namespace: atenetconsts.NamespaceATESystem}, dnsSvc); err != nil {
if errors.IsNotFound(err) {
slog.WarnContext(ctx, "dns service not found, skipping until it is available")
return nil
Expand Down
25 changes: 25 additions & 0 deletions internal/atenetconsts/atenetconsts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package atenetconsts holds the cluster object names atenet and the e2e suites share.
package atenetconsts

const (
// NamespaceATESystem is the namespace the ATE system components run in.
NamespaceATESystem = "ate-system"
// RouterService is the atenet router Service.
RouterService = "atenet-router"
// DNSService is the atenet CoreDNS Service.
DNSService = "dns"
)
58 changes: 58 additions & 0 deletions internal/e2e/netutil/clusterip.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package netutil holds DNS and address-family helpers for the e2e suites.
package netutil

import (
"net/netip"

corev1 "k8s.io/api/core/v1"
)

// ServiceClusterIPs holds a Service's cluster IPs split by family; a family
// the Service does not have is the empty string.
type ServiceClusterIPs struct {
V4 string
V6 string
}

// ClusterIPsByFamily gets the Service's cluster IPs split by IPv4 and IPv6.
func ClusterIPsByFamily(svc *corev1.Service) ServiceClusterIPs {
var ips ServiceClusterIPs
if svc == nil {
return ips
}
addrs := svc.Spec.ClusterIPs
if len(addrs) == 0 && svc.Spec.ClusterIP != "" {
addrs = []string{svc.Spec.ClusterIP}
}
for _, ip := range addrs {
if ip == "" || ip == corev1.ClusterIPNone {
continue
}
addr, err := netip.ParseAddr(ip)
if err != nil {
continue
}
switch {
case addr.Is4() && ips.V4 == "":
ips.V4 = ip
// A v4-mapped v6 address counts as neither family.
case addr.Is6() && !addr.Is4In6() && ips.V6 == "":
ips.V6 = ip
}
}
return ips
}
Loading
Loading