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>
112 lines
2.3 KiB
Go
112 lines
2.3 KiB
Go
package opts
|
|
|
|
import (
|
|
"encoding/csv"
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/docker/docker/api/types/container"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
// GpuOpts is a Value type for parsing mounts
|
|
type GpuOpts struct {
|
|
values []container.DeviceRequest
|
|
}
|
|
|
|
func parseCount(s string) (int, error) {
|
|
if s == "all" {
|
|
return -1, nil
|
|
}
|
|
i, err := strconv.Atoi(s)
|
|
return i, errors.Wrap(err, "count must be an integer")
|
|
}
|
|
|
|
// Set a new mount value
|
|
//
|
|
//nolint:gocyclo
|
|
func (o *GpuOpts) Set(value string) error {
|
|
csvReader := csv.NewReader(strings.NewReader(value))
|
|
fields, err := csvReader.Read()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
req := container.DeviceRequest{}
|
|
|
|
seen := map[string]struct{}{}
|
|
// Set writable as the default
|
|
for _, field := range fields {
|
|
key, val, withValue := strings.Cut(field, "=")
|
|
if _, ok := seen[key]; ok {
|
|
return fmt.Errorf("gpu request key '%s' can be specified only once", key)
|
|
}
|
|
seen[key] = struct{}{}
|
|
|
|
if !withValue {
|
|
seen["count"] = struct{}{}
|
|
req.Count, err = parseCount(key)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
continue
|
|
}
|
|
|
|
switch key {
|
|
case "driver":
|
|
req.Driver = val
|
|
case "count":
|
|
req.Count, err = parseCount(val)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
case "device":
|
|
req.DeviceIDs = strings.Split(val, ",")
|
|
case "capabilities":
|
|
req.Capabilities = [][]string{append(strings.Split(val, ","), "gpu")}
|
|
case "options":
|
|
r := csv.NewReader(strings.NewReader(val))
|
|
optFields, err := r.Read()
|
|
if err != nil {
|
|
return errors.Wrap(err, "failed to read gpu options")
|
|
}
|
|
req.Options = ConvertKVStringsToMap(optFields)
|
|
default:
|
|
return fmt.Errorf("unexpected key '%s' in '%s'", key, field)
|
|
}
|
|
}
|
|
|
|
if _, ok := seen["count"]; !ok && req.DeviceIDs == nil {
|
|
req.Count = 1
|
|
}
|
|
if req.Options == nil {
|
|
req.Options = make(map[string]string)
|
|
}
|
|
if req.Capabilities == nil {
|
|
req.Capabilities = [][]string{{"gpu"}}
|
|
}
|
|
|
|
o.values = append(o.values, req)
|
|
return nil
|
|
}
|
|
|
|
// Type returns the type of this option
|
|
func (*GpuOpts) Type() string {
|
|
return "gpu-request"
|
|
}
|
|
|
|
// String returns a string repr of this option
|
|
func (o *GpuOpts) String() string {
|
|
gpus := []string{}
|
|
for _, gpu := range o.values {
|
|
gpus = append(gpus, fmt.Sprintf("%v", gpu))
|
|
}
|
|
return strings.Join(gpus, ", ")
|
|
}
|
|
|
|
// Value returns the mounts
|
|
func (o *GpuOpts) Value() []container.DeviceRequest {
|
|
return o.values
|
|
}
|