Skip to content

Commit 631a3bf

Browse files
committed
chore: refactor inspect response
Signed-off-by: Arjun Raja Yogidas <[email protected]>
1 parent 42a83c8 commit 631a3bf

File tree

4 files changed

+49
-69
lines changed

4 files changed

+49
-69
lines changed

cmd/nerdctl/container/container_inspect_linux_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ func TestContainerInspectHostConfig(t *testing.T) {
255255
"--shm-size", "256m",
256256
"--runtime", "io.containerd.runtime.v1.linux",
257257
"--sysctl", "net.core.somaxconn=1024",
258-
"--device", "/dev/zero:/dev/null",
258+
"--device", "/dev/null:/dev/null",
259259
testutil.AlpineImage, "sleep", "infinity").AssertOK()
260260

261261
inspect := base.InspectContainer(testContainer)

pkg/cmd/container/create.go

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,8 @@ type internalLabels struct {
666666
// WithInternalLabels sets the internal labels for a container.
667667
func withInternalLabels(internalLabels internalLabels) (containerd.NewContainerOpts, error) {
668668
m := make(map[string]string)
669+
var hostConfigLabel dockercompat.HostConfigLabel
670+
var dnsSettings dockercompat.DNSSettings
669671
m[labels.Namespace] = internalLabels.namespace
670672
if internalLabels.name != "" {
671673
m[labels.Name] = internalLabels.name
@@ -748,44 +750,40 @@ func withInternalLabels(internalLabels internalLabels) (containerd.NewContainerO
748750
}
749751

750752
if internalLabels.blkioWeight > 0 {
751-
m[labels.BlkioWeight] = fmt.Sprintf("%d", internalLabels.blkioWeight)
753+
hostConfigLabel.BlkioWeight = internalLabels.blkioWeight
752754
}
753755

754756
if internalLabels.cidFile != "" {
755-
m[labels.CIdFile] = internalLabels.cidFile
757+
hostConfigLabel.CidFile = internalLabels.cidFile
756758
}
757759

758760
if len(internalLabels.dnsServers) > 0 {
759-
dnsServersJSON, err := json.Marshal(internalLabels.dnsServers)
760-
if err != nil {
761-
return nil, err
762-
}
763-
m[labels.DNSServer] = string(dnsServersJSON)
761+
dnsSettings.DNSServers = internalLabels.dnsServers
764762
}
765763

766764
if len(internalLabels.dnsSearchDomains) > 0 {
767-
dnsSearchJSON, err := json.Marshal(internalLabels.dnsSearchDomains)
768-
if err != nil {
769-
return nil, err
770-
}
771-
m[labels.DNSSearchDomains] = string(dnsSearchJSON)
765+
dnsSettings.DNSSearchDomains = internalLabels.dnsSearchDomains
772766
}
773767

774768
if len(internalLabels.dnsResolvConfOptions) > 0 {
775-
dnsResolvConfOptionsJSON, err := json.Marshal(internalLabels.dnsResolvConfOptions)
776-
if err != nil {
777-
return nil, err
778-
}
779-
m[labels.DNSResolvConfOptions] = string(dnsResolvConfOptionsJSON)
769+
dnsSettings.DNSResolvConfOptions = internalLabels.dnsResolvConfOptions
780770
}
781771

782772
if len(internalLabels.deviceMapping) > 0 {
783-
devicesJSON, err := json.Marshal(internalLabels.deviceMapping)
784-
if err != nil {
785-
return nil, err
786-
}
787-
m[labels.DeviceMapping] = string(devicesJSON)
773+
hostConfigLabel.DeviceMapping = internalLabels.deviceMapping
774+
}
775+
776+
hostConfigJSON, err := json.Marshal(hostConfigLabel)
777+
if err != nil {
778+
return nil, err
779+
}
780+
m[labels.HostConfigLabel] = string(hostConfigJSON)
781+
782+
dnsSettingsJSON, err := json.Marshal(dnsSettings)
783+
if err != nil {
784+
return nil, err
788785
}
786+
m[labels.DNSSetting] = string(dnsSettingsJSON)
789787

790788
return containerd.WithAdditionalContainerLabels(m), nil
791789
}

pkg/inspecttypes/dockercompat/dockercompat.go

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,12 @@ type DNSSettings struct {
236236
DNSSearchDomains []string
237237
}
238238

239+
type HostConfigLabel struct {
240+
BlkioWeight uint16
241+
CidFile string
242+
DeviceMapping []string
243+
}
244+
239245
type CPUSettings struct {
240246
cpuSetCpus string
241247
cpuSetMems string
@@ -359,18 +365,11 @@ func ContainerFromNative(n *native.Container) (*Container, error) {
359365
}
360366
}
361367

362-
if blkioWeightSet := n.Labels[labels.BlkioWeight]; blkioWeightSet != "" {
363-
var blkioWeight uint16
364-
_, err := fmt.Sscanf(blkioWeightSet, "%d", &blkioWeight)
365-
if err != nil {
366-
return nil, fmt.Errorf("failed to convert string to uint: %v", err)
367-
}
368-
c.HostConfig.BlkioWeight = blkioWeight
369-
}
368+
// var hostConfigLabel HostConfigLabel
369+
hostConfigLabel, err := getHostConfigLabelFromNative(n.Labels)
370370

371-
if cidFile := n.Labels[labels.CIdFile]; cidFile != "" {
372-
c.HostConfig.ContainerIDFile = cidFile
373-
}
371+
c.HostConfig.BlkioWeight = hostConfigLabel.BlkioWeight
372+
c.HostConfig.ContainerIDFile = hostConfigLabel.CidFile
374373

375374
groupAdd, err := groupAddFromNative(n.Spec.(*specs.Spec))
376375
if err != nil {
@@ -479,11 +478,7 @@ func ContainerFromNative(n *native.Container) (*Container, error) {
479478
}
480479
c.Config.Hostname = hostname
481480

482-
c.HostConfig.Devices = []string{}
483-
if nedctlDeviceMapping := n.Labels[labels.DeviceMapping]; nedctlDeviceMapping != "" {
484-
devices, _ := parseDeviceMapping(nedctlDeviceMapping)
485-
c.HostConfig.Devices = devices
486-
}
481+
c.HostConfig.Devices = hostConfigLabel.DeviceMapping
487482

488483
return c, nil
489484
}
@@ -737,24 +732,23 @@ func getMemorySettingsFromNative(sp *specs.Spec) (*MemorySetting, error) {
737732
func getDNSFromNative(Labels map[string]string) (*DNSSettings, error) {
738733
res := &DNSSettings{}
739734

740-
if dnsServers := Labels[labels.DNSServer]; dnsServers != "" {
741-
if err := json.Unmarshal([]byte(dnsServers), &res.DNSServers); err != nil {
742-
return nil, fmt.Errorf("failed to parse DNS servers: %v", err)
735+
if dnsSettingJSON, ok := Labels[labels.DNSSetting]; ok {
736+
if err := json.Unmarshal([]byte(dnsSettingJSON), &res); err != nil {
737+
return nil, fmt.Errorf("failed to parse DNS settings: %v", err)
743738
}
744739
}
745740

746-
if dnsOptions := Labels[labels.DNSResolvConfOptions]; dnsOptions != "" {
747-
if err := json.Unmarshal([]byte(dnsOptions), &res.DNSResolvConfOptions); err != nil {
748-
return nil, fmt.Errorf("failed to parse DNS options: %v", err)
749-
}
750-
}
741+
return res, nil
742+
}
751743

752-
if dnsSearch := Labels[labels.DNSSearchDomains]; dnsSearch != "" {
753-
if err := json.Unmarshal([]byte(dnsSearch), &res.DNSSearchDomains); err != nil {
754-
return nil, fmt.Errorf("failed to parse DNS search domains: %v", err)
744+
func getHostConfigLabelFromNative(Labels map[string]string) (*HostConfigLabel, error) {
745+
res := &HostConfigLabel{}
746+
747+
if hostConfigLabelJSON, ok := Labels[labels.HostConfigLabel]; ok {
748+
if err := json.Unmarshal([]byte(hostConfigLabelJSON), &res); err != nil {
749+
return nil, fmt.Errorf("failed to parse DNS servers: %v", err)
755750
}
756751
}
757-
758752
return res, nil
759753
}
760754

pkg/labels/labels.go

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -101,27 +101,15 @@ const (
101101
// (like "nerdctl/default-network=true" or "nerdctl/default-network=false")
102102
NerdctlDefaultNetwork = Prefix + "default-network"
103103

104-
// LogConfig defines the logging configuration passed to the container
105-
LogConfig = Prefix + "log-config"
106-
107104
// ContainerAutoRemove is to check whether the --rm option is specified.
108105
ContainerAutoRemove = Prefix + "auto-remove"
109106

110-
// BlkioWeight to check if the --blkio-weight is specified
111-
BlkioWeight = Prefix + "blkio-weight"
112-
113-
// Cidfile is the ContainerId file set via the --cidfile flag
114-
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"
107+
// LogConfig defines the logging configuration passed to the container
108+
LogConfig = Prefix + "log-config"
121109

122-
// DNSSearchDomains set custom DNS search domains
123-
DNSSearchDomains = Prefix + "dns-search"
110+
// HostConfigLabel sets the dockercompat host config values
111+
HostConfigLabel = Prefix + "host-config"
124112

125-
//DeviceMapping specifies mapping host device to the container
126-
DeviceMapping = Prefix + "devices"
113+
// DNSSettings sets the dockercompat Dns config values
114+
DNSSetting = Prefix + "dns"
127115
)

0 commit comments

Comments
 (0)