Skip to content

Commit

Permalink
Added support for generic resources in service create
Browse files Browse the repository at this point in the history
Signed-off-by: Renaud Gaubert <rgaubert@nvidia.com>
  • Loading branch information
Renaud Gaubert committed Aug 9, 2017
1 parent 4af6b5f commit ef12709
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
1 change: 1 addition & 0 deletions cli/command/service/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func newCreateCommand(dockerCli *command.DockerCli) *cobra.Command {
flags.SetAnnotation(flagDNSSearch, "version", []string{"1.25"})
flags.Var(&opts.hosts, flagHost, "Set one or more custom host-to-IP mappings (host:ip)")
flags.SetAnnotation(flagHost, "version", []string{"1.25"})
flags.Var(&opts.resources.resGenericResources, "generic-resources", "user defined resources request (e.g. gpu=3;fpga=1)")

flags.SetInterspersed(false)
return cmd
Expand Down
14 changes: 8 additions & 6 deletions cli/command/service/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,17 +222,19 @@ func (opts updateOptions) rollbackConfig(flags *pflag.FlagSet) *swarm.UpdateConf
}

type resourceOptions struct {
limitCPU opts.NanoCPUs
limitMemBytes opts.MemBytes
resCPU opts.NanoCPUs
resMemBytes opts.MemBytes
limitCPU opts.NanoCPUs
limitMemBytes opts.MemBytes
resCPU opts.NanoCPUs
resMemBytes opts.MemBytes
resGenericResources opts.GenericResource
}

func (r *resourceOptions) ToResourceRequirements() *swarm.ResourceRequirements {
return &swarm.ResourceRequirements{
Limits: &swarm.Resources{
NanoCPUs: r.limitCPU.Value(),
MemoryBytes: r.limitMemBytes.Value(),
NanoCPUs: r.limitCPU.Value(),
MemoryBytes: r.limitMemBytes.Value(),
GenericResources: r.resGenericResources.Value(),
},
Reservations: &swarm.Resources{
NanoCPUs: r.resCPU.Value(),
Expand Down
40 changes: 40 additions & 0 deletions opts/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import (
"regexp"
"strings"

"github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/daemon/cluster/convert"
mobyopts "github.com/docker/docker/opts"
"github.com/docker/swarmkit/api/genericresource"

"github.com/docker/docker/api/types/filters"
units "github.com/docker/go-units"
)
Expand Down Expand Up @@ -486,3 +491,38 @@ func (m *MemSwapBytes) UnmarshalJSON(s []byte) error {
b := MemBytes(*m)
return b.UnmarshalJSON(s)
}

type GenericResource []swarm.GenericResource

// Set sets the value of the MemSwapBytes by passing a string
func (r *GenericResource) Set(value string) error {
if value == "" {
return nil
}

val, err := mobyopts.ParseGenericResources(value)
if err != nil {
return err
}

*r = val

return nil
}

// Type returns the type
func (r *GenericResource) Type() string {
return "generic resource"
}

// Value returns the value in int64
func (r *GenericResource) Value() []swarm.GenericResource {
return []swarm.GenericResource(*r)
}

func (r *GenericResource) String() string {
return strings.Join(
genericresource.EnvFormat(convert.GenericResourcesToGRPC(r.Value()), ""),
";",
)
}

0 comments on commit ef12709

Please sign in to comment.