From 99acf7f7df8d39d1d8aceafd834f5bffc6688cfe Mon Sep 17 00:00:00 2001 From: Arjun Raja Yogidas Date: Thu, 2 Jan 2025 21:50:23 +0000 Subject: [PATCH 01/16] feat: add hostConfig to nerdctl inspect response Signed-off-by: Arjun Raja Yogidas --- pkg/cmd/container/create.go | 9 +++- pkg/inspecttypes/dockercompat/dockercompat.go | 53 ++++++++++++++++++- pkg/labels/labels.go | 3 ++ 3 files changed, 63 insertions(+), 2 deletions(-) diff --git a/pkg/cmd/container/create.go b/pkg/cmd/container/create.go index 77b5656e098..0cd0bfceb91 100644 --- a/pkg/cmd/container/create.go +++ b/pkg/cmd/container/create.go @@ -222,6 +222,7 @@ func Create(ctx context.Context, client *containerd.Client, args []string, netMa return nil, generateRemoveStateDirFunc(ctx, id, internalLabels), err } internalLabels.logURI = logConfig.LogURI + internalLabels.logConfig = logConfig restartOpts, err := generateRestartOpts(ctx, client, options.Restart, logConfig.LogURI, options.InRun) if err != nil { @@ -641,7 +642,8 @@ type internalLabels struct { // log logURI string // a label to check whether the --rm option is specified. - rm string + rm string + logConfig logging.LogConfig } // WithInternalLabels sets the internal labels for a container. @@ -672,6 +674,11 @@ func withInternalLabels(internalLabels internalLabels) (containerd.NewContainerO } if internalLabels.logURI != "" { m[labels.LogURI] = internalLabels.logURI + logConfigJSON, err := json.Marshal(internalLabels.logConfig) + if err != nil { + return nil, err + } + m[labels.LogConfig] = string(logConfigJSON) } if len(internalLabels.anonVolumes) > 0 { anonVolumeJSON, err := json.Marshal(internalLabels.anonVolumes) diff --git a/pkg/inspecttypes/dockercompat/dockercompat.go b/pkg/inspecttypes/dockercompat/dockercompat.go index aceec3bca3d..79c9c57224c 100644 --- a/pkg/inspecttypes/dockercompat/dockercompat.go +++ b/pkg/inspecttypes/dockercompat/dockercompat.go @@ -46,6 +46,7 @@ import ( "github.com/containerd/nerdctl/v2/pkg/imgutil" "github.com/containerd/nerdctl/v2/pkg/inspecttypes/native" "github.com/containerd/nerdctl/v2/pkg/labels" + "github.com/containerd/nerdctl/v2/pkg/logging" "github.com/containerd/nerdctl/v2/pkg/ocihook/state" ) @@ -94,6 +95,11 @@ type ImageMetadata struct { LastTagTime time.Time `json:",omitempty"` } +type LogConfig struct { + Type string + Config logging.LogConfig +} + // Container mimics a `docker container inspect` object. // From https://github.com/moby/moby/blob/v20.10.1/api/types/types.go#L340-L374 type Container struct { @@ -116,7 +122,7 @@ type Container struct { // TODO: ProcessLabel string AppArmorProfile string // TODO: ExecIDs []string - // TODO: HostConfig *container.HostConfig + HostConfig *HostConfig // TODO: GraphDriver GraphDriverData SizeRw *int64 `json:",omitempty"` SizeRootFs *int64 `json:",omitempty"` @@ -126,6 +132,15 @@ type Container struct { NetworkSettings *NetworkSettings } +// From https://github.com/moby/moby/blob/8dbd90ec00daa26dc45d7da2431c965dec99e8b4/api/types/container/host_config.go#L391 +// HostConfig the non-portable Config structure of a container. +type HostConfig struct { + ExtraHosts []string // List of extra hosts + PortBindings nat.PortMap // Port mapping between the exposed port (container) and the host + LogConfig LogConfig // Configuration of the logs for this container + +} + // From https://github.com/moby/moby/blob/v20.10.1/api/types/types.go#L416-L427 // MountPoint represents a mount point configuration inside the container. // This is used for reporting the mountpoints in use by a container. @@ -282,6 +297,32 @@ func ContainerFromNative(n *native.Container) (*Container, error) { c.Mounts = mounts } + c.HostConfig = new(HostConfig) + if nedctlExtraHosts := n.Labels[labels.ExtraHosts]; nedctlExtraHosts != "" { + c.HostConfig.ExtraHosts = parseExtraHosts(nedctlExtraHosts) + } + + if nerdctlLoguri := n.Labels[labels.LogURI]; nerdctlLoguri != "" { + c.HostConfig.LogConfig.Type = nerdctlLoguri + // c.HostConfig.LogConfig.Config = map[string]string{} + } + if logConfigJSON, ok := n.Labels[labels.LogConfig]; ok { + var logConfig logging.LogConfig + err := json.Unmarshal([]byte(logConfigJSON), &logConfig) + if err != nil { + return nil, fmt.Errorf("failed to unmarshal log config: %v", err) + } + + // Assign the parsed LogConfig to c.HostConfig.LogConfig + c.HostConfig.LogConfig.Config = logConfig + } else { + // If LogConfig label is not present, set default values + c.HostConfig.LogConfig.Config = logging.LogConfig{ + Driver: "json-file", + Opts: make(map[string]string), + } + } + cs := new(ContainerState) cs.Restarting = n.Labels[restart.StatusLabel] == string(containerd.Running) cs.Error = n.Labels[labels.Error] @@ -308,6 +349,7 @@ func ContainerFromNative(n *native.Container) (*Container, error) { return nil, err } c.NetworkSettings = nSettings + c.HostConfig.PortBindings = *nSettings.Ports } c.State = cs c.Config = &Config{ @@ -491,6 +533,15 @@ func convertToNatPort(portMappings []cni.PortMapping) (*nat.PortMap, error) { return &portMap, nil } +func parseExtraHosts(extraHostsJSON string) []string { + var extraHosts []string + if err := json.Unmarshal([]byte(extraHostsJSON), &extraHosts); err != nil { + // Handle error or return empty slice + return []string{} + } + return extraHosts +} + type IPAMConfig struct { Subnet string `json:"Subnet,omitempty"` Gateway string `json:"Gateway,omitempty"` diff --git a/pkg/labels/labels.go b/pkg/labels/labels.go index e2dd6ede16d..620108afb8e 100644 --- a/pkg/labels/labels.go +++ b/pkg/labels/labels.go @@ -101,6 +101,9 @@ const ( // (like "nerdctl/default-network=true" or "nerdctl/default-network=false") NerdctlDefaultNetwork = Prefix + "default-network" + // LogConfig defines the loggin configuration passed to the container + LogConfig = Prefix + "log-config" + // ContainerAutoRemove is to check whether the --rm option is specified. ContainerAutoRemove = Prefix + "auto-remove" ) From cfdf0444af147918f80a6d1c50fae5e33b7d8b55 Mon Sep 17 00:00:00 2001 From: Arjun Raja Yogidas Date: Fri, 3 Jan 2025 03:52:05 +0000 Subject: [PATCH 02/16] fix: add loggerLogConfig Signed-off-by: Arjun Raja Yogidas --- pkg/cmd/container/create.go | 27 +++++++++++--- pkg/inspecttypes/dockercompat/dockercompat.go | 36 +++++++++++++++---- pkg/labels/labels.go | 11 +++++- 3 files changed, 63 insertions(+), 11 deletions(-) diff --git a/pkg/cmd/container/create.go b/pkg/cmd/container/create.go index 0cd0bfceb91..f74d0da128a 100644 --- a/pkg/cmd/container/create.go +++ b/pkg/cmd/container/create.go @@ -326,6 +326,10 @@ func Create(ctx context.Context, client *containerd.Client, args []string, netMa internalLabels.rm = containerutil.EncodeContainerRmOptLabel(options.Rm) + internalLabels.cpusetCpus = options.CPUSetCPUs + internalLabels.cpusetMems = options.CPUSetMems + internalLabels.blkioWeight = options.BlkioWeight + // TODO: abolish internal labels and only use annotations ilOpt, err := withInternalLabels(internalLabels) if err != nil { @@ -617,10 +621,13 @@ func withStop(stopSignal string, stopTimeout int, ensuredImage *imgutil.EnsuredI type internalLabels struct { // labels from cmd options - namespace string - platform string - extraHosts []string - pidFile string + namespace string + platform string + extraHosts []string + pidFile string + blkioWeight uint16 + cpusetCpus string + cpusetMems string // labels from cmd options or automatically set name string hostname string @@ -730,6 +737,18 @@ func withInternalLabels(internalLabels internalLabels) (containerd.NewContainerO m[labels.ContainerAutoRemove] = internalLabels.rm } + if internalLabels.blkioWeight > 0 { + m[labels.BlkioWeight] = fmt.Sprintf("%d", internalLabels.blkioWeight) + } + + if internalLabels.cpusetMems != "" { + m[labels.CPUSetMems] = internalLabels.cpusetMems + } + + if internalLabels.cpusetCpus != "" { + m[labels.CPUSetCPUs] = internalLabels.cpusetCpus + } + return containerd.WithAdditionalContainerLabels(m), nil } diff --git a/pkg/inspecttypes/dockercompat/dockercompat.go b/pkg/inspecttypes/dockercompat/dockercompat.go index 79c9c57224c..82b50b6fa50 100644 --- a/pkg/inspecttypes/dockercompat/dockercompat.go +++ b/pkg/inspecttypes/dockercompat/dockercompat.go @@ -46,7 +46,6 @@ import ( "github.com/containerd/nerdctl/v2/pkg/imgutil" "github.com/containerd/nerdctl/v2/pkg/inspecttypes/native" "github.com/containerd/nerdctl/v2/pkg/labels" - "github.com/containerd/nerdctl/v2/pkg/logging" "github.com/containerd/nerdctl/v2/pkg/ocihook/state" ) @@ -97,7 +96,14 @@ type ImageMetadata struct { type LogConfig struct { Type string - Config logging.LogConfig + Config loggerLogConfig +} + +type loggerLogConfig struct { + Driver string `json:"driver"` + Opts map[string]string `json:"opts,omitempty"` + LogURI string `json:"-"` + Address string `json:"address"` } // Container mimics a `docker container inspect` object. @@ -138,7 +144,9 @@ type HostConfig struct { ExtraHosts []string // List of extra hosts PortBindings nat.PortMap // Port mapping between the exposed port (container) and the host LogConfig LogConfig // Configuration of the logs for this container - + BlkioWeight uint16 // Block IO weight (relative weight vs. other containers) + CpusetMems string // CpusetMems 0-2, 0,1 + CpusetCpus string // CpusetCpus 0-2, 0,1 } // From https://github.com/moby/moby/blob/v20.10.1/api/types/types.go#L416-L427 @@ -304,10 +312,9 @@ func ContainerFromNative(n *native.Container) (*Container, error) { if nerdctlLoguri := n.Labels[labels.LogURI]; nerdctlLoguri != "" { c.HostConfig.LogConfig.Type = nerdctlLoguri - // c.HostConfig.LogConfig.Config = map[string]string{} } if logConfigJSON, ok := n.Labels[labels.LogConfig]; ok { - var logConfig logging.LogConfig + var logConfig loggerLogConfig err := json.Unmarshal([]byte(logConfigJSON), &logConfig) if err != nil { return nil, fmt.Errorf("failed to unmarshal log config: %v", err) @@ -317,12 +324,29 @@ func ContainerFromNative(n *native.Container) (*Container, error) { c.HostConfig.LogConfig.Config = logConfig } else { // If LogConfig label is not present, set default values - c.HostConfig.LogConfig.Config = logging.LogConfig{ + c.HostConfig.LogConfig.Config = loggerLogConfig{ Driver: "json-file", Opts: make(map[string]string), } } + if blkioWeightSet := n.Labels[labels.BlkioWeight]; blkioWeightSet != "" { + var blkioWeight uint16 + _, err := fmt.Sscanf(blkioWeightSet, "%d", &blkioWeight) + if err != nil { + return nil, fmt.Errorf("failed to convert string to uint: %v", err) + } + c.HostConfig.BlkioWeight = blkioWeight + } + + if cpusetmems := n.Labels[labels.CPUSetMems]; cpusetmems != "" { + c.HostConfig.CpusetMems = cpusetmems + } + + if cpusetcpus := n.Labels[labels.CPUSetCPUs]; cpusetcpus != "" { + c.HostConfig.CpusetCpus = cpusetcpus + } + cs := new(ContainerState) cs.Restarting = n.Labels[restart.StatusLabel] == string(containerd.Running) cs.Error = n.Labels[labels.Error] diff --git a/pkg/labels/labels.go b/pkg/labels/labels.go index 620108afb8e..703159e259b 100644 --- a/pkg/labels/labels.go +++ b/pkg/labels/labels.go @@ -101,9 +101,18 @@ const ( // (like "nerdctl/default-network=true" or "nerdctl/default-network=false") NerdctlDefaultNetwork = Prefix + "default-network" - // LogConfig defines the loggin configuration passed to the container + // LogConfig defines the logging configuration passed to the container LogConfig = Prefix + "log-config" // ContainerAutoRemove is to check whether the --rm option is specified. ContainerAutoRemove = Prefix + "auto-remove" + + // BlkioWeight to check if the --blkio-weight is specified + BlkioWeight = Prefix + "blkio-weight" + + // CPUSetCPUs to check if the --cpuset-cpus is specified + CPUSetCPUs = Prefix + "cpuset-cpus" + + // CPUSetMems to check if the --cpuset-mems is specified + CPUSetMems = Prefix + "cpuset-mems" ) From 44e2c750b60ad3f1240425512ee2ca05d0cde201 Mon Sep 17 00:00:00 2001 From: Arjun Raja Yogidas Date: Thu, 16 Jan 2025 23:08:09 +0000 Subject: [PATCH 03/16] chore: add CIdFile and GroupAdd to nerdctl inspect response Signed-off-by: Arjun Raja Yogidas --- pkg/cmd/container/create.go | 20 ++++++++++ pkg/inspecttypes/dockercompat/dockercompat.go | 40 ++++++++++++++++--- pkg/labels/labels.go | 6 +++ 3 files changed, 60 insertions(+), 6 deletions(-) diff --git a/pkg/cmd/container/create.go b/pkg/cmd/container/create.go index f74d0da128a..4adb745c18d 100644 --- a/pkg/cmd/container/create.go +++ b/pkg/cmd/container/create.go @@ -99,6 +99,7 @@ func Create(ctx context.Context, client *containerd.Client, args []string, netMa if err := writeCIDFile(options.CidFile, id); err != nil { return nil, nil, err } + internalLabels.cidFile = options.CidFile } dataStore, err := clientutil.DataStore(options.GOptions.DataRoot, options.GOptions.Address) if err != nil { @@ -269,6 +270,7 @@ func Create(ctx context.Context, client *containerd.Client, args []string, netMa } opts = append(opts, uOpts...) gOpts, err := generateGroupsOpts(options.GroupAdd) + internalLabels.groupAdd = options.GroupAdd if err != nil { return nil, generateRemoveOrphanedDirsFunc(ctx, id, dataStore, internalLabels), err } @@ -651,6 +653,12 @@ type internalLabels struct { // a label to check whether the --rm option is specified. rm string logConfig logging.LogConfig + + // a label to chek if --cidfile is set + cidFile string + + // label to check if --group-add is set + groupAdd []string } // WithInternalLabels sets the internal labels for a container. @@ -749,6 +757,18 @@ func withInternalLabels(internalLabels internalLabels) (containerd.NewContainerO m[labels.CPUSetCPUs] = internalLabels.cpusetCpus } + if internalLabels.cidFile != "" { + m[labels.CIdFile] = internalLabels.cidFile + } + + if len(internalLabels.groupAdd) > 0 { + groupAddJSON, err := json.Marshal(internalLabels.groupAdd) + if err != nil { + return nil, err + } + m[labels.GroupAdd] = string(groupAddJSON) + } + return containerd.WithAdditionalContainerLabels(m), nil } diff --git a/pkg/inspecttypes/dockercompat/dockercompat.go b/pkg/inspecttypes/dockercompat/dockercompat.go index 82b50b6fa50..1a255c0c2a6 100644 --- a/pkg/inspecttypes/dockercompat/dockercompat.go +++ b/pkg/inspecttypes/dockercompat/dockercompat.go @@ -45,6 +45,7 @@ import ( "github.com/containerd/nerdctl/v2/pkg/imgutil" "github.com/containerd/nerdctl/v2/pkg/inspecttypes/native" + "github.com/containerd/nerdctl/v2/pkg/ipcutil" "github.com/containerd/nerdctl/v2/pkg/labels" "github.com/containerd/nerdctl/v2/pkg/ocihook/state" ) @@ -141,12 +142,15 @@ type Container struct { // From https://github.com/moby/moby/blob/8dbd90ec00daa26dc45d7da2431c965dec99e8b4/api/types/container/host_config.go#L391 // HostConfig the non-portable Config structure of a container. type HostConfig struct { - ExtraHosts []string // List of extra hosts - PortBindings nat.PortMap // Port mapping between the exposed port (container) and the host - LogConfig LogConfig // Configuration of the logs for this container - BlkioWeight uint16 // Block IO weight (relative weight vs. other containers) - CpusetMems string // CpusetMems 0-2, 0,1 - CpusetCpus string // CpusetCpus 0-2, 0,1 + ExtraHosts []string // List of extra hosts + PortBindings nat.PortMap // Port mapping between the exposed port (container) and the host + LogConfig LogConfig // Configuration of the logs for this container + BlkioWeight uint16 // Block IO weight (relative weight vs. other containers) + CpusetMems string // CpusetMems 0-2, 0,1 + CpusetCpus string // CpusetCpus 0-2, 0,1 + ContainerIDFile string // File (path) where the containerId is written + GroupAdd []string // GroupAdd specifies additional groups to join + IpcMode string // IPC namespace to use for the container } // From https://github.com/moby/moby/blob/v20.10.1/api/types/types.go#L416-L427 @@ -347,6 +351,22 @@ func ContainerFromNative(n *native.Container) (*Container, error) { c.HostConfig.CpusetCpus = cpusetcpus } + if cidFile := n.Labels[labels.CIdFile]; cidFile != "" { + c.HostConfig.ContainerIDFile = cidFile + } + + if groupAdd := n.Labels[labels.GroupAdd]; groupAdd != "" { + c.HostConfig.GroupAdd = parseGroups(groupAdd) + } + + if ipcMode := n.Labels[labels.IPC]; ipcMode != "" { + ipc, err := ipcutil.DecodeIPCLabel(ipcMode) + if err != nil { + return nil, fmt.Errorf("failed to Decode IPC Label: %v", err) + } + c.HostConfig.IpcMode = string(ipc.Mode) + } + cs := new(ContainerState) cs.Restarting = n.Labels[restart.StatusLabel] == string(containerd.Running) cs.Error = n.Labels[labels.Error] @@ -566,6 +586,14 @@ func parseExtraHosts(extraHostsJSON string) []string { return extraHosts } +func parseGroups(groupAddJSON string) []string { + var groupAdd []string + if err := json.Unmarshal([]byte(groupAddJSON), &groupAdd); err != nil { + return []string{} + } + return groupAdd +} + type IPAMConfig struct { Subnet string `json:"Subnet,omitempty"` Gateway string `json:"Gateway,omitempty"` diff --git a/pkg/labels/labels.go b/pkg/labels/labels.go index 703159e259b..2a3636cf659 100644 --- a/pkg/labels/labels.go +++ b/pkg/labels/labels.go @@ -115,4 +115,10 @@ const ( // CPUSetMems to check if the --cpuset-mems is specified CPUSetMems = Prefix + "cpuset-mems" + + // Cidfile is the ContainerId file set via the --cidfile flag + CIdFile = Prefix + "cid-file" + + // GroupAdd is the List of additional groups, set via --group-add flag + GroupAdd = Prefix + "group-add" ) From fbd4fa112e0b8a862b199834b0f69b5adce7dea0 Mon Sep 17 00:00:00 2001 From: Arjun Raja Yogidas Date: Fri, 17 Jan 2025 07:54:43 +0000 Subject: [PATCH 04/16] chore: use native inspect instead of labels Signed-off-by: Arjun Raja Yogidas --- pkg/cmd/container/create.go | 18 ----- pkg/inspecttypes/dockercompat/dockercompat.go | 81 ++++++++++++++----- pkg/labels/labels.go | 9 --- 3 files changed, 61 insertions(+), 47 deletions(-) diff --git a/pkg/cmd/container/create.go b/pkg/cmd/container/create.go index 4adb745c18d..fa5e5c391d5 100644 --- a/pkg/cmd/container/create.go +++ b/pkg/cmd/container/create.go @@ -328,8 +328,6 @@ func Create(ctx context.Context, client *containerd.Client, args []string, netMa internalLabels.rm = containerutil.EncodeContainerRmOptLabel(options.Rm) - internalLabels.cpusetCpus = options.CPUSetCPUs - internalLabels.cpusetMems = options.CPUSetMems internalLabels.blkioWeight = options.BlkioWeight // TODO: abolish internal labels and only use annotations @@ -749,26 +747,10 @@ func withInternalLabels(internalLabels internalLabels) (containerd.NewContainerO m[labels.BlkioWeight] = fmt.Sprintf("%d", internalLabels.blkioWeight) } - if internalLabels.cpusetMems != "" { - m[labels.CPUSetMems] = internalLabels.cpusetMems - } - - if internalLabels.cpusetCpus != "" { - m[labels.CPUSetCPUs] = internalLabels.cpusetCpus - } - if internalLabels.cidFile != "" { m[labels.CIdFile] = internalLabels.cidFile } - if len(internalLabels.groupAdd) > 0 { - groupAddJSON, err := json.Marshal(internalLabels.groupAdd) - if err != nil { - return nil, err - } - m[labels.GroupAdd] = string(groupAddJSON) - } - return containerd.WithAdditionalContainerLabels(m), nil } diff --git a/pkg/inspecttypes/dockercompat/dockercompat.go b/pkg/inspecttypes/dockercompat/dockercompat.go index 1a255c0c2a6..2071f333028 100644 --- a/pkg/inspecttypes/dockercompat/dockercompat.go +++ b/pkg/inspecttypes/dockercompat/dockercompat.go @@ -146,8 +146,10 @@ type HostConfig struct { PortBindings nat.PortMap // Port mapping between the exposed port (container) and the host LogConfig LogConfig // Configuration of the logs for this container BlkioWeight uint16 // Block IO weight (relative weight vs. other containers) - CpusetMems string // CpusetMems 0-2, 0,1 - CpusetCpus string // CpusetCpus 0-2, 0,1 + CPUSetMems string `json:"CpusetMems"` // CpusetMems 0-2, 0,1 + CPUSetCPUs string `json:"CpusetCpus"` // CpusetCpus 0-2, 0,1 + CPUQuota int64 `json:"CpuQuota"` // CPU CFS (Completely Fair Scheduler) quota + CPUShares uint64 `json:"CpuShares"` // CPU shares (relative weight vs. other containers) ContainerIDFile string // File (path) where the containerId is written GroupAdd []string // GroupAdd specifies additional groups to join IpcMode string // IPC namespace to use for the container @@ -218,6 +220,13 @@ type NetworkSettings struct { Networks map[string]*NetworkEndpointSettings } +type CPUSettings struct { + cpuSetCpus string + cpuSetMems string + cpuShares uint64 + cpuQuota int64 +} + // DefaultNetworkSettings is from https://github.com/moby/moby/blob/v20.10.1/api/types/types.go#L405-L414 type DefaultNetworkSettings struct { // TODO EndpointID string // EndpointID uniquely represents a service endpoint in a Sandbox @@ -343,22 +352,17 @@ func ContainerFromNative(n *native.Container) (*Container, error) { c.HostConfig.BlkioWeight = blkioWeight } - if cpusetmems := n.Labels[labels.CPUSetMems]; cpusetmems != "" { - c.HostConfig.CpusetMems = cpusetmems - } - - if cpusetcpus := n.Labels[labels.CPUSetCPUs]; cpusetcpus != "" { - c.HostConfig.CpusetCpus = cpusetcpus - } - if cidFile := n.Labels[labels.CIdFile]; cidFile != "" { c.HostConfig.ContainerIDFile = cidFile } - if groupAdd := n.Labels[labels.GroupAdd]; groupAdd != "" { - c.HostConfig.GroupAdd = parseGroups(groupAdd) + groupAdd, err := groupAddFromNative(n.Spec.(*specs.Spec)) + if err != nil { + return nil, fmt.Errorf("failed to groupAdd from native spec: %v", err) } + c.HostConfig.GroupAdd = groupAdd + if ipcMode := n.Labels[labels.IPC]; ipcMode != "" { ipc, err := ipcutil.DecodeIPCLabel(ipcMode) if err != nil { @@ -395,6 +399,16 @@ func ContainerFromNative(n *native.Container) (*Container, error) { c.NetworkSettings = nSettings c.HostConfig.PortBindings = *nSettings.Ports } + + cpuSetting, err := cpuSettingsFromNative(n.Spec.(*specs.Spec)) + if err != nil { + return nil, fmt.Errorf("failed to Decode cpuSettings: %v", err) + } + c.HostConfig.CPUSetCPUs = cpuSetting.cpuSetCpus + c.HostConfig.CPUSetMems = cpuSetting.cpuSetMems + c.HostConfig.CPUQuota = cpuSetting.cpuQuota + c.HostConfig.CPUShares = cpuSetting.cpuShares + c.State = cs c.Config = &Config{ Labels: n.Labels, @@ -559,6 +573,41 @@ func networkSettingsFromNative(n *native.NetNS, sp *specs.Spec) (*NetworkSetting return res, nil } +func cpuSettingsFromNative(sp *specs.Spec) (*CPUSettings, error) { + res := &CPUSettings{} + if sp.Linux != nil && sp.Linux.Resources != nil && sp.Linux.Resources.CPU != nil { + if sp.Linux.Resources.CPU.Cpus != "" { + res.cpuSetCpus = sp.Linux.Resources.CPU.Cpus + } + + if sp.Linux.Resources.CPU.Mems != "" { + res.cpuSetMems = sp.Linux.Resources.CPU.Mems + } + + if sp.Linux.Resources.CPU.Shares != nil && *sp.Linux.Resources.CPU.Shares > 0 { + res.cpuShares = *sp.Linux.Resources.CPU.Shares + } + + if sp.Linux.Resources.CPU.Quota != nil && *sp.Linux.Resources.CPU.Quota > 0 { + res.cpuQuota = *sp.Linux.Resources.CPU.Quota + } + } + + return res, nil +} + +func groupAddFromNative(sp *specs.Spec) ([]string, error) { + res := []string{} + if sp.Process != nil && sp.Process.User.AdditionalGids != nil { + for _, gid := range sp.Process.User.AdditionalGids { + if gid != 0 { + res = append(res, strconv.FormatUint(uint64(gid), 10)) + } + } + } + return res, nil +} + func convertToNatPort(portMappings []cni.PortMapping) (*nat.PortMap, error) { portMap := make(nat.PortMap) for _, portMapping := range portMappings { @@ -586,14 +635,6 @@ func parseExtraHosts(extraHostsJSON string) []string { return extraHosts } -func parseGroups(groupAddJSON string) []string { - var groupAdd []string - if err := json.Unmarshal([]byte(groupAddJSON), &groupAdd); err != nil { - return []string{} - } - return groupAdd -} - type IPAMConfig struct { Subnet string `json:"Subnet,omitempty"` Gateway string `json:"Gateway,omitempty"` diff --git a/pkg/labels/labels.go b/pkg/labels/labels.go index 2a3636cf659..4e3f667c51a 100644 --- a/pkg/labels/labels.go +++ b/pkg/labels/labels.go @@ -110,15 +110,6 @@ const ( // BlkioWeight to check if the --blkio-weight is specified BlkioWeight = Prefix + "blkio-weight" - // CPUSetCPUs to check if the --cpuset-cpus is specified - CPUSetCPUs = Prefix + "cpuset-cpus" - - // CPUSetMems to check if the --cpuset-mems is specified - CPUSetMems = Prefix + "cpuset-mems" - // Cidfile is the ContainerId file set via the --cidfile flag CIdFile = Prefix + "cid-file" - - // GroupAdd is the List of additional groups, set via --group-add flag - GroupAdd = Prefix + "group-add" ) From 0395ab7ec2db8675174e3fad680b5d900d345f9c Mon Sep 17 00:00:00 2001 From: Arjun Raja Yogidas Date: Fri, 17 Jan 2025 23:21:21 +0000 Subject: [PATCH 05/16] chore: add tests Signed-off-by: Arjun Raja Yogidas --- .../container/container_inspect_linux_test.go | 57 +++++++++++++++++++ .../dockercompat/dockercompat_test.go | 30 ++++++++++ 2 files changed, 87 insertions(+) diff --git a/cmd/nerdctl/container/container_inspect_linux_test.go b/cmd/nerdctl/container/container_inspect_linux_test.go index 2fadc2b5048..98efbef401d 100644 --- a/cmd/nerdctl/container/container_inspect_linux_test.go +++ b/cmd/nerdctl/container/container_inspect_linux_test.go @@ -229,3 +229,60 @@ func TestContainerInspectState(t *testing.T) { } } + +func TestContainerInspectHostConfig(t *testing.T) { + testContainer := testutil.Identifier(t) + + base := testutil.NewBase(t) + defer base.Cmd("rm", "-f", testContainer).Run() + + // Run a container with various HostConfig options + base.Cmd("run", "-d", "--name", testContainer, + "--cpuset-cpus", "0-1", + "--cpuset-mems", "0", + "--blkio-weight", "500", + "--cpu-shares", "1024", + "--cpu-quota", "100000", + "--group-add", "1000", + "--group-add", "2000", + "--add-host", "host1:10.0.0.1", + "--add-host", "host2:10.0.0.2", + "--ipc", "host", + testutil.AlpineImage, "sleep", "infinity").AssertOK() + + inspect := base.InspectContainer(testContainer) + + assert.Equal(t, "0-1", inspect.HostConfig.CPUSetCPUs) + assert.Equal(t, "0", inspect.HostConfig.CPUSetMems) + assert.Equal(t, uint16(500), inspect.HostConfig.BlkioWeight) + assert.Equal(t, uint64(1024), inspect.HostConfig.CPUShares) + assert.Equal(t, int64(100000), inspect.HostConfig.CPUQuota) + assert.DeepEqual(t, []string{"1000", "2000"}, inspect.HostConfig.GroupAdd) + expectedExtraHosts := []string{"host1:10.0.0.1", "host2:10.0.0.2"} + assert.DeepEqual(t, expectedExtraHosts, inspect.HostConfig.ExtraHosts) + assert.Equal(t, "host", inspect.HostConfig.IpcMode) + assert.Equal(t, "json-file", inspect.HostConfig.LogConfig.Type) + assert.Equal(t, "json-file", inspect.HostConfig.LogConfig.Config.Driver) +} + +func TestContainerInspectHostConfigDefaults(t *testing.T) { + testContainer := testutil.Identifier(t) + + base := testutil.NewBase(t) + defer base.Cmd("rm", "-f", testContainer).Run() + + // Run a container without specifying HostConfig options + base.Cmd("run", "-d", "--name", testContainer, testutil.AlpineImage, "sleep", "infinity").AssertOK() + + inspect := base.InspectContainer(testContainer) + assert.Equal(t, "", inspect.HostConfig.CPUSetCPUs) + assert.Equal(t, "", inspect.HostConfig.CPUSetMems) + assert.Equal(t, uint16(0), inspect.HostConfig.BlkioWeight) + assert.Equal(t, uint64(0), inspect.HostConfig.CPUShares) + assert.Equal(t, int64(0), inspect.HostConfig.CPUQuota) + assert.Equal(t, 0, len(inspect.HostConfig.GroupAdd)) + assert.Equal(t, 0, len(inspect.HostConfig.ExtraHosts)) + assert.Equal(t, "", inspect.HostConfig.IpcMode) + assert.Equal(t, "json-file", inspect.HostConfig.LogConfig.Type) + assert.Equal(t, "json-file", inspect.HostConfig.LogConfig.Config.Driver) +} diff --git a/pkg/inspecttypes/dockercompat/dockercompat_test.go b/pkg/inspecttypes/dockercompat/dockercompat_test.go index 12814bc31fc..e60e8b45d4a 100644 --- a/pkg/inspecttypes/dockercompat/dockercompat_test.go +++ b/pkg/inspecttypes/dockercompat/dockercompat_test.go @@ -75,6 +75,16 @@ func TestContainerFromNative(t *testing.T) { Pid: 10000, FinishedAt: "", }, + HostConfig: &HostConfig{ + PortBindings: nat.PortMap{}, + GroupAdd: []string{}, + LogConfig: LogConfig{ + Config: loggerLogConfig{ + Driver: "json-file", + Opts: map[string]string{}, + }, + }, + }, Mounts: []MountPoint{ { Type: "bind", @@ -150,6 +160,16 @@ func TestContainerFromNative(t *testing.T) { Pid: 10000, FinishedAt: "", }, + HostConfig: &HostConfig{ + PortBindings: nat.PortMap{}, + GroupAdd: []string{}, + LogConfig: LogConfig{ + Config: loggerLogConfig{ + Driver: "json-file", + Opts: map[string]string{}, + }, + }, + }, Mounts: []MountPoint{ { Type: "bind", @@ -222,6 +242,16 @@ func TestContainerFromNative(t *testing.T) { Pid: 10000, FinishedAt: "", }, + HostConfig: &HostConfig{ + PortBindings: nat.PortMap{}, + GroupAdd: []string{}, + LogConfig: LogConfig{ + Config: loggerLogConfig{ + Driver: "json-file", + Opts: map[string]string{}, + }, + }, + }, Mounts: []MountPoint{ { Type: "bind", From bf18a4add4f764ec4d2e7e39cb3f49456d98adac Mon Sep 17 00:00:00 2001 From: Arjun Raja Yogidas Date: Sun, 19 Jan 2025 18:51:49 +0000 Subject: [PATCH 06/16] chore: add Memory and CgroupNsMode Signed-off-by: Arjun Raja Yogidas --- .../container/container_inspect_linux_test.go | 8 +++ pkg/inspecttypes/dockercompat/dockercompat.go | 54 +++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/cmd/nerdctl/container/container_inspect_linux_test.go b/cmd/nerdctl/container/container_inspect_linux_test.go index 98efbef401d..b9f398bd782 100644 --- a/cmd/nerdctl/container/container_inspect_linux_test.go +++ b/cmd/nerdctl/container/container_inspect_linux_test.go @@ -248,6 +248,8 @@ func TestContainerInspectHostConfig(t *testing.T) { "--add-host", "host1:10.0.0.1", "--add-host", "host2:10.0.0.2", "--ipc", "host", + "--memory", "512m", + "--oom-kill-disable", testutil.AlpineImage, "sleep", "infinity").AssertOK() inspect := base.InspectContainer(testContainer) @@ -263,6 +265,9 @@ func TestContainerInspectHostConfig(t *testing.T) { assert.Equal(t, "host", inspect.HostConfig.IpcMode) assert.Equal(t, "json-file", inspect.HostConfig.LogConfig.Type) assert.Equal(t, "json-file", inspect.HostConfig.LogConfig.Config.Driver) + assert.Equal(t, int64(536870912), inspect.HostConfig.Memory) + assert.Equal(t, int64(1073741824), inspect.HostConfig.MemorySwap) + assert.Equal(t, bool(true), inspect.HostConfig.OomKillDisable) } func TestContainerInspectHostConfigDefaults(t *testing.T) { @@ -285,4 +290,7 @@ func TestContainerInspectHostConfigDefaults(t *testing.T) { assert.Equal(t, "", inspect.HostConfig.IpcMode) assert.Equal(t, "json-file", inspect.HostConfig.LogConfig.Type) assert.Equal(t, "json-file", inspect.HostConfig.LogConfig.Config.Driver) + assert.Equal(t, int64(0), inspect.HostConfig.Memory) + assert.Equal(t, int64(0), inspect.HostConfig.MemorySwap) + assert.Equal(t, bool(false), inspect.HostConfig.OomKillDisable) } diff --git a/pkg/inspecttypes/dockercompat/dockercompat.go b/pkg/inspecttypes/dockercompat/dockercompat.go index 2071f333028..7dbb386a35d 100644 --- a/pkg/inspecttypes/dockercompat/dockercompat.go +++ b/pkg/inspecttypes/dockercompat/dockercompat.go @@ -153,6 +153,10 @@ type HostConfig struct { ContainerIDFile string // File (path) where the containerId is written GroupAdd []string // GroupAdd specifies additional groups to join IpcMode string // IPC namespace to use for the container + CgroupnsMode string // Cgroup namespace mode to use for the container + Memory int64 // Memory limit (in bytes) + MemorySwap int64 // Total memory usage (memory + swap); set `-1` to enable unlimited swap + OomKillDisable bool // specifies whether to disable OOM Killer } // From https://github.com/moby/moby/blob/v20.10.1/api/types/types.go#L416-L427 @@ -409,6 +413,20 @@ func ContainerFromNative(n *native.Container) (*Container, error) { c.HostConfig.CPUQuota = cpuSetting.cpuQuota c.HostConfig.CPUShares = cpuSetting.cpuShares + cgroupNamespace, err := getCgroupnsFromNative(n.Spec.(*specs.Spec)) + if err != nil { + return nil, fmt.Errorf("failed to Decode cgroupNamespace: %v", err) + } + c.HostConfig.CgroupnsMode = cgroupNamespace + + memorySettings, err := getMemorySettingsFromNative(n.Spec.(*specs.Spec)) + if err != nil { + return nil, fmt.Errorf("failed to Decode memory Settings: %v", err) + } + + c.HostConfig.OomKillDisable = memorySettings.DisableOOMKiller + c.HostConfig.Memory = memorySettings.Limit + c.HostConfig.MemorySwap = memorySettings.Swap c.State = cs c.Config = &Config{ Labels: n.Labels, @@ -596,6 +614,18 @@ func cpuSettingsFromNative(sp *specs.Spec) (*CPUSettings, error) { return res, nil } +func getCgroupnsFromNative(sp *specs.Spec) (string, error) { + res := "" + if sp.Linux != nil && len(sp.Linux.Namespaces) != 0 { + for _, ns := range sp.Linux.Namespaces { + if ns.Type == "cgroup" { + res = "private" + } + } + } + return res, nil +} + func groupAddFromNative(sp *specs.Spec) ([]string, error) { res := []string{} if sp.Process != nil && sp.Process.User.AdditionalGids != nil { @@ -635,6 +665,24 @@ func parseExtraHosts(extraHostsJSON string) []string { return extraHosts } +func getMemorySettingsFromNative(sp *specs.Spec) (*MemorySetting, error) { + res := &MemorySetting{} + if sp.Linux != nil && sp.Linux.Resources != nil && sp.Linux.Resources.Memory != nil { + if sp.Linux.Resources.Memory.DisableOOMKiller != nil { + res.DisableOOMKiller = *sp.Linux.Resources.Memory.DisableOOMKiller + } + + if sp.Linux.Resources.Memory.Limit != nil { + res.Limit = *sp.Linux.Resources.Memory.Limit + } + + if sp.Linux.Resources.Memory.Swap != nil { + res.Swap = *sp.Linux.Resources.Memory.Swap + } + } + return res, nil +} + type IPAMConfig struct { Subnet string `json:"Subnet,omitempty"` Gateway string `json:"Gateway,omitempty"` @@ -665,6 +713,12 @@ type structuredCNI struct { } `json:"plugins"` } +type MemorySetting struct { + Limit int64 `json:"limit"` + Swap int64 `json:"swap"` + DisableOOMKiller bool `json:"disableOOMKiller"` +} + func NetworkFromNative(n *native.Network) (*Network, error) { var res Network From 70b8975874612aa31553cd43a23802166646d824 Mon Sep 17 00:00:00 2001 From: Arjun Raja Yogidas Date: Mon, 20 Jan 2025 03:53:54 +0000 Subject: [PATCH 07/16] chore: add dns config to inspect response Signed-off-by: Arjun Raja Yogidas --- .../container/container_inspect_linux_test.go | 48 +++++++++++++++++++ pkg/cmd/container/create.go | 42 +++++++++++++--- pkg/inspecttypes/dockercompat/dockercompat.go | 43 +++++++++++++++++ pkg/labels/labels.go | 9 ++++ 4 files changed, 135 insertions(+), 7 deletions(-) diff --git a/cmd/nerdctl/container/container_inspect_linux_test.go b/cmd/nerdctl/container/container_inspect_linux_test.go index b9f398bd782..a3e8dffad88 100644 --- a/cmd/nerdctl/container/container_inspect_linux_test.go +++ b/cmd/nerdctl/container/container_inspect_linux_test.go @@ -294,3 +294,51 @@ func TestContainerInspectHostConfigDefaults(t *testing.T) { assert.Equal(t, int64(0), inspect.HostConfig.MemorySwap) assert.Equal(t, bool(false), inspect.HostConfig.OomKillDisable) } + +func TestContainerInspectHostConfigDNS(t *testing.T) { + testContainer := testutil.Identifier(t) + + base := testutil.NewBase(t) + defer base.Cmd("rm", "-f", testContainer).Run() + + // Run a container with DNS options + base.Cmd("run", "-d", "--name", testContainer, + "--dns", "8.8.8.8", + "--dns", "1.1.1.1", + "--dns-search", "example.com", + "--dns-search", "test.local", + "--dns-option", "ndots:5", + "--dns-option", "timeout:3", + testutil.AlpineImage, "sleep", "infinity").AssertOK() + + inspect := base.InspectContainer(testContainer) + + // Check DNS servers + expectedDNSServers := []string{"8.8.8.8", "1.1.1.1"} + assert.DeepEqual(t, expectedDNSServers, inspect.HostConfig.DNS) + + // Check DNS search domains + expectedDNSSearch := []string{"example.com", "test.local"} + assert.DeepEqual(t, expectedDNSSearch, inspect.HostConfig.DNSSearch) + + // Check DNS options + expectedDNSOptions := []string{"ndots:5", "timeout:3"} + assert.DeepEqual(t, expectedDNSOptions, inspect.HostConfig.DNSOptions) +} + +func TestContainerInspectHostConfigDNSDefaults(t *testing.T) { + testContainer := testutil.Identifier(t) + + base := testutil.NewBase(t) + defer base.Cmd("rm", "-f", testContainer).Run() + + // Run a container without specifying DNS options + base.Cmd("run", "-d", "--name", testContainer, testutil.AlpineImage, "sleep", "infinity").AssertOK() + + inspect := base.InspectContainer(testContainer) + + // Check that DNS settings are empty by default + assert.Equal(t, 0, len(inspect.HostConfig.DNS)) + assert.Equal(t, 0, len(inspect.HostConfig.DNSSearch)) + assert.Equal(t, 0, len(inspect.HostConfig.DNSOptions)) +} diff --git a/pkg/cmd/container/create.go b/pkg/cmd/container/create.go index fa5e5c391d5..bd0cc29f6cc 100644 --- a/pkg/cmd/container/create.go +++ b/pkg/cmd/container/create.go @@ -626,19 +626,20 @@ type internalLabels struct { extraHosts []string pidFile string blkioWeight uint16 - cpusetCpus string - cpusetMems string // labels from cmd options or automatically set name string hostname string // automatically generated stateDir string // network - networks []string - ipAddress string - ip6Address string - ports []cni.PortMapping - macAddress string + networks []string + ipAddress string + ip6Address string + ports []cni.PortMapping + macAddress string + dnsServers []string + dnsSearchDomains []string + dnsResolvConfOptions []string // volume mountPoints []*mountutil.Processed anonVolumes []string @@ -751,6 +752,30 @@ func withInternalLabels(internalLabels internalLabels) (containerd.NewContainerO m[labels.CIdFile] = internalLabels.cidFile } + if len(internalLabels.dnsServers) > 0 { + dnsServersJSON, err := json.Marshal(internalLabels.dnsServers) + if err != nil { + return nil, err + } + m[labels.DnsServer] = string(dnsServersJSON) + } + + if len(internalLabels.dnsSearchDomains) > 0 { + dnsSearchJSON, err := json.Marshal(internalLabels.dnsSearchDomains) + if err != nil { + return nil, err + } + m[labels.DNSSearchDomains] = string(dnsSearchJSON) + } + + if len(internalLabels.dnsResolvConfOptions) > 0 { + dnsResolvConfOptionsJSON, err := json.Marshal(internalLabels.dnsResolvConfOptions) + if err != nil { + return nil, err + } + m[labels.DNSResolvConfOptions] = string(dnsResolvConfOptionsJSON) + } + return containerd.WithAdditionalContainerLabels(m), nil } @@ -762,6 +787,9 @@ func (il *internalLabels) loadNetOpts(opts types.NetworkOptions) { il.ip6Address = opts.IP6Address il.networks = opts.NetworkSlice il.macAddress = opts.MACAddress + il.dnsServers = opts.DNSServers + il.dnsSearchDomains = opts.DNSSearchDomains + il.dnsResolvConfOptions = opts.DNSResolvConfOptions } func dockercompatMounts(mountPoints []*mountutil.Processed) []dockercompat.MountPoint { diff --git a/pkg/inspecttypes/dockercompat/dockercompat.go b/pkg/inspecttypes/dockercompat/dockercompat.go index 7dbb386a35d..b6fd5e26671 100644 --- a/pkg/inspecttypes/dockercompat/dockercompat.go +++ b/pkg/inspecttypes/dockercompat/dockercompat.go @@ -157,6 +157,9 @@ type HostConfig struct { Memory int64 // Memory limit (in bytes) MemorySwap int64 // Total memory usage (memory + swap); set `-1` to enable unlimited swap OomKillDisable bool // specifies whether to disable OOM Killer + DNS []string `json:"Dns"` // List of DNS server to lookup + DNSOptions []string `json:"DnsOptions"` // List of DNSOption to look for + DNSSearch []string `json:"DnsSearch"` // List of DNSSearch to look for } // From https://github.com/moby/moby/blob/v20.10.1/api/types/types.go#L416-L427 @@ -224,6 +227,12 @@ type NetworkSettings struct { Networks map[string]*NetworkEndpointSettings } +type DNSSettings struct { + DNSServers []string + DNSResolvConfOptions []string + DNSSearchDomains []string +} + type CPUSettings struct { cpuSetCpus string cpuSetMems string @@ -427,6 +436,16 @@ func ContainerFromNative(n *native.Container) (*Container, error) { c.HostConfig.OomKillDisable = memorySettings.DisableOOMKiller c.HostConfig.Memory = memorySettings.Limit c.HostConfig.MemorySwap = memorySettings.Swap + + dnsSettings, err := getDnsFromNative(n.Labels) + if err != nil { + return nil, fmt.Errorf("failed to Decode dns Settings: %v", err) + } + + c.HostConfig.DNS = dnsSettings.DNSServers + c.HostConfig.DNSOptions = dnsSettings.DNSResolvConfOptions + c.HostConfig.DNSSearch = dnsSettings.DNSSearchDomains + c.State = cs c.Config = &Config{ Labels: n.Labels, @@ -683,6 +702,30 @@ func getMemorySettingsFromNative(sp *specs.Spec) (*MemorySetting, error) { return res, nil } +func getDnsFromNative(Labels map[string]string) (*DNSSettings, error) { + res := &DNSSettings{} + + if dnsServers := Labels[labels.DnsServer]; dnsServers != "" { + if err := json.Unmarshal([]byte(dnsServers), &res.DNSServers); err != nil { + return nil, fmt.Errorf("failed to parse DNS servers: %v", err) + } + } + + if dnsOptions := Labels[labels.DNSResolvConfOptions]; dnsOptions != "" { + if err := json.Unmarshal([]byte(dnsOptions), &res.DNSResolvConfOptions); err != nil { + return nil, fmt.Errorf("failed to parse DNS options: %v", err) + } + } + + if dnsSearch := Labels[labels.DNSSearchDomains]; dnsSearch != "" { + if err := json.Unmarshal([]byte(dnsSearch), &res.DNSSearchDomains); err != nil { + return nil, fmt.Errorf("failed to parse DNS search domains: %v", err) + } + } + + return res, nil +} + type IPAMConfig struct { Subnet string `json:"Subnet,omitempty"` Gateway string `json:"Gateway,omitempty"` diff --git a/pkg/labels/labels.go b/pkg/labels/labels.go index 4e3f667c51a..1229c16470e 100644 --- a/pkg/labels/labels.go +++ b/pkg/labels/labels.go @@ -112,4 +112,13 @@ const ( // Cidfile is the ContainerId file set via the --cidfile flag CIdFile = Prefix + "cid-file" + + // Custom DNS lookup servers. + DnsServer = Prefix + "dns" + + // DNSResolvConfOptions set DNS options + DNSResolvConfOptions = Prefix + "dns-options" + + // DNSSearchDomains set custom DNS search domains + DNSSearchDomains = Prefix + "dns-search" ) From 48d15e1f662c97ae335de3b0237ba5d7f13a9818 Mon Sep 17 00:00:00 2001 From: Arjun Raja Yogidas Date: Mon, 20 Jan 2025 04:31:19 +0000 Subject: [PATCH 08/16] chore: add oomScoreAdj to inspect response Signed-off-by: Arjun Raja Yogidas --- pkg/inspecttypes/dockercompat/dockercompat.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pkg/inspecttypes/dockercompat/dockercompat.go b/pkg/inspecttypes/dockercompat/dockercompat.go index b6fd5e26671..22afcc02a83 100644 --- a/pkg/inspecttypes/dockercompat/dockercompat.go +++ b/pkg/inspecttypes/dockercompat/dockercompat.go @@ -160,6 +160,7 @@ type HostConfig struct { DNS []string `json:"Dns"` // List of DNS server to lookup DNSOptions []string `json:"DnsOptions"` // List of DNSOption to look for DNSSearch []string `json:"DnsSearch"` // List of DNSSearch to look for + OomScoreAdj int // specifies the tune container’s OOM preferences (-1000 to 1000, rootless: 100 to 1000) } // From https://github.com/moby/moby/blob/v20.10.1/api/types/types.go#L416-L427 @@ -446,6 +447,9 @@ func ContainerFromNative(n *native.Container) (*Container, error) { c.HostConfig.DNSOptions = dnsSettings.DNSResolvConfOptions c.HostConfig.DNSSearch = dnsSettings.DNSSearchDomains + oomScoreAdj, _ := getOomScoreAdjFromNative(n.Spec.(*specs.Spec)) + c.HostConfig.OomScoreAdj = oomScoreAdj + c.State = cs c.Config = &Config{ Labels: n.Labels, @@ -726,6 +730,14 @@ func getDnsFromNative(Labels map[string]string) (*DNSSettings, error) { return res, nil } +func getOomScoreAdjFromNative(sp *specs.Spec) (int, error) { + var res int + if sp.Process != nil && sp.Process.OOMScoreAdj != nil { + res = *sp.Process.OOMScoreAdj + } + return res, nil +} + type IPAMConfig struct { Subnet string `json:"Subnet,omitempty"` Gateway string `json:"Gateway,omitempty"` From 6dbf0c5b6f55d405728d182020db781d684bbc4a Mon Sep 17 00:00:00 2001 From: Arjun Raja Yogidas Date: Mon, 20 Jan 2025 20:11:16 +0000 Subject: [PATCH 09/16] chore: add ReadonlyRootfs,UTSMode,ShmSize to inspect response Signed-off-by: Arjun Raja Yogidas --- .../container/container_inspect_linux_test.go | 9 ++++ pkg/inspecttypes/dockercompat/dockercompat.go | 49 +++++++++++++++++++ .../dockercompat/dockercompat_test.go | 3 ++ 3 files changed, 61 insertions(+) diff --git a/cmd/nerdctl/container/container_inspect_linux_test.go b/cmd/nerdctl/container/container_inspect_linux_test.go index a3e8dffad88..76accb05ba0 100644 --- a/cmd/nerdctl/container/container_inspect_linux_test.go +++ b/cmd/nerdctl/container/container_inspect_linux_test.go @@ -250,6 +250,9 @@ func TestContainerInspectHostConfig(t *testing.T) { "--ipc", "host", "--memory", "512m", "--oom-kill-disable", + "--read-only", + "--uts", "host", + "--shm-size", "256m", testutil.AlpineImage, "sleep", "infinity").AssertOK() inspect := base.InspectContainer(testContainer) @@ -268,6 +271,9 @@ func TestContainerInspectHostConfig(t *testing.T) { assert.Equal(t, int64(536870912), inspect.HostConfig.Memory) assert.Equal(t, int64(1073741824), inspect.HostConfig.MemorySwap) assert.Equal(t, bool(true), inspect.HostConfig.OomKillDisable) + assert.Equal(t, true, inspect.HostConfig.ReadonlyRootfs) + assert.Equal(t, "host", inspect.HostConfig.UTSMode) + assert.Equal(t, int64(268435456), inspect.HostConfig.ShmSize) } func TestContainerInspectHostConfigDefaults(t *testing.T) { @@ -293,6 +299,9 @@ func TestContainerInspectHostConfigDefaults(t *testing.T) { assert.Equal(t, int64(0), inspect.HostConfig.Memory) assert.Equal(t, int64(0), inspect.HostConfig.MemorySwap) assert.Equal(t, bool(false), inspect.HostConfig.OomKillDisable) + assert.Equal(t, false, inspect.HostConfig.ReadonlyRootfs) + assert.Equal(t, "", inspect.HostConfig.UTSMode) + assert.Equal(t, int64(67108864), inspect.HostConfig.ShmSize) } func TestContainerInspectHostConfigDNS(t *testing.T) { diff --git a/pkg/inspecttypes/dockercompat/dockercompat.go b/pkg/inspecttypes/dockercompat/dockercompat.go index 22afcc02a83..5137c5216fe 100644 --- a/pkg/inspecttypes/dockercompat/dockercompat.go +++ b/pkg/inspecttypes/dockercompat/dockercompat.go @@ -36,6 +36,7 @@ import ( "time" "github.com/docker/go-connections/nat" + "github.com/docker/go-units" "github.com/opencontainers/runtime-spec/specs-go" containerd "github.com/containerd/containerd/v2/client" @@ -161,6 +162,10 @@ type HostConfig struct { DNSOptions []string `json:"DnsOptions"` // List of DNSOption to look for DNSSearch []string `json:"DnsSearch"` // List of DNSSearch to look for OomScoreAdj int // specifies the tune container’s OOM preferences (-1000 to 1000, rootless: 100 to 1000) + ReadonlyRootfs bool // Is the container root filesystem in read-only + UTSMode string // UTS namespace to use for the container + ShmSize int64 // Size of /dev/shm in bytes. The size must be greater than 0. + } // From https://github.com/moby/moby/blob/v20.10.1/api/types/types.go#L416-L427 @@ -450,6 +455,17 @@ func ContainerFromNative(n *native.Container) (*Container, error) { oomScoreAdj, _ := getOomScoreAdjFromNative(n.Spec.(*specs.Spec)) c.HostConfig.OomScoreAdj = oomScoreAdj + c.HostConfig.ReadonlyRootfs = false + if n.Spec.(*specs.Spec).Root != nil && n.Spec.(*specs.Spec).Root.Readonly { + c.HostConfig.ReadonlyRootfs = n.Spec.(*specs.Spec).Root.Readonly + } + + utsMode, _ := getUtsModeFromNative(n.Spec.(*specs.Spec)) + c.HostConfig.UTSMode = utsMode + + shmSize, _ := getShmSizeFromNative(n.Spec.(*specs.Spec)) + c.HostConfig.ShmSize = shmSize + c.State = cs c.Config = &Config{ Labels: n.Labels, @@ -738,6 +754,39 @@ func getOomScoreAdjFromNative(sp *specs.Spec) (int, error) { return res, nil } +func getUtsModeFromNative(sp *specs.Spec) (string, error) { + if sp.Linux != nil && len(sp.Linux.Namespaces) > 0 { + for _, ns := range sp.Linux.Namespaces { + if ns.Type == "uts" { + return "", nil + } + } + } + return "host", nil +} + +func getShmSizeFromNative(sp *specs.Spec) (int64, error) { + var res int64 + + if sp.Mounts != nil && len(sp.Mounts) > 0 { + for _, mount := range sp.Mounts { + if mount.Destination == "/dev/shm" { + for _, option := range mount.Options { + if strings.HasPrefix(option, "size=") { + sizeStr := strings.TrimPrefix(option, "size=") + size, err := units.RAMInBytes(sizeStr) + if err != nil { + return 0, fmt.Errorf("failed to parse shm size: %v", err) + } + res = size + } + } + } + } + } + return res, nil +} + type IPAMConfig struct { Subnet string `json:"Subnet,omitempty"` Gateway string `json:"Gateway,omitempty"` diff --git a/pkg/inspecttypes/dockercompat/dockercompat_test.go b/pkg/inspecttypes/dockercompat/dockercompat_test.go index e60e8b45d4a..f35410fac1e 100644 --- a/pkg/inspecttypes/dockercompat/dockercompat_test.go +++ b/pkg/inspecttypes/dockercompat/dockercompat_test.go @@ -84,6 +84,7 @@ func TestContainerFromNative(t *testing.T) { Opts: map[string]string{}, }, }, + UTSMode: "host", }, Mounts: []MountPoint{ { @@ -169,6 +170,7 @@ func TestContainerFromNative(t *testing.T) { Opts: map[string]string{}, }, }, + UTSMode: "host", }, Mounts: []MountPoint{ { @@ -251,6 +253,7 @@ func TestContainerFromNative(t *testing.T) { Opts: map[string]string{}, }, }, + UTSMode: "host", }, Mounts: []MountPoint{ { From a304a27eebcae9de135f991140cf64627e1895b0 Mon Sep 17 00:00:00 2001 From: Arjun Raja Yogidas Date: Tue, 21 Jan 2025 02:42:46 +0000 Subject: [PATCH 10/16] chore: add runtime and sysctl to inspect response Signed-off-by: Arjun Raja Yogidas --- .../container/container_inspect_linux_test.go | 9 +++ pkg/inspecttypes/dockercompat/dockercompat.go | 62 ++++++++++++------- 2 files changed, 48 insertions(+), 23 deletions(-) diff --git a/cmd/nerdctl/container/container_inspect_linux_test.go b/cmd/nerdctl/container/container_inspect_linux_test.go index 76accb05ba0..4eb0ae629d4 100644 --- a/cmd/nerdctl/container/container_inspect_linux_test.go +++ b/cmd/nerdctl/container/container_inspect_linux_test.go @@ -253,6 +253,8 @@ func TestContainerInspectHostConfig(t *testing.T) { "--read-only", "--uts", "host", "--shm-size", "256m", + "--runtime", "io.containerd.runtime.v1.linux", + "--sysctl", "net.core.somaxconn=1024", testutil.AlpineImage, "sleep", "infinity").AssertOK() inspect := base.InspectContainer(testContainer) @@ -274,6 +276,11 @@ func TestContainerInspectHostConfig(t *testing.T) { assert.Equal(t, true, inspect.HostConfig.ReadonlyRootfs) assert.Equal(t, "host", inspect.HostConfig.UTSMode) assert.Equal(t, int64(268435456), inspect.HostConfig.ShmSize) + assert.Equal(t, "io.containerd.runtime.v1.linux", inspect.HostConfig.Runtime) + expectedSysctls := map[string]string{ + "net.core.somaxconn": "1024", + } + assert.DeepEqual(t, expectedSysctls, inspect.HostConfig.Sysctls) } func TestContainerInspectHostConfigDefaults(t *testing.T) { @@ -302,6 +309,8 @@ func TestContainerInspectHostConfigDefaults(t *testing.T) { assert.Equal(t, false, inspect.HostConfig.ReadonlyRootfs) assert.Equal(t, "", inspect.HostConfig.UTSMode) assert.Equal(t, int64(67108864), inspect.HostConfig.ShmSize) + assert.Equal(t, "io.containerd.runc.v2", inspect.HostConfig.Runtime) + assert.Equal(t, 0, len(inspect.HostConfig.Sysctls)) } func TestContainerInspectHostConfigDNS(t *testing.T) { diff --git a/pkg/inspecttypes/dockercompat/dockercompat.go b/pkg/inspecttypes/dockercompat/dockercompat.go index 5137c5216fe..1ca6eddb03e 100644 --- a/pkg/inspecttypes/dockercompat/dockercompat.go +++ b/pkg/inspecttypes/dockercompat/dockercompat.go @@ -143,29 +143,30 @@ type Container struct { // From https://github.com/moby/moby/blob/8dbd90ec00daa26dc45d7da2431c965dec99e8b4/api/types/container/host_config.go#L391 // HostConfig the non-portable Config structure of a container. type HostConfig struct { - ExtraHosts []string // List of extra hosts - PortBindings nat.PortMap // Port mapping between the exposed port (container) and the host - LogConfig LogConfig // Configuration of the logs for this container - BlkioWeight uint16 // Block IO weight (relative weight vs. other containers) - CPUSetMems string `json:"CpusetMems"` // CpusetMems 0-2, 0,1 - CPUSetCPUs string `json:"CpusetCpus"` // CpusetCpus 0-2, 0,1 - CPUQuota int64 `json:"CpuQuota"` // CPU CFS (Completely Fair Scheduler) quota - CPUShares uint64 `json:"CpuShares"` // CPU shares (relative weight vs. other containers) - ContainerIDFile string // File (path) where the containerId is written - GroupAdd []string // GroupAdd specifies additional groups to join - IpcMode string // IPC namespace to use for the container - CgroupnsMode string // Cgroup namespace mode to use for the container - Memory int64 // Memory limit (in bytes) - MemorySwap int64 // Total memory usage (memory + swap); set `-1` to enable unlimited swap - OomKillDisable bool // specifies whether to disable OOM Killer - DNS []string `json:"Dns"` // List of DNS server to lookup - DNSOptions []string `json:"DnsOptions"` // List of DNSOption to look for - DNSSearch []string `json:"DnsSearch"` // List of DNSSearch to look for - OomScoreAdj int // specifies the tune container’s OOM preferences (-1000 to 1000, rootless: 100 to 1000) - ReadonlyRootfs bool // Is the container root filesystem in read-only - UTSMode string // UTS namespace to use for the container - ShmSize int64 // Size of /dev/shm in bytes. The size must be greater than 0. - + ExtraHosts []string // List of extra hosts + PortBindings nat.PortMap // Port mapping between the exposed port (container) and the host + LogConfig LogConfig // Configuration of the logs for this container + BlkioWeight uint16 // Block IO weight (relative weight vs. other containers) + CPUSetMems string `json:"CpusetMems"` // CpusetMems 0-2, 0,1 + CPUSetCPUs string `json:"CpusetCpus"` // CpusetCpus 0-2, 0,1 + CPUQuota int64 `json:"CpuQuota"` // CPU CFS (Completely Fair Scheduler) quota + CPUShares uint64 `json:"CpuShares"` // CPU shares (relative weight vs. other containers) + ContainerIDFile string // File (path) where the containerId is written + GroupAdd []string // GroupAdd specifies additional groups to join + IpcMode string // IPC namespace to use for the container + CgroupnsMode string // Cgroup namespace mode to use for the container + Memory int64 // Memory limit (in bytes) + MemorySwap int64 // Total memory usage (memory + swap); set `-1` to enable unlimited swap + OomKillDisable bool // specifies whether to disable OOM Killer + DNS []string `json:"Dns"` // List of DNS server to lookup + DNSOptions []string `json:"DnsOptions"` // List of DNSOption to look for + DNSSearch []string `json:"DnsSearch"` // List of DNSSearch to look for + OomScoreAdj int // specifies the tune container’s OOM preferences (-1000 to 1000, rootless: 100 to 1000) + ReadonlyRootfs bool // Is the container root filesystem in read-only + UTSMode string // UTS namespace to use for the container + ShmSize int64 // Size of /dev/shm in bytes. The size must be greater than 0. + Sysctls map[string]string // List of Namespaced sysctls used for the container + Runtime string // Runtime to use with this container } // From https://github.com/moby/moby/blob/v20.10.1/api/types/types.go#L416-L427 @@ -466,6 +467,13 @@ func ContainerFromNative(n *native.Container) (*Container, error) { shmSize, _ := getShmSizeFromNative(n.Spec.(*specs.Spec)) c.HostConfig.ShmSize = shmSize + sysctls, _ := getSysctlFromNative(n.Spec.(*specs.Spec)) + c.HostConfig.Sysctls = sysctls + + if n.Runtime.Name != "" { + c.HostConfig.Runtime = n.Runtime.Name + } + c.State = cs c.Config = &Config{ Labels: n.Labels, @@ -787,6 +795,14 @@ func getShmSizeFromNative(sp *specs.Spec) (int64, error) { return res, nil } +func getSysctlFromNative(sp *specs.Spec) (map[string]string, error) { + var res map[string]string + if sp.Linux != nil && sp.Linux.Sysctl != nil { + res = sp.Linux.Sysctl + } + return res, nil +} + type IPAMConfig struct { Subnet string `json:"Subnet,omitempty"` Gateway string `json:"Gateway,omitempty"` From 42c33ccfe268bba19874cba8340bea1fae68a75c Mon Sep 17 00:00:00 2001 From: Arjun Raja Yogidas Date: Tue, 21 Jan 2025 04:10:07 +0000 Subject: [PATCH 11/16] chore: fix logConfig inspect response Signed-off-by: Arjun Raja Yogidas --- pkg/inspecttypes/dockercompat/dockercompat.go | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/pkg/inspecttypes/dockercompat/dockercompat.go b/pkg/inspecttypes/dockercompat/dockercompat.go index 1ca6eddb03e..32b3adc02d9 100644 --- a/pkg/inspecttypes/dockercompat/dockercompat.go +++ b/pkg/inspecttypes/dockercompat/dockercompat.go @@ -96,11 +96,6 @@ type ImageMetadata struct { LastTagTime time.Time `json:",omitempty"` } -type LogConfig struct { - Type string - Config loggerLogConfig -} - type loggerLogConfig struct { Driver string `json:"driver"` Opts map[string]string `json:"opts,omitempty"` @@ -145,7 +140,7 @@ type Container struct { type HostConfig struct { ExtraHosts []string // List of extra hosts PortBindings nat.PortMap // Port mapping between the exposed port (container) and the host - LogConfig LogConfig // Configuration of the logs for this container + LogConfig loggerLogConfig // Configuration of the logs for this container BlkioWeight uint16 // Block IO weight (relative weight vs. other containers) CPUSetMems string `json:"CpusetMems"` // CpusetMems 0-2, 0,1 CPUSetCPUs string `json:"CpusetCpus"` // CpusetCpus 0-2, 0,1 @@ -344,7 +339,7 @@ func ContainerFromNative(n *native.Container) (*Container, error) { } if nerdctlLoguri := n.Labels[labels.LogURI]; nerdctlLoguri != "" { - c.HostConfig.LogConfig.Type = nerdctlLoguri + c.HostConfig.LogConfig.LogURI = nerdctlLoguri } if logConfigJSON, ok := n.Labels[labels.LogConfig]; ok { var logConfig loggerLogConfig @@ -354,10 +349,10 @@ func ContainerFromNative(n *native.Container) (*Container, error) { } // Assign the parsed LogConfig to c.HostConfig.LogConfig - c.HostConfig.LogConfig.Config = logConfig + c.HostConfig.LogConfig = logConfig } else { // If LogConfig label is not present, set default values - c.HostConfig.LogConfig.Config = loggerLogConfig{ + c.HostConfig.LogConfig = loggerLogConfig{ Driver: "json-file", Opts: make(map[string]string), } From f98666e4ce381628115cc6342a1da28435e53756 Mon Sep 17 00:00:00 2001 From: Arjun Raja Yogidas Date: Tue, 21 Jan 2025 21:41:32 +0000 Subject: [PATCH 12/16] chore: add device to inspect response Signed-off-by: Arjun Raja Yogidas --- .../container/container_inspect_linux_test.go | 10 ++++--- pkg/cmd/container/create.go | 13 ++++++++- pkg/cmd/container/run_cgroup_linux.go | 3 ++- pkg/cmd/container/run_linux.go | 2 +- pkg/inspecttypes/dockercompat/dockercompat.go | 24 ++++++++++++++--- .../dockercompat/dockercompat_test.go | 27 +++++++++---------- pkg/labels/labels.go | 5 +++- 7 files changed, 57 insertions(+), 27 deletions(-) diff --git a/cmd/nerdctl/container/container_inspect_linux_test.go b/cmd/nerdctl/container/container_inspect_linux_test.go index 4eb0ae629d4..150ecd0cbd8 100644 --- a/cmd/nerdctl/container/container_inspect_linux_test.go +++ b/cmd/nerdctl/container/container_inspect_linux_test.go @@ -255,6 +255,7 @@ func TestContainerInspectHostConfig(t *testing.T) { "--shm-size", "256m", "--runtime", "io.containerd.runtime.v1.linux", "--sysctl", "net.core.somaxconn=1024", + "--device", "/dev/zero:/dev/null", testutil.AlpineImage, "sleep", "infinity").AssertOK() inspect := base.InspectContainer(testContainer) @@ -268,8 +269,7 @@ func TestContainerInspectHostConfig(t *testing.T) { expectedExtraHosts := []string{"host1:10.0.0.1", "host2:10.0.0.2"} assert.DeepEqual(t, expectedExtraHosts, inspect.HostConfig.ExtraHosts) assert.Equal(t, "host", inspect.HostConfig.IpcMode) - assert.Equal(t, "json-file", inspect.HostConfig.LogConfig.Type) - assert.Equal(t, "json-file", inspect.HostConfig.LogConfig.Config.Driver) + assert.Equal(t, "json-file", inspect.HostConfig.LogConfig.Driver) assert.Equal(t, int64(536870912), inspect.HostConfig.Memory) assert.Equal(t, int64(1073741824), inspect.HostConfig.MemorySwap) assert.Equal(t, bool(true), inspect.HostConfig.OomKillDisable) @@ -281,6 +281,8 @@ func TestContainerInspectHostConfig(t *testing.T) { "net.core.somaxconn": "1024", } assert.DeepEqual(t, expectedSysctls, inspect.HostConfig.Sysctls) + expectedDevices := []string{"/dev/null:/dev/null"} + assert.DeepEqual(t, expectedDevices, inspect.HostConfig.Devices) } func TestContainerInspectHostConfigDefaults(t *testing.T) { @@ -301,8 +303,7 @@ func TestContainerInspectHostConfigDefaults(t *testing.T) { assert.Equal(t, 0, len(inspect.HostConfig.GroupAdd)) assert.Equal(t, 0, len(inspect.HostConfig.ExtraHosts)) assert.Equal(t, "", inspect.HostConfig.IpcMode) - assert.Equal(t, "json-file", inspect.HostConfig.LogConfig.Type) - assert.Equal(t, "json-file", inspect.HostConfig.LogConfig.Config.Driver) + assert.Equal(t, "json-file", inspect.HostConfig.LogConfig.Driver) assert.Equal(t, int64(0), inspect.HostConfig.Memory) assert.Equal(t, int64(0), inspect.HostConfig.MemorySwap) assert.Equal(t, bool(false), inspect.HostConfig.OomKillDisable) @@ -311,6 +312,7 @@ func TestContainerInspectHostConfigDefaults(t *testing.T) { assert.Equal(t, int64(67108864), inspect.HostConfig.ShmSize) assert.Equal(t, "io.containerd.runc.v2", inspect.HostConfig.Runtime) assert.Equal(t, 0, len(inspect.HostConfig.Sysctls)) + assert.Equal(t, 0, len(inspect.HostConfig.Devices)) } func TestContainerInspectHostConfigDNS(t *testing.T) { diff --git a/pkg/cmd/container/create.go b/pkg/cmd/container/create.go index bd0cc29f6cc..a38ab885fb8 100644 --- a/pkg/cmd/container/create.go +++ b/pkg/cmd/container/create.go @@ -658,6 +658,9 @@ type internalLabels struct { // label to check if --group-add is set groupAdd []string + + // label for device mapping set by the --device flag + deviceMapping []string } // WithInternalLabels sets the internal labels for a container. @@ -757,7 +760,7 @@ func withInternalLabels(internalLabels internalLabels) (containerd.NewContainerO if err != nil { return nil, err } - m[labels.DnsServer] = string(dnsServersJSON) + m[labels.DNSServer] = string(dnsServersJSON) } if len(internalLabels.dnsSearchDomains) > 0 { @@ -776,6 +779,14 @@ func withInternalLabels(internalLabels internalLabels) (containerd.NewContainerO m[labels.DNSResolvConfOptions] = string(dnsResolvConfOptionsJSON) } + if len(internalLabels.deviceMapping) > 0 { + devicesJSON, err := json.Marshal(internalLabels.deviceMapping) + if err != nil { + return nil, err + } + m[labels.DeviceMapping] = string(devicesJSON) + } + return containerd.WithAdditionalContainerLabels(m), nil } diff --git a/pkg/cmd/container/run_cgroup_linux.go b/pkg/cmd/container/run_cgroup_linux.go index af43b12c1fd..cfb668debde 100644 --- a/pkg/cmd/container/run_cgroup_linux.go +++ b/pkg/cmd/container/run_cgroup_linux.go @@ -41,7 +41,7 @@ type customMemoryOptions struct { disableOOMKiller *bool } -func generateCgroupOpts(id string, options types.ContainerCreateOptions) ([]oci.SpecOpts, error) { +func generateCgroupOpts(id string, options types.ContainerCreateOptions, internalLabels *internalLabels) ([]oci.SpecOpts, error) { if options.KernelMemory != "" { log.L.Warnf("The --kernel-memory flag is no longer supported. This flag is a noop.") } @@ -206,6 +206,7 @@ func generateCgroupOpts(id string, options types.ContainerCreateOptions) ([]oci. return nil, fmt.Errorf("failed to parse device %q: %w", f, err) } opts = append(opts, oci.WithDevices(devPath, conPath, mode)) + internalLabels.deviceMapping = append(internalLabels.deviceMapping, f) } return opts, nil diff --git a/pkg/cmd/container/run_linux.go b/pkg/cmd/container/run_linux.go index cbe38d62e46..3280d3e532d 100644 --- a/pkg/cmd/container/run_linux.go +++ b/pkg/cmd/container/run_linux.go @@ -56,7 +56,7 @@ func setPlatformOptions(ctx context.Context, client *containerd.Client, id, uts {Type: "cgroup", Source: "cgroup", Destination: "/sys/fs/cgroup", Options: []string{"ro", "nosuid", "noexec", "nodev"}}, })) - cgOpts, err := generateCgroupOpts(id, options) + cgOpts, err := generateCgroupOpts(id, options, internalLabels) if err != nil { return nil, err } diff --git a/pkg/inspecttypes/dockercompat/dockercompat.go b/pkg/inspecttypes/dockercompat/dockercompat.go index 32b3adc02d9..f49dc122f69 100644 --- a/pkg/inspecttypes/dockercompat/dockercompat.go +++ b/pkg/inspecttypes/dockercompat/dockercompat.go @@ -162,6 +162,7 @@ type HostConfig struct { ShmSize int64 // Size of /dev/shm in bytes. The size must be greater than 0. Sysctls map[string]string // List of Namespaced sysctls used for the container Runtime string // Runtime to use with this container + Devices []string // List of devices to map inside the container } // From https://github.com/moby/moby/blob/v20.10.1/api/types/types.go#L416-L427 @@ -439,7 +440,7 @@ func ContainerFromNative(n *native.Container) (*Container, error) { c.HostConfig.Memory = memorySettings.Limit c.HostConfig.MemorySwap = memorySettings.Swap - dnsSettings, err := getDnsFromNative(n.Labels) + dnsSettings, err := getDNSFromNative(n.Labels) if err != nil { return nil, fmt.Errorf("failed to Decode dns Settings: %v", err) } @@ -478,6 +479,12 @@ func ContainerFromNative(n *native.Container) (*Container, error) { } c.Config.Hostname = hostname + c.HostConfig.Devices = []string{} + if nedctlDeviceMapping := n.Labels[labels.DeviceMapping]; nedctlDeviceMapping != "" { + devices, _ := parseDeviceMapping(nedctlDeviceMapping) + c.HostConfig.Devices = devices + } + return c, nil } @@ -725,10 +732,10 @@ func getMemorySettingsFromNative(sp *specs.Spec) (*MemorySetting, error) { return res, nil } -func getDnsFromNative(Labels map[string]string) (*DNSSettings, error) { +func getDNSFromNative(Labels map[string]string) (*DNSSettings, error) { res := &DNSSettings{} - if dnsServers := Labels[labels.DnsServer]; dnsServers != "" { + if dnsServers := Labels[labels.DNSServer]; dnsServers != "" { if err := json.Unmarshal([]byte(dnsServers), &res.DNSServers); err != nil { return nil, fmt.Errorf("failed to parse DNS servers: %v", err) } @@ -771,7 +778,7 @@ func getUtsModeFromNative(sp *specs.Spec) (string, error) { func getShmSizeFromNative(sp *specs.Spec) (int64, error) { var res int64 - if sp.Mounts != nil && len(sp.Mounts) > 0 { + if len(sp.Mounts) > 0 { for _, mount := range sp.Mounts { if mount.Destination == "/dev/shm" { for _, option := range mount.Options { @@ -798,6 +805,15 @@ func getSysctlFromNative(sp *specs.Spec) (map[string]string, error) { return res, nil } +func parseDeviceMapping(deviceMappingJSON string) ([]string, error) { + var devices []string + err := json.Unmarshal([]byte(deviceMappingJSON), &devices) + if err != nil { + return nil, fmt.Errorf("failed to parse device mapping: %v", err) + } + return devices, nil +} + type IPAMConfig struct { Subnet string `json:"Subnet,omitempty"` Gateway string `json:"Gateway,omitempty"` diff --git a/pkg/inspecttypes/dockercompat/dockercompat_test.go b/pkg/inspecttypes/dockercompat/dockercompat_test.go index f35410fac1e..4043b4cb9f0 100644 --- a/pkg/inspecttypes/dockercompat/dockercompat_test.go +++ b/pkg/inspecttypes/dockercompat/dockercompat_test.go @@ -78,13 +78,12 @@ func TestContainerFromNative(t *testing.T) { HostConfig: &HostConfig{ PortBindings: nat.PortMap{}, GroupAdd: []string{}, - LogConfig: LogConfig{ - Config: loggerLogConfig{ - Driver: "json-file", - Opts: map[string]string{}, - }, + LogConfig: loggerLogConfig{ + Driver: "json-file", + Opts: map[string]string{}, }, UTSMode: "host", + Devices: []string{}, }, Mounts: []MountPoint{ { @@ -164,13 +163,12 @@ func TestContainerFromNative(t *testing.T) { HostConfig: &HostConfig{ PortBindings: nat.PortMap{}, GroupAdd: []string{}, - LogConfig: LogConfig{ - Config: loggerLogConfig{ - Driver: "json-file", - Opts: map[string]string{}, - }, + LogConfig: loggerLogConfig{ + Driver: "json-file", + Opts: map[string]string{}, }, UTSMode: "host", + Devices: []string{}, }, Mounts: []MountPoint{ { @@ -247,13 +245,12 @@ func TestContainerFromNative(t *testing.T) { HostConfig: &HostConfig{ PortBindings: nat.PortMap{}, GroupAdd: []string{}, - LogConfig: LogConfig{ - Config: loggerLogConfig{ - Driver: "json-file", - Opts: map[string]string{}, - }, + LogConfig: loggerLogConfig{ + Driver: "json-file", + Opts: map[string]string{}, }, UTSMode: "host", + Devices: []string{}, }, Mounts: []MountPoint{ { diff --git a/pkg/labels/labels.go b/pkg/labels/labels.go index 1229c16470e..fda2d50f521 100644 --- a/pkg/labels/labels.go +++ b/pkg/labels/labels.go @@ -114,11 +114,14 @@ const ( CIdFile = Prefix + "cid-file" // Custom DNS lookup servers. - DnsServer = Prefix + "dns" + DNSServer = Prefix + "dns" // DNSResolvConfOptions set DNS options DNSResolvConfOptions = Prefix + "dns-options" // DNSSearchDomains set custom DNS search domains DNSSearchDomains = Prefix + "dns-search" + + //DeviceMapping specifies mapping host device to the container + DeviceMapping = Prefix + "devices" ) From a71684a9d662d1003893dd1879530d1aaed50946 Mon Sep 17 00:00:00 2001 From: Arjun Raja Yogidas Date: Wed, 22 Jan 2025 15:43:58 +0000 Subject: [PATCH 13/16] chore: refactor inspect response Signed-off-by: Arjun Raja Yogidas --- .../container/container_inspect_linux_test.go | 2 +- pkg/cmd/container/create.go | 42 ++++++++-------- pkg/inspecttypes/dockercompat/dockercompat.go | 50 ++++++++----------- pkg/labels/labels.go | 24 +++------ 4 files changed, 49 insertions(+), 69 deletions(-) diff --git a/cmd/nerdctl/container/container_inspect_linux_test.go b/cmd/nerdctl/container/container_inspect_linux_test.go index 150ecd0cbd8..6ba468f8052 100644 --- a/cmd/nerdctl/container/container_inspect_linux_test.go +++ b/cmd/nerdctl/container/container_inspect_linux_test.go @@ -255,7 +255,7 @@ func TestContainerInspectHostConfig(t *testing.T) { "--shm-size", "256m", "--runtime", "io.containerd.runtime.v1.linux", "--sysctl", "net.core.somaxconn=1024", - "--device", "/dev/zero:/dev/null", + "--device", "/dev/null:/dev/null", testutil.AlpineImage, "sleep", "infinity").AssertOK() inspect := base.InspectContainer(testContainer) diff --git a/pkg/cmd/container/create.go b/pkg/cmd/container/create.go index a38ab885fb8..2e5675bec8b 100644 --- a/pkg/cmd/container/create.go +++ b/pkg/cmd/container/create.go @@ -666,6 +666,8 @@ type internalLabels struct { // WithInternalLabels sets the internal labels for a container. func withInternalLabels(internalLabels internalLabels) (containerd.NewContainerOpts, error) { m := make(map[string]string) + var hostConfigLabel dockercompat.HostConfigLabel + var dnsSettings dockercompat.DNSSettings m[labels.Namespace] = internalLabels.namespace if internalLabels.name != "" { m[labels.Name] = internalLabels.name @@ -748,44 +750,40 @@ func withInternalLabels(internalLabels internalLabels) (containerd.NewContainerO } if internalLabels.blkioWeight > 0 { - m[labels.BlkioWeight] = fmt.Sprintf("%d", internalLabels.blkioWeight) + hostConfigLabel.BlkioWeight = internalLabels.blkioWeight } if internalLabels.cidFile != "" { - m[labels.CIdFile] = internalLabels.cidFile + hostConfigLabel.CidFile = internalLabels.cidFile } if len(internalLabels.dnsServers) > 0 { - dnsServersJSON, err := json.Marshal(internalLabels.dnsServers) - if err != nil { - return nil, err - } - m[labels.DNSServer] = string(dnsServersJSON) + dnsSettings.DNSServers = internalLabels.dnsServers } if len(internalLabels.dnsSearchDomains) > 0 { - dnsSearchJSON, err := json.Marshal(internalLabels.dnsSearchDomains) - if err != nil { - return nil, err - } - m[labels.DNSSearchDomains] = string(dnsSearchJSON) + dnsSettings.DNSSearchDomains = internalLabels.dnsSearchDomains } if len(internalLabels.dnsResolvConfOptions) > 0 { - dnsResolvConfOptionsJSON, err := json.Marshal(internalLabels.dnsResolvConfOptions) - if err != nil { - return nil, err - } - m[labels.DNSResolvConfOptions] = string(dnsResolvConfOptionsJSON) + dnsSettings.DNSResolvConfOptions = internalLabels.dnsResolvConfOptions } if len(internalLabels.deviceMapping) > 0 { - devicesJSON, err := json.Marshal(internalLabels.deviceMapping) - if err != nil { - return nil, err - } - m[labels.DeviceMapping] = string(devicesJSON) + hostConfigLabel.DeviceMapping = internalLabels.deviceMapping + } + + hostConfigJSON, err := json.Marshal(hostConfigLabel) + if err != nil { + return nil, err + } + m[labels.HostConfigLabel] = string(hostConfigJSON) + + dnsSettingsJSON, err := json.Marshal(dnsSettings) + if err != nil { + return nil, err } + m[labels.DNSSetting] = string(dnsSettingsJSON) return containerd.WithAdditionalContainerLabels(m), nil } diff --git a/pkg/inspecttypes/dockercompat/dockercompat.go b/pkg/inspecttypes/dockercompat/dockercompat.go index f49dc122f69..b9051f8b229 100644 --- a/pkg/inspecttypes/dockercompat/dockercompat.go +++ b/pkg/inspecttypes/dockercompat/dockercompat.go @@ -236,6 +236,12 @@ type DNSSettings struct { DNSSearchDomains []string } +type HostConfigLabel struct { + BlkioWeight uint16 + CidFile string + DeviceMapping []string +} + type CPUSettings struct { cpuSetCpus string cpuSetMems string @@ -359,18 +365,11 @@ func ContainerFromNative(n *native.Container) (*Container, error) { } } - if blkioWeightSet := n.Labels[labels.BlkioWeight]; blkioWeightSet != "" { - var blkioWeight uint16 - _, err := fmt.Sscanf(blkioWeightSet, "%d", &blkioWeight) - if err != nil { - return nil, fmt.Errorf("failed to convert string to uint: %v", err) - } - c.HostConfig.BlkioWeight = blkioWeight - } + // var hostConfigLabel HostConfigLabel + hostConfigLabel, err := getHostConfigLabelFromNative(n.Labels) - if cidFile := n.Labels[labels.CIdFile]; cidFile != "" { - c.HostConfig.ContainerIDFile = cidFile - } + c.HostConfig.BlkioWeight = hostConfigLabel.BlkioWeight + c.HostConfig.ContainerIDFile = hostConfigLabel.CidFile groupAdd, err := groupAddFromNative(n.Spec.(*specs.Spec)) if err != nil { @@ -479,11 +478,7 @@ func ContainerFromNative(n *native.Container) (*Container, error) { } c.Config.Hostname = hostname - c.HostConfig.Devices = []string{} - if nedctlDeviceMapping := n.Labels[labels.DeviceMapping]; nedctlDeviceMapping != "" { - devices, _ := parseDeviceMapping(nedctlDeviceMapping) - c.HostConfig.Devices = devices - } + c.HostConfig.Devices = hostConfigLabel.DeviceMapping return c, nil } @@ -735,24 +730,23 @@ func getMemorySettingsFromNative(sp *specs.Spec) (*MemorySetting, error) { func getDNSFromNative(Labels map[string]string) (*DNSSettings, error) { res := &DNSSettings{} - if dnsServers := Labels[labels.DNSServer]; dnsServers != "" { - if err := json.Unmarshal([]byte(dnsServers), &res.DNSServers); err != nil { - return nil, fmt.Errorf("failed to parse DNS servers: %v", err) + if dnsSettingJSON, ok := Labels[labels.DNSSetting]; ok { + if err := json.Unmarshal([]byte(dnsSettingJSON), &res); err != nil { + return nil, fmt.Errorf("failed to parse DNS settings: %v", err) } } - if dnsOptions := Labels[labels.DNSResolvConfOptions]; dnsOptions != "" { - if err := json.Unmarshal([]byte(dnsOptions), &res.DNSResolvConfOptions); err != nil { - return nil, fmt.Errorf("failed to parse DNS options: %v", err) - } - } + return res, nil +} - if dnsSearch := Labels[labels.DNSSearchDomains]; dnsSearch != "" { - if err := json.Unmarshal([]byte(dnsSearch), &res.DNSSearchDomains); err != nil { - return nil, fmt.Errorf("failed to parse DNS search domains: %v", err) +func getHostConfigLabelFromNative(Labels map[string]string) (*HostConfigLabel, error) { + res := &HostConfigLabel{} + + if hostConfigLabelJSON, ok := Labels[labels.HostConfigLabel]; ok { + if err := json.Unmarshal([]byte(hostConfigLabelJSON), &res); err != nil { + return nil, fmt.Errorf("failed to parse DNS servers: %v", err) } } - return res, nil } diff --git a/pkg/labels/labels.go b/pkg/labels/labels.go index fda2d50f521..b9ff58cb2cc 100644 --- a/pkg/labels/labels.go +++ b/pkg/labels/labels.go @@ -101,27 +101,15 @@ const ( // (like "nerdctl/default-network=true" or "nerdctl/default-network=false") NerdctlDefaultNetwork = Prefix + "default-network" - // LogConfig defines the logging configuration passed to the container - LogConfig = Prefix + "log-config" - // ContainerAutoRemove is to check whether the --rm option is specified. ContainerAutoRemove = Prefix + "auto-remove" - // BlkioWeight to check if the --blkio-weight is specified - BlkioWeight = Prefix + "blkio-weight" - - // Cidfile is the ContainerId file set via the --cidfile flag - CIdFile = Prefix + "cid-file" - - // Custom DNS lookup servers. - DNSServer = Prefix + "dns" - - // DNSResolvConfOptions set DNS options - DNSResolvConfOptions = Prefix + "dns-options" + // LogConfig defines the logging configuration passed to the container + LogConfig = Prefix + "log-config" - // DNSSearchDomains set custom DNS search domains - DNSSearchDomains = Prefix + "dns-search" + // HostConfigLabel sets the dockercompat host config values + HostConfigLabel = Prefix + "host-config" - //DeviceMapping specifies mapping host device to the container - DeviceMapping = Prefix + "devices" + // DNSSettings sets the dockercompat Dns config values + DNSSetting = Prefix + "dns" ) From 64ad42444f94a17ac824ce91267efe79d87a53b8 Mon Sep 17 00:00:00 2001 From: Arjun Raja Yogidas Date: Wed, 22 Jan 2025 21:18:56 +0000 Subject: [PATCH 14/16] chore: add pidMode to inspect response Signed-off-by: Arjun Raja Yogidas --- .../container/container_inspect_linux_test.go | 36 ++++++++++++++++++ pkg/inspecttypes/dockercompat/dockercompat.go | 38 +++++++++++++------ .../dockercompat/dockercompat_test.go | 4 +- 3 files changed, 64 insertions(+), 14 deletions(-) diff --git a/cmd/nerdctl/container/container_inspect_linux_test.go b/cmd/nerdctl/container/container_inspect_linux_test.go index 6ba468f8052..9528d0553e0 100644 --- a/cmd/nerdctl/container/container_inspect_linux_test.go +++ b/cmd/nerdctl/container/container_inspect_linux_test.go @@ -362,3 +362,39 @@ func TestContainerInspectHostConfigDNSDefaults(t *testing.T) { assert.Equal(t, 0, len(inspect.HostConfig.DNSSearch)) assert.Equal(t, 0, len(inspect.HostConfig.DNSOptions)) } + +func TestContainerInspectHostConfigPID(t *testing.T) { + testContainer1 := testutil.Identifier(t) + testContainer2 := testutil.Identifier(t) + + base := testutil.NewBase(t) + defer base.Cmd("rm", "-f", testContainer1, testContainer2).Run() + + // Run the first container + base.Cmd("run", "-d", "--name", testContainer1, testutil.AlpineImage, "sleep", "infinity").AssertOK() + + // Run a container with PID namespace options + base.Cmd("run", "-d", "--name", testContainer2, + "--pid", fmt.Sprintf("container:%s", testContainer1), + testutil.AlpineImage, "sleep", "infinity").AssertOK() + + inspect := base.InspectContainer(testContainer2) + + assert.Equal(t, fmt.Sprintf("container:%s", testContainer1), inspect.HostConfig.PidMode) + +} + +func TestContainerInspectHostConfigPIDDefaults(t *testing.T) { + testContainer := testutil.Identifier(t) + + base := testutil.NewBase(t) + defer base.Cmd("rm", "-f", testContainer).Run() + + // Run a container without specifying PID options + base.Cmd("run", "-d", "--name", testContainer, testutil.AlpineImage, "sleep", "infinity").AssertOK() + + inspect := base.InspectContainer(testContainer) + + // Check that PID mode is empty (private) by default + assert.Equal(t, "", inspect.HostConfig.PidMode) +} diff --git a/pkg/inspecttypes/dockercompat/dockercompat.go b/pkg/inspecttypes/dockercompat/dockercompat.go index b9051f8b229..d4fea7e667e 100644 --- a/pkg/inspecttypes/dockercompat/dockercompat.go +++ b/pkg/inspecttypes/dockercompat/dockercompat.go @@ -163,6 +163,8 @@ type HostConfig struct { Sysctls map[string]string // List of Namespaced sysctls used for the container Runtime string // Runtime to use with this container Devices []string // List of devices to map inside the container + PidMode string // PID namespace to use for the container + Tmpfs []MountPoint `json:",omitempty"` // List of tmpfs (mounts) used for the container } // From https://github.com/moby/moby/blob/v20.10.1/api/types/types.go#L416-L427 @@ -292,6 +294,7 @@ func ContainerFromNative(n *native.Container) (*Container, error) { // XXX is this always right? what if the container OS is NOT the same as the host OS? Platform: runtime.GOOS, // for Docker compatibility, this Platform string does NOT contain arch like "/amd64" } + c.HostConfig = new(HostConfig) if n.Labels[restart.StatusLabel] == string(containerd.Running) { c.RestartCount, _ = strconv.Atoi(n.Labels[restart.CountLabel]) } @@ -332,15 +335,20 @@ func ContainerFromNative(n *native.Container) (*Container, error) { } } + var tmpfsMounts []MountPoint + if nerdctlMounts := n.Labels[labels.Mounts]; nerdctlMounts != "" { mounts, err := parseMounts(nerdctlMounts) if err != nil { return nil, err } c.Mounts = mounts + if len(mounts) > 0 { + tmpfsMounts = filterTmpfsMounts(mounts) + } } + c.HostConfig.Tmpfs = tmpfsMounts - c.HostConfig = new(HostConfig) if nedctlExtraHosts := n.Labels[labels.ExtraHosts]; nedctlExtraHosts != "" { c.HostConfig.ExtraHosts = parseExtraHosts(nedctlExtraHosts) } @@ -366,7 +374,7 @@ func ContainerFromNative(n *native.Container) (*Container, error) { } // var hostConfigLabel HostConfigLabel - hostConfigLabel, err := getHostConfigLabelFromNative(n.Labels) + hostConfigLabel, _ := getHostConfigLabelFromNative(n.Labels) c.HostConfig.BlkioWeight = hostConfigLabel.BlkioWeight c.HostConfig.ContainerIDFile = hostConfigLabel.CidFile @@ -480,6 +488,11 @@ func ContainerFromNative(n *native.Container) (*Container, error) { c.HostConfig.Devices = hostConfigLabel.DeviceMapping + var pidMode string + if n.Labels[labels.PIDContainer] != "" { + pidMode = n.Labels[labels.PIDContainer] + } + c.HostConfig.PidMode = pidMode return c, nil } @@ -550,6 +563,18 @@ func mountsFromNative(spMounts []specs.Mount) []MountPoint { return mountpoints } +// filterTmpfsMounts filters the tmpfs mounts +func filterTmpfsMounts(spMounts []MountPoint) []MountPoint { + mountpoints := make([]MountPoint, 0, len(spMounts)) + for _, m := range spMounts { + if m.Type == "tmpfs" { + mountpoints = append(mountpoints, m) + } + } + + return mountpoints +} + func statusFromNative(x containerd.Status, labels map[string]string) string { switch s := x.Status; s { case containerd.Stopped: @@ -799,15 +824,6 @@ func getSysctlFromNative(sp *specs.Spec) (map[string]string, error) { return res, nil } -func parseDeviceMapping(deviceMappingJSON string) ([]string, error) { - var devices []string - err := json.Unmarshal([]byte(deviceMappingJSON), &devices) - if err != nil { - return nil, fmt.Errorf("failed to parse device mapping: %v", err) - } - return devices, nil -} - type IPAMConfig struct { Subnet string `json:"Subnet,omitempty"` Gateway string `json:"Gateway,omitempty"` diff --git a/pkg/inspecttypes/dockercompat/dockercompat_test.go b/pkg/inspecttypes/dockercompat/dockercompat_test.go index 4043b4cb9f0..ff84a5bd891 100644 --- a/pkg/inspecttypes/dockercompat/dockercompat_test.go +++ b/pkg/inspecttypes/dockercompat/dockercompat_test.go @@ -83,7 +83,7 @@ func TestContainerFromNative(t *testing.T) { Opts: map[string]string{}, }, UTSMode: "host", - Devices: []string{}, + Tmpfs: []MountPoint{}, }, Mounts: []MountPoint{ { @@ -168,7 +168,6 @@ func TestContainerFromNative(t *testing.T) { Opts: map[string]string{}, }, UTSMode: "host", - Devices: []string{}, }, Mounts: []MountPoint{ { @@ -250,7 +249,6 @@ func TestContainerFromNative(t *testing.T) { Opts: map[string]string{}, }, UTSMode: "host", - Devices: []string{}, }, Mounts: []MountPoint{ { From 43ac48cf5526e99a6321e30bcd244d29c93504a6 Mon Sep 17 00:00:00 2001 From: Arjun Raja Yogidas Date: Fri, 24 Jan 2025 22:42:01 +0000 Subject: [PATCH 15/16] chore: fix compatability errors Signed-off-by: Arjun Raja Yogidas --- .../container/container_inspect_linux_test.go | 71 +++++++----- pkg/cmd/container/create.go | 7 +- pkg/cmd/container/run_cgroup_linux.go | 7 +- pkg/inspecttypes/dockercompat/dockercompat.go | 103 +++++++++--------- .../dockercompat/dockercompat_test.go | 10 +- 5 files changed, 114 insertions(+), 84 deletions(-) diff --git a/cmd/nerdctl/container/container_inspect_linux_test.go b/cmd/nerdctl/container/container_inspect_linux_test.go index 9528d0553e0..e7fdce5337f 100644 --- a/cmd/nerdctl/container/container_inspect_linux_test.go +++ b/cmd/nerdctl/container/container_inspect_linux_test.go @@ -68,13 +68,12 @@ func TestContainerInspectContainsMounts(t *testing.T) { testutil.NginxAlpineImage).AssertOK() inspect := base.InspectContainer(testContainer) - // convert array to map to get by key of Destination actual := make(map[string]dockercompat.MountPoint) for i := range inspect.Mounts { actual[inspect.Mounts[i].Destination] = inspect.Mounts[i] } - + t.Logf("actual in TestContainerInspectContainsMounts: %+v", actual) const localDriver = "local" expected := []struct { @@ -249,13 +248,11 @@ func TestContainerInspectHostConfig(t *testing.T) { "--add-host", "host2:10.0.0.2", "--ipc", "host", "--memory", "512m", - "--oom-kill-disable", "--read-only", - "--uts", "host", "--shm-size", "256m", - "--runtime", "io.containerd.runtime.v1.linux", + "--uts", "host", "--sysctl", "net.core.somaxconn=1024", - "--device", "/dev/null:/dev/null", + "--runtime", "io.containerd.runc.v2", testutil.AlpineImage, "sleep", "infinity").AssertOK() inspect := base.InspectContainer(testContainer) @@ -265,24 +262,17 @@ func TestContainerInspectHostConfig(t *testing.T) { assert.Equal(t, uint16(500), inspect.HostConfig.BlkioWeight) assert.Equal(t, uint64(1024), inspect.HostConfig.CPUShares) assert.Equal(t, int64(100000), inspect.HostConfig.CPUQuota) - assert.DeepEqual(t, []string{"1000", "2000"}, inspect.HostConfig.GroupAdd) + assert.Assert(t, contains(inspect.HostConfig.GroupAdd, "1000"), "Expected '1000' to be in GroupAdd") + assert.Assert(t, contains(inspect.HostConfig.GroupAdd, "2000"), "Expected '2000' to be in GroupAdd") expectedExtraHosts := []string{"host1:10.0.0.1", "host2:10.0.0.2"} assert.DeepEqual(t, expectedExtraHosts, inspect.HostConfig.ExtraHosts) assert.Equal(t, "host", inspect.HostConfig.IpcMode) assert.Equal(t, "json-file", inspect.HostConfig.LogConfig.Driver) assert.Equal(t, int64(536870912), inspect.HostConfig.Memory) assert.Equal(t, int64(1073741824), inspect.HostConfig.MemorySwap) - assert.Equal(t, bool(true), inspect.HostConfig.OomKillDisable) assert.Equal(t, true, inspect.HostConfig.ReadonlyRootfs) assert.Equal(t, "host", inspect.HostConfig.UTSMode) assert.Equal(t, int64(268435456), inspect.HostConfig.ShmSize) - assert.Equal(t, "io.containerd.runtime.v1.linux", inspect.HostConfig.Runtime) - expectedSysctls := map[string]string{ - "net.core.somaxconn": "1024", - } - assert.DeepEqual(t, expectedSysctls, inspect.HostConfig.Sysctls) - expectedDevices := []string{"/dev/null:/dev/null"} - assert.DeepEqual(t, expectedDevices, inspect.HostConfig.Devices) } func TestContainerInspectHostConfigDefaults(t *testing.T) { @@ -295,21 +285,22 @@ func TestContainerInspectHostConfigDefaults(t *testing.T) { base.Cmd("run", "-d", "--name", testContainer, testutil.AlpineImage, "sleep", "infinity").AssertOK() inspect := base.InspectContainer(testContainer) + t.Logf("HostConfig in TestContainerInspectHostConfigDefaults: %+v", inspect.HostConfig) assert.Equal(t, "", inspect.HostConfig.CPUSetCPUs) assert.Equal(t, "", inspect.HostConfig.CPUSetMems) assert.Equal(t, uint16(0), inspect.HostConfig.BlkioWeight) assert.Equal(t, uint64(0), inspect.HostConfig.CPUShares) assert.Equal(t, int64(0), inspect.HostConfig.CPUQuota) - assert.Equal(t, 0, len(inspect.HostConfig.GroupAdd)) + assert.Equal(t, 10, len(inspect.HostConfig.GroupAdd)) assert.Equal(t, 0, len(inspect.HostConfig.ExtraHosts)) - assert.Equal(t, "", inspect.HostConfig.IpcMode) + assert.Equal(t, "private", inspect.HostConfig.IpcMode) assert.Equal(t, "json-file", inspect.HostConfig.LogConfig.Driver) assert.Equal(t, int64(0), inspect.HostConfig.Memory) assert.Equal(t, int64(0), inspect.HostConfig.MemorySwap) assert.Equal(t, bool(false), inspect.HostConfig.OomKillDisable) - assert.Equal(t, false, inspect.HostConfig.ReadonlyRootfs) + assert.Equal(t, bool(false), inspect.HostConfig.ReadonlyRootfs) assert.Equal(t, "", inspect.HostConfig.UTSMode) - assert.Equal(t, int64(67108864), inspect.HostConfig.ShmSize) + assert.Equal(t, 0, inspect.HostConfig.ShmSize) assert.Equal(t, "io.containerd.runc.v2", inspect.HostConfig.Runtime) assert.Equal(t, 0, len(inspect.HostConfig.Sysctls)) assert.Equal(t, 0, len(inspect.HostConfig.Devices)) @@ -364,8 +355,8 @@ func TestContainerInspectHostConfigDNSDefaults(t *testing.T) { } func TestContainerInspectHostConfigPID(t *testing.T) { - testContainer1 := testutil.Identifier(t) - testContainer2 := testutil.Identifier(t) + testContainer1 := testutil.Identifier(t) + "-container1" + testContainer2 := testutil.Identifier(t) + "-container2" base := testutil.NewBase(t) defer base.Cmd("rm", "-f", testContainer1, testContainer2).Run() @@ -373,14 +364,15 @@ func TestContainerInspectHostConfigPID(t *testing.T) { // Run the first container base.Cmd("run", "-d", "--name", testContainer1, testutil.AlpineImage, "sleep", "infinity").AssertOK() - // Run a container with PID namespace options + containerID1 := strings.TrimSpace(base.Cmd("inspect", "-f", "{{.Id}}", testContainer1).Out()) + base.Cmd("run", "-d", "--name", testContainer2, "--pid", fmt.Sprintf("container:%s", testContainer1), testutil.AlpineImage, "sleep", "infinity").AssertOK() inspect := base.InspectContainer(testContainer2) - assert.Equal(t, fmt.Sprintf("container:%s", testContainer1), inspect.HostConfig.PidMode) + assert.Equal(t, containerID1, inspect.HostConfig.PidMode) } @@ -390,11 +382,40 @@ func TestContainerInspectHostConfigPIDDefaults(t *testing.T) { base := testutil.NewBase(t) defer base.Cmd("rm", "-f", testContainer).Run() - // Run a container without specifying PID options base.Cmd("run", "-d", "--name", testContainer, testutil.AlpineImage, "sleep", "infinity").AssertOK() inspect := base.InspectContainer(testContainer) - // Check that PID mode is empty (private) by default assert.Equal(t, "", inspect.HostConfig.PidMode) } + +func TestContainerInspectDevices(t *testing.T) { + testContainer := testutil.Identifier(t) + + base := testutil.NewBase(t) + defer base.Cmd("rm", "-f", testContainer).Run() + + base.Cmd("run", "-d", "--name", testContainer, + "--device", "/dev/zero:/dev/xvda", + testutil.AlpineImage, "sleep", "infinity").AssertOK() + + inspect := base.InspectContainer(testContainer) + + expectedDevices := []dockercompat.DeviceMapping{ + { + PathOnHost: "/dev/zero", + PathInContainer: "/dev/xvda", + CgroupPermissions: "rwm", + }, + } + assert.DeepEqual(t, expectedDevices, inspect.HostConfig.Devices) +} + +func contains(slice []string, item string) bool { + for _, s := range slice { + if s == item { + return true + } + } + return false +} diff --git a/pkg/cmd/container/create.go b/pkg/cmd/container/create.go index 2e5675bec8b..44873a53c4d 100644 --- a/pkg/cmd/container/create.go +++ b/pkg/cmd/container/create.go @@ -224,6 +224,9 @@ func Create(ctx context.Context, client *containerd.Client, args []string, netMa } internalLabels.logURI = logConfig.LogURI internalLabels.logConfig = logConfig + if logConfig.Driver == "" && logConfig.Address == options.GOptions.Address { + internalLabels.logConfig.Driver = "json-file" + } restartOpts, err := generateRestartOpts(ctx, client, options.Restart, logConfig.LogURI, options.InRun) if err != nil { @@ -660,7 +663,7 @@ type internalLabels struct { groupAdd []string // label for device mapping set by the --device flag - deviceMapping []string + deviceMapping []dockercompat.DeviceMapping } // WithInternalLabels sets the internal labels for a container. @@ -770,7 +773,7 @@ func withInternalLabels(internalLabels internalLabels) (containerd.NewContainerO } if len(internalLabels.deviceMapping) > 0 { - hostConfigLabel.DeviceMapping = internalLabels.deviceMapping + hostConfigLabel.Devices = append(hostConfigLabel.Devices, internalLabels.deviceMapping...) } hostConfigJSON, err := json.Marshal(hostConfigLabel) diff --git a/pkg/cmd/container/run_cgroup_linux.go b/pkg/cmd/container/run_cgroup_linux.go index cfb668debde..a4d6fb7a266 100644 --- a/pkg/cmd/container/run_cgroup_linux.go +++ b/pkg/cmd/container/run_cgroup_linux.go @@ -32,6 +32,7 @@ import ( "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/infoutil" + "github.com/containerd/nerdctl/v2/pkg/inspecttypes/dockercompat" "github.com/containerd/nerdctl/v2/pkg/rootlessutil" ) @@ -206,7 +207,11 @@ func generateCgroupOpts(id string, options types.ContainerCreateOptions, interna return nil, fmt.Errorf("failed to parse device %q: %w", f, err) } opts = append(opts, oci.WithDevices(devPath, conPath, mode)) - internalLabels.deviceMapping = append(internalLabels.deviceMapping, f) + var deviceMap dockercompat.DeviceMapping + deviceMap.PathOnHost = devPath + deviceMap.PathInContainer = conPath + deviceMap.CgroupPermissions = mode + internalLabels.deviceMapping = append(internalLabels.deviceMapping, deviceMap) } return opts, nil diff --git a/pkg/inspecttypes/dockercompat/dockercompat.go b/pkg/inspecttypes/dockercompat/dockercompat.go index d4fea7e667e..934c95d9db3 100644 --- a/pkg/inspecttypes/dockercompat/dockercompat.go +++ b/pkg/inspecttypes/dockercompat/dockercompat.go @@ -96,7 +96,7 @@ type ImageMetadata struct { LastTagTime time.Time `json:",omitempty"` } -type loggerLogConfig struct { +type LoggerLogConfig struct { Driver string `json:"driver"` Opts map[string]string `json:"opts,omitempty"` LogURI string `json:"-"` @@ -140,7 +140,7 @@ type Container struct { type HostConfig struct { ExtraHosts []string // List of extra hosts PortBindings nat.PortMap // Port mapping between the exposed port (container) and the host - LogConfig loggerLogConfig // Configuration of the logs for this container + LogConfig LoggerLogConfig // Configuration of the logs for this container BlkioWeight uint16 // Block IO weight (relative weight vs. other containers) CPUSetMems string `json:"CpusetMems"` // CpusetMems 0-2, 0,1 CPUSetCPUs string `json:"CpusetCpus"` // CpusetCpus 0-2, 0,1 @@ -148,7 +148,7 @@ type HostConfig struct { CPUShares uint64 `json:"CpuShares"` // CPU shares (relative weight vs. other containers) ContainerIDFile string // File (path) where the containerId is written GroupAdd []string // GroupAdd specifies additional groups to join - IpcMode string // IPC namespace to use for the container + IpcMode string `json:"IpcMode"` // IPC namespace to use for the container CgroupnsMode string // Cgroup namespace mode to use for the container Memory int64 // Memory limit (in bytes) MemorySwap int64 // Total memory usage (memory + swap); set `-1` to enable unlimited swap @@ -162,9 +162,9 @@ type HostConfig struct { ShmSize int64 // Size of /dev/shm in bytes. The size must be greater than 0. Sysctls map[string]string // List of Namespaced sysctls used for the container Runtime string // Runtime to use with this container - Devices []string // List of devices to map inside the container + Devices []DeviceMapping // List of devices to map inside the container PidMode string // PID namespace to use for the container - Tmpfs []MountPoint `json:",omitempty"` // List of tmpfs (mounts) used for the container + Tmpfs map[string]string `json:"Tmpfs,omitempty"` // List of tmpfs (mounts) used for the container } // From https://github.com/moby/moby/blob/v20.10.1/api/types/types.go#L416-L427 @@ -239,9 +239,15 @@ type DNSSettings struct { } type HostConfigLabel struct { - BlkioWeight uint16 - CidFile string - DeviceMapping []string + BlkioWeight uint16 + CidFile string + Devices []DeviceMapping +} + +type DeviceMapping struct { + PathOnHost string + PathInContainer string + CgroupPermissions string } type CPUSettings struct { @@ -335,19 +341,19 @@ func ContainerFromNative(n *native.Container) (*Container, error) { } } - var tmpfsMounts []MountPoint - + c.HostConfig.Tmpfs = make(map[string]string) if nerdctlMounts := n.Labels[labels.Mounts]; nerdctlMounts != "" { mounts, err := parseMounts(nerdctlMounts) if err != nil { return nil, err } c.Mounts = mounts - if len(mounts) > 0 { - tmpfsMounts = filterTmpfsMounts(mounts) + for _, mount := range mounts { + if mount.Type == "tmpfs" { + c.HostConfig.Tmpfs[mount.Destination] = mount.Mode + } } } - c.HostConfig.Tmpfs = tmpfsMounts if nedctlExtraHosts := n.Labels[labels.ExtraHosts]; nedctlExtraHosts != "" { c.HostConfig.ExtraHosts = parseExtraHosts(nedctlExtraHosts) @@ -357,7 +363,7 @@ func ContainerFromNative(n *native.Container) (*Container, error) { c.HostConfig.LogConfig.LogURI = nerdctlLoguri } if logConfigJSON, ok := n.Labels[labels.LogConfig]; ok { - var logConfig loggerLogConfig + var logConfig LoggerLogConfig err := json.Unmarshal([]byte(logConfigJSON), &logConfig) if err != nil { return nil, fmt.Errorf("failed to unmarshal log config: %v", err) @@ -367,7 +373,7 @@ func ContainerFromNative(n *native.Container) (*Container, error) { c.HostConfig.LogConfig = logConfig } else { // If LogConfig label is not present, set default values - c.HostConfig.LogConfig = loggerLogConfig{ + c.HostConfig.LogConfig = LoggerLogConfig{ Driver: "json-file", Opts: make(map[string]string), } @@ -385,6 +391,7 @@ func ContainerFromNative(n *native.Container) (*Container, error) { } c.HostConfig.GroupAdd = groupAdd + c.HostConfig.ShmSize = 0 if ipcMode := n.Labels[labels.IPC]; ipcMode != "" { ipc, err := ipcutil.DecodeIPCLabel(ipcMode) @@ -392,6 +399,13 @@ func ContainerFromNative(n *native.Container) (*Container, error) { return nil, fmt.Errorf("failed to Decode IPC Label: %v", err) } c.HostConfig.IpcMode = string(ipc.Mode) + if ipc.ShmSize != "" { + shmSize, err := units.RAMInBytes(ipc.ShmSize) + if err != nil { + return nil, fmt.Errorf("failed to parse ShmSize: %v", err) + } + c.HostConfig.ShmSize = shmSize + } } cs := new(ContainerState) @@ -467,9 +481,6 @@ func ContainerFromNative(n *native.Container) (*Container, error) { utsMode, _ := getUtsModeFromNative(n.Spec.(*specs.Spec)) c.HostConfig.UTSMode = utsMode - shmSize, _ := getShmSizeFromNative(n.Spec.(*specs.Spec)) - c.HostConfig.ShmSize = shmSize - sysctls, _ := getSysctlFromNative(n.Spec.(*specs.Spec)) c.HostConfig.Sysctls = sysctls @@ -486,7 +497,7 @@ func ContainerFromNative(n *native.Container) (*Container, error) { } c.Config.Hostname = hostname - c.HostConfig.Devices = hostConfigLabel.DeviceMapping + c.HostConfig.Devices = hostConfigLabel.Devices var pidMode string if n.Labels[labels.PIDContainer] != "" { @@ -563,18 +574,6 @@ func mountsFromNative(spMounts []specs.Mount) []MountPoint { return mountpoints } -// filterTmpfsMounts filters the tmpfs mounts -func filterTmpfsMounts(spMounts []MountPoint) []MountPoint { - mountpoints := make([]MountPoint, 0, len(spMounts)) - for _, m := range spMounts { - if m.Type == "tmpfs" { - mountpoints = append(mountpoints, m) - } - } - - return mountpoints -} - func statusFromNative(x containerd.Status, labels map[string]string) string { switch s := x.Status; s { case containerd.Stopped: @@ -794,27 +793,27 @@ func getUtsModeFromNative(sp *specs.Spec) (string, error) { return "host", nil } -func getShmSizeFromNative(sp *specs.Spec) (int64, error) { - var res int64 - - if len(sp.Mounts) > 0 { - for _, mount := range sp.Mounts { - if mount.Destination == "/dev/shm" { - for _, option := range mount.Options { - if strings.HasPrefix(option, "size=") { - sizeStr := strings.TrimPrefix(option, "size=") - size, err := units.RAMInBytes(sizeStr) - if err != nil { - return 0, fmt.Errorf("failed to parse shm size: %v", err) - } - res = size - } - } - } - } - } - return res, nil -} +// func getShmSizeFromNative(sp *specs.Spec) (int64, error) { +// var res int64 + +// if len(sp.Mounts) > 0 { +// for _, mount := range sp.Mounts { +// if mount.Destination == "/dev/shm" { +// for _, option := range mount.Options { +// if strings.HasPrefix(option, "size=") { +// sizeStr := strings.TrimPrefix(option, "size=") +// size, err := units.RAMInBytes(sizeStr) +// if err != nil { +// return 0, fmt.Errorf("failed to parse shm size: %v", err) +// } +// res = size +// } +// } +// } +// } +// } +// return res, nil +// } func getSysctlFromNative(sp *specs.Spec) (map[string]string, error) { var res map[string]string diff --git a/pkg/inspecttypes/dockercompat/dockercompat_test.go b/pkg/inspecttypes/dockercompat/dockercompat_test.go index ff84a5bd891..a85fdd24ea0 100644 --- a/pkg/inspecttypes/dockercompat/dockercompat_test.go +++ b/pkg/inspecttypes/dockercompat/dockercompat_test.go @@ -78,12 +78,12 @@ func TestContainerFromNative(t *testing.T) { HostConfig: &HostConfig{ PortBindings: nat.PortMap{}, GroupAdd: []string{}, - LogConfig: loggerLogConfig{ + LogConfig: LoggerLogConfig{ Driver: "json-file", Opts: map[string]string{}, }, UTSMode: "host", - Tmpfs: []MountPoint{}, + Tmpfs: map[string]string{}, }, Mounts: []MountPoint{ { @@ -163,11 +163,12 @@ func TestContainerFromNative(t *testing.T) { HostConfig: &HostConfig{ PortBindings: nat.PortMap{}, GroupAdd: []string{}, - LogConfig: loggerLogConfig{ + LogConfig: LoggerLogConfig{ Driver: "json-file", Opts: map[string]string{}, }, UTSMode: "host", + Tmpfs: map[string]string{}, }, Mounts: []MountPoint{ { @@ -244,11 +245,12 @@ func TestContainerFromNative(t *testing.T) { HostConfig: &HostConfig{ PortBindings: nat.PortMap{}, GroupAdd: []string{}, - LogConfig: loggerLogConfig{ + LogConfig: LoggerLogConfig{ Driver: "json-file", Opts: map[string]string{}, }, UTSMode: "host", + Tmpfs: map[string]string{}, }, Mounts: []MountPoint{ { From 7b2a88c5f9508196ec14fd911cc729c3f4868a5e Mon Sep 17 00:00:00 2001 From: Arjun Raja Yogidas Date: Fri, 24 Jan 2025 22:42:01 +0000 Subject: [PATCH 16/16] chore: fix compatability errors Signed-off-by: Arjun Raja Yogidas --- .../container/container_inspect_linux_test.go | 71 +++++++----- pkg/cmd/container/create.go | 7 +- pkg/cmd/container/run_cgroup_linux.go | 7 +- pkg/inspecttypes/dockercompat/dockercompat.go | 103 +++++++++--------- .../dockercompat/dockercompat_test.go | 10 +- 5 files changed, 114 insertions(+), 84 deletions(-) diff --git a/cmd/nerdctl/container/container_inspect_linux_test.go b/cmd/nerdctl/container/container_inspect_linux_test.go index 9528d0553e0..8a8b88b3359 100644 --- a/cmd/nerdctl/container/container_inspect_linux_test.go +++ b/cmd/nerdctl/container/container_inspect_linux_test.go @@ -68,13 +68,12 @@ func TestContainerInspectContainsMounts(t *testing.T) { testutil.NginxAlpineImage).AssertOK() inspect := base.InspectContainer(testContainer) - // convert array to map to get by key of Destination actual := make(map[string]dockercompat.MountPoint) for i := range inspect.Mounts { actual[inspect.Mounts[i].Destination] = inspect.Mounts[i] } - + t.Logf("actual in TestContainerInspectContainsMounts: %+v", actual) const localDriver = "local" expected := []struct { @@ -249,13 +248,11 @@ func TestContainerInspectHostConfig(t *testing.T) { "--add-host", "host2:10.0.0.2", "--ipc", "host", "--memory", "512m", - "--oom-kill-disable", "--read-only", - "--uts", "host", "--shm-size", "256m", - "--runtime", "io.containerd.runtime.v1.linux", + "--uts", "host", "--sysctl", "net.core.somaxconn=1024", - "--device", "/dev/null:/dev/null", + "--runtime", "io.containerd.runc.v2", testutil.AlpineImage, "sleep", "infinity").AssertOK() inspect := base.InspectContainer(testContainer) @@ -265,24 +262,17 @@ func TestContainerInspectHostConfig(t *testing.T) { assert.Equal(t, uint16(500), inspect.HostConfig.BlkioWeight) assert.Equal(t, uint64(1024), inspect.HostConfig.CPUShares) assert.Equal(t, int64(100000), inspect.HostConfig.CPUQuota) - assert.DeepEqual(t, []string{"1000", "2000"}, inspect.HostConfig.GroupAdd) + assert.Assert(t, contains(inspect.HostConfig.GroupAdd, "1000"), "Expected '1000' to be in GroupAdd") + assert.Assert(t, contains(inspect.HostConfig.GroupAdd, "2000"), "Expected '2000' to be in GroupAdd") expectedExtraHosts := []string{"host1:10.0.0.1", "host2:10.0.0.2"} assert.DeepEqual(t, expectedExtraHosts, inspect.HostConfig.ExtraHosts) assert.Equal(t, "host", inspect.HostConfig.IpcMode) assert.Equal(t, "json-file", inspect.HostConfig.LogConfig.Driver) assert.Equal(t, int64(536870912), inspect.HostConfig.Memory) assert.Equal(t, int64(1073741824), inspect.HostConfig.MemorySwap) - assert.Equal(t, bool(true), inspect.HostConfig.OomKillDisable) assert.Equal(t, true, inspect.HostConfig.ReadonlyRootfs) assert.Equal(t, "host", inspect.HostConfig.UTSMode) assert.Equal(t, int64(268435456), inspect.HostConfig.ShmSize) - assert.Equal(t, "io.containerd.runtime.v1.linux", inspect.HostConfig.Runtime) - expectedSysctls := map[string]string{ - "net.core.somaxconn": "1024", - } - assert.DeepEqual(t, expectedSysctls, inspect.HostConfig.Sysctls) - expectedDevices := []string{"/dev/null:/dev/null"} - assert.DeepEqual(t, expectedDevices, inspect.HostConfig.Devices) } func TestContainerInspectHostConfigDefaults(t *testing.T) { @@ -295,21 +285,22 @@ func TestContainerInspectHostConfigDefaults(t *testing.T) { base.Cmd("run", "-d", "--name", testContainer, testutil.AlpineImage, "sleep", "infinity").AssertOK() inspect := base.InspectContainer(testContainer) + t.Logf("HostConfig in TestContainerInspectHostConfigDefaults: %+v", inspect.HostConfig) assert.Equal(t, "", inspect.HostConfig.CPUSetCPUs) assert.Equal(t, "", inspect.HostConfig.CPUSetMems) assert.Equal(t, uint16(0), inspect.HostConfig.BlkioWeight) assert.Equal(t, uint64(0), inspect.HostConfig.CPUShares) assert.Equal(t, int64(0), inspect.HostConfig.CPUQuota) - assert.Equal(t, 0, len(inspect.HostConfig.GroupAdd)) + assert.Equal(t, 10, len(inspect.HostConfig.GroupAdd)) assert.Equal(t, 0, len(inspect.HostConfig.ExtraHosts)) - assert.Equal(t, "", inspect.HostConfig.IpcMode) + assert.Equal(t, "private", inspect.HostConfig.IpcMode) assert.Equal(t, "json-file", inspect.HostConfig.LogConfig.Driver) assert.Equal(t, int64(0), inspect.HostConfig.Memory) assert.Equal(t, int64(0), inspect.HostConfig.MemorySwap) assert.Equal(t, bool(false), inspect.HostConfig.OomKillDisable) - assert.Equal(t, false, inspect.HostConfig.ReadonlyRootfs) + assert.Equal(t, bool(false), inspect.HostConfig.ReadonlyRootfs) assert.Equal(t, "", inspect.HostConfig.UTSMode) - assert.Equal(t, int64(67108864), inspect.HostConfig.ShmSize) + assert.Equal(t, int64(0), inspect.HostConfig.ShmSize) assert.Equal(t, "io.containerd.runc.v2", inspect.HostConfig.Runtime) assert.Equal(t, 0, len(inspect.HostConfig.Sysctls)) assert.Equal(t, 0, len(inspect.HostConfig.Devices)) @@ -364,8 +355,8 @@ func TestContainerInspectHostConfigDNSDefaults(t *testing.T) { } func TestContainerInspectHostConfigPID(t *testing.T) { - testContainer1 := testutil.Identifier(t) - testContainer2 := testutil.Identifier(t) + testContainer1 := testutil.Identifier(t) + "-container1" + testContainer2 := testutil.Identifier(t) + "-container2" base := testutil.NewBase(t) defer base.Cmd("rm", "-f", testContainer1, testContainer2).Run() @@ -373,14 +364,15 @@ func TestContainerInspectHostConfigPID(t *testing.T) { // Run the first container base.Cmd("run", "-d", "--name", testContainer1, testutil.AlpineImage, "sleep", "infinity").AssertOK() - // Run a container with PID namespace options + containerID1 := strings.TrimSpace(base.Cmd("inspect", "-f", "{{.Id}}", testContainer1).Out()) + base.Cmd("run", "-d", "--name", testContainer2, "--pid", fmt.Sprintf("container:%s", testContainer1), testutil.AlpineImage, "sleep", "infinity").AssertOK() inspect := base.InspectContainer(testContainer2) - assert.Equal(t, fmt.Sprintf("container:%s", testContainer1), inspect.HostConfig.PidMode) + assert.Equal(t, containerID1, inspect.HostConfig.PidMode) } @@ -390,11 +382,40 @@ func TestContainerInspectHostConfigPIDDefaults(t *testing.T) { base := testutil.NewBase(t) defer base.Cmd("rm", "-f", testContainer).Run() - // Run a container without specifying PID options base.Cmd("run", "-d", "--name", testContainer, testutil.AlpineImage, "sleep", "infinity").AssertOK() inspect := base.InspectContainer(testContainer) - // Check that PID mode is empty (private) by default assert.Equal(t, "", inspect.HostConfig.PidMode) } + +func TestContainerInspectDevices(t *testing.T) { + testContainer := testutil.Identifier(t) + + base := testutil.NewBase(t) + defer base.Cmd("rm", "-f", testContainer).Run() + + base.Cmd("run", "-d", "--name", testContainer, + "--device", "/dev/zero:/dev/xvda", + testutil.AlpineImage, "sleep", "infinity").AssertOK() + + inspect := base.InspectContainer(testContainer) + + expectedDevices := []dockercompat.DeviceMapping{ + { + PathOnHost: "/dev/zero", + PathInContainer: "/dev/xvda", + CgroupPermissions: "rwm", + }, + } + assert.DeepEqual(t, expectedDevices, inspect.HostConfig.Devices) +} + +func contains(slice []string, item string) bool { + for _, s := range slice { + if s == item { + return true + } + } + return false +} diff --git a/pkg/cmd/container/create.go b/pkg/cmd/container/create.go index 2e5675bec8b..44873a53c4d 100644 --- a/pkg/cmd/container/create.go +++ b/pkg/cmd/container/create.go @@ -224,6 +224,9 @@ func Create(ctx context.Context, client *containerd.Client, args []string, netMa } internalLabels.logURI = logConfig.LogURI internalLabels.logConfig = logConfig + if logConfig.Driver == "" && logConfig.Address == options.GOptions.Address { + internalLabels.logConfig.Driver = "json-file" + } restartOpts, err := generateRestartOpts(ctx, client, options.Restart, logConfig.LogURI, options.InRun) if err != nil { @@ -660,7 +663,7 @@ type internalLabels struct { groupAdd []string // label for device mapping set by the --device flag - deviceMapping []string + deviceMapping []dockercompat.DeviceMapping } // WithInternalLabels sets the internal labels for a container. @@ -770,7 +773,7 @@ func withInternalLabels(internalLabels internalLabels) (containerd.NewContainerO } if len(internalLabels.deviceMapping) > 0 { - hostConfigLabel.DeviceMapping = internalLabels.deviceMapping + hostConfigLabel.Devices = append(hostConfigLabel.Devices, internalLabels.deviceMapping...) } hostConfigJSON, err := json.Marshal(hostConfigLabel) diff --git a/pkg/cmd/container/run_cgroup_linux.go b/pkg/cmd/container/run_cgroup_linux.go index cfb668debde..a4d6fb7a266 100644 --- a/pkg/cmd/container/run_cgroup_linux.go +++ b/pkg/cmd/container/run_cgroup_linux.go @@ -32,6 +32,7 @@ import ( "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/infoutil" + "github.com/containerd/nerdctl/v2/pkg/inspecttypes/dockercompat" "github.com/containerd/nerdctl/v2/pkg/rootlessutil" ) @@ -206,7 +207,11 @@ func generateCgroupOpts(id string, options types.ContainerCreateOptions, interna return nil, fmt.Errorf("failed to parse device %q: %w", f, err) } opts = append(opts, oci.WithDevices(devPath, conPath, mode)) - internalLabels.deviceMapping = append(internalLabels.deviceMapping, f) + var deviceMap dockercompat.DeviceMapping + deviceMap.PathOnHost = devPath + deviceMap.PathInContainer = conPath + deviceMap.CgroupPermissions = mode + internalLabels.deviceMapping = append(internalLabels.deviceMapping, deviceMap) } return opts, nil diff --git a/pkg/inspecttypes/dockercompat/dockercompat.go b/pkg/inspecttypes/dockercompat/dockercompat.go index d4fea7e667e..934c95d9db3 100644 --- a/pkg/inspecttypes/dockercompat/dockercompat.go +++ b/pkg/inspecttypes/dockercompat/dockercompat.go @@ -96,7 +96,7 @@ type ImageMetadata struct { LastTagTime time.Time `json:",omitempty"` } -type loggerLogConfig struct { +type LoggerLogConfig struct { Driver string `json:"driver"` Opts map[string]string `json:"opts,omitempty"` LogURI string `json:"-"` @@ -140,7 +140,7 @@ type Container struct { type HostConfig struct { ExtraHosts []string // List of extra hosts PortBindings nat.PortMap // Port mapping between the exposed port (container) and the host - LogConfig loggerLogConfig // Configuration of the logs for this container + LogConfig LoggerLogConfig // Configuration of the logs for this container BlkioWeight uint16 // Block IO weight (relative weight vs. other containers) CPUSetMems string `json:"CpusetMems"` // CpusetMems 0-2, 0,1 CPUSetCPUs string `json:"CpusetCpus"` // CpusetCpus 0-2, 0,1 @@ -148,7 +148,7 @@ type HostConfig struct { CPUShares uint64 `json:"CpuShares"` // CPU shares (relative weight vs. other containers) ContainerIDFile string // File (path) where the containerId is written GroupAdd []string // GroupAdd specifies additional groups to join - IpcMode string // IPC namespace to use for the container + IpcMode string `json:"IpcMode"` // IPC namespace to use for the container CgroupnsMode string // Cgroup namespace mode to use for the container Memory int64 // Memory limit (in bytes) MemorySwap int64 // Total memory usage (memory + swap); set `-1` to enable unlimited swap @@ -162,9 +162,9 @@ type HostConfig struct { ShmSize int64 // Size of /dev/shm in bytes. The size must be greater than 0. Sysctls map[string]string // List of Namespaced sysctls used for the container Runtime string // Runtime to use with this container - Devices []string // List of devices to map inside the container + Devices []DeviceMapping // List of devices to map inside the container PidMode string // PID namespace to use for the container - Tmpfs []MountPoint `json:",omitempty"` // List of tmpfs (mounts) used for the container + Tmpfs map[string]string `json:"Tmpfs,omitempty"` // List of tmpfs (mounts) used for the container } // From https://github.com/moby/moby/blob/v20.10.1/api/types/types.go#L416-L427 @@ -239,9 +239,15 @@ type DNSSettings struct { } type HostConfigLabel struct { - BlkioWeight uint16 - CidFile string - DeviceMapping []string + BlkioWeight uint16 + CidFile string + Devices []DeviceMapping +} + +type DeviceMapping struct { + PathOnHost string + PathInContainer string + CgroupPermissions string } type CPUSettings struct { @@ -335,19 +341,19 @@ func ContainerFromNative(n *native.Container) (*Container, error) { } } - var tmpfsMounts []MountPoint - + c.HostConfig.Tmpfs = make(map[string]string) if nerdctlMounts := n.Labels[labels.Mounts]; nerdctlMounts != "" { mounts, err := parseMounts(nerdctlMounts) if err != nil { return nil, err } c.Mounts = mounts - if len(mounts) > 0 { - tmpfsMounts = filterTmpfsMounts(mounts) + for _, mount := range mounts { + if mount.Type == "tmpfs" { + c.HostConfig.Tmpfs[mount.Destination] = mount.Mode + } } } - c.HostConfig.Tmpfs = tmpfsMounts if nedctlExtraHosts := n.Labels[labels.ExtraHosts]; nedctlExtraHosts != "" { c.HostConfig.ExtraHosts = parseExtraHosts(nedctlExtraHosts) @@ -357,7 +363,7 @@ func ContainerFromNative(n *native.Container) (*Container, error) { c.HostConfig.LogConfig.LogURI = nerdctlLoguri } if logConfigJSON, ok := n.Labels[labels.LogConfig]; ok { - var logConfig loggerLogConfig + var logConfig LoggerLogConfig err := json.Unmarshal([]byte(logConfigJSON), &logConfig) if err != nil { return nil, fmt.Errorf("failed to unmarshal log config: %v", err) @@ -367,7 +373,7 @@ func ContainerFromNative(n *native.Container) (*Container, error) { c.HostConfig.LogConfig = logConfig } else { // If LogConfig label is not present, set default values - c.HostConfig.LogConfig = loggerLogConfig{ + c.HostConfig.LogConfig = LoggerLogConfig{ Driver: "json-file", Opts: make(map[string]string), } @@ -385,6 +391,7 @@ func ContainerFromNative(n *native.Container) (*Container, error) { } c.HostConfig.GroupAdd = groupAdd + c.HostConfig.ShmSize = 0 if ipcMode := n.Labels[labels.IPC]; ipcMode != "" { ipc, err := ipcutil.DecodeIPCLabel(ipcMode) @@ -392,6 +399,13 @@ func ContainerFromNative(n *native.Container) (*Container, error) { return nil, fmt.Errorf("failed to Decode IPC Label: %v", err) } c.HostConfig.IpcMode = string(ipc.Mode) + if ipc.ShmSize != "" { + shmSize, err := units.RAMInBytes(ipc.ShmSize) + if err != nil { + return nil, fmt.Errorf("failed to parse ShmSize: %v", err) + } + c.HostConfig.ShmSize = shmSize + } } cs := new(ContainerState) @@ -467,9 +481,6 @@ func ContainerFromNative(n *native.Container) (*Container, error) { utsMode, _ := getUtsModeFromNative(n.Spec.(*specs.Spec)) c.HostConfig.UTSMode = utsMode - shmSize, _ := getShmSizeFromNative(n.Spec.(*specs.Spec)) - c.HostConfig.ShmSize = shmSize - sysctls, _ := getSysctlFromNative(n.Spec.(*specs.Spec)) c.HostConfig.Sysctls = sysctls @@ -486,7 +497,7 @@ func ContainerFromNative(n *native.Container) (*Container, error) { } c.Config.Hostname = hostname - c.HostConfig.Devices = hostConfigLabel.DeviceMapping + c.HostConfig.Devices = hostConfigLabel.Devices var pidMode string if n.Labels[labels.PIDContainer] != "" { @@ -563,18 +574,6 @@ func mountsFromNative(spMounts []specs.Mount) []MountPoint { return mountpoints } -// filterTmpfsMounts filters the tmpfs mounts -func filterTmpfsMounts(spMounts []MountPoint) []MountPoint { - mountpoints := make([]MountPoint, 0, len(spMounts)) - for _, m := range spMounts { - if m.Type == "tmpfs" { - mountpoints = append(mountpoints, m) - } - } - - return mountpoints -} - func statusFromNative(x containerd.Status, labels map[string]string) string { switch s := x.Status; s { case containerd.Stopped: @@ -794,27 +793,27 @@ func getUtsModeFromNative(sp *specs.Spec) (string, error) { return "host", nil } -func getShmSizeFromNative(sp *specs.Spec) (int64, error) { - var res int64 - - if len(sp.Mounts) > 0 { - for _, mount := range sp.Mounts { - if mount.Destination == "/dev/shm" { - for _, option := range mount.Options { - if strings.HasPrefix(option, "size=") { - sizeStr := strings.TrimPrefix(option, "size=") - size, err := units.RAMInBytes(sizeStr) - if err != nil { - return 0, fmt.Errorf("failed to parse shm size: %v", err) - } - res = size - } - } - } - } - } - return res, nil -} +// func getShmSizeFromNative(sp *specs.Spec) (int64, error) { +// var res int64 + +// if len(sp.Mounts) > 0 { +// for _, mount := range sp.Mounts { +// if mount.Destination == "/dev/shm" { +// for _, option := range mount.Options { +// if strings.HasPrefix(option, "size=") { +// sizeStr := strings.TrimPrefix(option, "size=") +// size, err := units.RAMInBytes(sizeStr) +// if err != nil { +// return 0, fmt.Errorf("failed to parse shm size: %v", err) +// } +// res = size +// } +// } +// } +// } +// } +// return res, nil +// } func getSysctlFromNative(sp *specs.Spec) (map[string]string, error) { var res map[string]string diff --git a/pkg/inspecttypes/dockercompat/dockercompat_test.go b/pkg/inspecttypes/dockercompat/dockercompat_test.go index ff84a5bd891..a85fdd24ea0 100644 --- a/pkg/inspecttypes/dockercompat/dockercompat_test.go +++ b/pkg/inspecttypes/dockercompat/dockercompat_test.go @@ -78,12 +78,12 @@ func TestContainerFromNative(t *testing.T) { HostConfig: &HostConfig{ PortBindings: nat.PortMap{}, GroupAdd: []string{}, - LogConfig: loggerLogConfig{ + LogConfig: LoggerLogConfig{ Driver: "json-file", Opts: map[string]string{}, }, UTSMode: "host", - Tmpfs: []MountPoint{}, + Tmpfs: map[string]string{}, }, Mounts: []MountPoint{ { @@ -163,11 +163,12 @@ func TestContainerFromNative(t *testing.T) { HostConfig: &HostConfig{ PortBindings: nat.PortMap{}, GroupAdd: []string{}, - LogConfig: loggerLogConfig{ + LogConfig: LoggerLogConfig{ Driver: "json-file", Opts: map[string]string{}, }, UTSMode: "host", + Tmpfs: map[string]string{}, }, Mounts: []MountPoint{ { @@ -244,11 +245,12 @@ func TestContainerFromNative(t *testing.T) { HostConfig: &HostConfig{ PortBindings: nat.PortMap{}, GroupAdd: []string{}, - LogConfig: loggerLogConfig{ + LogConfig: LoggerLogConfig{ Driver: "json-file", Opts: map[string]string{}, }, UTSMode: "host", + Tmpfs: map[string]string{}, }, Mounts: []MountPoint{ {