From bee756f800986cccfe259178cf3404c773aa7b8f Mon Sep 17 00:00:00 2001 From: Arjun Raja Yogidas Date: Thu, 2 Jan 2025 21:50:23 +0000 Subject: [PATCH 01/17] 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 c7f58409225..155069e48bd 100644 --- a/pkg/cmd/container/create.go +++ b/pkg/cmd/container/create.go @@ -222,6 +222,7 @@ func Create(ctx context.Context, client *containerd.Client, args []string, netMa return nil, generateRemoveStateDirFunc(ctx, id, internalLabels), err } internalLabels.logURI = logConfig.LogURI + internalLabels.logConfig = logConfig restartOpts, err := generateRestartOpts(ctx, client, options.Restart, logConfig.LogURI, options.InRun) if err != nil { @@ -641,7 +642,8 @@ type internalLabels struct { // log logURI string // a label to check whether the --rm option is specified. - rm string + rm string + logConfig logging.LogConfig } // WithInternalLabels sets the internal labels for a container. @@ -672,6 +674,11 @@ func withInternalLabels(internalLabels internalLabels) (containerd.NewContainerO } if internalLabels.logURI != "" { m[labels.LogURI] = internalLabels.logURI + logConfigJSON, err := json.Marshal(internalLabels.logConfig) + if err != nil { + return nil, err + } + m[labels.LogConfig] = string(logConfigJSON) } if len(internalLabels.anonVolumes) > 0 { anonVolumeJSON, err := json.Marshal(internalLabels.anonVolumes) diff --git a/pkg/inspecttypes/dockercompat/dockercompat.go b/pkg/inspecttypes/dockercompat/dockercompat.go index cd3b76ac24f..981e4fb556e 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{ @@ -493,6 +535,15 @@ func convertToNatPort(portMappings []cni.PortMapping) (*nat.PortMap, error) { return &portMap, nil } +func parseExtraHosts(extraHostsJSON string) []string { + var extraHosts []string + if err := json.Unmarshal([]byte(extraHostsJSON), &extraHosts); err != nil { + // Handle error or return empty slice + return []string{} + } + return extraHosts +} + type IPAMConfig struct { Subnet string `json:"Subnet,omitempty"` Gateway string `json:"Gateway,omitempty"` diff --git a/pkg/labels/labels.go b/pkg/labels/labels.go index e2dd6ede16d..620108afb8e 100644 --- a/pkg/labels/labels.go +++ b/pkg/labels/labels.go @@ -101,6 +101,9 @@ const ( // (like "nerdctl/default-network=true" or "nerdctl/default-network=false") NerdctlDefaultNetwork = Prefix + "default-network" + // LogConfig defines the loggin configuration passed to the container + LogConfig = Prefix + "log-config" + // ContainerAutoRemove is to check whether the --rm option is specified. ContainerAutoRemove = Prefix + "auto-remove" ) From 5244ebf40518004ed6c7b7846ed4e2617af54606 Mon Sep 17 00:00:00 2001 From: Arjun Raja Yogidas Date: Fri, 3 Jan 2025 03:52:05 +0000 Subject: [PATCH 02/17] 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 155069e48bd..39182fde0e5 100644 --- a/pkg/cmd/container/create.go +++ b/pkg/cmd/container/create.go @@ -326,6 +326,10 @@ func Create(ctx context.Context, client *containerd.Client, args []string, netMa internalLabels.rm = containerutil.EncodeContainerRmOptLabel(options.Rm) + internalLabels.cpusetCpus = options.CPUSetCPUs + internalLabels.cpusetMems = options.CPUSetMems + internalLabels.blkioWeight = options.BlkioWeight + // TODO: abolish internal labels and only use annotations ilOpt, err := withInternalLabels(internalLabels) if err != nil { @@ -617,10 +621,13 @@ func withStop(stopSignal string, stopTimeout int, ensuredImage *imgutil.EnsuredI type internalLabels struct { // labels from cmd options - namespace string - platform string - extraHosts []string - pidFile string + namespace string + platform string + extraHosts []string + pidFile string + blkioWeight uint16 + cpusetCpus string + cpusetMems string // labels from cmd options or automatically set name string hostname string @@ -730,6 +737,18 @@ func withInternalLabels(internalLabels internalLabels) (containerd.NewContainerO m[labels.ContainerAutoRemove] = internalLabels.rm } + if internalLabels.blkioWeight > 0 { + m[labels.BlkioWeight] = fmt.Sprintf("%d", internalLabels.blkioWeight) + } + + if internalLabels.cpusetMems != "" { + m[labels.CPUSetMems] = internalLabels.cpusetMems + } + + if internalLabels.cpusetCpus != "" { + m[labels.CPUSetCPUs] = internalLabels.cpusetCpus + } + return containerd.WithAdditionalContainerLabels(m), nil } diff --git a/pkg/inspecttypes/dockercompat/dockercompat.go b/pkg/inspecttypes/dockercompat/dockercompat.go index 981e4fb556e..7264f6a20a2 100644 --- a/pkg/inspecttypes/dockercompat/dockercompat.go +++ b/pkg/inspecttypes/dockercompat/dockercompat.go @@ -46,7 +46,6 @@ import ( "github.com/containerd/nerdctl/v2/pkg/imgutil" "github.com/containerd/nerdctl/v2/pkg/inspecttypes/native" "github.com/containerd/nerdctl/v2/pkg/labels" - "github.com/containerd/nerdctl/v2/pkg/logging" "github.com/containerd/nerdctl/v2/pkg/ocihook/state" ) @@ -97,7 +96,14 @@ type ImageMetadata struct { type LogConfig struct { Type string - Config logging.LogConfig + Config loggerLogConfig +} + +type loggerLogConfig struct { + Driver string `json:"driver"` + Opts map[string]string `json:"opts,omitempty"` + LogURI string `json:"-"` + Address string `json:"address"` } // Container mimics a `docker container inspect` object. @@ -138,7 +144,9 @@ type HostConfig struct { ExtraHosts []string // List of extra hosts PortBindings nat.PortMap // Port mapping between the exposed port (container) and the host LogConfig LogConfig // Configuration of the logs for this container - + BlkioWeight uint16 // Block IO weight (relative weight vs. other containers) + CpusetMems string // CpusetMems 0-2, 0,1 + CpusetCpus string // CpusetCpus 0-2, 0,1 } // From https://github.com/moby/moby/blob/v20.10.1/api/types/types.go#L416-L427 @@ -304,10 +312,9 @@ func ContainerFromNative(n *native.Container) (*Container, error) { if nerdctlLoguri := n.Labels[labels.LogURI]; nerdctlLoguri != "" { c.HostConfig.LogConfig.Type = nerdctlLoguri - // c.HostConfig.LogConfig.Config = map[string]string{} } if logConfigJSON, ok := n.Labels[labels.LogConfig]; ok { - var logConfig logging.LogConfig + var logConfig loggerLogConfig err := json.Unmarshal([]byte(logConfigJSON), &logConfig) if err != nil { return nil, fmt.Errorf("failed to unmarshal log config: %v", err) @@ -317,12 +324,29 @@ func ContainerFromNative(n *native.Container) (*Container, error) { c.HostConfig.LogConfig.Config = logConfig } else { // If LogConfig label is not present, set default values - c.HostConfig.LogConfig.Config = logging.LogConfig{ + c.HostConfig.LogConfig.Config = loggerLogConfig{ Driver: "json-file", Opts: make(map[string]string), } } + if blkioWeightSet := n.Labels[labels.BlkioWeight]; blkioWeightSet != "" { + var blkioWeight uint16 + _, err := fmt.Sscanf(blkioWeightSet, "%d", &blkioWeight) + if err != nil { + return nil, fmt.Errorf("failed to convert string to uint: %v", err) + } + c.HostConfig.BlkioWeight = blkioWeight + } + + if cpusetmems := n.Labels[labels.CPUSetMems]; cpusetmems != "" { + c.HostConfig.CpusetMems = cpusetmems + } + + if cpusetcpus := n.Labels[labels.CPUSetCPUs]; cpusetcpus != "" { + c.HostConfig.CpusetCpus = cpusetcpus + } + cs := new(ContainerState) cs.Restarting = n.Labels[restart.StatusLabel] == string(containerd.Running) cs.Error = n.Labels[labels.Error] diff --git a/pkg/labels/labels.go b/pkg/labels/labels.go index 620108afb8e..703159e259b 100644 --- a/pkg/labels/labels.go +++ b/pkg/labels/labels.go @@ -101,9 +101,18 @@ const ( // (like "nerdctl/default-network=true" or "nerdctl/default-network=false") NerdctlDefaultNetwork = Prefix + "default-network" - // LogConfig defines the loggin configuration passed to the container + // LogConfig defines the logging configuration passed to the container LogConfig = Prefix + "log-config" // ContainerAutoRemove is to check whether the --rm option is specified. ContainerAutoRemove = Prefix + "auto-remove" + + // BlkioWeight to check if the --blkio-weight is specified + BlkioWeight = Prefix + "blkio-weight" + + // CPUSetCPUs to check if the --cpuset-cpus is specified + CPUSetCPUs = Prefix + "cpuset-cpus" + + // CPUSetMems to check if the --cpuset-mems is specified + CPUSetMems = Prefix + "cpuset-mems" ) From 7b7e993ee4af10fd0ed7961860598332f68859da Mon Sep 17 00:00:00 2001 From: Arjun Raja Yogidas Date: Thu, 16 Jan 2025 23:08:09 +0000 Subject: [PATCH 03/17] 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 39182fde0e5..40f6cb789fc 100644 --- a/pkg/cmd/container/create.go +++ b/pkg/cmd/container/create.go @@ -99,6 +99,7 @@ func Create(ctx context.Context, client *containerd.Client, args []string, netMa if err := writeCIDFile(options.CidFile, id); err != nil { return nil, nil, err } + internalLabels.cidFile = options.CidFile } dataStore, err := clientutil.DataStore(options.GOptions.DataRoot, options.GOptions.Address) if err != nil { @@ -269,6 +270,7 @@ func Create(ctx context.Context, client *containerd.Client, args []string, netMa } opts = append(opts, uOpts...) gOpts, err := generateGroupsOpts(options.GroupAdd) + internalLabels.groupAdd = options.GroupAdd if err != nil { return nil, generateRemoveOrphanedDirsFunc(ctx, id, dataStore, internalLabels), err } @@ -651,6 +653,12 @@ type internalLabels struct { // a label to check whether the --rm option is specified. rm string logConfig logging.LogConfig + + // a label to chek if --cidfile is set + cidFile string + + // label to check if --group-add is set + groupAdd []string } // WithInternalLabels sets the internal labels for a container. @@ -749,6 +757,18 @@ func withInternalLabels(internalLabels internalLabels) (containerd.NewContainerO m[labels.CPUSetCPUs] = internalLabels.cpusetCpus } + if internalLabels.cidFile != "" { + m[labels.CIdFile] = internalLabels.cidFile + } + + if len(internalLabels.groupAdd) > 0 { + groupAddJSON, err := json.Marshal(internalLabels.groupAdd) + if err != nil { + return nil, err + } + m[labels.GroupAdd] = string(groupAddJSON) + } + return containerd.WithAdditionalContainerLabels(m), nil } diff --git a/pkg/inspecttypes/dockercompat/dockercompat.go b/pkg/inspecttypes/dockercompat/dockercompat.go index 7264f6a20a2..0ae23bec948 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] @@ -568,6 +588,14 @@ func parseExtraHosts(extraHostsJSON string) []string { return extraHosts } +func parseGroups(groupAddJSON string) []string { + var groupAdd []string + if err := json.Unmarshal([]byte(groupAddJSON), &groupAdd); err != nil { + return []string{} + } + return groupAdd +} + type IPAMConfig struct { Subnet string `json:"Subnet,omitempty"` Gateway string `json:"Gateway,omitempty"` diff --git a/pkg/labels/labels.go b/pkg/labels/labels.go index 703159e259b..2a3636cf659 100644 --- a/pkg/labels/labels.go +++ b/pkg/labels/labels.go @@ -115,4 +115,10 @@ const ( // CPUSetMems to check if the --cpuset-mems is specified CPUSetMems = Prefix + "cpuset-mems" + + // Cidfile is the ContainerId file set via the --cidfile flag + CIdFile = Prefix + "cid-file" + + // GroupAdd is the List of additional groups, set via --group-add flag + GroupAdd = Prefix + "group-add" ) From f1437fe3eda2ae339b11c0a14bd0676a49a39db6 Mon Sep 17 00:00:00 2001 From: Arjun Raja Yogidas Date: Fri, 17 Jan 2025 07:54:43 +0000 Subject: [PATCH 04/17] 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 40f6cb789fc..9f83c77b007 100644 --- a/pkg/cmd/container/create.go +++ b/pkg/cmd/container/create.go @@ -328,8 +328,6 @@ func Create(ctx context.Context, client *containerd.Client, args []string, netMa internalLabels.rm = containerutil.EncodeContainerRmOptLabel(options.Rm) - internalLabels.cpusetCpus = options.CPUSetCPUs - internalLabels.cpusetMems = options.CPUSetMems internalLabels.blkioWeight = options.BlkioWeight // TODO: abolish internal labels and only use annotations @@ -749,26 +747,10 @@ func withInternalLabels(internalLabels internalLabels) (containerd.NewContainerO m[labels.BlkioWeight] = fmt.Sprintf("%d", internalLabels.blkioWeight) } - if internalLabels.cpusetMems != "" { - m[labels.CPUSetMems] = internalLabels.cpusetMems - } - - if internalLabels.cpusetCpus != "" { - m[labels.CPUSetCPUs] = internalLabels.cpusetCpus - } - if internalLabels.cidFile != "" { m[labels.CIdFile] = internalLabels.cidFile } - if len(internalLabels.groupAdd) > 0 { - groupAddJSON, err := json.Marshal(internalLabels.groupAdd) - if err != nil { - return nil, err - } - m[labels.GroupAdd] = string(groupAddJSON) - } - return containerd.WithAdditionalContainerLabels(m), nil } diff --git a/pkg/inspecttypes/dockercompat/dockercompat.go b/pkg/inspecttypes/dockercompat/dockercompat.go index 0ae23bec948..5b78f2be774 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, @@ -561,6 +575,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 { @@ -588,14 +637,6 @@ func parseExtraHosts(extraHostsJSON string) []string { return extraHosts } -func parseGroups(groupAddJSON string) []string { - var groupAdd []string - if err := json.Unmarshal([]byte(groupAddJSON), &groupAdd); err != nil { - return []string{} - } - return groupAdd -} - type IPAMConfig struct { Subnet string `json:"Subnet,omitempty"` Gateway string `json:"Gateway,omitempty"` diff --git a/pkg/labels/labels.go b/pkg/labels/labels.go index 2a3636cf659..4e3f667c51a 100644 --- a/pkg/labels/labels.go +++ b/pkg/labels/labels.go @@ -110,15 +110,6 @@ const ( // BlkioWeight to check if the --blkio-weight is specified BlkioWeight = Prefix + "blkio-weight" - // CPUSetCPUs to check if the --cpuset-cpus is specified - CPUSetCPUs = Prefix + "cpuset-cpus" - - // CPUSetMems to check if the --cpuset-mems is specified - CPUSetMems = Prefix + "cpuset-mems" - // Cidfile is the ContainerId file set via the --cidfile flag CIdFile = Prefix + "cid-file" - - // GroupAdd is the List of additional groups, set via --group-add flag - GroupAdd = Prefix + "group-add" ) From e214bb3ceb5faf5944aabf95658177ee85a152f0 Mon Sep 17 00:00:00 2001 From: Arjun Raja Yogidas Date: Fri, 17 Jan 2025 23:21:21 +0000 Subject: [PATCH 05/17] 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 49a810c0df4b7198820177ce70ec89908b6e2474 Mon Sep 17 00:00:00 2001 From: Arjun Raja Yogidas Date: Sun, 19 Jan 2025 18:51:49 +0000 Subject: [PATCH 06/17] 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 5b78f2be774..f7760df5043 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, @@ -598,6 +616,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 { @@ -637,6 +667,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"` @@ -667,6 +715,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 ac061b4be9f4898254a0a5068de2bfb038fdb645 Mon Sep 17 00:00:00 2001 From: Arjun Raja Yogidas Date: Mon, 20 Jan 2025 03:53:54 +0000 Subject: [PATCH 07/17] 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 9f83c77b007..31fcc7b5537 100644 --- a/pkg/cmd/container/create.go +++ b/pkg/cmd/container/create.go @@ -626,19 +626,20 @@ type internalLabels struct { extraHosts []string pidFile string blkioWeight uint16 - cpusetCpus string - cpusetMems string // labels from cmd options or automatically set name string hostname string // automatically generated stateDir string // network - networks []string - ipAddress string - ip6Address string - ports []cni.PortMapping - macAddress string + networks []string + ipAddress string + ip6Address string + ports []cni.PortMapping + macAddress string + dnsServers []string + dnsSearchDomains []string + dnsResolvConfOptions []string // volume mountPoints []*mountutil.Processed anonVolumes []string @@ -751,6 +752,30 @@ func withInternalLabels(internalLabels internalLabels) (containerd.NewContainerO m[labels.CIdFile] = internalLabels.cidFile } + if len(internalLabels.dnsServers) > 0 { + dnsServersJSON, err := json.Marshal(internalLabels.dnsServers) + if err != nil { + return nil, err + } + m[labels.DnsServer] = string(dnsServersJSON) + } + + if len(internalLabels.dnsSearchDomains) > 0 { + dnsSearchJSON, err := json.Marshal(internalLabels.dnsSearchDomains) + if err != nil { + return nil, err + } + m[labels.DNSSearchDomains] = string(dnsSearchJSON) + } + + if len(internalLabels.dnsResolvConfOptions) > 0 { + dnsResolvConfOptionsJSON, err := json.Marshal(internalLabels.dnsResolvConfOptions) + if err != nil { + return nil, err + } + m[labels.DNSResolvConfOptions] = string(dnsResolvConfOptionsJSON) + } + return containerd.WithAdditionalContainerLabels(m), nil } @@ -762,6 +787,9 @@ func (il *internalLabels) loadNetOpts(opts types.NetworkOptions) { il.ip6Address = opts.IP6Address il.networks = opts.NetworkSlice il.macAddress = opts.MACAddress + il.dnsServers = opts.DNSServers + il.dnsSearchDomains = opts.DNSSearchDomains + il.dnsResolvConfOptions = opts.DNSResolvConfOptions } func dockercompatMounts(mountPoints []*mountutil.Processed) []dockercompat.MountPoint { diff --git a/pkg/inspecttypes/dockercompat/dockercompat.go b/pkg/inspecttypes/dockercompat/dockercompat.go index f7760df5043..03807fa2206 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, @@ -685,6 +704,30 @@ func getMemorySettingsFromNative(sp *specs.Spec) (*MemorySetting, error) { return res, nil } +func getDnsFromNative(Labels map[string]string) (*DNSSettings, error) { + res := &DNSSettings{} + + if dnsServers := Labels[labels.DnsServer]; dnsServers != "" { + if err := json.Unmarshal([]byte(dnsServers), &res.DNSServers); err != nil { + return nil, fmt.Errorf("failed to parse DNS servers: %v", err) + } + } + + if dnsOptions := Labels[labels.DNSResolvConfOptions]; dnsOptions != "" { + if err := json.Unmarshal([]byte(dnsOptions), &res.DNSResolvConfOptions); err != nil { + return nil, fmt.Errorf("failed to parse DNS options: %v", err) + } + } + + if dnsSearch := Labels[labels.DNSSearchDomains]; dnsSearch != "" { + if err := json.Unmarshal([]byte(dnsSearch), &res.DNSSearchDomains); err != nil { + return nil, fmt.Errorf("failed to parse DNS search domains: %v", err) + } + } + + return res, nil +} + type IPAMConfig struct { Subnet string `json:"Subnet,omitempty"` Gateway string `json:"Gateway,omitempty"` diff --git a/pkg/labels/labels.go b/pkg/labels/labels.go index 4e3f667c51a..1229c16470e 100644 --- a/pkg/labels/labels.go +++ b/pkg/labels/labels.go @@ -112,4 +112,13 @@ const ( // Cidfile is the ContainerId file set via the --cidfile flag CIdFile = Prefix + "cid-file" + + // Custom DNS lookup servers. + DnsServer = Prefix + "dns" + + // DNSResolvConfOptions set DNS options + DNSResolvConfOptions = Prefix + "dns-options" + + // DNSSearchDomains set custom DNS search domains + DNSSearchDomains = Prefix + "dns-search" ) From 4c6ed57fbea3ec8243a97347f44d70a46a164219 Mon Sep 17 00:00:00 2001 From: Arjun Raja Yogidas Date: Mon, 20 Jan 2025 04:31:19 +0000 Subject: [PATCH 08/17] 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 03807fa2206..0be81146ef4 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, @@ -728,6 +732,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 3b9fa1615853dd0b8861043a4dd77eecf24284db Mon Sep 17 00:00:00 2001 From: Arjun Raja Yogidas Date: Mon, 20 Jan 2025 20:11:16 +0000 Subject: [PATCH 09/17] 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 0be81146ef4..8e0f3cbf1c5 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, @@ -740,6 +756,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 b0a9fb5b199301bbfa516fe2fb0b3ef8dbe36d86 Mon Sep 17 00:00:00 2001 From: Arjun Raja Yogidas Date: Tue, 21 Jan 2025 02:42:46 +0000 Subject: [PATCH 10/17] 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 8e0f3cbf1c5..dbf6b564f2c 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, @@ -789,6 +797,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 230b659b56cbb1f624485fee8d04dba7e74cd4e8 Mon Sep 17 00:00:00 2001 From: Arjun Raja Yogidas Date: Tue, 21 Jan 2025 04:10:07 +0000 Subject: [PATCH 11/17] 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 dbf6b564f2c..593ffd57a5f 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 42a83c8b4425607b044c356560e1e17fbdf83a37 Mon Sep 17 00:00:00 2001 From: Arjun Raja Yogidas Date: Tue, 21 Jan 2025 21:41:32 +0000 Subject: [PATCH 12/17] 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 31fcc7b5537..88678529f00 100644 --- a/pkg/cmd/container/create.go +++ b/pkg/cmd/container/create.go @@ -658,6 +658,9 @@ type internalLabels struct { // label to check if --group-add is set groupAdd []string + + // label for device mapping set by the --device flag + deviceMapping []string } // WithInternalLabels sets the internal labels for a container. @@ -757,7 +760,7 @@ func withInternalLabels(internalLabels internalLabels) (containerd.NewContainerO if err != nil { return nil, err } - m[labels.DnsServer] = string(dnsServersJSON) + m[labels.DNSServer] = string(dnsServersJSON) } if len(internalLabels.dnsSearchDomains) > 0 { @@ -776,6 +779,14 @@ func withInternalLabels(internalLabels internalLabels) (containerd.NewContainerO m[labels.DNSResolvConfOptions] = string(dnsResolvConfOptionsJSON) } + if len(internalLabels.deviceMapping) > 0 { + devicesJSON, err := json.Marshal(internalLabels.deviceMapping) + if err != nil { + return nil, err + } + m[labels.DeviceMapping] = string(devicesJSON) + } + return containerd.WithAdditionalContainerLabels(m), nil } diff --git a/pkg/cmd/container/run_cgroup_linux.go b/pkg/cmd/container/run_cgroup_linux.go index af43b12c1fd..cfb668debde 100644 --- a/pkg/cmd/container/run_cgroup_linux.go +++ b/pkg/cmd/container/run_cgroup_linux.go @@ -41,7 +41,7 @@ type customMemoryOptions struct { disableOOMKiller *bool } -func generateCgroupOpts(id string, options types.ContainerCreateOptions) ([]oci.SpecOpts, error) { +func generateCgroupOpts(id string, options types.ContainerCreateOptions, internalLabels *internalLabels) ([]oci.SpecOpts, error) { if options.KernelMemory != "" { log.L.Warnf("The --kernel-memory flag is no longer supported. This flag is a noop.") } @@ -206,6 +206,7 @@ func generateCgroupOpts(id string, options types.ContainerCreateOptions) ([]oci. return nil, fmt.Errorf("failed to parse device %q: %w", f, err) } opts = append(opts, oci.WithDevices(devPath, conPath, mode)) + internalLabels.deviceMapping = append(internalLabels.deviceMapping, f) } return opts, nil diff --git a/pkg/cmd/container/run_linux.go b/pkg/cmd/container/run_linux.go index cbe38d62e46..3280d3e532d 100644 --- a/pkg/cmd/container/run_linux.go +++ b/pkg/cmd/container/run_linux.go @@ -56,7 +56,7 @@ func setPlatformOptions(ctx context.Context, client *containerd.Client, id, uts {Type: "cgroup", Source: "cgroup", Destination: "/sys/fs/cgroup", Options: []string{"ro", "nosuid", "noexec", "nodev"}}, })) - cgOpts, err := generateCgroupOpts(id, options) + cgOpts, err := generateCgroupOpts(id, options, internalLabels) if err != nil { return nil, err } diff --git a/pkg/inspecttypes/dockercompat/dockercompat.go b/pkg/inspecttypes/dockercompat/dockercompat.go index 593ffd57a5f..e7d8a4ff1b7 100644 --- a/pkg/inspecttypes/dockercompat/dockercompat.go +++ b/pkg/inspecttypes/dockercompat/dockercompat.go @@ -162,6 +162,7 @@ type HostConfig struct { ShmSize int64 // Size of /dev/shm in bytes. The size must be greater than 0. Sysctls map[string]string // List of Namespaced sysctls used for the container Runtime string // Runtime to use with this container + Devices []string // List of devices to map inside the container } // From https://github.com/moby/moby/blob/v20.10.1/api/types/types.go#L416-L427 @@ -439,7 +440,7 @@ func ContainerFromNative(n *native.Container) (*Container, error) { c.HostConfig.Memory = memorySettings.Limit c.HostConfig.MemorySwap = memorySettings.Swap - dnsSettings, err := getDnsFromNative(n.Labels) + dnsSettings, err := getDNSFromNative(n.Labels) if err != nil { return nil, fmt.Errorf("failed to Decode dns Settings: %v", err) } @@ -478,6 +479,12 @@ func ContainerFromNative(n *native.Container) (*Container, error) { } c.Config.Hostname = hostname + c.HostConfig.Devices = []string{} + if nedctlDeviceMapping := n.Labels[labels.DeviceMapping]; nedctlDeviceMapping != "" { + devices, _ := parseDeviceMapping(nedctlDeviceMapping) + c.HostConfig.Devices = devices + } + return c, nil } @@ -727,10 +734,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) } @@ -773,7 +780,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 { @@ -800,6 +807,15 @@ func getSysctlFromNative(sp *specs.Spec) (map[string]string, error) { return res, nil } +func parseDeviceMapping(deviceMappingJSON string) ([]string, error) { + var devices []string + err := json.Unmarshal([]byte(deviceMappingJSON), &devices) + if err != nil { + return nil, fmt.Errorf("failed to parse device mapping: %v", err) + } + return devices, nil +} + type IPAMConfig struct { Subnet string `json:"Subnet,omitempty"` Gateway string `json:"Gateway,omitempty"` diff --git a/pkg/inspecttypes/dockercompat/dockercompat_test.go b/pkg/inspecttypes/dockercompat/dockercompat_test.go index f35410fac1e..4043b4cb9f0 100644 --- a/pkg/inspecttypes/dockercompat/dockercompat_test.go +++ b/pkg/inspecttypes/dockercompat/dockercompat_test.go @@ -78,13 +78,12 @@ func TestContainerFromNative(t *testing.T) { HostConfig: &HostConfig{ PortBindings: nat.PortMap{}, GroupAdd: []string{}, - LogConfig: LogConfig{ - Config: loggerLogConfig{ - Driver: "json-file", - Opts: map[string]string{}, - }, + LogConfig: loggerLogConfig{ + Driver: "json-file", + Opts: map[string]string{}, }, UTSMode: "host", + Devices: []string{}, }, Mounts: []MountPoint{ { @@ -164,13 +163,12 @@ func TestContainerFromNative(t *testing.T) { HostConfig: &HostConfig{ PortBindings: nat.PortMap{}, GroupAdd: []string{}, - LogConfig: LogConfig{ - Config: loggerLogConfig{ - Driver: "json-file", - Opts: map[string]string{}, - }, + LogConfig: loggerLogConfig{ + Driver: "json-file", + Opts: map[string]string{}, }, UTSMode: "host", + Devices: []string{}, }, Mounts: []MountPoint{ { @@ -247,13 +245,12 @@ func TestContainerFromNative(t *testing.T) { HostConfig: &HostConfig{ PortBindings: nat.PortMap{}, GroupAdd: []string{}, - LogConfig: LogConfig{ - Config: loggerLogConfig{ - Driver: "json-file", - Opts: map[string]string{}, - }, + LogConfig: loggerLogConfig{ + Driver: "json-file", + Opts: map[string]string{}, }, UTSMode: "host", + Devices: []string{}, }, Mounts: []MountPoint{ { diff --git a/pkg/labels/labels.go b/pkg/labels/labels.go index 1229c16470e..fda2d50f521 100644 --- a/pkg/labels/labels.go +++ b/pkg/labels/labels.go @@ -114,11 +114,14 @@ const ( CIdFile = Prefix + "cid-file" // Custom DNS lookup servers. - DnsServer = Prefix + "dns" + DNSServer = Prefix + "dns" // DNSResolvConfOptions set DNS options DNSResolvConfOptions = Prefix + "dns-options" // DNSSearchDomains set custom DNS search domains DNSSearchDomains = Prefix + "dns-search" + + //DeviceMapping specifies mapping host device to the container + DeviceMapping = Prefix + "devices" ) From 631a3bf16a4f572f3d446f11d6caa765f59ca55e Mon Sep 17 00:00:00 2001 From: Arjun Raja Yogidas Date: Wed, 22 Jan 2025 15:43:58 +0000 Subject: [PATCH 13/17] 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 88678529f00..df7bd52e816 100644 --- a/pkg/cmd/container/create.go +++ b/pkg/cmd/container/create.go @@ -666,6 +666,8 @@ type internalLabels struct { // WithInternalLabels sets the internal labels for a container. func withInternalLabels(internalLabels internalLabels) (containerd.NewContainerOpts, error) { m := make(map[string]string) + var hostConfigLabel dockercompat.HostConfigLabel + var dnsSettings dockercompat.DNSSettings m[labels.Namespace] = internalLabels.namespace if internalLabels.name != "" { m[labels.Name] = internalLabels.name @@ -748,44 +750,40 @@ func withInternalLabels(internalLabels internalLabels) (containerd.NewContainerO } if internalLabels.blkioWeight > 0 { - m[labels.BlkioWeight] = fmt.Sprintf("%d", internalLabels.blkioWeight) + hostConfigLabel.BlkioWeight = internalLabels.blkioWeight } if internalLabels.cidFile != "" { - m[labels.CIdFile] = internalLabels.cidFile + hostConfigLabel.CidFile = internalLabels.cidFile } if len(internalLabels.dnsServers) > 0 { - dnsServersJSON, err := json.Marshal(internalLabels.dnsServers) - if err != nil { - return nil, err - } - m[labels.DNSServer] = string(dnsServersJSON) + dnsSettings.DNSServers = internalLabels.dnsServers } if len(internalLabels.dnsSearchDomains) > 0 { - dnsSearchJSON, err := json.Marshal(internalLabels.dnsSearchDomains) - if err != nil { - return nil, err - } - m[labels.DNSSearchDomains] = string(dnsSearchJSON) + dnsSettings.DNSSearchDomains = internalLabels.dnsSearchDomains } if len(internalLabels.dnsResolvConfOptions) > 0 { - dnsResolvConfOptionsJSON, err := json.Marshal(internalLabels.dnsResolvConfOptions) - if err != nil { - return nil, err - } - m[labels.DNSResolvConfOptions] = string(dnsResolvConfOptionsJSON) + dnsSettings.DNSResolvConfOptions = internalLabels.dnsResolvConfOptions } if len(internalLabels.deviceMapping) > 0 { - devicesJSON, err := json.Marshal(internalLabels.deviceMapping) - if err != nil { - return nil, err - } - m[labels.DeviceMapping] = string(devicesJSON) + hostConfigLabel.DeviceMapping = internalLabels.deviceMapping + } + + hostConfigJSON, err := json.Marshal(hostConfigLabel) + if err != nil { + return nil, err + } + m[labels.HostConfigLabel] = string(hostConfigJSON) + + dnsSettingsJSON, err := json.Marshal(dnsSettings) + if err != nil { + return nil, err } + m[labels.DNSSetting] = string(dnsSettingsJSON) return containerd.WithAdditionalContainerLabels(m), nil } diff --git a/pkg/inspecttypes/dockercompat/dockercompat.go b/pkg/inspecttypes/dockercompat/dockercompat.go index e7d8a4ff1b7..9e1f72355d8 100644 --- a/pkg/inspecttypes/dockercompat/dockercompat.go +++ b/pkg/inspecttypes/dockercompat/dockercompat.go @@ -236,6 +236,12 @@ type DNSSettings struct { DNSSearchDomains []string } +type HostConfigLabel struct { + BlkioWeight uint16 + CidFile string + DeviceMapping []string +} + type CPUSettings struct { cpuSetCpus string cpuSetMems string @@ -359,18 +365,11 @@ func ContainerFromNative(n *native.Container) (*Container, error) { } } - if blkioWeightSet := n.Labels[labels.BlkioWeight]; blkioWeightSet != "" { - var blkioWeight uint16 - _, err := fmt.Sscanf(blkioWeightSet, "%d", &blkioWeight) - if err != nil { - return nil, fmt.Errorf("failed to convert string to uint: %v", err) - } - c.HostConfig.BlkioWeight = blkioWeight - } + // var hostConfigLabel HostConfigLabel + hostConfigLabel, err := getHostConfigLabelFromNative(n.Labels) - if cidFile := n.Labels[labels.CIdFile]; cidFile != "" { - c.HostConfig.ContainerIDFile = cidFile - } + c.HostConfig.BlkioWeight = hostConfigLabel.BlkioWeight + c.HostConfig.ContainerIDFile = hostConfigLabel.CidFile groupAdd, err := groupAddFromNative(n.Spec.(*specs.Spec)) if err != nil { @@ -479,11 +478,7 @@ func ContainerFromNative(n *native.Container) (*Container, error) { } c.Config.Hostname = hostname - c.HostConfig.Devices = []string{} - if nedctlDeviceMapping := n.Labels[labels.DeviceMapping]; nedctlDeviceMapping != "" { - devices, _ := parseDeviceMapping(nedctlDeviceMapping) - c.HostConfig.Devices = devices - } + c.HostConfig.Devices = hostConfigLabel.DeviceMapping return c, nil } @@ -737,24 +732,23 @@ func getMemorySettingsFromNative(sp *specs.Spec) (*MemorySetting, error) { func getDNSFromNative(Labels map[string]string) (*DNSSettings, error) { res := &DNSSettings{} - if dnsServers := Labels[labels.DNSServer]; dnsServers != "" { - if err := json.Unmarshal([]byte(dnsServers), &res.DNSServers); err != nil { - return nil, fmt.Errorf("failed to parse DNS servers: %v", err) + if dnsSettingJSON, ok := Labels[labels.DNSSetting]; ok { + if err := json.Unmarshal([]byte(dnsSettingJSON), &res); err != nil { + return nil, fmt.Errorf("failed to parse DNS settings: %v", err) } } - if dnsOptions := Labels[labels.DNSResolvConfOptions]; dnsOptions != "" { - if err := json.Unmarshal([]byte(dnsOptions), &res.DNSResolvConfOptions); err != nil { - return nil, fmt.Errorf("failed to parse DNS options: %v", err) - } - } + return res, nil +} - if dnsSearch := Labels[labels.DNSSearchDomains]; dnsSearch != "" { - if err := json.Unmarshal([]byte(dnsSearch), &res.DNSSearchDomains); err != nil { - return nil, fmt.Errorf("failed to parse DNS search domains: %v", err) +func getHostConfigLabelFromNative(Labels map[string]string) (*HostConfigLabel, error) { + res := &HostConfigLabel{} + + if hostConfigLabelJSON, ok := Labels[labels.HostConfigLabel]; ok { + if err := json.Unmarshal([]byte(hostConfigLabelJSON), &res); err != nil { + return nil, fmt.Errorf("failed to parse DNS servers: %v", err) } } - return res, nil } diff --git a/pkg/labels/labels.go b/pkg/labels/labels.go index fda2d50f521..b9ff58cb2cc 100644 --- a/pkg/labels/labels.go +++ b/pkg/labels/labels.go @@ -101,27 +101,15 @@ const ( // (like "nerdctl/default-network=true" or "nerdctl/default-network=false") NerdctlDefaultNetwork = Prefix + "default-network" - // LogConfig defines the logging configuration passed to the container - LogConfig = Prefix + "log-config" - // ContainerAutoRemove is to check whether the --rm option is specified. ContainerAutoRemove = Prefix + "auto-remove" - // BlkioWeight to check if the --blkio-weight is specified - BlkioWeight = Prefix + "blkio-weight" - - // Cidfile is the ContainerId file set via the --cidfile flag - CIdFile = Prefix + "cid-file" - - // Custom DNS lookup servers. - DNSServer = Prefix + "dns" - - // DNSResolvConfOptions set DNS options - DNSResolvConfOptions = Prefix + "dns-options" + // LogConfig defines the logging configuration passed to the container + LogConfig = Prefix + "log-config" - // DNSSearchDomains set custom DNS search domains - DNSSearchDomains = Prefix + "dns-search" + // HostConfigLabel sets the dockercompat host config values + HostConfigLabel = Prefix + "host-config" - //DeviceMapping specifies mapping host device to the container - DeviceMapping = Prefix + "devices" + // DNSSettings sets the dockercompat Dns config values + DNSSetting = Prefix + "dns" ) From c79d003d24f81aca458bc78ffe63ba2894c9b910 Mon Sep 17 00:00:00 2001 From: Arjun Raja Yogidas Date: Wed, 22 Jan 2025 21:18:56 +0000 Subject: [PATCH 14/17] 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 9e1f72355d8..4c018a3f93c 100644 --- a/pkg/inspecttypes/dockercompat/dockercompat.go +++ b/pkg/inspecttypes/dockercompat/dockercompat.go @@ -163,6 +163,8 @@ type HostConfig struct { Sysctls map[string]string // List of Namespaced sysctls used for the container Runtime string // Runtime to use with this container Devices []string // List of devices to map inside the container + PidMode string // PID namespace to use for the container + Tmpfs []MountPoint `json:",omitempty"` // List of tmpfs (mounts) used for the container } // From https://github.com/moby/moby/blob/v20.10.1/api/types/types.go#L416-L427 @@ -292,6 +294,7 @@ func ContainerFromNative(n *native.Container) (*Container, error) { // XXX is this always right? what if the container OS is NOT the same as the host OS? Platform: runtime.GOOS, // for Docker compatibility, this Platform string does NOT contain arch like "/amd64" } + c.HostConfig = new(HostConfig) if n.Labels[restart.StatusLabel] == string(containerd.Running) { c.RestartCount, _ = strconv.Atoi(n.Labels[restart.CountLabel]) } @@ -332,15 +335,20 @@ func ContainerFromNative(n *native.Container) (*Container, error) { } } + var tmpfsMounts []MountPoint + if nerdctlMounts := n.Labels[labels.Mounts]; nerdctlMounts != "" { mounts, err := parseMounts(nerdctlMounts) if err != nil { return nil, err } c.Mounts = mounts + if len(mounts) > 0 { + tmpfsMounts = filterTmpfsMounts(mounts) + } } + c.HostConfig.Tmpfs = tmpfsMounts - c.HostConfig = new(HostConfig) if nedctlExtraHosts := n.Labels[labels.ExtraHosts]; nedctlExtraHosts != "" { c.HostConfig.ExtraHosts = parseExtraHosts(nedctlExtraHosts) } @@ -366,7 +374,7 @@ func ContainerFromNative(n *native.Container) (*Container, error) { } // var hostConfigLabel HostConfigLabel - hostConfigLabel, err := getHostConfigLabelFromNative(n.Labels) + hostConfigLabel, _ := getHostConfigLabelFromNative(n.Labels) c.HostConfig.BlkioWeight = hostConfigLabel.BlkioWeight c.HostConfig.ContainerIDFile = hostConfigLabel.CidFile @@ -480,6 +488,11 @@ func ContainerFromNative(n *native.Container) (*Container, error) { c.HostConfig.Devices = hostConfigLabel.DeviceMapping + var pidMode string + if n.Labels[labels.PIDContainer] != "" { + pidMode = n.Labels[labels.PIDContainer] + } + c.HostConfig.PidMode = pidMode return c, nil } @@ -552,6 +565,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: @@ -801,15 +826,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 f4d4dcf4234bc19eb36f2ff23ab50ffd1fd02b8b Mon Sep 17 00:00:00 2001 From: Arjun Raja Yogidas Date: Fri, 24 Jan 2025 22:42:01 +0000 Subject: [PATCH 15/17] chore: fix compatability errors Signed-off-by: Arjun Raja Yogidas --- .../container/container_inspect_linux_test.go | 123 ++++++++++++++---- 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, 142 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..f57e8b4fecc 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,59 @@ 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() + + // 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 df7bd52e816..049b9b44085 100644 --- a/pkg/cmd/container/create.go +++ b/pkg/cmd/container/create.go @@ -224,6 +224,9 @@ func Create(ctx context.Context, client *containerd.Client, args []string, netMa } internalLabels.logURI = logConfig.LogURI internalLabels.logConfig = logConfig + if logConfig.Driver == "" && logConfig.Address == options.GOptions.Address { + internalLabels.logConfig.Driver = "json-file" + } restartOpts, err := generateRestartOpts(ctx, client, options.Restart, logConfig.LogURI, options.InRun) if err != nil { @@ -660,7 +663,7 @@ type internalLabels struct { groupAdd []string // label for device mapping set by the --device flag - deviceMapping []string + deviceMapping []dockercompat.DeviceMapping } // WithInternalLabels sets the internal labels for a container. @@ -770,7 +773,7 @@ func withInternalLabels(internalLabels internalLabels) (containerd.NewContainerO } if len(internalLabels.deviceMapping) > 0 { - hostConfigLabel.DeviceMapping = internalLabels.deviceMapping + hostConfigLabel.Devices = append(hostConfigLabel.Devices, internalLabels.deviceMapping...) } hostConfigJSON, err := json.Marshal(hostConfigLabel) diff --git a/pkg/cmd/container/run_cgroup_linux.go b/pkg/cmd/container/run_cgroup_linux.go index cfb668debde..a4d6fb7a266 100644 --- a/pkg/cmd/container/run_cgroup_linux.go +++ b/pkg/cmd/container/run_cgroup_linux.go @@ -32,6 +32,7 @@ import ( "github.com/containerd/nerdctl/v2/pkg/api/types" "github.com/containerd/nerdctl/v2/pkg/infoutil" + "github.com/containerd/nerdctl/v2/pkg/inspecttypes/dockercompat" "github.com/containerd/nerdctl/v2/pkg/rootlessutil" ) @@ -206,7 +207,11 @@ func generateCgroupOpts(id string, options types.ContainerCreateOptions, interna return nil, fmt.Errorf("failed to parse device %q: %w", f, err) } opts = append(opts, oci.WithDevices(devPath, conPath, mode)) - internalLabels.deviceMapping = append(internalLabels.deviceMapping, f) + var deviceMap dockercompat.DeviceMapping + deviceMap.PathOnHost = devPath + deviceMap.PathInContainer = conPath + deviceMap.CgroupPermissions = mode + internalLabels.deviceMapping = append(internalLabels.deviceMapping, deviceMap) } return opts, nil diff --git a/pkg/inspecttypes/dockercompat/dockercompat.go b/pkg/inspecttypes/dockercompat/dockercompat.go index 4c018a3f93c..e249ee04d60 100644 --- a/pkg/inspecttypes/dockercompat/dockercompat.go +++ b/pkg/inspecttypes/dockercompat/dockercompat.go @@ -96,7 +96,7 @@ type ImageMetadata struct { LastTagTime time.Time `json:",omitempty"` } -type loggerLogConfig struct { +type LoggerLogConfig struct { Driver string `json:"driver"` Opts map[string]string `json:"opts,omitempty"` LogURI string `json:"-"` @@ -140,7 +140,7 @@ type Container struct { type HostConfig struct { ExtraHosts []string // List of extra hosts PortBindings nat.PortMap // Port mapping between the exposed port (container) and the host - LogConfig loggerLogConfig // Configuration of the logs for this container + LogConfig LoggerLogConfig // Configuration of the logs for this container BlkioWeight uint16 // Block IO weight (relative weight vs. other containers) CPUSetMems string `json:"CpusetMems"` // CpusetMems 0-2, 0,1 CPUSetCPUs string `json:"CpusetCpus"` // CpusetCpus 0-2, 0,1 @@ -148,7 +148,7 @@ type HostConfig struct { CPUShares uint64 `json:"CpuShares"` // CPU shares (relative weight vs. other containers) ContainerIDFile string // File (path) where the containerId is written GroupAdd []string // GroupAdd specifies additional groups to join - IpcMode string // IPC namespace to use for the container + IpcMode string `json:"IpcMode"` // IPC namespace to use for the container CgroupnsMode string // Cgroup namespace mode to use for the container Memory int64 // Memory limit (in bytes) MemorySwap int64 // Total memory usage (memory + swap); set `-1` to enable unlimited swap @@ -162,9 +162,9 @@ type HostConfig struct { ShmSize int64 // Size of /dev/shm in bytes. The size must be greater than 0. Sysctls map[string]string // List of Namespaced sysctls used for the container Runtime string // Runtime to use with this container - Devices []string // List of devices to map inside the container + Devices []DeviceMapping // List of devices to map inside the container PidMode string // PID namespace to use for the container - Tmpfs []MountPoint `json:",omitempty"` // List of tmpfs (mounts) used for the container + Tmpfs map[string]string `json:"Tmpfs,omitempty"` // List of tmpfs (mounts) used for the container } // From https://github.com/moby/moby/blob/v20.10.1/api/types/types.go#L416-L427 @@ -239,9 +239,15 @@ type DNSSettings struct { } type HostConfigLabel struct { - BlkioWeight uint16 - CidFile string - DeviceMapping []string + BlkioWeight uint16 + CidFile string + Devices []DeviceMapping +} + +type DeviceMapping struct { + PathOnHost string + PathInContainer string + CgroupPermissions string } type CPUSettings struct { @@ -335,19 +341,19 @@ func ContainerFromNative(n *native.Container) (*Container, error) { } } - var tmpfsMounts []MountPoint - + c.HostConfig.Tmpfs = make(map[string]string) if nerdctlMounts := n.Labels[labels.Mounts]; nerdctlMounts != "" { mounts, err := parseMounts(nerdctlMounts) if err != nil { return nil, err } c.Mounts = mounts - if len(mounts) > 0 { - tmpfsMounts = filterTmpfsMounts(mounts) + for _, mount := range mounts { + if mount.Type == "tmpfs" { + c.HostConfig.Tmpfs[mount.Destination] = mount.Mode + } } } - c.HostConfig.Tmpfs = tmpfsMounts if nedctlExtraHosts := n.Labels[labels.ExtraHosts]; nedctlExtraHosts != "" { c.HostConfig.ExtraHosts = parseExtraHosts(nedctlExtraHosts) @@ -357,7 +363,7 @@ func ContainerFromNative(n *native.Container) (*Container, error) { c.HostConfig.LogConfig.LogURI = nerdctlLoguri } if logConfigJSON, ok := n.Labels[labels.LogConfig]; ok { - var logConfig loggerLogConfig + var logConfig LoggerLogConfig err := json.Unmarshal([]byte(logConfigJSON), &logConfig) if err != nil { return nil, fmt.Errorf("failed to unmarshal log config: %v", err) @@ -367,7 +373,7 @@ func ContainerFromNative(n *native.Container) (*Container, error) { c.HostConfig.LogConfig = logConfig } else { // If LogConfig label is not present, set default values - c.HostConfig.LogConfig = loggerLogConfig{ + c.HostConfig.LogConfig = LoggerLogConfig{ Driver: "json-file", Opts: make(map[string]string), } @@ -385,6 +391,7 @@ func ContainerFromNative(n *native.Container) (*Container, error) { } c.HostConfig.GroupAdd = groupAdd + c.HostConfig.ShmSize = 0 if ipcMode := n.Labels[labels.IPC]; ipcMode != "" { ipc, err := ipcutil.DecodeIPCLabel(ipcMode) @@ -392,6 +399,13 @@ func ContainerFromNative(n *native.Container) (*Container, error) { return nil, fmt.Errorf("failed to Decode IPC Label: %v", err) } c.HostConfig.IpcMode = string(ipc.Mode) + if ipc.ShmSize != "" { + shmSize, err := units.RAMInBytes(ipc.ShmSize) + if err != nil { + return nil, fmt.Errorf("failed to parse ShmSize: %v", err) + } + c.HostConfig.ShmSize = shmSize + } } cs := new(ContainerState) @@ -467,9 +481,6 @@ func ContainerFromNative(n *native.Container) (*Container, error) { utsMode, _ := getUtsModeFromNative(n.Spec.(*specs.Spec)) c.HostConfig.UTSMode = utsMode - shmSize, _ := getShmSizeFromNative(n.Spec.(*specs.Spec)) - c.HostConfig.ShmSize = shmSize - sysctls, _ := getSysctlFromNative(n.Spec.(*specs.Spec)) c.HostConfig.Sysctls = sysctls @@ -486,7 +497,7 @@ func ContainerFromNative(n *native.Container) (*Container, error) { } c.Config.Hostname = hostname - c.HostConfig.Devices = hostConfigLabel.DeviceMapping + c.HostConfig.Devices = hostConfigLabel.Devices var pidMode string if n.Labels[labels.PIDContainer] != "" { @@ -565,18 +576,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: @@ -796,28 +795,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{ { From 5743205c9f455702974b28c02a7f4d2f57f30ac1 Mon Sep 17 00:00:00 2001 From: Arjun Raja Yogidas Date: Sun, 2 Feb 2025 18:30:03 +0000 Subject: [PATCH 16/17] investigate devicemapping failures Signed-off-by: Arjun Raja Yogidas --- .github/workflows/test.yml | 52 +++++++++++++-------------- pkg/cmd/container/create.go | 4 +++ pkg/cmd/container/run_cgroup_linux.go | 2 ++ pkg/cmd/container/run_linux.go | 1 + 4 files changed, 33 insertions(+), 26 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 5e76c9b911d..b05a9df4b08 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -33,9 +33,9 @@ jobs: - runner: ubuntu-24.04 containerd: v2.0.2 arch: amd64 - - runner: arm64-8core-32gb - containerd: v2.0.2 - arch: arm64 + # - runner: arm64-8core-32gb + # containerd: v2.0.2 + # arch: arm64 env: CONTAINERD_VERSION: "${{ matrix.containerd }}" ARCH: "${{ matrix.arch }}" @@ -108,18 +108,18 @@ jobs: containerd: v1.6.36 runner: "ubuntu-20.04" arch: amd64 - - ubuntu: 22.04 - containerd: v1.7.25 - runner: "ubuntu-22.04" - arch: amd64 + # - ubuntu: 22.04 + # containerd: v1.7.25 + # runner: "ubuntu-22.04" + # arch: amd64 - ubuntu: 24.04 containerd: v2.0.2 runner: "ubuntu-24.04" arch: amd64 - - ubuntu: 24.04 - containerd: v2.0.2 - runner: arm64-8core-32gb - arch: arm64 + # - ubuntu: 24.04 + # containerd: v2.0.2 + # runner: arm64-8core-32gb + # arch: arm64 env: CONTAINERD_VERSION: "${{ matrix.containerd }}" ARCH: "${{ matrix.arch }}" @@ -233,21 +233,21 @@ jobs: rootlesskit: v1.1.1 # Deprecated target: rootless arch: amd64 - - ubuntu: 22.04 - containerd: v1.7.25 - rootlesskit: v2.3.2 - target: rootless - arch: amd64 - - ubuntu: 24.04 - containerd: v2.0.2 - rootlesskit: v2.3.2 - target: rootless - arch: amd64 - - ubuntu: 24.04 - containerd: v1.7.25 - rootlesskit: v2.3.2 - target: rootless-port-slirp4netns - arch: amd64 + # - ubuntu: 22.04 + # containerd: v1.7.25 + # rootlesskit: v2.3.2 + # target: rootless + # arch: amd64 + # - ubuntu: 24.04 + # containerd: v2.0.2 + # rootlesskit: v2.3.2 + # target: rootless + # arch: amd64 + # - ubuntu: 24.04 + # containerd: v1.7.25 + # rootlesskit: v2.3.2 + # target: rootless-port-slirp4netns + # arch: amd64 env: CONTAINERD_VERSION: "${{ matrix.containerd }}" ARCH: "${{ matrix.arch }}" diff --git a/pkg/cmd/container/create.go b/pkg/cmd/container/create.go index 049b9b44085..70f976ccd0f 100644 --- a/pkg/cmd/container/create.go +++ b/pkg/cmd/container/create.go @@ -118,6 +118,8 @@ func Create(ctx context.Context, client *containerd.Client, args []string, netMa oci.WithDefaultSpec(), ) + log.L.Infof("(TestContainerInspectDevices INFO) Calling setPlatformOptions with options.device = %v", options.Device) + platformOpts, err := setPlatformOptions(ctx, client, id, netManager.NetworkOptions().UTSNamespace, &internalLabels, options) if err != nil { return nil, generateRemoveStateDirFunc(ctx, id, internalLabels), err @@ -772,7 +774,9 @@ func withInternalLabels(internalLabels internalLabels) (containerd.NewContainerO dnsSettings.DNSResolvConfOptions = internalLabels.dnsResolvConfOptions } + log.L.Infof("(TestContainerInspectDevices INFO) before len(internalLabels.deviceMapping) = %v", len(internalLabels.deviceMapping)) if len(internalLabels.deviceMapping) > 0 { + log.L.Warn("(TestContainerInspectDevices INFO) pulling deviceMapping from internal labels ") hostConfigLabel.Devices = append(hostConfigLabel.Devices, internalLabels.deviceMapping...) } diff --git a/pkg/cmd/container/run_cgroup_linux.go b/pkg/cmd/container/run_cgroup_linux.go index a4d6fb7a266..e6d2211f242 100644 --- a/pkg/cmd/container/run_cgroup_linux.go +++ b/pkg/cmd/container/run_cgroup_linux.go @@ -201,6 +201,7 @@ func generateCgroupOpts(id string, options types.ContainerCreateOptions, interna return nil, fmt.Errorf("unknown cgroupns mode %q", options.Cgroupns) } + log.L.Info("(TestContainerInspectDevices INFO) before for loop") for _, f := range options.Device { devPath, conPath, mode, err := ParseDevice(f) if err != nil { @@ -212,6 +213,7 @@ func generateCgroupOpts(id string, options types.ContainerCreateOptions, interna deviceMap.PathInContainer = conPath deviceMap.CgroupPermissions = mode internalLabels.deviceMapping = append(internalLabels.deviceMapping, deviceMap) + log.L.Warnf("(TestContainerInspectDevices INFO) setting the device mapping info %v", deviceMap) } return opts, nil diff --git a/pkg/cmd/container/run_linux.go b/pkg/cmd/container/run_linux.go index 3280d3e532d..f2fe6a4d607 100644 --- a/pkg/cmd/container/run_linux.go +++ b/pkg/cmd/container/run_linux.go @@ -56,6 +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"}}, })) + log.L.Infof("(TestContainerInspectDevices INFO) Calling generateCgroupOpts with options.device = %v", options.Device) cgOpts, err := generateCgroupOpts(id, options, internalLabels) if err != nil { return nil, err From 864a486be46925f4a3d52b6ff4d5b037760e3013 Mon Sep 17 00:00:00 2001 From: Arjun Raja Yogidas Date: Tue, 11 Feb 2025 18:09:25 +0000 Subject: [PATCH 17/17] chore: investigate error cause Signed-off-by: Arjun Raja Yogidas --- .github/workflows/test.yml | 244 +++++++++--------- .../container/container_inspect_linux_test.go | 4 + 2 files changed, 126 insertions(+), 122 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index b05a9df4b08..0bce70bcd90 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -94,129 +94,129 @@ jobs: - name: "Run unit tests" run: make test-unit - test-integration: - needs: build-dependencies - timeout-minutes: 30 - name: rootful | ${{ matrix.containerd }} | ${{ matrix.runner }} - runs-on: "${{ matrix.runner }}" - strategy: - fail-fast: false - matrix: - # ubuntu-20.04: cgroup v1, ubuntu-22.04 and later: cgroup v2 - include: - - ubuntu: 20.04 - containerd: v1.6.36 - runner: "ubuntu-20.04" - arch: amd64 - # - ubuntu: 22.04 - # containerd: v1.7.25 - # runner: "ubuntu-22.04" - # arch: amd64 - - ubuntu: 24.04 - containerd: v2.0.2 - runner: "ubuntu-24.04" - arch: amd64 - # - ubuntu: 24.04 - # containerd: v2.0.2 - # runner: arm64-8core-32gb - # arch: arm64 - env: - CONTAINERD_VERSION: "${{ matrix.containerd }}" - ARCH: "${{ matrix.arch }}" - UBUNTU_VERSION: "${{ matrix.ubuntu }}" - steps: - - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 - with: - fetch-depth: 1 - - name: "Expose GitHub Runtime variables for gha" - uses: crazy-max/ghaction-github-runtime@b3a9207c0e1ef41f4cf215303c976869d0c2c1c4 # v3.0.0 - - name: "Prepare integration test environment" - run: | - docker buildx create --name with-gha --use - docker buildx build \ - --output=type=docker \ - --cache-from type=gha,scope=${ARCH}-${CONTAINERD_VERSION} \ - -t test-integration --target test-integration --build-arg UBUNTU_VERSION=${UBUNTU_VERSION} --build-arg CONTAINERD_VERSION=${CONTAINERD_VERSION} . - - name: "Remove snap loopback devices (conflicts with our loopback devices in TestRunDevice)" - run: | - sudo systemctl disable --now snapd.service snapd.socket - sudo apt-get purge -y snapd - sudo losetup -Dv - sudo losetup -lv - - name: "Register QEMU (tonistiigi/binfmt)" - run: | - # `--install all` will only install emulation for architectures that cannot be natively executed - # Since some arm64 platforms do provide native fallback execution for 32 bits, - # armv7 emulation may or may not be installed, causing variance in the result of `uname -m`. - # To avoid that, we explicitly list the architectures we do want emulation for. - docker run --privileged --rm tonistiigi/binfmt --install linux/amd64 - docker run --privileged --rm tonistiigi/binfmt --install linux/arm64 - docker run --privileged --rm tonistiigi/binfmt --install linux/arm/v7 - - name: "Run integration tests" - run: docker run -t --rm --privileged test-integration ./hack/test-integration.sh -test.only-flaky=false - - name: "Run integration tests (flaky)" - run: docker run -t --rm --privileged test-integration ./hack/test-integration.sh -test.only-flaky=true + # test-integration: + # needs: build-dependencies + # timeout-minutes: 30 + # name: rootful | ${{ matrix.containerd }} | ${{ matrix.runner }} + # runs-on: "${{ matrix.runner }}" + # strategy: + # fail-fast: false + # matrix: + # # ubuntu-20.04: cgroup v1, ubuntu-22.04 and later: cgroup v2 + # include: + # - ubuntu: 20.04 + # containerd: v1.6.36 + # runner: "ubuntu-20.04" + # arch: amd64 + # # - ubuntu: 22.04 + # # containerd: v1.7.25 + # # runner: "ubuntu-22.04" + # # arch: amd64 + # - ubuntu: 24.04 + # containerd: v2.0.2 + # runner: "ubuntu-24.04" + # arch: amd64 + # # - ubuntu: 24.04 + # # containerd: v2.0.2 + # # runner: arm64-8core-32gb + # # arch: arm64 + # env: + # CONTAINERD_VERSION: "${{ matrix.containerd }}" + # ARCH: "${{ matrix.arch }}" + # UBUNTU_VERSION: "${{ matrix.ubuntu }}" + # steps: + # - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + # with: + # fetch-depth: 1 + # - name: "Expose GitHub Runtime variables for gha" + # uses: crazy-max/ghaction-github-runtime@b3a9207c0e1ef41f4cf215303c976869d0c2c1c4 # v3.0.0 + # - name: "Prepare integration test environment" + # run: | + # docker buildx create --name with-gha --use + # docker buildx build \ + # --output=type=docker \ + # --cache-from type=gha,scope=${ARCH}-${CONTAINERD_VERSION} \ + # -t test-integration --target test-integration --build-arg UBUNTU_VERSION=${UBUNTU_VERSION} --build-arg CONTAINERD_VERSION=${CONTAINERD_VERSION} . + # - name: "Remove snap loopback devices (conflicts with our loopback devices in TestRunDevice)" + # run: | + # sudo systemctl disable --now snapd.service snapd.socket + # sudo apt-get purge -y snapd + # sudo losetup -Dv + # sudo losetup -lv + # - name: "Register QEMU (tonistiigi/binfmt)" + # run: | + # # `--install all` will only install emulation for architectures that cannot be natively executed + # # Since some arm64 platforms do provide native fallback execution for 32 bits, + # # armv7 emulation may or may not be installed, causing variance in the result of `uname -m`. + # # To avoid that, we explicitly list the architectures we do want emulation for. + # docker run --privileged --rm tonistiigi/binfmt --install linux/amd64 + # docker run --privileged --rm tonistiigi/binfmt --install linux/arm64 + # docker run --privileged --rm tonistiigi/binfmt --install linux/arm/v7 + # - name: "Run integration tests" + # run: docker run -t --rm --privileged test-integration ./hack/test-integration.sh -test.only-flaky=false + # - name: "Run integration tests (flaky)" + # run: docker run -t --rm --privileged test-integration ./hack/test-integration.sh -test.only-flaky=true - test-integration-ipv6: - needs: build-dependencies - timeout-minutes: 15 - name: ipv6 | ${{ matrix.containerd }} | ${{ matrix.ubuntu }} - runs-on: "ubuntu-${{ matrix.ubuntu }}" - strategy: - fail-fast: false - matrix: - include: - - ubuntu: 24.04 - containerd: v2.0.2 - arch: amd64 - env: - CONTAINERD_VERSION: "${{ matrix.containerd }}" - ARCH: "${{ matrix.arch }}" - UBUNTU_VERSION: "${{ matrix.ubuntu }}" - steps: - - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 - with: - fetch-depth: 1 - - name: Enable ipv4 and ipv6 forwarding - run: | - sudo sysctl -w net.ipv6.conf.all.forwarding=1 - sudo sysctl -w net.ipv4.ip_forward=1 - - name: "Expose GitHub Runtime variables for gha" - uses: crazy-max/ghaction-github-runtime@b3a9207c0e1ef41f4cf215303c976869d0c2c1c4 # v3.0.0 - - name: Enable IPv6 for Docker, and configure docker to use containerd for gha - run: | - sudo mkdir -p /etc/docker - echo '{"ipv6": true, "fixed-cidr-v6": "2001:db8:1::/64", "experimental": true, "ip6tables": true}' | sudo tee /etc/docker/daemon.json - sudo systemctl restart docker - - name: "Prepare integration test environment" - run: | - docker buildx create --name with-gha --use - docker buildx build \ - --output=type=docker \ - --cache-from type=gha,scope=${ARCH}-${CONTAINERD_VERSION} \ - -t test-integration --target test-integration --build-arg UBUNTU_VERSION=${UBUNTU_VERSION} --build-arg CONTAINERD_VERSION=${CONTAINERD_VERSION} . - - name: "Remove snap loopback devices (conflicts with our loopback devices in TestRunDevice)" - run: | - sudo systemctl disable --now snapd.service snapd.socket - sudo apt-get purge -y snapd - sudo losetup -Dv - sudo losetup -lv - - name: "Register QEMU (tonistiigi/binfmt)" - run: | - # `--install all` will only install emulation for architectures that cannot be natively executed - # Since some arm64 platforms do provide native fallback execution for 32 bits, - # armv7 emulation may or may not be installed, causing variance in the result of `uname -m`. - # To avoid that, we explicitly list the architectures we do want emulation for. - docker run --privileged --rm tonistiigi/binfmt --install linux/amd64 - docker run --privileged --rm tonistiigi/binfmt --install linux/arm64 - docker run --privileged --rm tonistiigi/binfmt --install linux/arm/v7 - - name: "Run integration tests" - # The nested IPv6 network inside docker and qemu is complex and needs a bunch of sysctl config. - # Therefore, it's hard to debug why the IPv6 tests fail in such an isolation layer. - # On the other side, using the host network is easier at configuration. - # Besides, each job is running on a different instance, which means using host network here - # is safe and has no side effects on others. - run: docker run --network host -t --rm --privileged test-integration ./hack/test-integration.sh -test.only-ipv6 + # test-integration-ipv6: + # needs: build-dependencies + # timeout-minutes: 15 + # name: ipv6 | ${{ matrix.containerd }} | ${{ matrix.ubuntu }} + # runs-on: "ubuntu-${{ matrix.ubuntu }}" + # strategy: + # fail-fast: false + # matrix: + # include: + # - ubuntu: 24.04 + # containerd: v2.0.2 + # arch: amd64 + # env: + # CONTAINERD_VERSION: "${{ matrix.containerd }}" + # ARCH: "${{ matrix.arch }}" + # UBUNTU_VERSION: "${{ matrix.ubuntu }}" + # steps: + # - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + # with: + # fetch-depth: 1 + # - name: Enable ipv4 and ipv6 forwarding + # run: | + # sudo sysctl -w net.ipv6.conf.all.forwarding=1 + # sudo sysctl -w net.ipv4.ip_forward=1 + # - name: "Expose GitHub Runtime variables for gha" + # uses: crazy-max/ghaction-github-runtime@b3a9207c0e1ef41f4cf215303c976869d0c2c1c4 # v3.0.0 + # - name: Enable IPv6 for Docker, and configure docker to use containerd for gha + # run: | + # sudo mkdir -p /etc/docker + # echo '{"ipv6": true, "fixed-cidr-v6": "2001:db8:1::/64", "experimental": true, "ip6tables": true}' | sudo tee /etc/docker/daemon.json + # sudo systemctl restart docker + # - name: "Prepare integration test environment" + # run: | + # docker buildx create --name with-gha --use + # docker buildx build \ + # --output=type=docker \ + # --cache-from type=gha,scope=${ARCH}-${CONTAINERD_VERSION} \ + # -t test-integration --target test-integration --build-arg UBUNTU_VERSION=${UBUNTU_VERSION} --build-arg CONTAINERD_VERSION=${CONTAINERD_VERSION} . + # - name: "Remove snap loopback devices (conflicts with our loopback devices in TestRunDevice)" + # run: | + # sudo systemctl disable --now snapd.service snapd.socket + # sudo apt-get purge -y snapd + # sudo losetup -Dv + # sudo losetup -lv + # - name: "Register QEMU (tonistiigi/binfmt)" + # run: | + # # `--install all` will only install emulation for architectures that cannot be natively executed + # # Since some arm64 platforms do provide native fallback execution for 32 bits, + # # armv7 emulation may or may not be installed, causing variance in the result of `uname -m`. + # # To avoid that, we explicitly list the architectures we do want emulation for. + # docker run --privileged --rm tonistiigi/binfmt --install linux/amd64 + # docker run --privileged --rm tonistiigi/binfmt --install linux/arm64 + # docker run --privileged --rm tonistiigi/binfmt --install linux/arm/v7 + # - name: "Run integration tests" + # # The nested IPv6 network inside docker and qemu is complex and needs a bunch of sysctl config. + # # Therefore, it's hard to debug why the IPv6 tests fail in such an isolation layer. + # # On the other side, using the host network is easier at configuration. + # # Besides, each job is running on a different instance, which means using host network here + # # is safe and has no side effects on others. + # run: docker run --network host -t --rm --privileged test-integration ./hack/test-integration.sh -test.only-ipv6 test-integration-rootless: needs: build-dependencies diff --git a/cmd/nerdctl/container/container_inspect_linux_test.go b/cmd/nerdctl/container/container_inspect_linux_test.go index f57e8b4fecc..2231dda76eb 100644 --- a/cmd/nerdctl/container/container_inspect_linux_test.go +++ b/cmd/nerdctl/container/container_inspect_linux_test.go @@ -422,6 +422,10 @@ func TestContainerInspectDevices(t *testing.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 {