@@ -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
222231type 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 ,
@@ -559,6 +573,41 @@ func networkSettingsFromNative(n *native.NetNS, sp *specs.Spec) (*NetworkSetting
559573 return res , nil
560574}
561575
576+ func cpuSettingsFromNative (sp * specs.Spec ) (* CPUSettings , error ) {
577+ res := & CPUSettings {}
578+ if sp .Linux != nil && sp .Linux .Resources != nil && sp .Linux .Resources .CPU != nil {
579+ if sp .Linux .Resources .CPU .Cpus != "" {
580+ res .cpuSetCpus = sp .Linux .Resources .CPU .Cpus
581+ }
582+
583+ if sp .Linux .Resources .CPU .Mems != "" {
584+ res .cpuSetMems = sp .Linux .Resources .CPU .Mems
585+ }
586+
587+ if sp .Linux .Resources .CPU .Shares != nil && * sp .Linux .Resources .CPU .Shares > 0 {
588+ res .cpuShares = * sp .Linux .Resources .CPU .Shares
589+ }
590+
591+ if sp .Linux .Resources .CPU .Quota != nil && * sp .Linux .Resources .CPU .Quota > 0 {
592+ res .cpuQuota = * sp .Linux .Resources .CPU .Quota
593+ }
594+ }
595+
596+ return res , nil
597+ }
598+
599+ func groupAddFromNative (sp * specs.Spec ) ([]string , error ) {
600+ res := []string {}
601+ if sp .Process != nil && sp .Process .User .AdditionalGids != nil {
602+ for _ , gid := range sp .Process .User .AdditionalGids {
603+ if gid != 0 {
604+ res = append (res , strconv .FormatUint (uint64 (gid ), 10 ))
605+ }
606+ }
607+ }
608+ return res , nil
609+ }
610+
562611func convertToNatPort (portMappings []cni.PortMapping ) (* nat.PortMap , error ) {
563612 portMap := make (nat.PortMap )
564613 for _ , portMapping := range portMappings {
@@ -586,14 +635,6 @@ func parseExtraHosts(extraHostsJSON string) []string {
586635 return extraHosts
587636}
588637
589- func parseGroups (groupAddJSON string ) []string {
590- var groupAdd []string
591- if err := json .Unmarshal ([]byte (groupAddJSON ), & groupAdd ); err != nil {
592- return []string {}
593- }
594- return groupAdd
595- }
596-
597638type IPAMConfig struct {
598639 Subnet string `json:"Subnet,omitempty"`
599640 Gateway string `json:"Gateway,omitempty"`
0 commit comments