@@ -146,8 +146,10 @@ type HostConfig struct {
146
146
PortBindings nat.PortMap // Port mapping between the exposed port (container) and the host
147
147
LogConfig LogConfig // Configuration of the logs for this container
148
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
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)
151
153
ContainerIDFile string // File (path) where the containerId is written
152
154
GroupAdd []string // GroupAdd specifies additional groups to join
153
155
IpcMode string // IPC namespace to use for the container
@@ -218,6 +220,13 @@ type NetworkSettings struct {
218
220
Networks map [string ]* NetworkEndpointSettings
219
221
}
220
222
223
+ type CPUSettings struct {
224
+ cpuSetCpus string
225
+ cpuSetMems string
226
+ cpuShares uint64
227
+ cpuQuota int64
228
+ }
229
+
221
230
// DefaultNetworkSettings is from https://github.com/moby/moby/blob/v20.10.1/api/types/types.go#L405-L414
222
231
type DefaultNetworkSettings struct {
223
232
// TODO EndpointID string // EndpointID uniquely represents a service endpoint in a Sandbox
@@ -343,22 +352,17 @@ func ContainerFromNative(n *native.Container) (*Container, error) {
343
352
c .HostConfig .BlkioWeight = blkioWeight
344
353
}
345
354
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
-
354
355
if cidFile := n .Labels [labels .CIdFile ]; cidFile != "" {
355
356
c .HostConfig .ContainerIDFile = cidFile
356
357
}
357
358
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 )
360
362
}
361
363
364
+ c .HostConfig .GroupAdd = groupAdd
365
+
362
366
if ipcMode := n .Labels [labels .IPC ]; ipcMode != "" {
363
367
ipc , err := ipcutil .DecodeIPCLabel (ipcMode )
364
368
if err != nil {
@@ -395,6 +399,16 @@ func ContainerFromNative(n *native.Container) (*Container, error) {
395
399
c .NetworkSettings = nSettings
396
400
c .HostConfig .PortBindings = * nSettings .Ports
397
401
}
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
+
398
412
c .State = cs
399
413
c .Config = & Config {
400
414
Labels : n .Labels ,
@@ -561,6 +575,41 @@ func networkSettingsFromNative(n *native.NetNS, sp *specs.Spec) (*NetworkSetting
561
575
return res , nil
562
576
}
563
577
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
+
564
613
func convertToNatPort (portMappings []cni.PortMapping ) (* nat.PortMap , error ) {
565
614
portMap := make (nat.PortMap )
566
615
for _ , portMapping := range portMappings {
@@ -588,14 +637,6 @@ func parseExtraHosts(extraHostsJSON string) []string {
588
637
return extraHosts
589
638
}
590
639
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
-
599
640
type IPAMConfig struct {
600
641
Subnet string `json:"Subnet,omitempty"`
601
642
Gateway string `json:"Gateway,omitempty"`
0 commit comments