Added support of Generic resources in compose file

Signed-off-by: Renaud Gaubert <renaud.gaubert@gmail.com>
This commit is contained in:
Renaud Gaubert 2017-11-17 23:05:59 +01:00 committed by Renaud Gaubert
parent 20a6ff32ee
commit 1ff73f867d
7 changed files with 93 additions and 19 deletions

View File

@ -77,8 +77,6 @@ func runCreate(dockerCli command.Cli, flags *pflag.FlagSet, opts *serviceOptions
return err return err
} }
fmt.Printf("%v\n", service.TaskTemplate.Resources)
specifiedSecrets := opts.secrets.Value() specifiedSecrets := opts.secrets.Value()
if len(specifiedSecrets) > 0 { if len(specifiedSecrets) > 0 {
// parse and validate secrets // parse and validate secrets

View File

@ -510,9 +510,25 @@ func convertResources(source composetypes.Resources) (*swarm.ResourceRequirement
return nil, err return nil, err
} }
} }
var generic []swarm.GenericResource
for _, res := range source.Reservations.GenericResources {
var r swarm.GenericResource
if res.DiscreteResourceSpec != nil {
r.DiscreteResourceSpec = &swarm.DiscreteGenericResource{
Kind: res.DiscreteResourceSpec.Kind,
Value: res.DiscreteResourceSpec.Value,
}
}
generic = append(generic, r)
}
resources.Reservations = &swarm.Resources{ resources.Reservations = &swarm.Resources{
NanoCPUs: cpus, NanoCPUs: cpus,
MemoryBytes: int64(source.Reservations.MemoryBytes), MemoryBytes: int64(source.Reservations.MemoryBytes),
GenericResources: generic,
} }
} }
return resources, nil return resources, nil

View File

@ -1,4 +1,4 @@
version: "3.4" version: "3.5"
services: services:
foo: foo:
@ -16,7 +16,6 @@ services:
labels: [FOO=BAR] labels: [FOO=BAR]
cap_add: cap_add:
- ALL - ALL
@ -54,6 +53,13 @@ services:
reservations: reservations:
cpus: '0.0001' cpus: '0.0001'
memory: 20M memory: 20M
generic_resources:
- discrete_resource_spec:
kind: 'gpu'
value: 2
- discrete_resource_spec:
kind: 'ssd'
value: 1
restart_policy: restart_policy:
condition: on-failure condition: on-failure
delay: 5s delay: 5s

View File

@ -879,6 +879,20 @@ func TestFullExample(t *testing.T) {
Reservations: &types.Resource{ Reservations: &types.Resource{
NanoCPUs: "0.0001", NanoCPUs: "0.0001",
MemoryBytes: 20 * 1024 * 1024, MemoryBytes: 20 * 1024 * 1024,
GenericResources: []types.GenericResource{
{
DiscreteResourceSpec: &types.DiscreteGenericResource{
Kind: "gpu",
Value: 2,
},
},
{
DiscreteResourceSpec: &types.DiscreteGenericResource{
Kind: "ssd",
Value: 1,
},
},
},
}, },
}, },
RestartPolicy: &types.RestartPolicy{ RestartPolicy: &types.RestartPolicy{

File diff suppressed because one or more lines are too long

View File

@ -354,8 +354,23 @@
"resources": { "resources": {
"type": "object", "type": "object",
"properties": { "properties": {
"limits": {"$ref": "#/definitions/resource"}, "limits": {
"reservations": {"$ref": "#/definitions/resource"} "type": "object",
"properties": {
"cpus": {"type": "string"},
"memory": {"type": "string"}
},
"additionalProperties": false
},
"reservations": {
"type": "object",
"properties": {
"cpus": {"type": "string"},
"memory": {"type": "string"},
"generic_resources": {"$ref": "#/definitions/generic_resources"}
},
"additionalProperties": false
}
}, },
"additionalProperties": false "additionalProperties": false
}, },
@ -390,14 +405,23 @@
"additionalProperties": false "additionalProperties": false
}, },
"resource": { "generic_resources": {
"id": "#/definitions/resource", "id": "#/definitions/generic_resources",
"type": "array",
"items": {
"type": "object", "type": "object",
"properties": { "properties": {
"cpus": {"type": "string"}, "discrete_resource_spec": {
"memory": {"type": "string"} "type": "object",
"properties": {
"kind": {"type": "string"},
"value": {"type": "number"}
}, },
"additionalProperties": false "additionalProperties": false
}
},
"additionalProperties": false
}
}, },
"network": { "network": {

View File

@ -219,6 +219,22 @@ type Resource struct {
// TODO: types to convert from units and ratios // TODO: types to convert from units and ratios
NanoCPUs string `mapstructure:"cpus"` NanoCPUs string `mapstructure:"cpus"`
MemoryBytes UnitBytes `mapstructure:"memory"` MemoryBytes UnitBytes `mapstructure:"memory"`
GenericResources []GenericResource `mapstructure:"generic_resources"`
}
// GenericResource represents a "user defined" resource which can
// only be an integer (e.g: SSD=3) for a service
type GenericResource struct {
DiscreteResourceSpec *DiscreteGenericResource `mapstructure:"discrete_resource_spec"`
}
// DiscreteGenericResource represents a "user defined" resource which is defined
// as an integer
// "Kind" is used to describe the Kind of a resource (e.g: "GPU", "FPGA", "SSD", ...)
// Value is used to count the resource (SSD=5, HDD=3, ...)
type DiscreteGenericResource struct {
Kind string
Value int64
} }
// UnitBytes is the bytes type // UnitBytes is the bytes type