Browse code

Merge pull request #13722 from samuelkarp/CpuShareRemoteAPI

Adjust disallowed CpuShares in /containers/create

Arnaud Porterie authored on 2015/06/12 06:20:50
Showing 4 changed files
... ...
@@ -897,6 +897,7 @@ func (s *Server) postContainersCreate(version version.Version, w http.ResponseWr
897 897
 	if err != nil {
898 898
 		return err
899 899
 	}
900
+	adjustCpuShares(version, hostConfig)
900 901
 
901 902
 	containerId, warnings, err := s.daemon.ContainerCreate(name, config, hostConfig)
902 903
 	if err != nil {
... ...
@@ -8,12 +8,21 @@ import (
8 8
 	"net/http"
9 9
 	"strconv"
10 10
 
11
+	"github.com/Sirupsen/logrus"
11 12
 	"github.com/docker/docker/daemon"
12 13
 	"github.com/docker/docker/pkg/sockets"
13 14
 	"github.com/docker/docker/pkg/systemd"
15
+	"github.com/docker/docker/pkg/version"
16
+	"github.com/docker/docker/runconfig"
14 17
 	"github.com/docker/libnetwork/portallocator"
15 18
 )
16 19
 
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
+
17 26
 // newServer sets up the required serverClosers and does protocol specific checking.
18 27
 func (s *Server) newServer(proto, addr string) ([]serverCloser, error) {
19 28
 	var (
... ...
@@ -96,3 +105,18 @@ func allocateDaemonPort(addr string) error {
96 96
 	}
97 97
 	return nil
98 98
 }
99
+
100
+func adjustCpuShares(version version.Version, hostConfig *runconfig.HostConfig) {
101
+	if version.LessThan("1.19") {
102
+		if hostConfig.CpuShares > 0 {
103
+			// Handle unsupported CpuShares
104
+			if hostConfig.CpuShares < linuxMinCpuShares {
105
+				logrus.Warnf("Changing requested CpuShares of %d to minimum allowed of %d", hostConfig.CpuShares, linuxMinCpuShares)
106
+				hostConfig.CpuShares = linuxMinCpuShares
107
+			} else if hostConfig.CpuShares > linuxMaxCpuShares {
108
+				logrus.Warnf("Changing requested CpuShares of %d to maximum allowed of %d", hostConfig.CpuShares, linuxMaxCpuShares)
109
+				hostConfig.CpuShares = linuxMaxCpuShares
110
+			}
111
+		}
112
+	}
113
+}
99 114
new file mode 100644
... ...
@@ -0,0 +1,68 @@
0
+// +build linux
1
+
2
+package server
3
+
4
+import (
5
+	"testing"
6
+
7
+	"github.com/docker/docker/pkg/version"
8
+	"github.com/docker/docker/runconfig"
9
+)
10
+
11
+func TestAdjustCpuSharesOldApi(t *testing.T) {
12
+	apiVersion := version.Version("1.18")
13
+	hostConfig := &runconfig.HostConfig{
14
+		CpuShares: linuxMinCpuShares - 1,
15
+	}
16
+	adjustCpuShares(apiVersion, hostConfig)
17
+	if hostConfig.CpuShares != linuxMinCpuShares {
18
+		t.Errorf("Expected CpuShares to be %d", linuxMinCpuShares)
19
+	}
20
+
21
+	hostConfig.CpuShares = linuxMaxCpuShares + 1
22
+	adjustCpuShares(apiVersion, hostConfig)
23
+	if hostConfig.CpuShares != linuxMaxCpuShares {
24
+		t.Errorf("Expected CpuShares to be %d", linuxMaxCpuShares)
25
+	}
26
+
27
+	hostConfig.CpuShares = 0
28
+	adjustCpuShares(apiVersion, hostConfig)
29
+	if hostConfig.CpuShares != 0 {
30
+		t.Error("Expected CpuShares to be unchanged")
31
+	}
32
+
33
+	hostConfig.CpuShares = 1024
34
+	adjustCpuShares(apiVersion, hostConfig)
35
+	if hostConfig.CpuShares != 1024 {
36
+		t.Error("Expected CpuShares to be unchanged")
37
+	}
38
+}
39
+
40
+func TestAdjustCpuSharesNoAdjustment(t *testing.T) {
41
+	apiVersion := version.Version("1.19")
42
+	hostConfig := &runconfig.HostConfig{
43
+		CpuShares: linuxMinCpuShares - 1,
44
+	}
45
+	adjustCpuShares(apiVersion, hostConfig)
46
+	if hostConfig.CpuShares != linuxMinCpuShares-1 {
47
+		t.Errorf("Expected CpuShares to be %d", linuxMinCpuShares-1)
48
+	}
49
+
50
+	hostConfig.CpuShares = linuxMaxCpuShares + 1
51
+	adjustCpuShares(apiVersion, hostConfig)
52
+	if hostConfig.CpuShares != linuxMaxCpuShares+1 {
53
+		t.Errorf("Expected CpuShares to be %d", linuxMaxCpuShares+1)
54
+	}
55
+
56
+	hostConfig.CpuShares = 0
57
+	adjustCpuShares(apiVersion, hostConfig)
58
+	if hostConfig.CpuShares != 0 {
59
+		t.Error("Expected CpuShares to be unchanged")
60
+	}
61
+
62
+	hostConfig.CpuShares = 1024
63
+	adjustCpuShares(apiVersion, hostConfig)
64
+	if hostConfig.CpuShares != 1024 {
65
+		t.Error("Expected CpuShares to be unchanged")
66
+	}
67
+}
... ...
@@ -54,3 +54,6 @@ func (s *Server) AcceptConnections(d *daemon.Daemon) {
54 54
 func allocateDaemonPort(addr string) error {
55 55
 	return nil
56 56
 }
57
+
58
+func adjustCpuShares(version version.Version, hostConfig *runconfig.HostConfig) {
59
+}