Skip to content

Commit 9ad91a3

Browse files
committed
chore: refactor inspect response
Signed-off-by: Arjun Raja Yogidas <[email protected]>
1 parent 09cd942 commit 9ad91a3

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
@@ -667,6 +667,8 @@ type internalLabels struct {
667667
// WithInternalLabels sets the internal labels for a container.
668668
func withInternalLabels(internalLabels internalLabels) (containerd.NewContainerOpts, error) {
669669
m := make(map[string]string)
670+
var hostConfigLabel dockercompat.HostConfigLabel
671+
var dnsSettings dockercompat.DNSSettings
670672
m[labels.Namespace] = internalLabels.namespace
671673
if internalLabels.name != "" {
672674
m[labels.Name] = internalLabels.name
@@ -750,44 +752,40 @@ func withInternalLabels(internalLabels internalLabels) (containerd.NewContainerO
750752
}
751753

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

756758
if internalLabels.cidFile != "" {
757-
m[labels.CIdFile] = internalLabels.cidFile
759+
hostConfigLabel.CidFile = internalLabels.cidFile
758760
}
759761

760762
if len(internalLabels.dnsServers) > 0 {
761-
dnsServersJSON, err := json.Marshal(internalLabels.dnsServers)
762-
if err != nil {
763-
return nil, err
764-
}
765-
m[labels.DNSServer] = string(dnsServersJSON)
763+
dnsSettings.DNSServers = internalLabels.dnsServers
766764
}
767765

768766
if len(internalLabels.dnsSearchDomains) > 0 {
769-
dnsSearchJSON, err := json.Marshal(internalLabels.dnsSearchDomains)
770-
if err != nil {
771-
return nil, err
772-
}
773-
m[labels.DNSSearchDomains] = string(dnsSearchJSON)
767+
dnsSettings.DNSSearchDomains = internalLabels.dnsSearchDomains
774768
}
775769

776770
if len(internalLabels.dnsResolvConfOptions) > 0 {
777-
dnsResolvConfOptionsJSON, err := json.Marshal(internalLabels.dnsResolvConfOptions)
778-
if err != nil {
779-
return nil, err
780-
}
781-
m[labels.DNSResolvConfOptions] = string(dnsResolvConfOptionsJSON)
771+
dnsSettings.DNSResolvConfOptions = internalLabels.dnsResolvConfOptions
782772
}
783773

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

792790
return containerd.WithAdditionalContainerLabels(m), nil
793791
}

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 {
@@ -483,11 +482,7 @@ func ContainerFromNative(n *native.Container) (*Container, error) {
483482
c.Config.Domainname = n.Labels[labels.Domainname]
484483
}
485484

486-
c.HostConfig.Devices = []string{}
487-
if nedctlDeviceMapping := n.Labels[labels.DeviceMapping]; nedctlDeviceMapping != "" {
488-
devices, _ := parseDeviceMapping(nedctlDeviceMapping)
489-
c.HostConfig.Devices = devices
490-
}
485+
c.HostConfig.Devices = hostConfigLabel.DeviceMapping
491486

492487
return c, nil
493488
}
@@ -741,24 +736,23 @@ func getMemorySettingsFromNative(sp *specs.Spec) (*MemorySetting, error) {
741736
func getDNSFromNative(Labels map[string]string) (*DNSSettings, error) {
742737
res := &DNSSettings{}
743738

744-
if dnsServers := Labels[labels.DNSServer]; dnsServers != "" {
745-
if err := json.Unmarshal([]byte(dnsServers), &res.DNSServers); err != nil {
746-
return nil, fmt.Errorf("failed to parse DNS servers: %v", err)
739+
if dnsSettingJSON, ok := Labels[labels.DNSSetting]; ok {
740+
if err := json.Unmarshal([]byte(dnsSettingJSON), &res); err != nil {
741+
return nil, fmt.Errorf("failed to parse DNS settings: %v", err)
747742
}
748743
}
749744

750-
if dnsOptions := Labels[labels.DNSResolvConfOptions]; dnsOptions != "" {
751-
if err := json.Unmarshal([]byte(dnsOptions), &res.DNSResolvConfOptions); err != nil {
752-
return nil, fmt.Errorf("failed to parse DNS options: %v", err)
753-
}
754-
}
745+
return res, nil
746+
}
755747

756-
if dnsSearch := Labels[labels.DNSSearchDomains]; dnsSearch != "" {
757-
if err := json.Unmarshal([]byte(dnsSearch), &res.DNSSearchDomains); err != nil {
758-
return nil, fmt.Errorf("failed to parse DNS search domains: %v", err)
748+
func getHostConfigLabelFromNative(Labels map[string]string) (*HostConfigLabel, error) {
749+
res := &HostConfigLabel{}
750+
751+
if hostConfigLabelJSON, ok := Labels[labels.HostConfigLabel]; ok {
752+
if err := json.Unmarshal([]byte(hostConfigLabelJSON), &res); err != nil {
753+
return nil, fmt.Errorf("failed to parse DNS servers: %v", err)
759754
}
760755
}
761-
762756
return res, nil
763757
}
764758

pkg/labels/labels.go

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

107-
// LogConfig defines the logging configuration passed to the container
108-
LogConfig = Prefix + "log-config"
109-
110107
// ContainerAutoRemove is to check whether the --rm option is specified.
111108
ContainerAutoRemove = Prefix + "auto-remove"
112109

113-
// BlkioWeight to check if the --blkio-weight is specified
114-
BlkioWeight = Prefix + "blkio-weight"
115-
116-
// Cidfile is the ContainerId file set via the --cidfile flag
117-
CIdFile = Prefix + "cid-file"
118-
119-
// Custom DNS lookup servers.
120-
DNSServer = Prefix + "dns"
121-
122-
// DNSResolvConfOptions set DNS options
123-
DNSResolvConfOptions = Prefix + "dns-options"
110+
// LogConfig defines the logging configuration passed to the container
111+
LogConfig = Prefix + "log-config"
124112

125-
// DNSSearchDomains set custom DNS search domains
126-
DNSSearchDomains = Prefix + "dns-search"
113+
// HostConfigLabel sets the dockercompat host config values
114+
HostConfigLabel = Prefix + "host-config"
127115

128-
//DeviceMapping specifies mapping host device to the container
129-
DeviceMapping = Prefix + "devices"
116+
// DNSSettings sets the dockercompat Dns config values
117+
DNSSetting = Prefix + "dns"
130118
)

0 commit comments

Comments
 (0)