throttledevice.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package opts
  2. import (
  3. "fmt"
  4. "strconv"
  5. "strings"
  6. "github.com/docker/docker/api/types/blkiodev"
  7. "github.com/docker/go-units"
  8. )
  9. // ValidatorThrottleFctType defines a validator function that returns a validated struct and/or an error.
  10. type ValidatorThrottleFctType func(val string) (*blkiodev.ThrottleDevice, error)
  11. // ValidateThrottleBpsDevice validates that the specified string has a valid device-rate format.
  12. func ValidateThrottleBpsDevice(val string) (*blkiodev.ThrottleDevice, error) {
  13. split := strings.SplitN(val, ":", 2)
  14. if len(split) != 2 {
  15. return nil, fmt.Errorf("bad format: %s", val)
  16. }
  17. if !strings.HasPrefix(split[0], "/dev/") {
  18. return nil, fmt.Errorf("bad format for device path: %s", val)
  19. }
  20. rate, err := units.RAMInBytes(split[1])
  21. if err != nil {
  22. 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)
  23. }
  24. if rate < 0 {
  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. return &blkiodev.ThrottleDevice{
  28. Path: split[0],
  29. Rate: uint64(rate),
  30. }, nil
  31. }
  32. // ValidateThrottleIOpsDevice validates that the specified string has a valid device-rate format.
  33. func ValidateThrottleIOpsDevice(val string) (*blkiodev.ThrottleDevice, error) {
  34. split := strings.SplitN(val, ":", 2)
  35. if len(split) != 2 {
  36. return nil, fmt.Errorf("bad format: %s", val)
  37. }
  38. if !strings.HasPrefix(split[0], "/dev/") {
  39. return nil, fmt.Errorf("bad format for device path: %s", val)
  40. }
  41. rate, err := strconv.ParseUint(split[1], 10, 64)
  42. if err != nil {
  43. return nil, fmt.Errorf("invalid rate for device: %s. The correct format is <device-path>:<number>. Number must be a positive integer", val)
  44. }
  45. if rate < 0 {
  46. return nil, fmt.Errorf("invalid rate for device: %s. The correct format is <device-path>:<number>. Number must be a positive integer", val)
  47. }
  48. return &blkiodev.ThrottleDevice{
  49. Path: split[0],
  50. Rate: uint64(rate),
  51. }, nil
  52. }
  53. // ThrottledeviceOpt defines a map of ThrottleDevices
  54. type ThrottledeviceOpt struct {
  55. values []*blkiodev.ThrottleDevice
  56. validator ValidatorThrottleFctType
  57. }
  58. // NewThrottledeviceOpt creates a new ThrottledeviceOpt
  59. func NewThrottledeviceOpt(validator ValidatorThrottleFctType) ThrottledeviceOpt {
  60. values := []*blkiodev.ThrottleDevice{}
  61. return ThrottledeviceOpt{
  62. values: values,
  63. validator: validator,
  64. }
  65. }
  66. // Set validates a ThrottleDevice and sets its name as a key in ThrottledeviceOpt
  67. func (opt *ThrottledeviceOpt) Set(val string) error {
  68. var value *blkiodev.ThrottleDevice
  69. if opt.validator != nil {
  70. v, err := opt.validator(val)
  71. if err != nil {
  72. return err
  73. }
  74. value = v
  75. }
  76. (opt.values) = append((opt.values), value)
  77. return nil
  78. }
  79. // String returns ThrottledeviceOpt values as a string.
  80. func (opt *ThrottledeviceOpt) String() string {
  81. var out []string
  82. for _, v := range opt.values {
  83. out = append(out, v.String())
  84. }
  85. return fmt.Sprintf("%v", out)
  86. }
  87. // GetList returns a slice of pointers to ThrottleDevices.
  88. func (opt *ThrottledeviceOpt) GetList() []*blkiodev.ThrottleDevice {
  89. var throttledevice []*blkiodev.ThrottleDevice
  90. throttledevice = append(throttledevice, opt.values...)
  91. return throttledevice
  92. }
  93. // Type returns the option type
  94. func (opt *ThrottledeviceOpt) Type() string {
  95. return "list"
  96. }