From ba924a97407706cb69402a696399c3cf83c02153 Mon Sep 17 00:00:00 2001 From: Arjun Raja Yogidas Date: Thu, 2 Jan 2025 21:50:23 +0000 Subject: [PATCH 01/15] 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 3faf01c4dd4..023760480ae 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 { @@ -642,7 +643,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. @@ -674,6 +676,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 317da3ccce4..6f31f23b89d 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{ @@ -497,6 +539,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 bfcd8b863b1..2507d4ccf4b 100644 --- a/pkg/labels/labels.go +++ b/pkg/labels/labels.go @@ -104,6 +104,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 cc480c3d43b2f76174bc87912621f06a6fd47da2 Mon Sep 17 00:00:00 2001 From: Arjun Raja Yogidas Date: Fri, 3 Jan 2025 03:52:05 +0000 Subject: [PATCH 02/15] 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 023760480ae..89d91bf1e0e 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 @@ -732,6 +739,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 6f31f23b89d..aa87f0fccc1 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 2507d4ccf4b..d8d704d55dc 100644 --- a/pkg/labels/labels.go +++ b/pkg/labels/labels.go @@ -104,9 +104,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 69120e5508b3eff2dcce16f066f675c1ffa1ef3c Mon Sep 17 00:00:00 2001 From: Arjun Raja Yogidas Date: Thu, 16 Jan 2025 23:08:09 +0000 Subject: [PATCH 03/15] 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 89d91bf1e0e..b6104a9ba9b 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 } @@ -652,6 +654,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. @@ -751,6 +759,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 aa87f0fccc1..a3bf433fcf0 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] @@ -572,6 +592,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 d8d704d55dc..b3b46163432 100644 --- a/pkg/labels/labels.go +++ b/pkg/labels/labels.go @@ -118,4 +118,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 b71c803a5c615f822d03b7a38e4bc800f70b335f Mon Sep 17 00:00:00 2001 From: Arjun Raja Yogidas Date: Fri, 17 Jan 2025 07:54:43 +0000 Subject: [PATCH 04/15] 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 b6104a9ba9b..f5ac679a240 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 @@ -751,26 +749,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 a3bf433fcf0..9c8453988b3 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, @@ -565,6 +579,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 { @@ -592,14 +641,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 b3b46163432..7b80ee911eb 100644 --- a/pkg/labels/labels.go +++ b/pkg/labels/labels.go @@ -113,15 +113,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 fbc1b51712d0aa8f9f37b7f8065ca43f894dd40c Mon Sep 17 00:00:00 2001 From: Arjun Raja Yogidas Date: Fri, 17 Jan 2025 23:21:21 +0000 Subject: [PATCH 05/15] 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 28caef15c8c446afd14f70251f0e1cb146d40990 Mon Sep 17 00:00:00 2001 From: Arjun Raja Yogidas Date: Sun, 19 Jan 2025 18:51:49 +0000 Subject: [PATCH 06/15] 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 9c8453988b3..f16fb54552d 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, @@ -602,6 +620,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 { @@ -641,6 +671,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"` @@ -671,6 +719,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 93b629240ead3a412edf20e3d80e2462242d157a Mon Sep 17 00:00:00 2001 From: Arjun Raja Yogidas Date: Mon, 20 Jan 2025 03:53:54 +0000 Subject: [PATCH 07/15] 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 f5ac679a240..4286ae189e5 100644 --- a/pkg/cmd/container/create.go +++ b/pkg/cmd/container/create.go @@ -626,8 +626,6 @@ type internalLabels struct { extraHosts []string pidFile string blkioWeight uint16 - cpusetCpus string - cpusetMems string // labels from cmd options or automatically set name string hostname string @@ -635,11 +633,14 @@ type internalLabels struct { // 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 @@ -753,6 +754,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 } @@ -765,6 +790,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 f16fb54552d..341666cad1e 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, @@ -689,6 +708,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 7b80ee911eb..2374463cc76 100644 --- a/pkg/labels/labels.go +++ b/pkg/labels/labels.go @@ -115,4 +115,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 dadf92906b1507ff6f5cb5b7576090ff0687fc10 Mon Sep 17 00:00:00 2001 From: Arjun Raja Yogidas Date: Mon, 20 Jan 2025 04:31:19 +0000 Subject: [PATCH 08/15] 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 341666cad1e..b547e6f16a1 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, @@ -732,6 +736,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 448aad6823a9fe2bc0e94a856b909918652174a8 Mon Sep 17 00:00:00 2001 From: Arjun Raja Yogidas Date: Mon, 20 Jan 2025 20:11:16 +0000 Subject: [PATCH 09/15] 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 b547e6f16a1..93b212b4069 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, @@ -744,6 +760,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 1b49e6e607e235c1dfabada960b4fe8f95b54e97 Mon Sep 17 00:00:00 2001 From: Arjun Raja Yogidas Date: Tue, 21 Jan 2025 02:42:46 +0000 Subject: [PATCH 10/15] 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 93b212b4069..9922a0de81a 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, @@ -793,6 +801,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 df7b2624e2061368eb0132c7a3e9cf9ed3a1c1f0 Mon Sep 17 00:00:00 2001 From: Arjun Raja Yogidas Date: Tue, 21 Jan 2025 04:10:07 +0000 Subject: [PATCH 11/15] 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 9922a0de81a..9642f4b0287 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 60db863b9903069c5e0c266b7922eab9ac6c4811 Mon Sep 17 00:00:00 2001 From: Arjun Raja Yogidas Date: Tue, 21 Jan 2025 21:41:32 +0000 Subject: [PATCH 12/15] 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 4286ae189e5..b4e85b275cd 100644 --- a/pkg/cmd/container/create.go +++ b/pkg/cmd/container/create.go @@ -659,6 +659,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. @@ -759,7 +762,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 { @@ -778,6 +781,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 9642f4b0287..ebc799f9aa6 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) } @@ -482,6 +483,12 @@ func ContainerFromNative(n *native.Container) (*Container, error) { c.Config.Domainname = n.Labels[labels.Domainname] } + c.HostConfig.Devices = []string{} + if nedctlDeviceMapping := n.Labels[labels.DeviceMapping]; nedctlDeviceMapping != "" { + devices, _ := parseDeviceMapping(nedctlDeviceMapping) + c.HostConfig.Devices = devices + } + return c, nil } @@ -731,10 +738,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) } @@ -777,7 +784,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 { @@ -804,6 +811,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 2374463cc76..ecc5563805a 100644 --- a/pkg/labels/labels.go +++ b/pkg/labels/labels.go @@ -117,11 +117,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 128b4a53e2ce69a8a44ff25604bf3ad5cacefa39 Mon Sep 17 00:00:00 2001 From: Arjun Raja Yogidas Date: Wed, 22 Jan 2025 15:43:58 +0000 Subject: [PATCH 13/15] 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 b4e85b275cd..259525d8534 100644 --- a/pkg/cmd/container/create.go +++ b/pkg/cmd/container/create.go @@ -667,6 +667,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 @@ -750,44 +752,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 ebc799f9aa6..b22069d22e7 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 { @@ -483,11 +482,7 @@ func ContainerFromNative(n *native.Container) (*Container, error) { c.Config.Domainname = n.Labels[labels.Domainname] } - 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 } @@ -741,24 +736,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 ecc5563805a..fa23edbe9cd 100644 --- a/pkg/labels/labels.go +++ b/pkg/labels/labels.go @@ -104,27 +104,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 fd0a2c097c1353341d8c7257cb60c582794b0dbb Mon Sep 17 00:00:00 2001 From: Arjun Raja Yogidas Date: Wed, 22 Jan 2025 21:18:56 +0000 Subject: [PATCH 14/15] 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 b22069d22e7..22306cc1d46 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 @@ -484,6 +492,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 } @@ -556,6 +569,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: @@ -805,15 +830,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 d10510accd93e44e36ce1c586823667dc9bf0704 Mon Sep 17 00:00:00 2001 From: Arjun Raja Yogidas Date: Fri, 24 Jan 2025 22:42:01 +0000 Subject: [PATCH 15/15] chore: fix compatability errors Signed-off-by: Arjun Raja Yogidas --- .../container/container_inspect_linux_test.go | 127 ++++++++++++++---- pkg/cmd/container/create.go | 7 +- pkg/cmd/container/run_cgroup_linux.go | 7 +- pkg/inspecttypes/dockercompat/dockercompat.go | 83 +++++------- .../dockercompat/dockercompat_test.go | 10 +- 5 files changed, 146 insertions(+), 88 deletions(-) diff --git a/cmd/nerdctl/container/container_inspect_linux_test.go b/cmd/nerdctl/container/container_inspect_linux_test.go index 9528d0553e0..2231dda76eb 100644 --- a/cmd/nerdctl/container/container_inspect_linux_test.go +++ b/cmd/nerdctl/container/container_inspect_linux_test.go @@ -18,14 +18,17 @@ package container import ( "fmt" + "os" "strings" "testing" "github.com/docker/go-connections/nat" "gotest.tools/v3/assert" + "github.com/containerd/nerdctl/v2/pkg/infoutil" "github.com/containerd/nerdctl/v2/pkg/inspecttypes/dockercompat" "github.com/containerd/nerdctl/v2/pkg/labels" + "github.com/containerd/nerdctl/v2/pkg/rootlessutil" "github.com/containerd/nerdctl/v2/pkg/testutil" ) @@ -68,13 +71,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 { @@ -232,6 +234,9 @@ func TestContainerInspectState(t *testing.T) { func TestContainerInspectHostConfig(t *testing.T) { testContainer := testutil.Identifier(t) + if rootlessutil.IsRootless() && infoutil.CgroupsVersion() == "1" { + t.Skip("test skipped for rootless containers on cgroup v1") + } base := testutil.NewBase(t) defer base.Cmd("rm", "-f", testContainer).Run() @@ -249,13 +254,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 +268,16 @@ 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) { @@ -291,26 +286,41 @@ func TestContainerInspectHostConfigDefaults(t *testing.T) { base := testutil.NewBase(t) defer base.Cmd("rm", "-f", testContainer).Run() + var hc hostConfigValues + + if testutil.GetTarget() == testutil.Docker { + hc.Driver = "" + hc.GroupAddSize = 0 + hc.ShmSize = int64(67108864) + hc.Runtime = "runc" + } else { + hc.GroupAddSize = 10 + hc.Driver = "json-file" + hc.ShmSize = int64(0) + hc.Runtime = "io.containerd.runc.v2" + } + // Run a container without specifying HostConfig options 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, hc.GroupAddSize, 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.Driver) + assert.Equal(t, "private", inspect.HostConfig.IpcMode) + assert.Equal(t, hc.Driver, 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, "io.containerd.runc.v2", inspect.HostConfig.Runtime) + assert.Equal(t, hc.ShmSize, inspect.HostConfig.ShmSize) + assert.Equal(t, hc.Runtime, inspect.HostConfig.Runtime) assert.Equal(t, 0, len(inspect.HostConfig.Sysctls)) assert.Equal(t, 0, len(inspect.HostConfig.Devices)) } @@ -364,8 +374,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 +383,23 @@ 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()) + + var hc hostConfigValues + + if testutil.GetTarget() == testutil.Docker { + hc.PidMode = "container:" + containerID1 + } else { + hc.PidMode = containerID1 + } + 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, hc.PidMode, inspect.HostConfig.PidMode) } @@ -390,11 +409,63 @@ 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() + + if rootlessutil.IsRootless() && infoutil.CgroupsVersion() == "1" { + t.Skip("test skipped for rootless containers on cgroup v1") + } + + // Create a temporary directory + dir, err := os.MkdirTemp(t.TempDir(), "device-dir") + if err != nil { + t.Fatal(err) + } + + if testutil.GetTarget() == testutil.Docker { + dir = "/dev/zero" + } + + // Run the container with the directory mapped as a device + base.Cmd("run", "-d", "--name", testContainer, + "--device", dir+":/dev/xvda", + testutil.AlpineImage, "sleep", "infinity").AssertOK() + + inspect := base.InspectContainer(testContainer) + + expectedDevices := []dockercompat.DeviceMapping{ + { + PathOnHost: dir, + 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 +} + +type hostConfigValues struct { + Driver string + ShmSize int64 + PidMode string + GroupAddSize int + Runtime string +} diff --git a/pkg/cmd/container/create.go b/pkg/cmd/container/create.go index 259525d8534..671ab145ccc 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 { @@ -661,7 +664,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. @@ -772,7 +775,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 22306cc1d46..400daefcac5 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 @@ -490,7 +501,7 @@ func ContainerFromNative(n *native.Container) (*Container, error) { c.Config.Domainname = n.Labels[labels.Domainname] } - c.HostConfig.Devices = hostConfigLabel.DeviceMapping + c.HostConfig.Devices = hostConfigLabel.Devices var pidMode string if n.Labels[labels.PIDContainer] != "" { @@ -569,18 +580,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: @@ -800,28 +799,6 @@ 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 getSysctlFromNative(sp *specs.Spec) (map[string]string, error) { var res map[string]string if sp.Linux != nil && sp.Linux.Sysctl != nil { 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{ {