Repository: coredns/coredns (go)

Issue:
plugin/route53: init() allocates object we don't use
~~~
func init() {
	plugin.Register("route53",
		func(c *caddy.Controller) error {
			f := func(credential *credentials.Credentials) route53iface.Route53API {
				return route53.New(session.Must(session.NewSession(&aws.Config{
					Credentials: credential,
				})))
			}
			return setup(c, f)
		},
	)
}
~~~
this `init` allocates aws stuff even if I don't load the plugin. It should not do that.

Pull request:
plugin/route53: remove amazon intialization from init
Don't perform this code in the init, this allocated 1 megabyte of memory
even if you don't use the plugin. Looks to be only there for testing,
adding a comment to reflect that.

Fixes #3342

Patch:
diff --git a/plugin/clouddns/setup.go b/plugin/clouddns/setup.go
index 35c7e9179..7de8efe89 100644
--- a/plugin/clouddns/setup.go
+++ b/plugin/clouddns/setup.go
@@ -17,27 +17,23 @@ import (
 
 var log = clog.NewWithPlugin("clouddns")
 
-func init() {
-	plugin.Register("clouddns",
-		func(c *caddy.Controller) error {
-			f := func(ctx context.Context, opt option.ClientOption) (gcpDNS, error) {
-				var err error
-				var client *gcp.Service
-				if opt != nil {
-					client, err = gcp.NewService(ctx, opt)
-				} else {
-					// if credentials file is not provided in the Corefile
-					// authenticate the client using env variables
-					client, err = gcp.NewService(ctx)
-				}
-				return gcpClient{client}, err
-			}
-			return setup(c, f)
-		},
-	)
+func init() { plugin.Register("clouddns", setup) }
+
+// exposed for testing
+var f = func(ctx context.Context, opt option.ClientOption) (gcpDNS, error) {
+	var err error
+	var client *gcp.Service
+	if opt != nil {
+		client, err = gcp.NewService(ctx, opt)
+	} else {
+		// if credentials file is not provided in the Corefile
+		// authenticate the client using env variables
+		client, err = gcp.NewService(ctx)
+	}
+	return gcpClient{client}, err
 }
 
-func setup(c *caddy.Controller, f func(ctx context.Context, opt option.ClientOption) (gcpDNS, error)) error {
+func setup(c *caddy.Controller) error {
 	for c.Next() {
 		keyPairs := map[string]struct{}{}
 		keys := map[string][]string{}
diff --git a/plugin/route53/setup.go b/plugin/route53/setup.go
index 3b47fad59..c285bee2d 100644
--- a/plugin/route53/setup.go
+++ b/plugin/route53/setup.go
@@ -24,20 +24,14 @@ import (
 
 var log = clog.NewWithPlugin("route53")
 
-func init() {
-	plugin.Register("route53",
-		func(c *caddy.Controller) error {
-			f := func(credential *credentials.Credentials) route53iface.Route53API {
-				return route53.New(session.Must(session.NewSession(&aws.Config{
-					Credentials: credential,
-				})))
-			}
-			return setup(c, f)
-		},
-	)
+func init() { plugin.Register("route53", setup) }
+
+// exposed for testing
+var f = func(credential *credentials.Credentials) route53iface.Route53API {
+	return route53.New(session.Must(session.NewSession(&aws.Config{Credentials: credential})))
 }
 
-func setup(c *caddy.Controller, f func(*credentials.Credentials) route53iface.Route53API) error {
+func setup(c *caddy.Controller) error {
 	for c.Next() {
 		keyPairs := map[string]struct{}{}
 		keys := map[string][]string{}


Test patch:
diff --git a/plugin/clouddns/setup_test.go b/plugin/clouddns/setup_test.go
index be9c51d92..6d2997298 100644
--- a/plugin/clouddns/setup_test.go
+++ b/plugin/clouddns/setup_test.go
@@ -9,7 +9,7 @@ import (
 )
 
 func TestSetupCloudDNS(t *testing.T) {
-	f := func(ctx context.Context, opt option.ClientOption) (gcpDNS, error) {
+	f = func(ctx context.Context, opt option.ClientOption) (gcpDNS, error) {
 		return fakeGCPClient{}, nil
 	}
 
@@ -41,7 +41,7 @@ func TestSetupCloudDNS(t *testing.T) {
 
 	for _, test := range tests {
 		c := caddy.NewTestController("dns", test.body)
-		if err := setup(c, f); (err == nil) == test.expectedError {
+		if err := setup(c); (err == nil) == test.expectedError {
 			t.Errorf("Unexpected errors: %v", err)
 		}
 	}
diff --git a/plugin/route53/setup_test.go b/plugin/route53/setup_test.go
index 998285e46..cb73d8fef 100644
--- a/plugin/route53/setup_test.go
+++ b/plugin/route53/setup_test.go
@@ -9,7 +9,7 @@ import (
 )
 
 func TestSetupRoute53(t *testing.T) {
-	f := func(credential *credentials.Credentials) route53iface.Route53API {
+	f = func(credential *credentials.Credentials) route53iface.Route53API {
 		return fakeRoute53{}
 	}
 
@@ -73,7 +73,7 @@ func TestSetupRoute53(t *testing.T) {
 
 	for _, test := range tests {
 		c := caddy.NewTestController("dns", test.body)
-		if err := setup(c, f); (err == nil) == test.expectedError {
+		if err := setup(c); (err == nil) == test.expectedError {
 			t.Errorf("Unexpected errors: %v", err)
 		}
 	}