2016-12-23 20:09:12 +01:00
package opts
import (
"fmt"
"strconv"
"strings"
"github.com/docker/docker/api/types/blkiodev"
"github.com/docker/go-units"
)
// ValidatorThrottleFctType defines a validator function that returns a validated struct and/or an error.
type ValidatorThrottleFctType func ( val string ) ( * blkiodev . ThrottleDevice , error )
// ValidateThrottleBpsDevice validates that the specified string has a valid device-rate format.
func ValidateThrottleBpsDevice ( val string ) ( * blkiodev . ThrottleDevice , error ) {
2022-12-27 16:24:23 +01:00
k , v , ok := strings . Cut ( val , ":" )
if ! ok || k == "" {
2016-12-23 20:09:12 +01:00
return nil , fmt . Errorf ( "bad format: %s" , val )
}
2022-12-27 16:24:23 +01:00
// TODO(thaJeztah): should we really validate this on the client?
if ! strings . HasPrefix ( k , "/dev/" ) {
2016-12-23 20:09:12 +01:00
return nil , fmt . Errorf ( "bad format for device path: %s" , val )
}
2022-12-27 16:24:23 +01:00
rate , err := units . RAMInBytes ( v )
2016-12-23 20:09:12 +01:00
if err != nil {
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 )
}
if rate < 0 {
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 )
}
return & blkiodev . ThrottleDevice {
2023-02-03 11:39:40 +01:00
Path : k ,
2016-12-23 20:09:12 +01:00
Rate : uint64 ( rate ) ,
} , nil
}
// ValidateThrottleIOpsDevice validates that the specified string has a valid device-rate format.
func ValidateThrottleIOpsDevice ( val string ) ( * blkiodev . ThrottleDevice , error ) {
2022-12-27 16:24:23 +01:00
k , v , ok := strings . Cut ( val , ":" )
if ! ok || k == "" {
2016-12-23 20:09:12 +01:00
return nil , fmt . Errorf ( "bad format: %s" , val )
}
2022-12-27 16:24:23 +01:00
// TODO(thaJeztah): should we really validate this on the client?
if ! strings . HasPrefix ( k , "/dev/" ) {
2016-12-23 20:09:12 +01:00
return nil , fmt . Errorf ( "bad format for device path: %s" , val )
}
2022-12-27 16:24:23 +01:00
rate , err := strconv . ParseUint ( v , 10 , 64 )
2016-12-23 20:09:12 +01:00
if err != nil {
return nil , fmt . Errorf ( "invalid rate for device: %s. The correct format is <device-path>:<number>. Number must be a positive integer" , val )
}
2022-12-27 16:24:23 +01:00
return & blkiodev . ThrottleDevice { Path : k , Rate : rate } , nil
2016-12-23 20:09:12 +01:00
}
// ThrottledeviceOpt defines a map of ThrottleDevices
type ThrottledeviceOpt struct {
values [ ] * blkiodev . ThrottleDevice
validator ValidatorThrottleFctType
}
// NewThrottledeviceOpt creates a new ThrottledeviceOpt
func NewThrottledeviceOpt ( validator ValidatorThrottleFctType ) ThrottledeviceOpt {
return ThrottledeviceOpt {
2022-09-03 20:07:29 +02:00
values : [ ] * blkiodev . ThrottleDevice { } ,
2016-12-23 20:09:12 +01:00
validator : validator ,
}
}
// Set validates a ThrottleDevice and sets its name as a key in ThrottledeviceOpt
func ( opt * ThrottledeviceOpt ) Set ( val string ) error {
var value * blkiodev . ThrottleDevice
if opt . validator != nil {
v , err := opt . validator ( val )
if err != nil {
return err
}
value = v
}
2022-12-27 15:59:57 +01:00
opt . values = append ( opt . values , value )
2016-12-23 20:09:12 +01:00
return nil
}
// String returns ThrottledeviceOpt values as a string.
func ( opt * ThrottledeviceOpt ) String ( ) string {
2022-09-03 20:07:29 +02:00
out := make ( [ ] string , 0 , len ( opt . values ) )
2016-12-23 20:09:12 +01:00
for _ , v := range opt . values {
out = append ( out , v . String ( ) )
}
return fmt . Sprintf ( "%v" , out )
}
// GetList returns a slice of pointers to ThrottleDevices.
func ( opt * ThrottledeviceOpt ) GetList ( ) [ ] * blkiodev . ThrottleDevice {
2024-08-07 02:27:21 +00:00
out := make ( [ ] * blkiodev . ThrottleDevice , len ( opt . values ) )
2022-09-03 20:07:29 +02:00
copy ( out , opt . values )
return out
2016-12-23 20:09:12 +01:00
}
// Type returns the option type
opts: fix "unused-receiver", line-length-limit linting
opts/port.go:124:7: unused-receiver: method receiver 'p' is not referenced in method's body, consider removing or renaming it as _ (revive)
func (p *PortOpt) Type() string {
^
opts/mount.go:218:7: unused-receiver: method receiver 'm' is not referenced in method's body, consider removing or renaming it as _ (revive)
func (m *MountOpt) Type() string {
^
opts/quotedstring.go:16:7: unused-receiver: method receiver 's' is not referenced in method's body, consider removing or renaming it as _ (revive)
func (s *QuotedString) Type() string {
^
opts/secret.go:82:7: unused-receiver: method receiver 'o' is not referenced in method's body, consider removing or renaming it as _ (revive)
func (o *SecretOpt) Type() string {
^
opts/opts_test.go:235: line-length-limit: line is 283 characters, out of limit 200 (revive)
`foo.bar.baz.this.should.fail.on.long.name.because.it.is.longer.thanisshouldbethis.should.fail.on.long.name.because.it.is.longer.thanisshouldbethis.should.fail.on.long.name.because.it.is.longer.thanisshouldbethis.should.fail.on.long.name.because.it.is.longer.thanisshouldbe`,
opts/ulimit.go:61:7: unused-receiver: method receiver 'o' is not referenced in method's body, consider removing or renaming it as _ (revive)
func (o *UlimitOpt) Type() string {
^
opts/weightdevice.go:82:7: unused-receiver: method receiver 'opt' is not referenced in method's body, consider removing or renaming it as _ (revive)
func (opt *WeightdeviceOpt) Type() string {
^
opts/throttledevice.go:103:7: unused-receiver: method receiver 'opt' is not referenced in method's body, consider removing or renaming it as _ (revive)
func (opt *ThrottledeviceOpt) Type() string {
^
opts/duration.go:49:7: unused-receiver: method receiver 'd' is not referenced in method's body, consider removing or renaming it as _ (revive)
func (d *DurationOpt) Type() string {
^
opts/network.go:109:7: unused-receiver: method receiver 'n' is not referenced in method's body, consider removing or renaming it as _ (revive)
func (n *NetworkOpt) Type() string {
^
opts/network.go:119:7: unused-receiver: method receiver 'n' is not referenced in method's body, consider removing or renaming it as _ (revive)
func (n *NetworkOpt) String() string {
^
opts/opts.go:113:7: unused-receiver: method receiver 'opts' is not referenced in method's body, consider removing or renaming it as _ (revive)
func (opts *ListOpts) Type() string {
^
opts/pull_behavior.go:13:7: unused-receiver: method receiver 'p' is not referenced in method's body, consider removing or renaming it as _ (revive)
func (p *PullOpt) Type() string {
^
opts/config.go:83:7: unused-receiver: method receiver 'o' is not referenced in method's body, consider removing or renaming it as _ (revive)
func (o *ConfigOpt) Type() string {
^
opts/gpus.go:95:7: unused-receiver: method receiver 'o' is not referenced in method's body, consider removing or renaming it as _ (revive)
func (o *GpuOpts) Type() string {
^
opts/pull_behavior.go:23:7: unused-receiver: method receiver 'p' is not referenced in method's body, consider removing or renaming it as _ (revive)
func (p *PullOpt) IsBoolFlag() bool {
^
opts/opts.go:183:7: unused-receiver: method receiver 'opts' is not referenced in method's body, consider removing or renaming it as _ (revive)
func (opts *MapOpts) Type() string {
^
opts/opts.go:361:7: unused-receiver: method receiver 'o' is not referenced in method's body, consider removing or renaming it as _ (revive)
func (o *FilterOpt) Type() string {
^
opts/opts.go:389:7: unused-receiver: method receiver 'c' is not referenced in method's body, consider removing or renaming it as _ (revive)
func (c *NanoCPUs) Type() string {
^
opts/opts.go:466:7: unused-receiver: method receiver 'm' is not referenced in method's body, consider removing or renaming it as _ (revive)
func (m *MemBytes) Type() string {
^
opts/opts.go:501:7: unused-receiver: method receiver 'm' is not referenced in method's body, consider removing or renaming it as _ (revive)
func (m *MemSwapBytes) Type() string {
^
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-02-17 13:37:39 +01:00
func ( * ThrottledeviceOpt ) Type ( ) string {
Improve usage output for docker run
Commit a77f2450c70312f8c26877a18bfe2baa44d4abb9 switched `docker run`
to use the `pflags` package. Due to this change, the usage output for
the `--blkio-weight-device` and `--device-*` flags changed and now
showed `weighted-device`, and `throttled-device` as value type. As a
result, the output of `docker run --help` became a lot wider.
This patch changes the output to show `list` instead, which is
consistent with other options that allow to be set multiple times.
Output before this change;
Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Run a command in a new container
Options:
--blkio-weight uint16 Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0)
--blkio-weight-device weighted-device Block IO weight (relative device weight) (default [])
--device list Add a host device to the container (default [])
--device-read-bps throttled-device Limit read rate (bytes per second) from a device (default [])
--device-read-iops throttled-device Limit read rate (IO per second) from a device (default [])
--device-write-bps throttled-device Limit write rate (bytes per second) to a device (default [])
--device-write-iops throttled-device Limit write rate (IO per second) to a device (default [])
-w, --workdir string Working directory inside the container
Output after this change;
Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Run a command in a new container
Options:
--blkio-weight uint16 Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0)
--blkio-weight-device list Block IO weight (relative device weight) (default [])
--device list Add a host device to the container (default [])
--device-read-bps list Limit read rate (bytes per second) from a device (default [])
--device-read-iops list Limit read rate (IO per second) from a device (default [])
--device-write-bps list Limit write rate (bytes per second) to a device (default [])
--device-write-iops list Limit write rate (IO per second) to a device (default [])
-w, --workdir string Working directory inside the container
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2016-12-30 17:35:46 +01:00
return "list"
2016-12-23 20:09:12 +01:00
}