Skip to content

Commit 6e365e9

Browse files
committed
CRI: An empty DNSConfig != unspecified
If we find that DNSConfig is provided and empty (not nil), we should not replace it with the host's resolv.conf. Also adds tests. Signed-off-by: Tim Hockin <[email protected]>
1 parent ac54047 commit 6e365e9

File tree

2 files changed

+85
-13
lines changed

2 files changed

+85
-13
lines changed

pkg/cri/server/podsandbox/sandbox_run_linux.go

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -268,25 +268,21 @@ func (c *Controller) setupSandboxFiles(id string, config *runtime.PodSandboxConf
268268
}
269269

270270
// Set DNS options. Maintain a resolv.conf for the sandbox.
271-
var err error
272-
resolvContent := ""
271+
resolvPath := c.getResolvPath(id)
272+
273273
if dnsConfig := config.GetDnsConfig(); dnsConfig != nil {
274-
resolvContent, err = parseDNSOptions(dnsConfig.Servers, dnsConfig.Searches, dnsConfig.Options)
274+
resolvContent, err := parseDNSOptions(dnsConfig.Servers, dnsConfig.Searches, dnsConfig.Options)
275275
if err != nil {
276276
return fmt.Errorf("failed to parse sandbox DNSConfig %+v: %w", dnsConfig, err)
277277
}
278-
}
279-
resolvPath := c.getResolvPath(id)
280-
if resolvContent == "" {
281-
// copy host's resolv.conf to resolvPath
282-
err = c.os.CopyFile(resolvConfPath, resolvPath, 0644)
283-
if err != nil {
284-
return fmt.Errorf("failed to copy host's resolv.conf to %q: %w", resolvPath, err)
278+
if err := c.os.WriteFile(resolvPath, []byte(resolvContent), 0644); err != nil {
279+
return fmt.Errorf("failed to write resolv content to %q: %w", resolvPath, err)
285280
}
286281
} else {
287-
err = c.os.WriteFile(resolvPath, []byte(resolvContent), 0644)
288-
if err != nil {
289-
return fmt.Errorf("failed to write resolv content to %q: %w", resolvPath, err)
282+
// The DnsConfig was nil - we interpret that to mean "use the global
283+
// default", which is dubious but backwards-compatible.
284+
if err := c.os.CopyFile(resolvConfPath, resolvPath, 0644); err != nil {
285+
return fmt.Errorf("failed to copy host's resolv.conf to %q: %w", resolvPath, err)
290286
}
291287
}
292288

pkg/cri/server/podsandbox/sandbox_run_linux_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,82 @@ options timeout:1
511511
},
512512
},
513513
},
514+
{
515+
desc: "should create empty /etc/resolv.conf if DNSOptions is empty",
516+
dnsConfig: &runtime.DNSConfig{},
517+
ipcMode: runtime.NamespaceMode_NODE,
518+
expectedCalls: []ostesting.CalledDetail{
519+
{
520+
Name: "Hostname",
521+
},
522+
{
523+
Name: "WriteFile",
524+
Arguments: []interface{}{
525+
filepath.Join(testRootDir, sandboxesDir, testID, "hostname"),
526+
[]byte(realhostname + "\n"),
527+
os.FileMode(0644),
528+
},
529+
},
530+
{
531+
Name: "CopyFile",
532+
Arguments: []interface{}{
533+
"/etc/hosts",
534+
filepath.Join(testRootDir, sandboxesDir, testID, "hosts"),
535+
os.FileMode(0644),
536+
},
537+
},
538+
{
539+
Name: "WriteFile",
540+
Arguments: []interface{}{
541+
filepath.Join(testRootDir, sandboxesDir, testID, "resolv.conf"),
542+
[]byte{},
543+
os.FileMode(0644),
544+
},
545+
},
546+
{
547+
Name: "Stat",
548+
Arguments: []interface{}{"/dev/shm"},
549+
},
550+
},
551+
},
552+
{
553+
desc: "should copy host /etc/resolv.conf if DNSOptions is not set",
554+
dnsConfig: nil,
555+
ipcMode: runtime.NamespaceMode_NODE,
556+
expectedCalls: []ostesting.CalledDetail{
557+
{
558+
Name: "Hostname",
559+
},
560+
{
561+
Name: "WriteFile",
562+
Arguments: []interface{}{
563+
filepath.Join(testRootDir, sandboxesDir, testID, "hostname"),
564+
[]byte(realhostname + "\n"),
565+
os.FileMode(0644),
566+
},
567+
},
568+
{
569+
Name: "CopyFile",
570+
Arguments: []interface{}{
571+
"/etc/hosts",
572+
filepath.Join(testRootDir, sandboxesDir, testID, "hosts"),
573+
os.FileMode(0644),
574+
},
575+
},
576+
{
577+
Name: "CopyFile",
578+
Arguments: []interface{}{
579+
filepath.Join("/etc/resolv.conf"),
580+
filepath.Join(testRootDir, sandboxesDir, testID, "resolv.conf"),
581+
os.FileMode(0644),
582+
},
583+
},
584+
{
585+
Name: "Stat",
586+
Arguments: []interface{}{"/dev/shm"},
587+
},
588+
},
589+
},
514590
{
515591
desc: "should create sandbox shm when ipc namespace mode is not NODE",
516592
ipcMode: runtime.NamespaceMode_POD,

0 commit comments

Comments
 (0)