diff --git a/pkg/kubelet/network/dns/dns.go b/pkg/kubelet/network/dns/dns.go index 6781db7a4fd..061730261c8 100644 --- a/pkg/kubelet/network/dns/dns.go +++ b/pkg/kubelet/network/dns/dns.go @@ -273,13 +273,33 @@ func parseResolvConf(reader io.Reader) (nameservers []string, searches []string, } } if fields[0] == "options" { - options = fields[1:] + options = appendOptions(options, fields[1:]...) } } return nameservers, searches, options, utilerrors.NewAggregate(allErrors) } +// appendOptions appends options to the given list, but does not add duplicates. +// append option will overwrite the previous one either in new line or in the same line. +func appendOptions(options []string, newOption ...string) []string { + var optionMap = make(map[string]string) + for _, option := range options { + optName := strings.Split(option, ":")[0] + optionMap[optName] = option + } + for _, option := range newOption { + optName := strings.Split(option, ":")[0] + optionMap[optName] = option + } + + options = []string{} + for _, v := range optionMap { + options = append(options, v) + } + return options +} + func (c *Configurer) getHostDNSConfig() (*runtimeapi.DNSConfig, error) { var hostDNS, hostSearch, hostOptions []string // Get host DNS settings diff --git a/pkg/kubelet/network/dns/dns_test.go b/pkg/kubelet/network/dns/dns_test.go index f4d0561d373..ad451a15c1e 100644 --- a/pkg/kubelet/network/dns/dns_test.go +++ b/pkg/kubelet/network/dns/dns_test.go @@ -92,7 +92,11 @@ func TestParseResolvConf(t *testing.T) { {"#comment\nnameserver 1.2.3.4\n#comment\nsearch foo\ncomment", []string{"1.2.3.4"}, []string{"foo"}, []string{}, false}, {"options ", []string{}, []string{}, []string{}, false}, {"options ndots:5 attempts:2", []string{}, []string{}, []string{"ndots:5", "attempts:2"}, false}, + {"options ndots:1 ndots:5 attempts:2", []string{}, []string{}, []string{"ndots:5", "attempts:2"}, false}, + {"options ndots:1 edns0 attempts:2 single-request-reopen", []string{}, []string{}, []string{"edns0", "attempts:2", "single-request-reopen", "ndots:1"}, false}, {"options ndots:1\noptions ndots:5 attempts:3", []string{}, []string{}, []string{"ndots:5", "attempts:3"}, false}, + {"options ndots:1 timeout:3 timeout:1 attempts:3\noptions ndots:5", []string{}, []string{}, []string{"ndots:5", "timeout:1", "attempts:3"}, false}, + {"options ndots:1 attempts:3\noptions ndots:1 attempts:3 ndots:5", []string{}, []string{}, []string{"ndots:5", "attempts:3"}, false}, {"nameserver 1.2.3.4\nsearch foo\nnameserver 5.6.7.8\nsearch bar\noptions ndots:5 attempts:4", []string{"1.2.3.4", "5.6.7.8"}, []string{"bar"}, []string{"ndots:5", "attempts:4"}, false}, } for i, tc := range testCases {