forked from AliyunContainerService/pouch
/
blkio.go
185 lines (149 loc) · 4.47 KB
/
blkio.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package main
import (
"fmt"
"strconv"
"strings"
"github.com/alibaba/pouch/apis/types"
units "github.com/docker/go-units"
)
// WeightDevice defines weight device
type WeightDevice struct {
values []*types.WeightDevice
}
func getValidateWeightDevice(val string) (*types.WeightDevice, error) {
pairs := strings.Split(val, ":")
if len(pairs) != 2 {
return nil, fmt.Errorf("invalid weight device %s: format must be <device-id>:<weight> with weight in range [10, 1000]", val)
}
weight, err := strconv.ParseUint(pairs[1], 10, 0)
if err != nil {
return nil, fmt.Errorf("invalid weight device %s: weight cannot be less than 0", val)
}
if weight > 0 && (weight < 10 || weight > 1000) {
return nil, fmt.Errorf("invalid weight device %s: weight must be in range [10, 1000]", val)
}
return &types.WeightDevice{
Path: pairs[0],
Weight: uint16(weight),
}, nil
}
// Set implement WeightDevice as pflag.Value interface
func (w *WeightDevice) Set(val string) error {
v, err := getValidateWeightDevice(val)
if err != nil {
return err
}
w.values = append(w.values, v)
return nil
}
// String implement WeightDevice as pflag.Value interface
func (w *WeightDevice) String() string {
var str []string
for _, v := range w.values {
str = append(str, fmt.Sprintf("%s:%d", v.Path, v.Weight))
}
return fmt.Sprintf("%v", str)
}
// Type implement WeightDevice as pflag.Value interface
func (w *WeightDevice) Type() string {
return "value"
}
func (w *WeightDevice) value() []*types.WeightDevice {
var weightDevice []*types.WeightDevice
for _, v := range w.values {
weightDevice = append(weightDevice, v)
}
return weightDevice
}
// ThrottleBpsDevice defines throttle bps device
type ThrottleBpsDevice struct {
values []*types.ThrottleDevice
}
func getValidThrottleDeviceBps(val string) (*types.ThrottleDevice, error) {
pairs := strings.Split(val, ":")
if len(pairs) != 2 {
return nil, fmt.Errorf("invalid throttle device %s: format must be <device-id>:<rate> with optional rate unit", val)
}
rate, err := units.RAMInBytes(pairs[1])
if err != nil || rate < 0 {
return nil, fmt.Errorf("invalid rate %s for device %s: cannot be negative", pairs[1], pairs[0])
}
return &types.ThrottleDevice{
Path: pairs[0],
Rate: uint64(rate),
}, nil
}
// Set implement ThrottleBpsDevice as pflag.Value interface
func (t *ThrottleBpsDevice) Set(val string) error {
v, err := getValidThrottleDeviceBps(val)
if err != nil {
return err
}
t.values = append(t.values, v)
return nil
}
// String implement ThrottleBpsDevice as pflag.Value interface
func (t *ThrottleBpsDevice) String() string {
var str []string
for _, v := range t.values {
str = append(str, fmt.Sprintf("%s:%d", v.Path, v.Rate))
}
return fmt.Sprintf("%v", str)
}
// Type implement ThrottleBpsDevice as pflag.Value interface
func (t *ThrottleBpsDevice) Type() string {
return "value"
}
func (t *ThrottleBpsDevice) value() []*types.ThrottleDevice {
var throttleDevice []*types.ThrottleDevice
for _, v := range t.values {
throttleDevice = append(throttleDevice, v)
}
return throttleDevice
}
// ThrottleIOpsDevice defines throttle iops device
type ThrottleIOpsDevice struct {
values []*types.ThrottleDevice
}
func getValidThrottleDeviceIOps(val string) (*types.ThrottleDevice, error) {
pairs := strings.Split(val, ":")
if len(pairs) != 2 {
return nil, fmt.Errorf("invalid throttle device %s: format must be <device-id>:<rate> with optional rate unit", val)
}
rate, err := strconv.ParseUint(pairs[1], 10, 64)
if err != nil || rate < 0 {
return nil, fmt.Errorf("invalid rate %s for device %s: rate cannot be negative", pairs[1], pairs[0])
}
return &types.ThrottleDevice{
Path: pairs[0],
Rate: uint64(rate),
}, nil
}
// Set implement ThrottleIOpsDevice as pflag.Value interface
func (t *ThrottleIOpsDevice) Set(val string) error {
v, err := getValidThrottleDeviceIOps(val)
if err != nil {
return err
}
t.values = append(t.values, v)
return nil
}
// String implement ThrottleIOpsDevice as pflag.Value interface
func (t *ThrottleIOpsDevice) String() string {
var str []string
for _, v := range t.values {
str = append(str, fmt.Sprintf("%s:%d", v.Path, v.Rate))
}
return fmt.Sprintf("%v", str)
}
// Type implement ThrottleIOpsDevice as pflag.Value interface
func (t *ThrottleIOpsDevice) Type() string {
return "value"
}
func (t *ThrottleIOpsDevice) value() []*types.ThrottleDevice {
var throttleDevice []*types.ThrottleDevice
for _, v := range t.values {
throttleDevice = append(throttleDevice, v)
}
return throttleDevice
}