diff --git a/pkg/kubelet/network/dns/dns.go b/pkg/kubelet/network/dns/dns.go index 4a32ed3b618..4b491c567e2 100644 --- a/pkg/kubelet/network/dns/dns.go +++ b/pkg/kubelet/network/dns/dns.go @@ -272,7 +272,7 @@ func parseResolvConf(reader io.Reader) (nameservers []string, searches []string, } } if fields[0] == "options" { - options = fields[1:] + options = appendOptions(options, fields[1:]...) } } @@ -352,6 +352,26 @@ func mergeDNSOptions(existingDNSConfigOptions []string, dnsConfigOptions []v1.Po return options } +// 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 +} + // appendDNSConfig appends DNS servers, search paths and options given by // PodDNSConfig to the existing DNS config. Duplicated entries will be merged. // This assumes existingDNSConfig and dnsConfig are not nil. diff --git a/pkg/kubelet/network/dns/dns_test.go b/pkg/kubelet/network/dns/dns_test.go index 4c561307e30..e1c3c69c54e 100644 --- a/pkg/kubelet/network/dns/dns_test.go +++ b/pkg/kubelet/network/dns/dns_test.go @@ -91,7 +91,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 { @@ -100,7 +104,7 @@ func TestParseResolvConf(t *testing.T) { require.NoError(t, err) assert.EqualValues(t, tc.nameservers, ns, "test case [%d]: name servers", i) assert.EqualValues(t, tc.searches, srch, "test case [%d] searches", i) - assert.EqualValues(t, tc.options, opts, "test case [%d] options", i) + assert.EqualValues(t, sets.NewString(tc.options...), sets.NewString(opts...), "test case [%d] options", i) } else { require.Error(t, err, "tc.searches %v", tc.searches) }