Skip to content

Commit e0af23d

Browse files
committed
Cleanup: Merge adjustCPUShares to adoptContainerSettings
Signed-off-by: Qiang Huang <[email protected]>
1 parent efe4f0d commit e0af23d

File tree

7 files changed

+56
-49
lines changed

7 files changed

+56
-49
lines changed

api/server/container.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,9 +397,9 @@ func (s *Server) postContainersCreate(version version.Version, w http.ResponseWr
397397
if err != nil {
398398
return err
399399
}
400-
adjustCPUShares(version, hostConfig)
400+
adjustCPUShares := version.LessThan("1.19")
401401

402-
containerID, warnings, err := s.daemon.ContainerCreate(name, config, hostConfig)
402+
containerID, warnings, err := s.daemon.ContainerCreate(name, config, hostConfig, adjustCPUShares)
403403
if err != nil {
404404
return err
405405
}

api/server/server_unix.go

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,12 @@ import (
88
"net/http"
99
"strconv"
1010

11-
"github.com/Sirupsen/logrus"
1211
"github.com/docker/docker/daemon"
1312
"github.com/docker/docker/pkg/sockets"
1413
"github.com/docker/docker/pkg/systemd"
15-
"github.com/docker/docker/pkg/version"
16-
"github.com/docker/docker/runconfig"
1714
"github.com/docker/libnetwork/portallocator"
1815
)
1916

20-
const (
21-
// See http://git.kernel.org/cgit/linux/kernel/git/tip/tip.git/tree/kernel/sched/sched.h?id=8cd9234c64c584432f6992fe944ca9e46ca8ea76#n269
22-
linuxMinCPUShares = 2
23-
linuxMaxCPUShares = 262144
24-
)
25-
2617
// newServer sets up the required serverClosers and does protocol specific checking.
2718
func (s *Server) newServer(proto, addr string) ([]serverCloser, error) {
2819
var (
@@ -110,21 +101,6 @@ func allocateDaemonPort(addr string) error {
110101
return nil
111102
}
112103

113-
func adjustCPUShares(version version.Version, hostConfig *runconfig.HostConfig) {
114-
if version.LessThan("1.19") {
115-
if hostConfig != nil && hostConfig.CPUShares > 0 {
116-
// Handle unsupported CpuShares
117-
if hostConfig.CPUShares < linuxMinCPUShares {
118-
logrus.Warnf("Changing requested CpuShares of %d to minimum allowed of %d", hostConfig.CPUShares, linuxMinCPUShares)
119-
hostConfig.CPUShares = linuxMinCPUShares
120-
} else if hostConfig.CPUShares > linuxMaxCPUShares {
121-
logrus.Warnf("Changing requested CpuShares of %d to maximum allowed of %d", hostConfig.CPUShares, linuxMaxCPUShares)
122-
hostConfig.CPUShares = linuxMaxCPUShares
123-
}
124-
}
125-
}
126-
}
127-
128104
// getContainersByNameDownlevel performs processing for pre 1.20 APIs. This
129105
// is only relevant on non-Windows daemons.
130106
func getContainersByNameDownlevel(w http.ResponseWriter, s *Server, namevar string) error {

api/server/server_windows.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import (
88
"net/http"
99

1010
"github.com/docker/docker/daemon"
11-
"github.com/docker/docker/pkg/version"
12-
"github.com/docker/docker/runconfig"
1311
)
1412

1513
// NewServer sets up the required Server and does protocol specific checking.
@@ -59,9 +57,6 @@ func allocateDaemonPort(addr string) error {
5957
return nil
6058
}
6159

62-
func adjustCPUShares(version version.Version, hostConfig *runconfig.HostConfig) {
63-
}
64-
6560
// getContainersByNameDownlevel performs processing for pre 1.20 APIs. This
6661
// is only relevant on non-Windows daemons.
6762
func getContainersByNameDownlevel(w http.ResponseWriter, s *Server, namevar string) error {

daemon/create.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ import (
1111
"github.com/opencontainers/runc/libcontainer/label"
1212
)
1313

14-
func (daemon *Daemon) ContainerCreate(name string, config *runconfig.Config, hostConfig *runconfig.HostConfig) (string, []string, error) {
14+
func (daemon *Daemon) ContainerCreate(name string, config *runconfig.Config, hostConfig *runconfig.HostConfig, adjustCPUShares bool) (string, []string, error) {
1515
if config == nil {
1616
return "", nil, fmt.Errorf("Config cannot be empty in order to create a container")
1717
}
1818

19-
daemon.adaptContainerSettings(hostConfig)
19+
daemon.adaptContainerSettings(hostConfig, adjustCPUShares)
2020
warnings, err := daemon.verifyContainerSettings(hostConfig, config)
2121
if err != nil {
2222
return "", warnings, err

daemon/daemon_unix.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ import (
3131
"github.com/opencontainers/runc/libcontainer/label"
3232
)
3333

34+
const (
35+
// See https://git.kernel.org/cgit/linux/kernel/git/tip/tip.git/tree/kernel/sched/sched.h?id=8cd9234c64c584432f6992fe944ca9e46ca8ea76#n269
36+
linuxMinCPUShares = 2
37+
linuxMaxCPUShares = 262144
38+
)
39+
3440
func (daemon *Daemon) Changes(container *Container) ([]archive.Change, error) {
3541
initID := fmt.Sprintf("%s-init", container.ID)
3642
return daemon.driver.Changes(container.ID, initID)
@@ -118,10 +124,21 @@ func checkKernel() error {
118124

119125
// adaptContainerSettings is called during container creation to modify any
120126
// settings necessary in the HostConfig structure.
121-
func (daemon *Daemon) adaptContainerSettings(hostConfig *runconfig.HostConfig) {
127+
func (daemon *Daemon) adaptContainerSettings(hostConfig *runconfig.HostConfig, adjustCPUShares bool) {
122128
if hostConfig == nil {
123129
return
124130
}
131+
132+
if adjustCPUShares && hostConfig.CPUShares > 0 {
133+
// Handle unsupported CPUShares
134+
if hostConfig.CPUShares < linuxMinCPUShares {
135+
logrus.Warnf("Changing requested CPUShares of %d to minimum allowed of %d", hostConfig.CPUShares, linuxMinCPUShares)
136+
hostConfig.CPUShares = linuxMinCPUShares
137+
} else if hostConfig.CPUShares > linuxMaxCPUShares {
138+
logrus.Warnf("Changing requested CPUShares of %d to maximum allowed of %d", hostConfig.CPUShares, linuxMaxCPUShares)
139+
hostConfig.CPUShares = linuxMaxCPUShares
140+
}
141+
}
125142
if hostConfig.Memory > 0 && hostConfig.MemorySwap == 0 {
126143
// By default, MemorySwap is set to twice the size of Memory.
127144
hostConfig.MemorySwap = hostConfig.Memory * 2
Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,86 @@
1-
// +build linux
1+
// +build !windows
22

3-
package server
3+
package daemon
44

55
import (
6+
"io/ioutil"
7+
"os"
68
"testing"
79

8-
"github.com/docker/docker/pkg/version"
910
"github.com/docker/docker/runconfig"
1011
)
1112

12-
func TestAdjustCPUSharesOldApi(t *testing.T) {
13-
apiVersion := version.Version("1.18")
13+
func TestAdjustCPUShares(t *testing.T) {
14+
tmp, err := ioutil.TempDir("", "docker-daemon-unix-test-")
15+
if err != nil {
16+
t.Fatal(err)
17+
}
18+
defer os.RemoveAll(tmp)
19+
daemon := &Daemon{
20+
repository: tmp,
21+
root: tmp,
22+
}
23+
1424
hostConfig := &runconfig.HostConfig{
1525
CPUShares: linuxMinCPUShares - 1,
1626
}
17-
adjustCPUShares(apiVersion, hostConfig)
27+
daemon.adaptContainerSettings(hostConfig, true)
1828
if hostConfig.CPUShares != linuxMinCPUShares {
1929
t.Errorf("Expected CPUShares to be %d", linuxMinCPUShares)
2030
}
2131

2232
hostConfig.CPUShares = linuxMaxCPUShares + 1
23-
adjustCPUShares(apiVersion, hostConfig)
33+
daemon.adaptContainerSettings(hostConfig, true)
2434
if hostConfig.CPUShares != linuxMaxCPUShares {
2535
t.Errorf("Expected CPUShares to be %d", linuxMaxCPUShares)
2636
}
2737

2838
hostConfig.CPUShares = 0
29-
adjustCPUShares(apiVersion, hostConfig)
39+
daemon.adaptContainerSettings(hostConfig, true)
3040
if hostConfig.CPUShares != 0 {
3141
t.Error("Expected CPUShares to be unchanged")
3242
}
3343

3444
hostConfig.CPUShares = 1024
35-
adjustCPUShares(apiVersion, hostConfig)
45+
daemon.adaptContainerSettings(hostConfig, true)
3646
if hostConfig.CPUShares != 1024 {
3747
t.Error("Expected CPUShares to be unchanged")
3848
}
3949
}
4050

4151
func TestAdjustCPUSharesNoAdjustment(t *testing.T) {
42-
apiVersion := version.Version("1.19")
52+
tmp, err := ioutil.TempDir("", "docker-daemon-unix-test-")
53+
if err != nil {
54+
t.Fatal(err)
55+
}
56+
defer os.RemoveAll(tmp)
57+
daemon := &Daemon{
58+
repository: tmp,
59+
root: tmp,
60+
}
61+
4362
hostConfig := &runconfig.HostConfig{
4463
CPUShares: linuxMinCPUShares - 1,
4564
}
46-
adjustCPUShares(apiVersion, hostConfig)
65+
daemon.adaptContainerSettings(hostConfig, false)
4766
if hostConfig.CPUShares != linuxMinCPUShares-1 {
4867
t.Errorf("Expected CPUShares to be %d", linuxMinCPUShares-1)
4968
}
5069

5170
hostConfig.CPUShares = linuxMaxCPUShares + 1
52-
adjustCPUShares(apiVersion, hostConfig)
71+
daemon.adaptContainerSettings(hostConfig, false)
5372
if hostConfig.CPUShares != linuxMaxCPUShares+1 {
5473
t.Errorf("Expected CPUShares to be %d", linuxMaxCPUShares+1)
5574
}
5675

5776
hostConfig.CPUShares = 0
58-
adjustCPUShares(apiVersion, hostConfig)
77+
daemon.adaptContainerSettings(hostConfig, false)
5978
if hostConfig.CPUShares != 0 {
6079
t.Error("Expected CPUShares to be unchanged")
6180
}
6281

6382
hostConfig.CPUShares = 1024
64-
adjustCPUShares(apiVersion, hostConfig)
83+
daemon.adaptContainerSettings(hostConfig, false)
6584
if hostConfig.CPUShares != 1024 {
6685
t.Error("Expected CPUShares to be unchanged")
6786
}

daemon/daemon_windows.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func checkKernel() error {
7575

7676
// adaptContainerSettings is called during container creation to modify any
7777
// settings necessary in the HostConfig structure.
78-
func (daemon *Daemon) adaptContainerSettings(hostConfig *runconfig.HostConfig) {
78+
func (daemon *Daemon) adaptContainerSettings(hostConfig *runconfig.HostConfig, adjustCPUShares bool) {
7979
}
8080

8181
// verifyPlatformContainerSettings performs platform-specific validation of the

0 commit comments

Comments
 (0)