Browse code

Merge pull request #15075 from hqhq/hq_move_cpushare_change

Cleanup: Merge adjustCpuShares to adoptContainerSettings

Tibor Vass authored on 2015/08/06 10:53:58
Showing 8 changed files
... ...
@@ -397,9 +397,9 @@ func (s *Server) postContainersCreate(version version.Version, w http.ResponseWr
397 397
 	if err != nil {
398 398
 		return err
399 399
 	}
400
-	adjustCPUShares(version, hostConfig)
400
+	adjustCPUShares := version.LessThan("1.19")
401 401
 
402
-	containerID, warnings, err := s.daemon.ContainerCreate(name, config, hostConfig)
402
+	containerID, warnings, err := s.daemon.ContainerCreate(name, config, hostConfig, adjustCPUShares)
403 403
 	if err != nil {
404 404
 		return err
405 405
 	}
406 406
deleted file mode 100644
... ...
@@ -1,68 +0,0 @@
1
-// +build linux
2
-
3
-package server
4
-
5
-import (
6
-	"testing"
7
-
8
-	"github.com/docker/docker/pkg/version"
9
-	"github.com/docker/docker/runconfig"
10
-)
11
-
12
-func TestAdjustCPUSharesOldApi(t *testing.T) {
13
-	apiVersion := version.Version("1.18")
14
-	hostConfig := &runconfig.HostConfig{
15
-		CPUShares: linuxMinCPUShares - 1,
16
-	}
17
-	adjustCPUShares(apiVersion, hostConfig)
18
-	if hostConfig.CPUShares != linuxMinCPUShares {
19
-		t.Errorf("Expected CPUShares to be %d", linuxMinCPUShares)
20
-	}
21
-
22
-	hostConfig.CPUShares = linuxMaxCPUShares + 1
23
-	adjustCPUShares(apiVersion, hostConfig)
24
-	if hostConfig.CPUShares != linuxMaxCPUShares {
25
-		t.Errorf("Expected CPUShares to be %d", linuxMaxCPUShares)
26
-	}
27
-
28
-	hostConfig.CPUShares = 0
29
-	adjustCPUShares(apiVersion, hostConfig)
30
-	if hostConfig.CPUShares != 0 {
31
-		t.Error("Expected CPUShares to be unchanged")
32
-	}
33
-
34
-	hostConfig.CPUShares = 1024
35
-	adjustCPUShares(apiVersion, hostConfig)
36
-	if hostConfig.CPUShares != 1024 {
37
-		t.Error("Expected CPUShares to be unchanged")
38
-	}
39
-}
40
-
41
-func TestAdjustCPUSharesNoAdjustment(t *testing.T) {
42
-	apiVersion := version.Version("1.19")
43
-	hostConfig := &runconfig.HostConfig{
44
-		CPUShares: linuxMinCPUShares - 1,
45
-	}
46
-	adjustCPUShares(apiVersion, hostConfig)
47
-	if hostConfig.CPUShares != linuxMinCPUShares-1 {
48
-		t.Errorf("Expected CPUShares to be %d", linuxMinCPUShares-1)
49
-	}
50
-
51
-	hostConfig.CPUShares = linuxMaxCPUShares + 1
52
-	adjustCPUShares(apiVersion, hostConfig)
53
-	if hostConfig.CPUShares != linuxMaxCPUShares+1 {
54
-		t.Errorf("Expected CPUShares to be %d", linuxMaxCPUShares+1)
55
-	}
56
-
57
-	hostConfig.CPUShares = 0
58
-	adjustCPUShares(apiVersion, hostConfig)
59
-	if hostConfig.CPUShares != 0 {
60
-		t.Error("Expected CPUShares to be unchanged")
61
-	}
62
-
63
-	hostConfig.CPUShares = 1024
64
-	adjustCPUShares(apiVersion, hostConfig)
65
-	if hostConfig.CPUShares != 1024 {
66
-		t.Error("Expected CPUShares to be unchanged")
67
-	}
68
-}
... ...
@@ -8,21 +8,12 @@ import (
8 8
 	"net/http"
9 9
 	"strconv"
10 10
 
11
-	"github.com/Sirupsen/logrus"
12 11
 	"github.com/docker/docker/daemon"
13 12
 	"github.com/docker/docker/pkg/sockets"
14 13
 	"github.com/docker/docker/pkg/systemd"
15
-	"github.com/docker/docker/pkg/version"
16
-	"github.com/docker/docker/runconfig"
17 14
 	"github.com/docker/libnetwork/portallocator"
18 15
 )
19 16
 
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
-
26 17
 // newServer sets up the required serverClosers and does protocol specific checking.
27 18
 func (s *Server) newServer(proto, addr string) ([]serverCloser, error) {
28 19
 	var (
... ...
@@ -110,21 +101,6 @@ func allocateDaemonPort(addr string) error {
110 110
 	return nil
111 111
 }
112 112
 
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
-
128 113
 // getContainersByNameDownlevel performs processing for pre 1.20 APIs. This
129 114
 // is only relevant on non-Windows daemons.
130 115
 func getContainersByNameDownlevel(w http.ResponseWriter, s *Server, namevar string) error {
... ...
@@ -8,8 +8,6 @@ import (
8 8
 	"net/http"
9 9
 
10 10
 	"github.com/docker/docker/daemon"
11
-	"github.com/docker/docker/pkg/version"
12
-	"github.com/docker/docker/runconfig"
13 11
 )
14 12
 
15 13
 // NewServer sets up the required Server and does protocol specific checking.
... ...
@@ -59,9 +57,6 @@ func allocateDaemonPort(addr string) error {
59 59
 	return nil
60 60
 }
61 61
 
62
-func adjustCPUShares(version version.Version, hostConfig *runconfig.HostConfig) {
63
-}
64
-
65 62
 // getContainersByNameDownlevel performs processing for pre 1.20 APIs. This
66 63
 // is only relevant on non-Windows daemons.
67 64
 func getContainersByNameDownlevel(w http.ResponseWriter, s *Server, namevar string) error {
... ...
@@ -11,12 +11,12 @@ import (
11 11
 	"github.com/opencontainers/runc/libcontainer/label"
12 12
 )
13 13
 
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) {
15 15
 	if config == nil {
16 16
 		return "", nil, fmt.Errorf("Config cannot be empty in order to create a container")
17 17
 	}
18 18
 
19
-	daemon.adaptContainerSettings(hostConfig)
19
+	daemon.adaptContainerSettings(hostConfig, adjustCPUShares)
20 20
 	warnings, err := daemon.verifyContainerSettings(hostConfig, config)
21 21
 	if err != nil {
22 22
 		return "", warnings, err
... ...
@@ -31,6 +31,12 @@ import (
31 31
 	"github.com/opencontainers/runc/libcontainer/label"
32 32
 )
33 33
 
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
+
34 40
 func (daemon *Daemon) Changes(container *Container) ([]archive.Change, error) {
35 41
 	initID := fmt.Sprintf("%s-init", container.ID)
36 42
 	return daemon.driver.Changes(container.ID, initID)
... ...
@@ -118,10 +124,21 @@ func checkKernel() error {
118 118
 
119 119
 // adaptContainerSettings is called during container creation to modify any
120 120
 // settings necessary in the HostConfig structure.
121
-func (daemon *Daemon) adaptContainerSettings(hostConfig *runconfig.HostConfig) {
121
+func (daemon *Daemon) adaptContainerSettings(hostConfig *runconfig.HostConfig, adjustCPUShares bool) {
122 122
 	if hostConfig == nil {
123 123
 		return
124 124
 	}
125
+
126
+	if adjustCPUShares && hostConfig.CPUShares > 0 {
127
+		// Handle unsupported CPUShares
128
+		if hostConfig.CPUShares < linuxMinCPUShares {
129
+			logrus.Warnf("Changing requested CPUShares of %d to minimum allowed of %d", hostConfig.CPUShares, linuxMinCPUShares)
130
+			hostConfig.CPUShares = linuxMinCPUShares
131
+		} else if hostConfig.CPUShares > linuxMaxCPUShares {
132
+			logrus.Warnf("Changing requested CPUShares of %d to maximum allowed of %d", hostConfig.CPUShares, linuxMaxCPUShares)
133
+			hostConfig.CPUShares = linuxMaxCPUShares
134
+		}
135
+	}
125 136
 	if hostConfig.Memory > 0 && hostConfig.MemorySwap == 0 {
126 137
 		// By default, MemorySwap is set to twice the size of Memory.
127 138
 		hostConfig.MemorySwap = hostConfig.Memory * 2
128 139
new file mode 100644
... ...
@@ -0,0 +1,87 @@
0
+// +build !windows
1
+
2
+package daemon
3
+
4
+import (
5
+	"io/ioutil"
6
+	"os"
7
+	"testing"
8
+
9
+	"github.com/docker/docker/runconfig"
10
+)
11
+
12
+func TestAdjustCPUShares(t *testing.T) {
13
+	tmp, err := ioutil.TempDir("", "docker-daemon-unix-test-")
14
+	if err != nil {
15
+		t.Fatal(err)
16
+	}
17
+	defer os.RemoveAll(tmp)
18
+	daemon := &Daemon{
19
+		repository: tmp,
20
+		root:       tmp,
21
+	}
22
+
23
+	hostConfig := &runconfig.HostConfig{
24
+		CPUShares: linuxMinCPUShares - 1,
25
+	}
26
+	daemon.adaptContainerSettings(hostConfig, true)
27
+	if hostConfig.CPUShares != linuxMinCPUShares {
28
+		t.Errorf("Expected CPUShares to be %d", linuxMinCPUShares)
29
+	}
30
+
31
+	hostConfig.CPUShares = linuxMaxCPUShares + 1
32
+	daemon.adaptContainerSettings(hostConfig, true)
33
+	if hostConfig.CPUShares != linuxMaxCPUShares {
34
+		t.Errorf("Expected CPUShares to be %d", linuxMaxCPUShares)
35
+	}
36
+
37
+	hostConfig.CPUShares = 0
38
+	daemon.adaptContainerSettings(hostConfig, true)
39
+	if hostConfig.CPUShares != 0 {
40
+		t.Error("Expected CPUShares to be unchanged")
41
+	}
42
+
43
+	hostConfig.CPUShares = 1024
44
+	daemon.adaptContainerSettings(hostConfig, true)
45
+	if hostConfig.CPUShares != 1024 {
46
+		t.Error("Expected CPUShares to be unchanged")
47
+	}
48
+}
49
+
50
+func TestAdjustCPUSharesNoAdjustment(t *testing.T) {
51
+	tmp, err := ioutil.TempDir("", "docker-daemon-unix-test-")
52
+	if err != nil {
53
+		t.Fatal(err)
54
+	}
55
+	defer os.RemoveAll(tmp)
56
+	daemon := &Daemon{
57
+		repository: tmp,
58
+		root:       tmp,
59
+	}
60
+
61
+	hostConfig := &runconfig.HostConfig{
62
+		CPUShares: linuxMinCPUShares - 1,
63
+	}
64
+	daemon.adaptContainerSettings(hostConfig, false)
65
+	if hostConfig.CPUShares != linuxMinCPUShares-1 {
66
+		t.Errorf("Expected CPUShares to be %d", linuxMinCPUShares-1)
67
+	}
68
+
69
+	hostConfig.CPUShares = linuxMaxCPUShares + 1
70
+	daemon.adaptContainerSettings(hostConfig, false)
71
+	if hostConfig.CPUShares != linuxMaxCPUShares+1 {
72
+		t.Errorf("Expected CPUShares to be %d", linuxMaxCPUShares+1)
73
+	}
74
+
75
+	hostConfig.CPUShares = 0
76
+	daemon.adaptContainerSettings(hostConfig, false)
77
+	if hostConfig.CPUShares != 0 {
78
+		t.Error("Expected CPUShares to be unchanged")
79
+	}
80
+
81
+	hostConfig.CPUShares = 1024
82
+	daemon.adaptContainerSettings(hostConfig, false)
83
+	if hostConfig.CPUShares != 1024 {
84
+		t.Error("Expected CPUShares to be unchanged")
85
+	}
86
+}
... ...
@@ -75,7 +75,7 @@ func checkKernel() error {
75 75
 
76 76
 // adaptContainerSettings is called during container creation to modify any
77 77
 // settings necessary in the HostConfig structure.
78
-func (daemon *Daemon) adaptContainerSettings(hostConfig *runconfig.HostConfig) {
78
+func (daemon *Daemon) adaptContainerSettings(hostConfig *runconfig.HostConfig, adjustCPUShares bool) {
79 79
 }
80 80
 
81 81
 // verifyPlatformContainerSettings performs platform-specific validation of the