From 8170947af0f862f94797914ecfaef9ed53a11c95 Mon Sep 17 00:00:00 2001 From: Martin Linkhorst Date: Thu, 16 Mar 2017 17:10:33 +0100 Subject: [PATCH] remove provider store and don't initialize each DNS provider (#96) * fix(dnsprovider): do not always initialize each DNS provider * fix(dnsprovider): fix unnecessary error return value * ref(dnsprovider): drop the store and use a simple switch for lookup --- dnsprovider/google.go | 6 ++- dnsprovider/store.go | 38 ----------------- dnsprovider/store_test.go | 87 --------------------------------------- main.go | 18 ++++---- 4 files changed, 13 insertions(+), 136 deletions(-) delete mode 100644 dnsprovider/store.go delete mode 100644 dnsprovider/store_test.go diff --git a/dnsprovider/google.go b/dnsprovider/google.go index 7a08978b81..7d7eea8909 100644 --- a/dnsprovider/google.go +++ b/dnsprovider/google.go @@ -123,13 +123,15 @@ func NewGoogleProvider(project string, dryRun bool) (DNSProvider, error) { return nil, err } - return &googleProvider{ + provider := &googleProvider{ project: project, dryRun: dryRun, resourceRecordSetsClient: resourceRecordSetsService{dnsClient.ResourceRecordSets}, managedZonesClient: managedZonesService{dnsClient.ManagedZones}, changesClient: changesService{dnsClient.Changes}, - }, nil + } + + return provider, nil } // Zones returns the list of hosted zones. diff --git a/dnsprovider/store.go b/dnsprovider/store.go deleted file mode 100644 index 58d0023401..0000000000 --- a/dnsprovider/store.go +++ /dev/null @@ -1,38 +0,0 @@ -/* -Copyright 2017 The Kubernetes Authors. - -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 dnsprovider - -var store = map[string]DNSProvider{} - -// Register registers a DNSProvider under a given name. -func Register(name string, provider DNSProvider) { - store[name] = provider -} - -// Lookup returns a DNSProvider by the given name. -func Lookup(name string) DNSProvider { - return store[name] -} - -// LookupMultiple returns multiple DNSProviders given multiple names. -func LookupMultiple(names ...string) (providers []DNSProvider) { - for _, name := range names { - providers = append(providers, Lookup(name)) - } - - return providers -} diff --git a/dnsprovider/store_test.go b/dnsprovider/store_test.go deleted file mode 100644 index 2ff2451f3f..0000000000 --- a/dnsprovider/store_test.go +++ /dev/null @@ -1,87 +0,0 @@ -/* -Copyright 2017 The Kubernetes Authors. - -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 dnsprovider - -import "testing" - -func TestStore(t *testing.T) { - t.Run("RegisterAndLookup", testRegisterAndLookup) - t.Run("LookupMultiple", testLookupMultiple) -} - -// testRegisterAndLookup tests that a DNSProvider can be registered and looked up by name. -func testRegisterAndLookup(t *testing.T) { - for _, tc := range []struct { - title string - givenAndExpected map[string]DNSProvider - }{ - { - "registered provider is found by name", - map[string]DNSProvider{ - "foo": NewInMemoryProvider(), - }, - }, - } { - t.Run(tc.title, func(t *testing.T) { - for k, v := range tc.givenAndExpected { - Register(k, v) - } - - for k, v := range tc.givenAndExpected { - if Lookup(k) != v { - t.Errorf("expected %#v, got %#v", v, Lookup(k)) - } - } - }) - } -} - -// testLookupMultiple tests that DNSProviders can be looked up by providing multiple names. -func testLookupMultiple(t *testing.T) { - for _, tc := range []struct { - title string - givenAndExpected map[string]DNSProvider - }{ - { - "multiple registered providers are found by names", - map[string]DNSProvider{ - "foo": NewInMemoryProvider(), - "bar": NewInMemoryProvider(), - }, - }, - } { - t.Run(tc.title, func(t *testing.T) { - for k, v := range tc.givenAndExpected { - Register(k, v) - } - - names, sources := []string{}, []DNSProvider{} - for k, v := range tc.givenAndExpected { - names = append(names, k) - sources = append(sources, v) - } - - lookup := LookupMultiple(names...) - - for i := range names { - if lookup[i] != sources[i] { - t.Errorf("expected %#v, got %#v", sources[i], lookup[i]) - } - } - }) - } -} diff --git a/main.go b/main.go index dff3088c41..4dba20b567 100644 --- a/main.go +++ b/main.go @@ -79,23 +79,23 @@ func main() { sources := source.NewMultiSource(source.LookupMultiple(cfg.Sources...)...) - googleProvider, err := dnsprovider.NewGoogleProvider(cfg.GoogleProject, cfg.DryRun) - if err != nil { - log.Fatal(err) + var provider dnsprovider.DNSProvider + switch cfg.DNSProvider { + case "google": + provider, err = dnsprovider.NewGoogleProvider(cfg.GoogleProject, cfg.DryRun) + case "aws": + provider, err = dnsprovider.NewAWSProvider(cfg.DryRun) + default: + log.Fatalf("unknown dns provider: %s", cfg.DNSProvider) } - - awsProvider, err := dnsprovider.NewAWSProvider(cfg.DryRun) if err != nil { log.Fatal(err) } - dnsprovider.Register("google", googleProvider) - dnsprovider.Register("aws", awsProvider) - ctrl := controller.Controller{ Zone: cfg.Zone, Source: sources, - DNSProvider: dnsprovider.Lookup(cfg.DNSProvider), + DNSProvider: provider, } if cfg.Once {