Skip to content

Commit 87c908e

Browse files
authored
Merge pull request containerd#10462 from dims/automated-cherry-pick-of-#9730-upstream-release-1.7
[release/1.7] CRI: An empty DNSConfig != unspecified
2 parents b06e353 + 209ee4f commit 87c908e

File tree

4 files changed

+166
-26
lines changed

4 files changed

+166
-26
lines changed

pkg/cri/sbserver/podsandbox/sandbox_run_linux.go

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

255255
// Set DNS options. Maintain a resolv.conf for the sandbox.
256-
var err error
257-
resolvContent := ""
256+
resolvPath := c.getResolvPath(id)
257+
258258
if dnsConfig := config.GetDnsConfig(); dnsConfig != nil {
259-
resolvContent, err = parseDNSOptions(dnsConfig.Servers, dnsConfig.Searches, dnsConfig.Options)
259+
resolvContent, err := parseDNSOptions(dnsConfig.Servers, dnsConfig.Searches, dnsConfig.Options)
260260
if err != nil {
261261
return fmt.Errorf("failed to parse sandbox DNSConfig %+v: %w", dnsConfig, err)
262262
}
263-
}
264-
resolvPath := c.getResolvPath(id)
265-
if resolvContent == "" {
266-
// copy host's resolv.conf to resolvPath
267-
err = c.os.CopyFile(resolvConfPath, resolvPath, 0644)
268-
if err != nil {
269-
return fmt.Errorf("failed to copy host's resolv.conf to %q: %w", resolvPath, err)
263+
if err := c.os.WriteFile(resolvPath, []byte(resolvContent), 0644); err != nil {
264+
return fmt.Errorf("failed to write resolv content to %q: %w", resolvPath, err)
270265
}
271266
} else {
272-
err = c.os.WriteFile(resolvPath, []byte(resolvContent), 0644)
273-
if err != nil {
274-
return fmt.Errorf("failed to write resolv content to %q: %w", resolvPath, err)
267+
// The DnsConfig was nil - we interpret that to mean "use the global
268+
// default", which is dubious but backwards-compatible.
269+
if err := c.os.CopyFile(resolvConfPath, resolvPath, 0644); err != nil {
270+
return fmt.Errorf("failed to copy host's resolv.conf to %q: %w", resolvPath, err)
275271
}
276272
}
277273

pkg/cri/sbserver/podsandbox/sandbox_run_linux_test.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,80 @@ options timeout:1
360360
},
361361
},
362362
},
363+
"should create empty /etc/resolv.conf if DNSOptions is empty": {
364+
dnsConfig: &runtime.DNSConfig{},
365+
ipcMode: runtime.NamespaceMode_NODE,
366+
expectedCalls: []ostesting.CalledDetail{
367+
{
368+
Name: "Hostname",
369+
},
370+
{
371+
Name: "WriteFile",
372+
Arguments: []interface{}{
373+
filepath.Join(testRootDir, sandboxesDir, testID, "hostname"),
374+
[]byte(realhostname + "\n"),
375+
os.FileMode(0644),
376+
},
377+
},
378+
{
379+
Name: "CopyFile",
380+
Arguments: []interface{}{
381+
"/etc/hosts",
382+
filepath.Join(testRootDir, sandboxesDir, testID, "hosts"),
383+
os.FileMode(0644),
384+
},
385+
},
386+
{
387+
Name: "WriteFile",
388+
Arguments: []interface{}{
389+
filepath.Join(testRootDir, sandboxesDir, testID, "resolv.conf"),
390+
[]byte{},
391+
os.FileMode(0644),
392+
},
393+
},
394+
{
395+
Name: "Stat",
396+
Arguments: []interface{}{"/dev/shm"},
397+
},
398+
},
399+
},
400+
"should copy host /etc/resolv.conf if DNSOptions is not set": {
401+
dnsConfig: nil,
402+
ipcMode: runtime.NamespaceMode_NODE,
403+
expectedCalls: []ostesting.CalledDetail{
404+
{
405+
Name: "Hostname",
406+
},
407+
{
408+
Name: "WriteFile",
409+
Arguments: []interface{}{
410+
filepath.Join(testRootDir, sandboxesDir, testID, "hostname"),
411+
[]byte(realhostname + "\n"),
412+
os.FileMode(0644),
413+
},
414+
},
415+
{
416+
Name: "CopyFile",
417+
Arguments: []interface{}{
418+
"/etc/hosts",
419+
filepath.Join(testRootDir, sandboxesDir, testID, "hosts"),
420+
os.FileMode(0644),
421+
},
422+
},
423+
{
424+
Name: "CopyFile",
425+
Arguments: []interface{}{
426+
filepath.Join("/etc/resolv.conf"),
427+
filepath.Join(testRootDir, sandboxesDir, testID, "resolv.conf"),
428+
os.FileMode(0644),
429+
},
430+
},
431+
{
432+
Name: "Stat",
433+
Arguments: []interface{}{"/dev/shm"},
434+
},
435+
},
436+
},
363437
"should create sandbox shm when ipc namespace mode is not NODE": {
364438
ipcMode: runtime.NamespaceMode_POD,
365439
expectedCalls: []ostesting.CalledDetail{

pkg/cri/server/sandbox_run_linux.go

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

272272
// Set DNS options. Maintain a resolv.conf for the sandbox.
273-
var err error
274-
resolvContent := ""
273+
resolvPath := c.getResolvPath(id)
274+
275275
if dnsConfig := config.GetDnsConfig(); dnsConfig != nil {
276-
resolvContent, err = parseDNSOptions(dnsConfig.Servers, dnsConfig.Searches, dnsConfig.Options)
276+
resolvContent, err := parseDNSOptions(dnsConfig.Servers, dnsConfig.Searches, dnsConfig.Options)
277277
if err != nil {
278278
return fmt.Errorf("failed to parse sandbox DNSConfig %+v: %w", dnsConfig, err)
279279
}
280-
}
281-
resolvPath := c.getResolvPath(id)
282-
if resolvContent == "" {
283-
// copy host's resolv.conf to resolvPath
284-
err = c.os.CopyFile(resolvConfPath, resolvPath, 0644)
285-
if err != nil {
286-
return fmt.Errorf("failed to copy host's resolv.conf to %q: %w", resolvPath, err)
280+
if err := c.os.WriteFile(resolvPath, []byte(resolvContent), 0644); err != nil {
281+
return fmt.Errorf("failed to write resolv content to %q: %w", resolvPath, err)
287282
}
288283
} else {
289-
err = c.os.WriteFile(resolvPath, []byte(resolvContent), 0644)
290-
if err != nil {
291-
return fmt.Errorf("failed to write resolv content to %q: %w", resolvPath, err)
284+
// The DnsConfig was nil - we interpret that to mean "use the global
285+
// default", which is dubious but backwards-compatible.
286+
if err := c.os.CopyFile(resolvConfPath, resolvPath, 0644); err != nil {
287+
return fmt.Errorf("failed to copy host's resolv.conf to %q: %w", resolvPath, err)
292288
}
293289
}
294290

pkg/cri/server/sandbox_run_linux_test.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,80 @@ options timeout:1
497497
},
498498
},
499499
},
500+
"should create empty /etc/resolv.conf if DNSOptions is empty": {
501+
dnsConfig: &runtime.DNSConfig{},
502+
ipcMode: runtime.NamespaceMode_NODE,
503+
expectedCalls: []ostesting.CalledDetail{
504+
{
505+
Name: "Hostname",
506+
},
507+
{
508+
Name: "WriteFile",
509+
Arguments: []interface{}{
510+
filepath.Join(testRootDir, sandboxesDir, testID, "hostname"),
511+
[]byte(realhostname + "\n"),
512+
os.FileMode(0644),
513+
},
514+
},
515+
{
516+
Name: "CopyFile",
517+
Arguments: []interface{}{
518+
"/etc/hosts",
519+
filepath.Join(testRootDir, sandboxesDir, testID, "hosts"),
520+
os.FileMode(0644),
521+
},
522+
},
523+
{
524+
Name: "WriteFile",
525+
Arguments: []interface{}{
526+
filepath.Join(testRootDir, sandboxesDir, testID, "resolv.conf"),
527+
[]byte{},
528+
os.FileMode(0644),
529+
},
530+
},
531+
{
532+
Name: "Stat",
533+
Arguments: []interface{}{"/dev/shm"},
534+
},
535+
},
536+
},
537+
"should copy host /etc/resolv.conf if DNSOptions is not set": {
538+
dnsConfig: nil,
539+
ipcMode: runtime.NamespaceMode_NODE,
540+
expectedCalls: []ostesting.CalledDetail{
541+
{
542+
Name: "Hostname",
543+
},
544+
{
545+
Name: "WriteFile",
546+
Arguments: []interface{}{
547+
filepath.Join(testRootDir, sandboxesDir, testID, "hostname"),
548+
[]byte(realhostname + "\n"),
549+
os.FileMode(0644),
550+
},
551+
},
552+
{
553+
Name: "CopyFile",
554+
Arguments: []interface{}{
555+
"/etc/hosts",
556+
filepath.Join(testRootDir, sandboxesDir, testID, "hosts"),
557+
os.FileMode(0644),
558+
},
559+
},
560+
{
561+
Name: "CopyFile",
562+
Arguments: []interface{}{
563+
filepath.Join("/etc/resolv.conf"),
564+
filepath.Join(testRootDir, sandboxesDir, testID, "resolv.conf"),
565+
os.FileMode(0644),
566+
},
567+
},
568+
{
569+
Name: "Stat",
570+
Arguments: []interface{}{"/dev/shm"},
571+
},
572+
},
573+
},
500574
"should create sandbox shm when ipc namespace mode is not NODE": {
501575
ipcMode: runtime.NamespaceMode_POD,
502576
expectedCalls: []ostesting.CalledDetail{

0 commit comments

Comments
 (0)