kubelet: append options to pod if there are multi options in /etc/resolv.conf

This commit is contained in:
Paco Xu 2022-09-13 14:35:04 +08:00
parent bcea98234f
commit 468b2a2297
2 changed files with 25 additions and 1 deletions

View File

@ -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

View File

@ -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 {