plugin/clouddns: add refresh directive (#8616)

Brings clouddns to parity with route53, which already exposes a
refresh directive to control how often zones are re-synced. clouddns
currently hardcodes this to 1 minute (delay := 1 * time.Minute in Run)
with no way to change it.

Adds a refresh field to the CloudDNS struct, threads it through New(),
and has Run() use h.refresh instead of the hardcoded delay. Parses the
refresh directive in setup() mirroring route53's exact validation
(accepts a plain number as seconds or a duration string, must be
greater than 0).

Extends TestSetupCloudDNS with refresh coverage (valid duration, valid
plain number, missing arg, non-numeric, zero, negative). Verified it
fails without the setup.go change (unknown property error) and passes
with it.

Signed-off-by: stackedbyaradhya <aradhyaj736@gmail.com>
This commit is contained in:
Aradhya Jain
2026-10-09 07:17:32 +05:30
committed by GitHub
parent 45ac7fe659
commit 538c3554bc
5 changed files with 67 additions and 8 deletions

View File

@@ -21,6 +21,7 @@ the hosted zone.
clouddns [ZONE:PROJECT_ID:HOSTED_ZONE_NAME...] { clouddns [ZONE:PROJECT_ID:HOSTED_ZONE_NAME...] {
credentials [FILENAME] credentials [FILENAME]
fallthrough [ZONES...] fallthrough [ZONES...]
refresh DURATION
} }
~~~ ~~~
@@ -45,6 +46,12 @@ clouddns [ZONE:PROJECT_ID:HOSTED_ZONE_NAME...] {
authoritative. If specific zones are listed (for example `in-addr.arpa` and `ip6.arpa`), then authoritative. If specific zones are listed (for example `in-addr.arpa` and `ip6.arpa`), then
only queries for those zones will be subject to fallthrough. only queries for those zones will be subject to fallthrough.
* `refresh` can be used to control how long between record retrievals from Cloud DNS. It requires
a duration string as a parameter to specify the duration between update cycles. Each update
cycle may result in many Cloud DNS API calls depending on how many domains use this plugin and
how many records are in each. Adjusting the update frequency may help reduce the potential of
API throttling by Google Cloud. If not provided the default refresh is 1 minute.
## Examples ## Examples
Enable clouddns with implicit GCP credentials and resolve CNAMEs via 10.0.0.1: Enable clouddns with implicit GCP credentials and resolve CNAMEs via 10.0.0.1:
@@ -73,3 +80,13 @@ Enable clouddns with multiple hosted zones with the same domain:
clouddns example.org.:gcp-example-project:example-zone example.com.:gcp-example-project:other-example-zone clouddns example.org.:gcp-example-project:example-zone example.com.:gcp-example-project:other-example-zone
} }
~~~ ~~~
Enable clouddns and refresh records every 3 minutes:
~~~ txt
example.org {
clouddns example.org.:gcp-example-project:example-zone {
refresh 3m
}
}
~~~

View File

@@ -28,6 +28,7 @@ type CloudDNS struct {
zoneNames []string zoneNames []string
client gcpDNS client gcpDNS
upstream *upstream.Upstream upstream *upstream.Upstream
refresh time.Duration
zMu sync.RWMutex zMu sync.RWMutex
zones zones zones zones
@@ -46,8 +47,9 @@ type zones map[string][]*zone
// string of project name and hosted zone name lists as its values, validates // string of project name and hosted zone name lists as its values, validates
// that each domain name/zone id pair does exist, and returns a new *CloudDNS. // that each domain name/zone id pair does exist, and returns a new *CloudDNS.
// In addition to this, upstream is passed for doing recursive queries against CNAMEs. // In addition to this, upstream is passed for doing recursive queries against CNAMEs.
// refresh sets how often the zones are refreshed from Cloud DNS.
// Returns error if it cannot verify any given domain name/zone id pair. // Returns error if it cannot verify any given domain name/zone id pair.
func New(_ctx context.Context, c gcpDNS, keys map[string][]string, up *upstream.Upstream) (*CloudDNS, error) { func New(_ctx context.Context, c gcpDNS, keys map[string][]string, up *upstream.Upstream, refresh time.Duration) (*CloudDNS, error) {
zones := make(map[string][]*zone, len(keys)) zones := make(map[string][]*zone, len(keys))
zoneNames := make([]string, 0, len(keys)) zoneNames := make([]string, 0, len(keys))
for dnsName, hostedZoneDetails := range keys { for dnsName, hostedZoneDetails := range keys {
@@ -72,6 +74,7 @@ func New(_ctx context.Context, c gcpDNS, keys map[string][]string, up *upstream.
zoneNames: zoneNames, zoneNames: zoneNames,
zones: zones, zones: zones,
upstream: up, upstream: up,
refresh: refresh,
}, nil }, nil
} }
@@ -82,11 +85,10 @@ func (h *CloudDNS) Run(ctx context.Context) error {
return err return err
} }
go func() { go func() {
delay := 1 * time.Minute timer := time.NewTimer(h.refresh)
timer := time.NewTimer(delay)
defer timer.Stop() defer timer.Stop()
for { for {
timer.Reset(delay) timer.Reset(h.refresh)
select { select {
case <-ctx.Done(): case <-ctx.Done():
log.Debugf("Breaking out of CloudDNS update loop for %v: %v", h.zoneNames, ctx.Err()) log.Debugf("Breaking out of CloudDNS update loop for %v: %v", h.zoneNames, ctx.Err())

View File

@@ -6,6 +6,7 @@ import (
"reflect" "reflect"
"sync" "sync"
"testing" "testing"
"time"
"github.com/coredns/coredns/plugin/file" "github.com/coredns/coredns/plugin/file"
"github.com/coredns/coredns/plugin/pkg/dnstest" "github.com/coredns/coredns/plugin/pkg/dnstest"
@@ -134,7 +135,7 @@ func (c fakeGCPClient) listRRSets(_ctx context.Context, projectName, hostedZoneN
func TestCloudDNS(t *testing.T) { func TestCloudDNS(t *testing.T) {
ctx := context.Background() ctx := context.Background()
r, err := New(ctx, fakeGCPClient{}, map[string][]string{"bad.": {"bad-project:bad-zone"}}, &upstream.Upstream{}) r, err := New(ctx, fakeGCPClient{}, map[string][]string{"bad.": {"bad-project:bad-zone"}}, &upstream.Upstream{}, time.Minute)
if err != nil { if err != nil {
t.Fatalf("Failed to create Cloud DNS: %v", err) t.Fatalf("Failed to create Cloud DNS: %v", err)
} }
@@ -142,7 +143,7 @@ func TestCloudDNS(t *testing.T) {
t.Fatalf("Expected errors for zone bad.") t.Fatalf("Expected errors for zone bad.")
} }
r, err = New(ctx, fakeGCPClient{}, map[string][]string{"org.": {"sample-project-1:sample-zone-2", "sample-project-1:sample-zone-1"}, "gov.": {"sample-project-1:sample-zone-2", "sample-project-1:sample-zone-1"}}, &upstream.Upstream{}) r, err = New(ctx, fakeGCPClient{}, map[string][]string{"org.": {"sample-project-1:sample-zone-2", "sample-project-1:sample-zone-1"}, "gov.": {"sample-project-1:sample-zone-2", "sample-project-1:sample-zone-1"}}, &upstream.Upstream{}, time.Minute)
if err != nil { if err != nil {
t.Fatalf("Failed to create Cloud DNS: %v", err) t.Fatalf("Failed to create Cloud DNS: %v", err)
} }
@@ -339,6 +340,7 @@ func TestCloudDNSConcurrentServeDNS(t *testing.T) {
"org.": {"sample-project-1:sample-zone-2", "sample-project-1:sample-zone-1"}, "org.": {"sample-project-1:sample-zone-2", "sample-project-1:sample-zone-1"},
}, },
&upstream.Upstream{}, &upstream.Upstream{},
time.Minute,
) )
if err != nil { if err != nil {
t.Fatalf("Failed to create Cloud DNS: %v", err) t.Fatalf("Failed to create Cloud DNS: %v", err)
@@ -406,7 +408,8 @@ func TestCloudDNSConcurrentLookupNameCache(t *testing.T) {
map[string][]string{ map[string][]string{
"org.": {"sample-project-1:sample-zone-2", "sample-project-1:sample-zone-1"}, "org.": {"sample-project-1:sample-zone-2", "sample-project-1:sample-zone-1"},
}, },
&upstream.Upstream{}) &upstream.Upstream{},
time.Minute)
if err != nil { if err != nil {
t.Fatalf("Failed to create Cloud DNS: %v", err) t.Fatalf("Failed to create Cloud DNS: %v", err)
} }

View File

@@ -5,7 +5,9 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"os" "os"
"strconv"
"strings" "strings"
"time"
"github.com/coredns/caddy" "github.com/coredns/caddy"
"github.com/coredns/coredns/core/dnsserver" "github.com/coredns/coredns/core/dnsserver"
@@ -64,6 +66,7 @@ func setup(c *caddy.Controller) error {
} }
var opt option.ClientOption var opt option.ClientOption
refresh := time.Duration(1) * time.Minute // default update frequency to 1 minute
for c.NextBlock() { for c.NextBlock() {
switch c.Val() { switch c.Val() {
case "upstream": case "upstream":
@@ -79,6 +82,22 @@ func setup(c *caddy.Controller) error {
opt = option.WithAuthCredentialsFile(credType, c.Val()) opt = option.WithAuthCredentialsFile(credType, c.Val())
case "fallthrough": case "fallthrough":
fall.SetZonesFromArgs(c.RemainingArgs()) fall.SetZonesFromArgs(c.RemainingArgs())
case "refresh":
if !c.NextArg() {
return plugin.Error("clouddns", c.ArgErr())
}
refreshStr := c.Val()
_, err := strconv.Atoi(refreshStr)
if err == nil {
refreshStr += "s"
}
refresh, err = time.ParseDuration(refreshStr)
if err != nil {
return plugin.Error("clouddns", c.Errf("unable to parse duration: %v", err))
}
if refresh <= 0 {
return plugin.Error("clouddns", c.Errf("refresh interval must be greater than 0: %q", refreshStr))
}
default: default:
return plugin.Error("clouddns", c.Errf("unknown property %q", c.Val())) return plugin.Error("clouddns", c.Errf("unknown property %q", c.Val()))
} }
@@ -91,7 +110,7 @@ func setup(c *caddy.Controller) error {
return err return err
} }
h, err := New(ctx, client, keys, up) h, err := New(ctx, client, keys, up, refresh)
if err != nil { if err != nil {
cancel() cancel()
return plugin.Error("clouddns", c.Errf("failed to create plugin: %v", err)) return plugin.Error("clouddns", c.Errf("failed to create plugin: %v", err))

View File

@@ -55,6 +55,24 @@ func TestSetupCloudDNS(t *testing.T) {
}`, false}, }`, false},
{`clouddns example.org.:example-project:zone-name { {`clouddns example.org.:example-project:zone-name {
credentials credentials
}`, true},
{`clouddns example.org.:example-project:zone-name {
refresh 3m
}`, false},
{`clouddns example.org.:example-project:zone-name {
refresh 30
}`, false},
{`clouddns example.org.:example-project:zone-name {
refresh
}`, true},
{`clouddns example.org.:example-project:zone-name {
refresh abc
}`, true},
{`clouddns example.org.:example-project:zone-name {
refresh 0
}`, true},
{`clouddns example.org.:example-project:zone-name {
refresh -5m
}`, true}, }`, true},
{`clouddns example.org.:example-project:zone-name example.org.:example-project:zone-name { {`clouddns example.org.:example-project:zone-name example.org.:example-project:zone-name {
}`, true}, }`, true},