Skip to content

Commit f1437fe

Browse files
committed
chore: use native inspect instead of labels
Signed-off-by: Arjun Raja Yogidas <[email protected]>
1 parent 7b7e993 commit f1437fe

File tree

3 files changed

+61
-47
lines changed

3 files changed

+61
-47
lines changed

pkg/cmd/container/create.go

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,6 @@ func Create(ctx context.Context, client *containerd.Client, args []string, netMa
328328

329329
internalLabels.rm = containerutil.EncodeContainerRmOptLabel(options.Rm)
330330

331-
internalLabels.cpusetCpus = options.CPUSetCPUs
332-
internalLabels.cpusetMems = options.CPUSetMems
333331
internalLabels.blkioWeight = options.BlkioWeight
334332

335333
// TODO: abolish internal labels and only use annotations
@@ -749,26 +747,10 @@ func withInternalLabels(internalLabels internalLabels) (containerd.NewContainerO
749747
m[labels.BlkioWeight] = fmt.Sprintf("%d", internalLabels.blkioWeight)
750748
}
751749

752-
if internalLabels.cpusetMems != "" {
753-
m[labels.CPUSetMems] = internalLabels.cpusetMems
754-
}
755-
756-
if internalLabels.cpusetCpus != "" {
757-
m[labels.CPUSetCPUs] = internalLabels.cpusetCpus
758-
}
759-
760750
if internalLabels.cidFile != "" {
761751
m[labels.CIdFile] = internalLabels.cidFile
762752
}
763753

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-
772754
return containerd.WithAdditionalContainerLabels(m), nil
773755
}
774756

pkg/inspecttypes/dockercompat/dockercompat.go

Lines changed: 61 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,10 @@ type HostConfig struct {
146146
PortBindings nat.PortMap // Port mapping between the exposed port (container) and the host
147147
LogConfig LogConfig // Configuration of the logs for this container
148148
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
149+
CPUSetMems string `json:"CpusetMems"` // CpusetMems 0-2, 0,1
150+
CPUSetCPUs string `json:"CpusetCpus"` // CpusetCpus 0-2, 0,1
151+
CPUQuota int64 `json:"CpuQuota"` // CPU CFS (Completely Fair Scheduler) quota
152+
CPUShares uint64 `json:"CpuShares"` // CPU shares (relative weight vs. other containers)
151153
ContainerIDFile string // File (path) where the containerId is written
152154
GroupAdd []string // GroupAdd specifies additional groups to join
153155
IpcMode string // IPC namespace to use for the container
@@ -218,6 +220,13 @@ type NetworkSettings struct {
218220
Networks map[string]*NetworkEndpointSettings
219221
}
220222

223+
type CPUSettings struct {
224+
cpuSetCpus string
225+
cpuSetMems string
226+
cpuShares uint64
227+
cpuQuota int64
228+
}
229+
221230
// DefaultNetworkSettings is from https://github.com/moby/moby/blob/v20.10.1/api/types/types.go#L405-L414
222231
type DefaultNetworkSettings struct {
223232
// TODO EndpointID string // EndpointID uniquely represents a service endpoint in a Sandbox
@@ -343,22 +352,17 @@ func ContainerFromNative(n *native.Container) (*Container, error) {
343352
c.HostConfig.BlkioWeight = blkioWeight
344353
}
345354

346-
if cpusetmems := n.Labels[labels.CPUSetMems]; cpusetmems != "" {
347-
c.HostConfig.CpusetMems = cpusetmems
348-
}
349-
350-
if cpusetcpus := n.Labels[labels.CPUSetCPUs]; cpusetcpus != "" {
351-
c.HostConfig.CpusetCpus = cpusetcpus
352-
}
353-
354355
if cidFile := n.Labels[labels.CIdFile]; cidFile != "" {
355356
c.HostConfig.ContainerIDFile = cidFile
356357
}
357358

358-
if groupAdd := n.Labels[labels.GroupAdd]; groupAdd != "" {
359-
c.HostConfig.GroupAdd = parseGroups(groupAdd)
359+
groupAdd, err := groupAddFromNative(n.Spec.(*specs.Spec))
360+
if err != nil {
361+
return nil, fmt.Errorf("failed to groupAdd from native spec: %v", err)
360362
}
361363

364+
c.HostConfig.GroupAdd = groupAdd
365+
362366
if ipcMode := n.Labels[labels.IPC]; ipcMode != "" {
363367
ipc, err := ipcutil.DecodeIPCLabel(ipcMode)
364368
if err != nil {
@@ -395,6 +399,16 @@ func ContainerFromNative(n *native.Container) (*Container, error) {
395399
c.NetworkSettings = nSettings
396400
c.HostConfig.PortBindings = *nSettings.Ports
397401
}
402+
403+
cpuSetting, err := cpuSettingsFromNative(n.Spec.(*specs.Spec))
404+
if err != nil {
405+
return nil, fmt.Errorf("failed to Decode cpuSettings: %v", err)
406+
}
407+
c.HostConfig.CPUSetCPUs = cpuSetting.cpuSetCpus
408+
c.HostConfig.CPUSetMems = cpuSetting.cpuSetMems
409+
c.HostConfig.CPUQuota = cpuSetting.cpuQuota
410+
c.HostConfig.CPUShares = cpuSetting.cpuShares
411+
398412
c.State = cs
399413
c.Config = &Config{
400414
Labels: n.Labels,
@@ -561,6 +575,41 @@ func networkSettingsFromNative(n *native.NetNS, sp *specs.Spec) (*NetworkSetting
561575
return res, nil
562576
}
563577

578+
func cpuSettingsFromNative(sp *specs.Spec) (*CPUSettings, error) {
579+
res := &CPUSettings{}
580+
if sp.Linux != nil && sp.Linux.Resources != nil && sp.Linux.Resources.CPU != nil {
581+
if sp.Linux.Resources.CPU.Cpus != "" {
582+
res.cpuSetCpus = sp.Linux.Resources.CPU.Cpus
583+
}
584+
585+
if sp.Linux.Resources.CPU.Mems != "" {
586+
res.cpuSetMems = sp.Linux.Resources.CPU.Mems
587+
}
588+
589+
if sp.Linux.Resources.CPU.Shares != nil && *sp.Linux.Resources.CPU.Shares > 0 {
590+
res.cpuShares = *sp.Linux.Resources.CPU.Shares
591+
}
592+
593+
if sp.Linux.Resources.CPU.Quota != nil && *sp.Linux.Resources.CPU.Quota > 0 {
594+
res.cpuQuota = *sp.Linux.Resources.CPU.Quota
595+
}
596+
}
597+
598+
return res, nil
599+
}
600+
601+
func groupAddFromNative(sp *specs.Spec) ([]string, error) {
602+
res := []string{}
603+
if sp.Process != nil && sp.Process.User.AdditionalGids != nil {
604+
for _, gid := range sp.Process.User.AdditionalGids {
605+
if gid != 0 {
606+
res = append(res, strconv.FormatUint(uint64(gid), 10))
607+
}
608+
}
609+
}
610+
return res, nil
611+
}
612+
564613
func convertToNatPort(portMappings []cni.PortMapping) (*nat.PortMap, error) {
565614
portMap := make(nat.PortMap)
566615
for _, portMapping := range portMappings {
@@ -588,14 +637,6 @@ func parseExtraHosts(extraHostsJSON string) []string {
588637
return extraHosts
589638
}
590639

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-
599640
type IPAMConfig struct {
600641
Subnet string `json:"Subnet,omitempty"`
601642
Gateway string `json:"Gateway,omitempty"`

pkg/labels/labels.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,6 @@ const (
110110
// BlkioWeight to check if the --blkio-weight is specified
111111
BlkioWeight = Prefix + "blkio-weight"
112112

113-
// CPUSetCPUs to check if the --cpuset-cpus is specified
114-
CPUSetCPUs = Prefix + "cpuset-cpus"
115-
116-
// CPUSetMems to check if the --cpuset-mems is specified
117-
CPUSetMems = Prefix + "cpuset-mems"
118-
119113
// Cidfile is the ContainerId file set via the --cidfile flag
120114
CIdFile = Prefix + "cid-file"
121-
122-
// GroupAdd is the List of additional groups, set via --group-add flag
123-
GroupAdd = Prefix + "group-add"
124115
)

0 commit comments

Comments
 (0)