Skip to content

Commit 91a71e3

Browse files
jrussettmikexuu
authored andcommitted
Consolidate default DNS port logic in config
[#156210589](https://www.pivotaltracker.com/story/show/156210589) Signed-off-by: Michael Xu <mxu@pivotal.io>
1 parent 59272d1 commit 91a71e3

File tree

6 files changed

+22
-35
lines changed

6 files changed

+22
-35
lines changed

src/bosh-dns/dns/config/config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ func LoadFromFile(configFilePath string) (Config, error) {
101101
return Config{}, err
102102
}
103103

104+
c.ExcludedRecursors, err = AppendDefaultDNSPortIfMissing(c.ExcludedRecursors)
105+
if err != nil {
106+
return Config{}, err
107+
}
108+
104109
return c, nil
105110
}
106111

src/bosh-dns/dns/config/config_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ var _ = Describe("Config", func() {
6868
"handlers_files_glob": handlersFileGlob,
6969
"port": listenPort,
7070
"recursor_timeout": recursorTimeout,
71-
"excluded_recursors": []string{"169.254.169.254"},
71+
"excluded_recursors": []string{"169.254.169.254", "169.10.10.10:1234"},
7272
"timeout": timeout,
7373
"upcheck_domains": upcheckDomains,
7474
"api": map[string]interface{}{
@@ -125,7 +125,7 @@ var _ = Describe("Config", func() {
125125
Timeout: config.DurationJSON(timeoutDuration),
126126
RecursorTimeout: config.DurationJSON(recursorTimeoutDuration),
127127
Recursors: []string{},
128-
ExcludedRecursors: []string{"169.254.169.254"},
128+
ExcludedRecursors: []string{"169.254.169.254:53", "169.10.10.10:1234"},
129129
UpcheckDomains: []string{"upcheck.domain.", "health2.bosh."},
130130
AliasFilesGlob: aliasesFileGlob,
131131
HandlersFilesGlob: handlersFileGlob,

src/bosh-dns/dns/config/recursor.go

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
package config
22

3-
import (
4-
"fmt"
5-
"strings"
6-
)
7-
83
//go:generate counterfeiter . RecursorReader
94

105
type RecursorReader interface {
@@ -35,9 +30,7 @@ func ConfigureRecursors(reader RecursorReader, shuffler StringShuffler, dnsConfi
3530
shouldAdd := true
3631

3732
for _, excludedRecursor := range dnsConfig.ExcludedRecursors {
38-
formattedRecursor := addPortIfNecessary(recursor)
39-
excludedRecursor = addPortIfNecessary(excludedRecursor)
40-
if formattedRecursor == excludedRecursor {
33+
if recursor == excludedRecursor {
4134
shouldAdd = false
4235

4336
break
@@ -53,11 +46,3 @@ func ConfigureRecursors(reader RecursorReader, shuffler StringShuffler, dnsConfi
5346

5447
return nil
5548
}
56-
57-
func addPortIfNecessary(recursor string) string {
58-
if strings.HasSuffix(recursor, ":53") {
59-
return recursor
60-
}
61-
62-
return fmt.Sprintf("%s:53", recursor)
63-
}

src/bosh-dns/dns/config/recursor_reader.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package config
22

33
import (
4-
"fmt"
5-
64
"bosh-dns/dns/manager"
75
)
86

@@ -21,20 +19,19 @@ type recursorReader struct {
2119
}
2220

2321
func (r recursorReader) Get() ([]string, error) {
24-
recursors := []string{}
25-
2622
nameservers, err := r.manager.Read()
2723
if err != nil {
2824
return nil, err
2925
}
3026

27+
validRecursors := []string{}
3128
for _, server := range nameservers {
3229
if r.isValid(server) {
33-
recursors = append(recursors, fmt.Sprintf("%s:53", server))
30+
validRecursors = append(validRecursors, server)
3431
}
3532
}
3633

37-
return recursors, nil
34+
return AppendDefaultDNSPortIfMissing(validRecursors)
3835
}
3936

4037
func (r recursorReader) isNameServer(s string) bool {

src/bosh-dns/dns/config/recursor_reader_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,15 @@ var _ = Describe("RecursorReader", func() {
8080

8181
Context("when multiple recursors are configured", func() {
8282
BeforeEach(func() {
83-
dnsManager.ReadReturns(append(dnsServerDomainNames, "recursor-1", "recursor-2"), nil)
83+
dnsManager.ReadReturns(append(dnsServerDomainNames, "recursor-1", "recursor-2", "recursor-custom:1234"), nil)
8484
})
8585

8686
It("returns all entries except the DNS server itself", func() {
8787
recursors, err := recursorReader.Get()
8888

8989
Expect(err).ToNot(HaveOccurred())
90-
Expect(recursors).To(HaveLen(2))
91-
Expect(recursors).To(ConsistOf("recursor-1:53", "recursor-2:53"))
90+
Expect(recursors).To(HaveLen(3))
91+
Expect(recursors).To(ConsistOf("recursor-1:53", "recursor-2:53", "recursor-custom:1234"))
9292
})
9393
})
9494

src/bosh-dns/dns/config/recursor_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,21 @@ var _ = Describe("Recursor", func() {
2525

2626
Context("when dns config does not have any recursors configured", func() {
2727
BeforeEach(func() {
28-
resolvConfReader.GetReturns([]string{"some-recursor-1:53", "some-recursor-2:53"}, nil)
28+
resolvConfReader.GetReturns([]string{"some-recursor-1:53", "some-recursor-2:53", "recursor-custom:1234"}, nil)
2929
})
3030

3131
It("should generate recursors from the resolv.conf, shuffled", func() {
3232
err := config.ConfigureRecursors(resolvConfReader, stringShuffler, &dnsConfig)
3333
Expect(err).ToNot(HaveOccurred())
34-
Expect(dnsConfig.Recursors).Should(Equal([]string{"some-recursor-1:53", "some-recursor-2:53"}))
34+
Expect(dnsConfig.Recursors).Should(Equal([]string{"some-recursor-1:53", "some-recursor-2:53", "recursor-custom:1234"}))
3535
Expect(stringShuffler.ShuffleCallCount()).To(Equal(1))
3636

3737
Expect(resolvConfReader.GetCallCount()).To(Equal(1))
3838
})
3939

4040
Context("when unable to Get fails on RecursorReader", func() {
4141
BeforeEach(func() {
42-
resolvConfReader.GetReturns([]string{"some-recursor-1"}, errors.New("some-error"))
42+
resolvConfReader.GetReturns([]string{"some-recursor-1:53"}, errors.New("some-error"))
4343
})
4444

4545
It("should return the error", func() {
@@ -52,7 +52,7 @@ var _ = Describe("Recursor", func() {
5252

5353
Context("when excluding recursors", func() {
5454
BeforeEach(func() {
55-
dnsConfig.ExcludedRecursors = []string{"some-recursor-1"}
55+
dnsConfig.ExcludedRecursors = []string{"some-recursor-1:53", "recursor-custom:1234"}
5656
})
5757

5858
It("should exclude the recursor", func() {
@@ -67,27 +67,27 @@ var _ = Describe("Recursor", func() {
6767
Context("when dns config does has recursors configured", func() {
6868
BeforeEach(func() {
6969
dnsConfig = config.Config{
70-
Recursors: []string{"some-recursor-1", "some-recursor-2"},
70+
Recursors: []string{"some-recursor-1:53", "some-recursor-2:53", "recursor-custom:1234"},
7171
}
7272
})
7373

7474
It("should shuffle the recursors", func() {
7575
err := config.ConfigureRecursors(resolvConfReader, stringShuffler, &dnsConfig)
7676
Expect(err).ToNot(HaveOccurred())
7777
Expect(stringShuffler.ShuffleCallCount()).To(Equal(1))
78-
Expect(dnsConfig.Recursors).Should(Equal([]string{"some-recursor-1", "some-recursor-2"}))
78+
Expect(dnsConfig.Recursors).Should(Equal([]string{"some-recursor-1:53", "some-recursor-2:53", "recursor-custom:1234"}))
7979
})
8080

8181
Context("when excluding recursors", func() {
8282
BeforeEach(func() {
83-
dnsConfig.ExcludedRecursors = []string{"some-recursor-1"}
83+
dnsConfig.ExcludedRecursors = []string{"some-recursor-1:53", "recursor-custom:1234"}
8484
})
8585

8686
It("should exclude the recursor", func() {
8787
err := config.ConfigureRecursors(resolvConfReader, stringShuffler, &dnsConfig)
8888
Expect(err).ToNot(HaveOccurred())
8989
Expect(stringShuffler.ShuffleCallCount()).To(Equal(1))
90-
Expect(dnsConfig.Recursors).Should(Equal([]string{"some-recursor-2"}))
90+
Expect(dnsConfig.Recursors).Should(Equal([]string{"some-recursor-2:53"}))
9191
})
9292
})
9393
})

0 commit comments

Comments
 (0)