github.com/endophage/docker@v1.4.2-0.20161027011718-242853499895/runconfig/opts/throttledevice.go (about)

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