From ebc465d0dca8c98d44272e79acc1a43d63805520 Mon Sep 17 00:00:00 2001 From: Miek Gieben Date: Tue, 13 Aug 2019 15:02:29 +0000 Subject: [PATCH] plugin/route53: various updates (#3108) In the setup function use plugin.Error() to wrap the errors with the plugin name. Because there isn't a separate setup() function this is done for all returned errors. Remove *upstream.Upstream from the New parameters as this is always set and adjust the tests to account for this. Signed-off-by: Miek Gieben --- plugin/route53/route53.go | 12 ++++++------ plugin/route53/route53_test.go | 5 ++--- plugin/route53/setup.go | 25 +++++++++++-------------- 3 files changed, 19 insertions(+), 23 deletions(-) diff --git a/plugin/route53/route53.go b/plugin/route53/route53.go index 346defa66..644f7b431 100644 --- a/plugin/route53/route53.go +++ b/plugin/route53/route53.go @@ -46,11 +46,11 @@ type zone struct { type zones map[string][]*zone // New reads from the keys map which uses domain names as its key and hosted -// zone id lists as its values, validates that each domain name/zone id pair does -// exist, and returns a new *Route53. In addition to this, upstream is passed -// for doing recursive queries against CNAMEs. -// Returns error if it cannot verify any given domain name/zone id pair. -func New(ctx context.Context, c route53iface.Route53API, keys map[string][]string, up *upstream.Upstream, refresh time.Duration) (*Route53, error) { +// zone id lists as its values, validates that each domain name/zone id pair +// does exist, and returns a new *Route53. In addition to this, upstream is use +// for doing recursive queries against CNAMEs. Returns error if it cannot +// verify any given domain name/zone id pair. +func New(ctx context.Context, c route53iface.Route53API, keys map[string][]string, refresh time.Duration) (*Route53, error) { zones := make(map[string][]*zone, len(keys)) zoneNames := make([]string, 0, len(keys)) for dns, hostedZoneIDs := range keys { @@ -72,7 +72,7 @@ func New(ctx context.Context, c route53iface.Route53API, keys map[string][]strin client: c, zoneNames: zoneNames, zones: zones, - upstream: up, + upstream: upstream.New(), refresh: refresh, }, nil } diff --git a/plugin/route53/route53_test.go b/plugin/route53/route53_test.go index 64ea90d6a..8db42d252 100644 --- a/plugin/route53/route53_test.go +++ b/plugin/route53/route53_test.go @@ -9,7 +9,6 @@ import ( "github.com/coredns/coredns/plugin/pkg/dnstest" "github.com/coredns/coredns/plugin/pkg/fall" - "github.com/coredns/coredns/plugin/pkg/upstream" "github.com/coredns/coredns/plugin/test" crequest "github.com/coredns/coredns/request" @@ -80,7 +79,7 @@ func (fakeRoute53) ListResourceRecordSetsPagesWithContext(_ aws.Context, in *rou func TestRoute53(t *testing.T) { ctx := context.Background() - r, err := New(ctx, fakeRoute53{}, map[string][]string{"bad.": {"0987654321"}}, &upstream.Upstream{}, time.Duration(1) * time.Minute) + r, err := New(ctx, fakeRoute53{}, map[string][]string{"bad.": {"0987654321"}}, time.Duration(1)*time.Minute) if err != nil { t.Fatalf("Failed to create Route53: %v", err) } @@ -88,7 +87,7 @@ func TestRoute53(t *testing.T) { t.Fatalf("Expected errors for zone bad.") } - r, err = New(ctx, fakeRoute53{}, map[string][]string{"org.": {"1357986420", "1234567890"}, "gov.": {"Z098765432", "1234567890"}}, &upstream.Upstream{}, time.Duration(90) * time.Second) + r, err = New(ctx, fakeRoute53{}, map[string][]string{"org.": {"1357986420", "1234567890"}, "gov.": {"Z098765432", "1234567890"}}, time.Duration(90)*time.Second) if err != nil { t.Fatalf("Failed to create Route53: %v", err) } diff --git a/plugin/route53/setup.go b/plugin/route53/setup.go index 918b0e56a..23fb5e74f 100644 --- a/plugin/route53/setup.go +++ b/plugin/route53/setup.go @@ -11,7 +11,6 @@ import ( "github.com/coredns/coredns/plugin" "github.com/coredns/coredns/plugin/pkg/fall" clog "github.com/coredns/coredns/plugin/pkg/log" - "github.com/coredns/coredns/plugin/pkg/upstream" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/credentials" @@ -54,8 +53,6 @@ func setup(c *caddy.Controller, f func(*credentials.Credentials) route53iface.Ro var providers []credentials.Provider var fall fall.F - up := upstream.New() - refresh := time.Duration(1) * time.Minute // default update frequency to 1 minute args := c.RemainingArgs() @@ -63,14 +60,14 @@ func setup(c *caddy.Controller, f func(*credentials.Credentials) route53iface.Ro for i := 0; i < len(args); i++ { parts := strings.SplitN(args[i], ":", 2) if len(parts) != 2 { - return c.Errf("invalid zone '%s'", args[i]) + return plugin.Error("route53", c.Errf("invalid zone '%s'", args[i])) } dns, hostedZoneID := parts[0], parts[1] if dns == "" || hostedZoneID == "" { - return c.Errf("invalid zone '%s'", args[i]) + return plugin.Error("route53", c.Errf("invalid zone '%s'", args[i])) } if _, ok := keyPairs[args[i]]; ok { - return c.Errf("conflict zone '%s'", args[i]) + return plugin.Error("route53", c.Errf("conflict zone '%s'", args[i])) } keyPairs[args[i]] = struct{}{} @@ -82,7 +79,7 @@ func setup(c *caddy.Controller, f func(*credentials.Credentials) route53iface.Ro case "aws_access_key": v := c.RemainingArgs() if len(v) < 2 { - return c.Errf("invalid access key '%v'", v) + return plugin.Error("route53", c.Errf("invalid access key '%v'", v)) } providers = append(providers, &credentials.StaticProvider{ Value: credentials.Value{ @@ -112,16 +109,16 @@ func setup(c *caddy.Controller, f func(*credentials.Credentials) route53iface.Ro } refresh, err = time.ParseDuration(refreshStr) if err != nil { - return c.Errf("Unable to parse duration: '%v'", err) + return plugin.Error("route53", c.Errf("Unable to parse duration: '%v'", err)) } if refresh <= 0 { - return c.Errf("refresh interval must be greater than 0: %s", refreshStr) + return plugin.Error("route53", c.Errf("refresh interval must be greater than 0: %s", refreshStr)) } } else { - return c.ArgErr() + return plugin.Error("route53", c.ArgErr()) } default: - return c.Errf("unknown property '%s'", c.Val()) + return plugin.Error("route53", c.Errf("unknown property '%s'", c.Val())) } } providers = append(providers, &credentials.EnvProvider{}, sharedProvider, &ec2rolecreds.EC2RoleProvider{ @@ -129,13 +126,13 @@ func setup(c *caddy.Controller, f func(*credentials.Credentials) route53iface.Ro }) client := f(credentials.NewChainCredentials(providers)) ctx := context.Background() - h, err := New(ctx, client, keys, up, refresh) + h, err := New(ctx, client, keys, refresh) if err != nil { - return c.Errf("failed to create Route53 plugin: %v", err) + return plugin.Error("route53", c.Errf("failed to create Route53 plugin: %v", err)) } h.Fall = fall if err := h.Run(ctx); err != nil { - return c.Errf("failed to initialize Route53 plugin: %v", err) + return plugin.Error("route53", c.Errf("failed to initialize Route53 plugin: %v", err)) } dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler { h.Next = next