Skip to content

Commit ac061b4

Browse files
committed
chore: add dns config to inspect response
Signed-off-by: Arjun Raja Yogidas <[email protected]>
1 parent 49a810c commit ac061b4

File tree

4 files changed

+135
-7
lines changed

4 files changed

+135
-7
lines changed

cmd/nerdctl/container/container_inspect_linux_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,3 +294,51 @@ func TestContainerInspectHostConfigDefaults(t *testing.T) {
294294
assert.Equal(t, int64(0), inspect.HostConfig.MemorySwap)
295295
assert.Equal(t, bool(false), inspect.HostConfig.OomKillDisable)
296296
}
297+
298+
func TestContainerInspectHostConfigDNS(t *testing.T) {
299+
testContainer := testutil.Identifier(t)
300+
301+
base := testutil.NewBase(t)
302+
defer base.Cmd("rm", "-f", testContainer).Run()
303+
304+
// Run a container with DNS options
305+
base.Cmd("run", "-d", "--name", testContainer,
306+
"--dns", "8.8.8.8",
307+
"--dns", "1.1.1.1",
308+
"--dns-search", "example.com",
309+
"--dns-search", "test.local",
310+
"--dns-option", "ndots:5",
311+
"--dns-option", "timeout:3",
312+
testutil.AlpineImage, "sleep", "infinity").AssertOK()
313+
314+
inspect := base.InspectContainer(testContainer)
315+
316+
// Check DNS servers
317+
expectedDNSServers := []string{"8.8.8.8", "1.1.1.1"}
318+
assert.DeepEqual(t, expectedDNSServers, inspect.HostConfig.DNS)
319+
320+
// Check DNS search domains
321+
expectedDNSSearch := []string{"example.com", "test.local"}
322+
assert.DeepEqual(t, expectedDNSSearch, inspect.HostConfig.DNSSearch)
323+
324+
// Check DNS options
325+
expectedDNSOptions := []string{"ndots:5", "timeout:3"}
326+
assert.DeepEqual(t, expectedDNSOptions, inspect.HostConfig.DNSOptions)
327+
}
328+
329+
func TestContainerInspectHostConfigDNSDefaults(t *testing.T) {
330+
testContainer := testutil.Identifier(t)
331+
332+
base := testutil.NewBase(t)
333+
defer base.Cmd("rm", "-f", testContainer).Run()
334+
335+
// Run a container without specifying DNS options
336+
base.Cmd("run", "-d", "--name", testContainer, testutil.AlpineImage, "sleep", "infinity").AssertOK()
337+
338+
inspect := base.InspectContainer(testContainer)
339+
340+
// Check that DNS settings are empty by default
341+
assert.Equal(t, 0, len(inspect.HostConfig.DNS))
342+
assert.Equal(t, 0, len(inspect.HostConfig.DNSSearch))
343+
assert.Equal(t, 0, len(inspect.HostConfig.DNSOptions))
344+
}

pkg/cmd/container/create.go

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -626,19 +626,20 @@ type internalLabels struct {
626626
extraHosts []string
627627
pidFile string
628628
blkioWeight uint16
629-
cpusetCpus string
630-
cpusetMems string
631629
// labels from cmd options or automatically set
632630
name string
633631
hostname string
634632
// automatically generated
635633
stateDir string
636634
// network
637-
networks []string
638-
ipAddress string
639-
ip6Address string
640-
ports []cni.PortMapping
641-
macAddress string
635+
networks []string
636+
ipAddress string
637+
ip6Address string
638+
ports []cni.PortMapping
639+
macAddress string
640+
dnsServers []string
641+
dnsSearchDomains []string
642+
dnsResolvConfOptions []string
642643
// volume
643644
mountPoints []*mountutil.Processed
644645
anonVolumes []string
@@ -751,6 +752,30 @@ func withInternalLabels(internalLabels internalLabels) (containerd.NewContainerO
751752
m[labels.CIdFile] = internalLabels.cidFile
752753
}
753754

755+
if len(internalLabels.dnsServers) > 0 {
756+
dnsServersJSON, err := json.Marshal(internalLabels.dnsServers)
757+
if err != nil {
758+
return nil, err
759+
}
760+
m[labels.DnsServer] = string(dnsServersJSON)
761+
}
762+
763+
if len(internalLabels.dnsSearchDomains) > 0 {
764+
dnsSearchJSON, err := json.Marshal(internalLabels.dnsSearchDomains)
765+
if err != nil {
766+
return nil, err
767+
}
768+
m[labels.DNSSearchDomains] = string(dnsSearchJSON)
769+
}
770+
771+
if len(internalLabels.dnsResolvConfOptions) > 0 {
772+
dnsResolvConfOptionsJSON, err := json.Marshal(internalLabels.dnsResolvConfOptions)
773+
if err != nil {
774+
return nil, err
775+
}
776+
m[labels.DNSResolvConfOptions] = string(dnsResolvConfOptionsJSON)
777+
}
778+
754779
return containerd.WithAdditionalContainerLabels(m), nil
755780
}
756781

@@ -762,6 +787,9 @@ func (il *internalLabels) loadNetOpts(opts types.NetworkOptions) {
762787
il.ip6Address = opts.IP6Address
763788
il.networks = opts.NetworkSlice
764789
il.macAddress = opts.MACAddress
790+
il.dnsServers = opts.DNSServers
791+
il.dnsSearchDomains = opts.DNSSearchDomains
792+
il.dnsResolvConfOptions = opts.DNSResolvConfOptions
765793
}
766794

767795
func dockercompatMounts(mountPoints []*mountutil.Processed) []dockercompat.MountPoint {

pkg/inspecttypes/dockercompat/dockercompat.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@ type HostConfig struct {
157157
Memory int64 // Memory limit (in bytes)
158158
MemorySwap int64 // Total memory usage (memory + swap); set `-1` to enable unlimited swap
159159
OomKillDisable bool // specifies whether to disable OOM Killer
160+
DNS []string `json:"Dns"` // List of DNS server to lookup
161+
DNSOptions []string `json:"DnsOptions"` // List of DNSOption to look for
162+
DNSSearch []string `json:"DnsSearch"` // List of DNSSearch to look for
160163
}
161164

162165
// From https://github.com/moby/moby/blob/v20.10.1/api/types/types.go#L416-L427
@@ -224,6 +227,12 @@ type NetworkSettings struct {
224227
Networks map[string]*NetworkEndpointSettings
225228
}
226229

230+
type DNSSettings struct {
231+
DNSServers []string
232+
DNSResolvConfOptions []string
233+
DNSSearchDomains []string
234+
}
235+
227236
type CPUSettings struct {
228237
cpuSetCpus string
229238
cpuSetMems string
@@ -427,6 +436,16 @@ func ContainerFromNative(n *native.Container) (*Container, error) {
427436
c.HostConfig.OomKillDisable = memorySettings.DisableOOMKiller
428437
c.HostConfig.Memory = memorySettings.Limit
429438
c.HostConfig.MemorySwap = memorySettings.Swap
439+
440+
dnsSettings, err := getDnsFromNative(n.Labels)
441+
if err != nil {
442+
return nil, fmt.Errorf("failed to Decode dns Settings: %v", err)
443+
}
444+
445+
c.HostConfig.DNS = dnsSettings.DNSServers
446+
c.HostConfig.DNSOptions = dnsSettings.DNSResolvConfOptions
447+
c.HostConfig.DNSSearch = dnsSettings.DNSSearchDomains
448+
430449
c.State = cs
431450
c.Config = &Config{
432451
Labels: n.Labels,
@@ -685,6 +704,30 @@ func getMemorySettingsFromNative(sp *specs.Spec) (*MemorySetting, error) {
685704
return res, nil
686705
}
687706

707+
func getDnsFromNative(Labels map[string]string) (*DNSSettings, error) {
708+
res := &DNSSettings{}
709+
710+
if dnsServers := Labels[labels.DnsServer]; dnsServers != "" {
711+
if err := json.Unmarshal([]byte(dnsServers), &res.DNSServers); err != nil {
712+
return nil, fmt.Errorf("failed to parse DNS servers: %v", err)
713+
}
714+
}
715+
716+
if dnsOptions := Labels[labels.DNSResolvConfOptions]; dnsOptions != "" {
717+
if err := json.Unmarshal([]byte(dnsOptions), &res.DNSResolvConfOptions); err != nil {
718+
return nil, fmt.Errorf("failed to parse DNS options: %v", err)
719+
}
720+
}
721+
722+
if dnsSearch := Labels[labels.DNSSearchDomains]; dnsSearch != "" {
723+
if err := json.Unmarshal([]byte(dnsSearch), &res.DNSSearchDomains); err != nil {
724+
return nil, fmt.Errorf("failed to parse DNS search domains: %v", err)
725+
}
726+
}
727+
728+
return res, nil
729+
}
730+
688731
type IPAMConfig struct {
689732
Subnet string `json:"Subnet,omitempty"`
690733
Gateway string `json:"Gateway,omitempty"`

pkg/labels/labels.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,13 @@ const (
112112

113113
// Cidfile is the ContainerId file set via the --cidfile flag
114114
CIdFile = Prefix + "cid-file"
115+
116+
// Custom DNS lookup servers.
117+
DnsServer = Prefix + "dns"
118+
119+
// DNSResolvConfOptions set DNS options
120+
DNSResolvConfOptions = Prefix + "dns-options"
121+
122+
// DNSSearchDomains set custom DNS search domains
123+
DNSSearchDomains = Prefix + "dns-search"
115124
)

0 commit comments

Comments
 (0)