From f27a297595f2b38cba4b28d60bb8dd8702bfa841 Mon Sep 17 00:00:00 2001
From: Daniel Nephin <dnephin@docker.com>
Date: Mon, 9 Jan 2017 14:15:32 -0500
Subject: [PATCH] Fix parsing resources from compose file for stack deploy.

Signed-off-by: Daniel Nephin <dnephin@docker.com>
---
 cli/command/stack/deploy.go      | 19 +++++++++++++------
 cli/command/stack/deploy_test.go | 24 ++++++++++++++++++++++++
 2 files changed, 37 insertions(+), 6 deletions(-)

diff --git a/cli/command/stack/deploy.go b/cli/command/stack/deploy.go
index 8a18cff43a..d7e229f0ba 100644
--- a/cli/command/stack/deploy.go
+++ b/cli/command/stack/deploy.go
@@ -723,10 +723,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,
@@ -734,9 +738,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/cli/command/stack/deploy_test.go b/cli/command/stack/deploy_test.go
index 86ecf151d4..def014c997 100644
--- a/cli/command/stack/deploy_test.go
+++ b/cli/command/stack/deploy_test.go
@@ -5,6 +5,7 @@ import (
 
 	composetypes "github.com/aanand/compose-file/types"
 	"github.com/docker/docker/api/types/mount"
+	"github.com/docker/docker/api/types/swarm"
 	"github.com/docker/docker/pkg/testutil/assert"
 )
 
@@ -28,3 +29,26 @@ func TestConvertVolumeToMountInvalidFormat(t *testing.T) {
 		assert.Error(t, err, "invalid volume: "+vol)
 	}
 }
+
+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)
+}