From c2f0402f4d8b4f676931c9c670449f0629bc8ea7 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Mon, 9 Jan 2017 14:22:02 -0500 Subject: [PATCH] Fix parsing resources from compose file for stack deploy. Signed-off-by: Daniel Nephin --- compose/convert/service.go | 19 +++++++++++++------ compose/convert/service_test.go | 23 +++++++++++++++++++++++ 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/compose/convert/service.go b/compose/convert/service.go index 2a8ed8288d..37f3ece403 100644 --- a/compose/convert/service.go +++ b/compose/convert/service.go @@ -263,10 +263,14 @@ func convertUpdateConfig(source *composetypes.UpdateConfig) *swarm.UpdateConfig func convertResources(source composetypes.Resources) (*swarm.ResourceRequirements, error) { resources := &swarm.ResourceRequirements{} + var err error if source.Limits != nil { - cpus, err := opts.ParseCPUs(source.Limits.NanoCPUs) - if err != nil { - return nil, err + var cpus int64 + if source.Limits.NanoCPUs != "" { + cpus, err = opts.ParseCPUs(source.Limits.NanoCPUs) + if err != nil { + return nil, err + } } resources.Limits = &swarm.Resources{ NanoCPUs: cpus, @@ -274,9 +278,12 @@ func convertResources(source composetypes.Resources) (*swarm.ResourceRequirement } } if source.Reservations != nil { - cpus, err := opts.ParseCPUs(source.Reservations.NanoCPUs) - if err != nil { - return nil, err + var cpus int64 + if source.Reservations.NanoCPUs != "" { + cpus, err = opts.ParseCPUs(source.Reservations.NanoCPUs) + if err != nil { + return nil, err + } } resources.Reservations = &swarm.Resources{ NanoCPUs: cpus, diff --git a/compose/convert/service_test.go b/compose/convert/service_test.go index 45da764325..2e614d730c 100644 --- a/compose/convert/service_test.go +++ b/compose/convert/service_test.go @@ -80,6 +80,29 @@ func TestConvertResourcesFull(t *testing.T) { assert.DeepEqual(t, resources, expected) } +func TestConvertResourcesOnlyMemory(t *testing.T) { + source := composetypes.Resources{ + Limits: &composetypes.Resource{ + MemoryBytes: composetypes.UnitBytes(300000000), + }, + Reservations: &composetypes.Resource{ + MemoryBytes: composetypes.UnitBytes(200000000), + }, + } + resources, err := convertResources(source) + assert.NilError(t, err) + + expected := &swarm.ResourceRequirements{ + Limits: &swarm.Resources{ + MemoryBytes: 300000000, + }, + Reservations: &swarm.Resources{ + MemoryBytes: 200000000, + }, + } + assert.DeepEqual(t, resources, expected) +} + func TestConvertHealthcheck(t *testing.T) { retries := uint64(10) source := &composetypes.HealthCheckConfig{