Skip to content

Commit 7b7e993

Browse files
committed
chore: add CIdFile and GroupAdd to nerdctl inspect response
Signed-off-by: Arjun Raja Yogidas <[email protected]>
1 parent 5244ebf commit 7b7e993

File tree

3 files changed

+60
-6
lines changed

3 files changed

+60
-6
lines changed

pkg/cmd/container/create.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ func Create(ctx context.Context, client *containerd.Client, args []string, netMa
9999
if err := writeCIDFile(options.CidFile, id); err != nil {
100100
return nil, nil, err
101101
}
102+
internalLabels.cidFile = options.CidFile
102103
}
103104
dataStore, err := clientutil.DataStore(options.GOptions.DataRoot, options.GOptions.Address)
104105
if err != nil {
@@ -269,6 +270,7 @@ func Create(ctx context.Context, client *containerd.Client, args []string, netMa
269270
}
270271
opts = append(opts, uOpts...)
271272
gOpts, err := generateGroupsOpts(options.GroupAdd)
273+
internalLabels.groupAdd = options.GroupAdd
272274
if err != nil {
273275
return nil, generateRemoveOrphanedDirsFunc(ctx, id, dataStore, internalLabels), err
274276
}
@@ -651,6 +653,12 @@ type internalLabels struct {
651653
// a label to check whether the --rm option is specified.
652654
rm string
653655
logConfig logging.LogConfig
656+
657+
// a label to chek if --cidfile is set
658+
cidFile string
659+
660+
// label to check if --group-add is set
661+
groupAdd []string
654662
}
655663

656664
// WithInternalLabels sets the internal labels for a container.
@@ -749,6 +757,18 @@ func withInternalLabels(internalLabels internalLabels) (containerd.NewContainerO
749757
m[labels.CPUSetCPUs] = internalLabels.cpusetCpus
750758
}
751759

760+
if internalLabels.cidFile != "" {
761+
m[labels.CIdFile] = internalLabels.cidFile
762+
}
763+
764+
if len(internalLabels.groupAdd) > 0 {
765+
groupAddJSON, err := json.Marshal(internalLabels.groupAdd)
766+
if err != nil {
767+
return nil, err
768+
}
769+
m[labels.GroupAdd] = string(groupAddJSON)
770+
}
771+
752772
return containerd.WithAdditionalContainerLabels(m), nil
753773
}
754774

pkg/inspecttypes/dockercompat/dockercompat.go

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import (
4545

4646
"github.com/containerd/nerdctl/v2/pkg/imgutil"
4747
"github.com/containerd/nerdctl/v2/pkg/inspecttypes/native"
48+
"github.com/containerd/nerdctl/v2/pkg/ipcutil"
4849
"github.com/containerd/nerdctl/v2/pkg/labels"
4950
"github.com/containerd/nerdctl/v2/pkg/ocihook/state"
5051
)
@@ -141,12 +142,15 @@ type Container struct {
141142
// From https://github.com/moby/moby/blob/8dbd90ec00daa26dc45d7da2431c965dec99e8b4/api/types/container/host_config.go#L391
142143
// HostConfig the non-portable Config structure of a container.
143144
type HostConfig struct {
144-
ExtraHosts []string // List of extra hosts
145-
PortBindings nat.PortMap // Port mapping between the exposed port (container) and the host
146-
LogConfig LogConfig // Configuration of the logs for this container
147-
BlkioWeight uint16 // Block IO weight (relative weight vs. other containers)
148-
CpusetMems string // CpusetMems 0-2, 0,1
149-
CpusetCpus string // CpusetCpus 0-2, 0,1
145+
ExtraHosts []string // List of extra hosts
146+
PortBindings nat.PortMap // Port mapping between the exposed port (container) and the host
147+
LogConfig LogConfig // Configuration of the logs for this container
148+
BlkioWeight uint16 // Block IO weight (relative weight vs. other containers)
149+
CpusetMems string // CpusetMems 0-2, 0,1
150+
CpusetCpus string // CpusetCpus 0-2, 0,1
151+
ContainerIDFile string // File (path) where the containerId is written
152+
GroupAdd []string // GroupAdd specifies additional groups to join
153+
IpcMode string // IPC namespace to use for the container
150154
}
151155

152156
// 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) {
347351
c.HostConfig.CpusetCpus = cpusetcpus
348352
}
349353

354+
if cidFile := n.Labels[labels.CIdFile]; cidFile != "" {
355+
c.HostConfig.ContainerIDFile = cidFile
356+
}
357+
358+
if groupAdd := n.Labels[labels.GroupAdd]; groupAdd != "" {
359+
c.HostConfig.GroupAdd = parseGroups(groupAdd)
360+
}
361+
362+
if ipcMode := n.Labels[labels.IPC]; ipcMode != "" {
363+
ipc, err := ipcutil.DecodeIPCLabel(ipcMode)
364+
if err != nil {
365+
return nil, fmt.Errorf("failed to Decode IPC Label: %v", err)
366+
}
367+
c.HostConfig.IpcMode = string(ipc.Mode)
368+
}
369+
350370
cs := new(ContainerState)
351371
cs.Restarting = n.Labels[restart.StatusLabel] == string(containerd.Running)
352372
cs.Error = n.Labels[labels.Error]
@@ -568,6 +588,14 @@ func parseExtraHosts(extraHostsJSON string) []string {
568588
return extraHosts
569589
}
570590

591+
func parseGroups(groupAddJSON string) []string {
592+
var groupAdd []string
593+
if err := json.Unmarshal([]byte(groupAddJSON), &groupAdd); err != nil {
594+
return []string{}
595+
}
596+
return groupAdd
597+
}
598+
571599
type IPAMConfig struct {
572600
Subnet string `json:"Subnet,omitempty"`
573601
Gateway string `json:"Gateway,omitempty"`

pkg/labels/labels.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,10 @@ const (
115115

116116
// CPUSetMems to check if the --cpuset-mems is specified
117117
CPUSetMems = Prefix + "cpuset-mems"
118+
119+
// Cidfile is the ContainerId file set via the --cidfile flag
120+
CIdFile = Prefix + "cid-file"
121+
122+
// GroupAdd is the List of additional groups, set via --group-add flag
123+
GroupAdd = Prefix + "group-add"
118124
)

0 commit comments

Comments
 (0)