Skip to content

Commit

Permalink
kubelet: parseResolvConf: Handle "search ."
Browse files Browse the repository at this point in the history
When parsing a resolv.conf file that has "search .", parseResolvConf should
accept the "." entry verbatim.  Before this commit, parseResolvConf
unconditionally trimmed the "." suffix, which in the case of "." resulted
in a "" entry (that is, the empty string).  This empty entry could lead
parseResolvConf to produce a resolv.conf file with "search ".  Resolvers
could fail to parse such a resolv.conf file from parseResolvConf, thus
breaking DNS resolution in pods.  After this commit, parseResolvConf
accepts a resolv.conf file with "search ." and passes the "." entry through
verbatim to produce a valid resolv.conf file.  The "." suffix is still
trimmed for any entry that does not solely comprise ".".

Follow-up to commit a215a88.

* pkg/kubelet/network/dns/dns.go (parseResolvConf): Handle a "." entry in
the search path by copying it verbatim.
* pkg/kubelet/network/dns/dns_test.go (TestParseResolvConf): Add a test
case for "search .".
  • Loading branch information
Miciah committed Apr 12, 2022
1 parent 7380fc7 commit 5832b84
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
5 changes: 4 additions & 1 deletion pkg/kubelet/network/dns/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,10 @@ func parseResolvConf(reader io.Reader) (nameservers []string, searches []string,
// Normalise search fields so the same domain with and without trailing dot will only count once, to avoid hitting search validation limits.
searches = []string{}
for _, s := range fields[1:] {
searches = append(searches, strings.TrimSuffix(s, "."))
if s != "." {
s = strings.TrimSuffix(s, ".")
}
searches = append(searches, s)
}
}
if fields[0] == "options" {
Expand Down
1 change: 1 addition & 0 deletions pkg/kubelet/network/dns/dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func TestParseResolvConf(t *testing.T) {
{"nameserver 1.2.3.4\nnameserver 5.6.7.8", []string{"1.2.3.4", "5.6.7.8"}, []string{}, []string{}, false},
{"nameserver 1.2.3.4 #comment", []string{"1.2.3.4"}, []string{}, []string{}, false},
{"search ", []string{}, []string{}, []string{}, false}, // search empty
{"search .", []string{}, []string{"."}, []string{}, false},
{"search foo", []string{}, []string{"foo"}, []string{}, false},
{"search foo bar", []string{}, []string{"foo", "bar"}, []string{}, false},
{"search foo. bar", []string{}, []string{"foo", "bar"}, []string{}, false},
Expand Down

0 comments on commit 5832b84

Please sign in to comment.