Browse code

Move runconfig blkiodev options and parsing into runconfig/opts package.

Signed-off-by: Daniel Nephin <dnephin@docker.com>

Daniel Nephin authored on 2015/12/22 06:34:05
Showing 6 changed files
... ...
@@ -5,11 +5,7 @@ import (
5 5
 	"net"
6 6
 	"os"
7 7
 	"regexp"
8
-	"strconv"
9 8
 	"strings"
10
-
11
-	"github.com/docker/docker/api/types/blkiodev"
12
-	"github.com/docker/go-units"
13 9
 )
14 10
 
15 11
 var (
... ...
@@ -153,12 +149,6 @@ func NewMapOpts(values map[string]string, validator ValidatorFctType) *MapOpts {
153 153
 // ValidatorFctType defines a validator function that returns a validated string and/or an error.
154 154
 type ValidatorFctType func(val string) (string, error)
155 155
 
156
-// ValidatorWeightFctType defines a validator function that returns a validated struct and/or an error.
157
-type ValidatorWeightFctType func(val string) (*blkiodev.WeightDevice, error)
158
-
159
-// ValidatorThrottleFctType defines a validator function that returns a validated struct and/or an error.
160
-type ValidatorThrottleFctType func(val string) (*blkiodev.ThrottleDevice, error)
161
-
162 156
 // ValidatorFctListType defines a validator function that returns a validated list of string and/or an error
163 157
 type ValidatorFctListType func(val string) ([]string, error)
164 158
 
... ...
@@ -173,75 +163,6 @@ func ValidateAttach(val string) (string, error) {
173 173
 	return val, fmt.Errorf("valid streams are STDIN, STDOUT and STDERR")
174 174
 }
175 175
 
176
-// ValidateWeightDevice validates that the specified string has a valid device-weight format.
177
-func ValidateWeightDevice(val string) (*blkiodev.WeightDevice, error) {
178
-	split := strings.SplitN(val, ":", 2)
179
-	if len(split) != 2 {
180
-		return nil, fmt.Errorf("bad format: %s", val)
181
-	}
182
-	if !strings.HasPrefix(split[0], "/dev/") {
183
-		return nil, fmt.Errorf("bad format for device path: %s", val)
184
-	}
185
-	weight, err := strconv.ParseUint(split[1], 10, 0)
186
-	if err != nil {
187
-		return nil, fmt.Errorf("invalid weight for device: %s", val)
188
-	}
189
-	if weight > 0 && (weight < 10 || weight > 1000) {
190
-		return nil, fmt.Errorf("invalid weight for device: %s", val)
191
-	}
192
-
193
-	return &blkiodev.WeightDevice{
194
-		Path:   split[0],
195
-		Weight: uint16(weight),
196
-	}, nil
197
-}
198
-
199
-// ValidateThrottleBpsDevice validates that the specified string has a valid device-rate format.
200
-func ValidateThrottleBpsDevice(val string) (*blkiodev.ThrottleDevice, error) {
201
-	split := strings.SplitN(val, ":", 2)
202
-	if len(split) != 2 {
203
-		return nil, fmt.Errorf("bad format: %s", val)
204
-	}
205
-	if !strings.HasPrefix(split[0], "/dev/") {
206
-		return nil, fmt.Errorf("bad format for device path: %s", val)
207
-	}
208
-	rate, err := units.RAMInBytes(split[1])
209
-	if err != nil {
210
-		return nil, fmt.Errorf("invalid rate for device: %s. The correct format is <device-path>:<number>[<unit>]. Number must be a positive integer. Unit is optional and can be kb, mb, or gb", val)
211
-	}
212
-	if rate < 0 {
213
-		return nil, fmt.Errorf("invalid rate for device: %s. The correct format is <device-path>:<number>[<unit>]. Number must be a positive integer. Unit is optional and can be kb, mb, or gb", val)
214
-	}
215
-
216
-	return &blkiodev.ThrottleDevice{
217
-		Path: split[0],
218
-		Rate: uint64(rate),
219
-	}, nil
220
-}
221
-
222
-// ValidateThrottleIOpsDevice validates that the specified string has a valid device-rate format.
223
-func ValidateThrottleIOpsDevice(val string) (*blkiodev.ThrottleDevice, error) {
224
-	split := strings.SplitN(val, ":", 2)
225
-	if len(split) != 2 {
226
-		return nil, fmt.Errorf("bad format: %s", val)
227
-	}
228
-	if !strings.HasPrefix(split[0], "/dev/") {
229
-		return nil, fmt.Errorf("bad format for device path: %s", val)
230
-	}
231
-	rate, err := strconv.ParseUint(split[1], 10, 64)
232
-	if err != nil {
233
-		return nil, fmt.Errorf("invalid rate for device: %s. The correct format is <device-path>:<number>. Number must be a positive integer", val)
234
-	}
235
-	if rate < 0 {
236
-		return nil, fmt.Errorf("invalid rate for device: %s. The correct format is <device-path>:<number>. Number must be a positive integer", val)
237
-	}
238
-
239
-	return &blkiodev.ThrottleDevice{
240
-		Path: split[0],
241
-		Rate: uint64(rate),
242
-	}, nil
243
-}
244
-
245 176
 // ValidateEnv validates an environment variable and returns it.
246 177
 // If no value is specified, it returns the current value using os.Getenv.
247 178
 //
248 179
deleted file mode 100644
... ...
@@ -1,56 +0,0 @@
1
-package opts
2
-
3
-import (
4
-	"fmt"
5
-
6
-	"github.com/docker/docker/api/types/blkiodev"
7
-)
8
-
9
-// ThrottledeviceOpt defines a map of ThrottleDevices
10
-type ThrottledeviceOpt struct {
11
-	values    []*blkiodev.ThrottleDevice
12
-	validator ValidatorThrottleFctType
13
-}
14
-
15
-// NewThrottledeviceOpt creates a new ThrottledeviceOpt
16
-func NewThrottledeviceOpt(validator ValidatorThrottleFctType) ThrottledeviceOpt {
17
-	values := []*blkiodev.ThrottleDevice{}
18
-	return ThrottledeviceOpt{
19
-		values:    values,
20
-		validator: validator,
21
-	}
22
-}
23
-
24
-// Set validates a ThrottleDevice and sets its name as a key in ThrottledeviceOpt
25
-func (opt *ThrottledeviceOpt) Set(val string) error {
26
-	var value *blkiodev.ThrottleDevice
27
-	if opt.validator != nil {
28
-		v, err := opt.validator(val)
29
-		if err != nil {
30
-			return err
31
-		}
32
-		value = v
33
-	}
34
-	(opt.values) = append((opt.values), value)
35
-	return nil
36
-}
37
-
38
-// String returns ThrottledeviceOpt values as a string.
39
-func (opt *ThrottledeviceOpt) String() string {
40
-	var out []string
41
-	for _, v := range opt.values {
42
-		out = append(out, v.String())
43
-	}
44
-
45
-	return fmt.Sprintf("%v", out)
46
-}
47
-
48
-// GetList returns a slice of pointers to ThrottleDevices.
49
-func (opt *ThrottledeviceOpt) GetList() []*blkiodev.ThrottleDevice {
50
-	var throttledevice []*blkiodev.ThrottleDevice
51
-	for _, v := range opt.values {
52
-		throttledevice = append(throttledevice, v)
53
-	}
54
-
55
-	return throttledevice
56
-}
57 1
deleted file mode 100644
... ...
@@ -1,56 +0,0 @@
1
-package opts
2
-
3
-import (
4
-	"fmt"
5
-
6
-	"github.com/docker/docker/api/types/blkiodev"
7
-)
8
-
9
-// WeightdeviceOpt defines a map of WeightDevices
10
-type WeightdeviceOpt struct {
11
-	values    []*blkiodev.WeightDevice
12
-	validator ValidatorWeightFctType
13
-}
14
-
15
-// NewWeightdeviceOpt creates a new WeightdeviceOpt
16
-func NewWeightdeviceOpt(validator ValidatorWeightFctType) WeightdeviceOpt {
17
-	values := []*blkiodev.WeightDevice{}
18
-	return WeightdeviceOpt{
19
-		values:    values,
20
-		validator: validator,
21
-	}
22
-}
23
-
24
-// Set validates a WeightDevice and sets its name as a key in WeightdeviceOpt
25
-func (opt *WeightdeviceOpt) Set(val string) error {
26
-	var value *blkiodev.WeightDevice
27
-	if opt.validator != nil {
28
-		v, err := opt.validator(val)
29
-		if err != nil {
30
-			return err
31
-		}
32
-		value = v
33
-	}
34
-	(opt.values) = append((opt.values), value)
35
-	return nil
36
-}
37
-
38
-// String returns WeightdeviceOpt values as a string.
39
-func (opt *WeightdeviceOpt) String() string {
40
-	var out []string
41
-	for _, v := range opt.values {
42
-		out = append(out, v.String())
43
-	}
44
-
45
-	return fmt.Sprintf("%v", out)
46
-}
47
-
48
-// GetList returns a slice of pointers to WeightDevices.
49
-func (opt *WeightdeviceOpt) GetList() []*blkiodev.WeightDevice {
50
-	var weightdevice []*blkiodev.WeightDevice
51
-	for _, v := range opt.values {
52
-		weightdevice = append(weightdevice, v)
53
-	}
54
-
55
-	return weightdevice
56
-}
57 1
new file mode 100644
... ...
@@ -0,0 +1,108 @@
0
+package opts
1
+
2
+import (
3
+	"fmt"
4
+	"strconv"
5
+	"strings"
6
+
7
+	"github.com/docker/docker/api/types/blkiodev"
8
+	"github.com/docker/go-units"
9
+)
10
+
11
+// ValidatorThrottleFctType defines a validator function that returns a validated struct and/or an error.
12
+type ValidatorThrottleFctType func(val string) (*blkiodev.ThrottleDevice, error)
13
+
14
+// ValidateThrottleBpsDevice validates that the specified string has a valid device-rate format.
15
+func ValidateThrottleBpsDevice(val string) (*blkiodev.ThrottleDevice, error) {
16
+	split := strings.SplitN(val, ":", 2)
17
+	if len(split) != 2 {
18
+		return nil, fmt.Errorf("bad format: %s", val)
19
+	}
20
+	if !strings.HasPrefix(split[0], "/dev/") {
21
+		return nil, fmt.Errorf("bad format for device path: %s", val)
22
+	}
23
+	rate, err := units.RAMInBytes(split[1])
24
+	if err != nil {
25
+		return nil, fmt.Errorf("invalid rate for device: %s. The correct format is <device-path>:<number>[<unit>]. Number must be a positive integer. Unit is optional and can be kb, mb, or gb", val)
26
+	}
27
+	if rate < 0 {
28
+		return nil, fmt.Errorf("invalid rate for device: %s. The correct format is <device-path>:<number>[<unit>]. Number must be a positive integer. Unit is optional and can be kb, mb, or gb", val)
29
+	}
30
+
31
+	return &blkiodev.ThrottleDevice{
32
+		Path: split[0],
33
+		Rate: uint64(rate),
34
+	}, nil
35
+}
36
+
37
+// ValidateThrottleIOpsDevice validates that the specified string has a valid device-rate format.
38
+func ValidateThrottleIOpsDevice(val string) (*blkiodev.ThrottleDevice, error) {
39
+	split := strings.SplitN(val, ":", 2)
40
+	if len(split) != 2 {
41
+		return nil, fmt.Errorf("bad format: %s", val)
42
+	}
43
+	if !strings.HasPrefix(split[0], "/dev/") {
44
+		return nil, fmt.Errorf("bad format for device path: %s", val)
45
+	}
46
+	rate, err := strconv.ParseUint(split[1], 10, 64)
47
+	if err != nil {
48
+		return nil, fmt.Errorf("invalid rate for device: %s. The correct format is <device-path>:<number>. Number must be a positive integer", val)
49
+	}
50
+	if rate < 0 {
51
+		return nil, fmt.Errorf("invalid rate for device: %s. The correct format is <device-path>:<number>. Number must be a positive integer", val)
52
+	}
53
+
54
+	return &blkiodev.ThrottleDevice{
55
+		Path: split[0],
56
+		Rate: uint64(rate),
57
+	}, nil
58
+}
59
+
60
+// ThrottledeviceOpt defines a map of ThrottleDevices
61
+type ThrottledeviceOpt struct {
62
+	values    []*blkiodev.ThrottleDevice
63
+	validator ValidatorThrottleFctType
64
+}
65
+
66
+// NewThrottledeviceOpt creates a new ThrottledeviceOpt
67
+func NewThrottledeviceOpt(validator ValidatorThrottleFctType) ThrottledeviceOpt {
68
+	values := []*blkiodev.ThrottleDevice{}
69
+	return ThrottledeviceOpt{
70
+		values:    values,
71
+		validator: validator,
72
+	}
73
+}
74
+
75
+// Set validates a ThrottleDevice and sets its name as a key in ThrottledeviceOpt
76
+func (opt *ThrottledeviceOpt) Set(val string) error {
77
+	var value *blkiodev.ThrottleDevice
78
+	if opt.validator != nil {
79
+		v, err := opt.validator(val)
80
+		if err != nil {
81
+			return err
82
+		}
83
+		value = v
84
+	}
85
+	(opt.values) = append((opt.values), value)
86
+	return nil
87
+}
88
+
89
+// String returns ThrottledeviceOpt values as a string.
90
+func (opt *ThrottledeviceOpt) String() string {
91
+	var out []string
92
+	for _, v := range opt.values {
93
+		out = append(out, v.String())
94
+	}
95
+
96
+	return fmt.Sprintf("%v", out)
97
+}
98
+
99
+// GetList returns a slice of pointers to ThrottleDevices.
100
+func (opt *ThrottledeviceOpt) GetList() []*blkiodev.ThrottleDevice {
101
+	var throttledevice []*blkiodev.ThrottleDevice
102
+	for _, v := range opt.values {
103
+		throttledevice = append(throttledevice, v)
104
+	}
105
+
106
+	return throttledevice
107
+}
0 108
new file mode 100644
... ...
@@ -0,0 +1,84 @@
0
+package opts
1
+
2
+import (
3
+	"fmt"
4
+	"strconv"
5
+	"strings"
6
+
7
+	"github.com/docker/docker/api/types/blkiodev"
8
+)
9
+
10
+// ValidatorWeightFctType defines a validator function that returns a validated struct and/or an error.
11
+type ValidatorWeightFctType func(val string) (*blkiodev.WeightDevice, error)
12
+
13
+// ValidateWeightDevice validates that the specified string has a valid device-weight format.
14
+func ValidateWeightDevice(val string) (*blkiodev.WeightDevice, error) {
15
+	split := strings.SplitN(val, ":", 2)
16
+	if len(split) != 2 {
17
+		return nil, fmt.Errorf("bad format: %s", val)
18
+	}
19
+	if !strings.HasPrefix(split[0], "/dev/") {
20
+		return nil, fmt.Errorf("bad format for device path: %s", val)
21
+	}
22
+	weight, err := strconv.ParseUint(split[1], 10, 0)
23
+	if err != nil {
24
+		return nil, fmt.Errorf("invalid weight for device: %s", val)
25
+	}
26
+	if weight > 0 && (weight < 10 || weight > 1000) {
27
+		return nil, fmt.Errorf("invalid weight for device: %s", val)
28
+	}
29
+
30
+	return &blkiodev.WeightDevice{
31
+		Path:   split[0],
32
+		Weight: uint16(weight),
33
+	}, nil
34
+}
35
+
36
+// WeightdeviceOpt defines a map of WeightDevices
37
+type WeightdeviceOpt struct {
38
+	values    []*blkiodev.WeightDevice
39
+	validator ValidatorWeightFctType
40
+}
41
+
42
+// NewWeightdeviceOpt creates a new WeightdeviceOpt
43
+func NewWeightdeviceOpt(validator ValidatorWeightFctType) WeightdeviceOpt {
44
+	values := []*blkiodev.WeightDevice{}
45
+	return WeightdeviceOpt{
46
+		values:    values,
47
+		validator: validator,
48
+	}
49
+}
50
+
51
+// Set validates a WeightDevice and sets its name as a key in WeightdeviceOpt
52
+func (opt *WeightdeviceOpt) Set(val string) error {
53
+	var value *blkiodev.WeightDevice
54
+	if opt.validator != nil {
55
+		v, err := opt.validator(val)
56
+		if err != nil {
57
+			return err
58
+		}
59
+		value = v
60
+	}
61
+	(opt.values) = append((opt.values), value)
62
+	return nil
63
+}
64
+
65
+// String returns WeightdeviceOpt values as a string.
66
+func (opt *WeightdeviceOpt) String() string {
67
+	var out []string
68
+	for _, v := range opt.values {
69
+		out = append(out, v.String())
70
+	}
71
+
72
+	return fmt.Sprintf("%v", out)
73
+}
74
+
75
+// GetList returns a slice of pointers to WeightDevices.
76
+func (opt *WeightdeviceOpt) GetList() []*blkiodev.WeightDevice {
77
+	var weightdevice []*blkiodev.WeightDevice
78
+	for _, v := range opt.values {
79
+		weightdevice = append(weightdevice, v)
80
+	}
81
+
82
+	return weightdevice
83
+}
... ...
@@ -13,6 +13,7 @@ import (
13 13
 	"github.com/docker/docker/pkg/mount"
14 14
 	"github.com/docker/docker/pkg/parsers"
15 15
 	"github.com/docker/docker/pkg/signal"
16
+	runconfigopts "github.com/docker/docker/runconfig/opts"
16 17
 	"github.com/docker/docker/volume"
17 18
 	"github.com/docker/go-connections/nat"
18 19
 	"github.com/docker/go-units"
... ...
@@ -54,12 +55,12 @@ func Parse(cmd *flag.FlagSet, args []string) (*container.Config, *container.Host
54 54
 		flAttach            = opts.NewListOpts(opts.ValidateAttach)
55 55
 		flVolumes           = opts.NewListOpts(nil)
56 56
 		flTmpfs             = opts.NewListOpts(nil)
57
-		flBlkioWeightDevice = opts.NewWeightdeviceOpt(opts.ValidateWeightDevice)
58
-		flDeviceReadBps     = opts.NewThrottledeviceOpt(opts.ValidateThrottleBpsDevice)
59
-		flDeviceWriteBps    = opts.NewThrottledeviceOpt(opts.ValidateThrottleBpsDevice)
57
+		flBlkioWeightDevice = runconfigopts.NewWeightdeviceOpt(runconfigopts.ValidateWeightDevice)
58
+		flDeviceReadBps     = runconfigopts.NewThrottledeviceOpt(runconfigopts.ValidateThrottleBpsDevice)
59
+		flDeviceWriteBps    = runconfigopts.NewThrottledeviceOpt(runconfigopts.ValidateThrottleBpsDevice)
60 60
 		flLinks             = opts.NewListOpts(ValidateLink)
61
-		flDeviceReadIOps    = opts.NewThrottledeviceOpt(opts.ValidateThrottleIOpsDevice)
62
-		flDeviceWriteIOps   = opts.NewThrottledeviceOpt(opts.ValidateThrottleIOpsDevice)
61
+		flDeviceReadIOps    = runconfigopts.NewThrottledeviceOpt(runconfigopts.ValidateThrottleIOpsDevice)
62
+		flDeviceWriteIOps   = runconfigopts.NewThrottledeviceOpt(runconfigopts.ValidateThrottleIOpsDevice)
63 63
 		flEnv               = opts.NewListOpts(opts.ValidateEnv)
64 64
 		flLabels            = opts.NewListOpts(opts.ValidateEnv)
65 65
 		flDevices           = opts.NewListOpts(ValidateDevice)