From 16349f6e338eb992d875d283de5fe3e5190c771a Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Thu, 13 Dec 2018 01:58:12 +0100 Subject: [PATCH] Fix panic (npe) when updating service limits/reservations Signed-off-by: Sebastiaan van Stijn (cherry picked from commit 579bb918535bdab94dd29efaea5d81f39943a88a) Signed-off-by: Sebastiaan van Stijn --- cli/command/service/update.go | 6 +++++ cli/command/service/update_test.go | 36 ++++++++++++++++++++++++++++-- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/cli/command/service/update.go b/cli/command/service/update.go index 9076a9a4b9..97a67576a8 100644 --- a/cli/command/service/update.go +++ b/cli/command/service/update.go @@ -302,6 +302,12 @@ func updateService(ctx context.Context, apiClient client.NetworkAPIClient, flags if task.Resources == nil { task.Resources = &swarm.ResourceRequirements{} } + if task.Resources.Limits == nil { + task.Resources.Limits = &swarm.Resources{} + } + if task.Resources.Reservations == nil { + task.Resources.Reservations = &swarm.Resources{} + } return task.Resources } diff --git a/cli/command/service/update_test.go b/cli/command/service/update_test.go index 847b6ab128..5cfc3b85ed 100644 --- a/cli/command/service/update_test.go +++ b/cli/command/service/update_test.go @@ -617,6 +617,38 @@ func TestUpdateIsolationValid(t *testing.T) { // and that values are not updated are not reset to their default value func TestUpdateLimitsReservations(t *testing.T) { spec := swarm.ServiceSpec{ + TaskTemplate: swarm.TaskSpec{ + ContainerSpec: &swarm.ContainerSpec{}, + }, + } + + // test that updating works if the service did not previously + // have limits set (https://github.com/moby/moby/issues/38363) + flags := newUpdateCommand(nil).Flags() + err := flags.Set(flagLimitCPU, "2") + assert.NilError(t, err) + err = flags.Set(flagLimitMemory, "200M") + assert.NilError(t, err) + err = updateService(context.Background(), nil, flags, &spec) + assert.NilError(t, err) + + spec = swarm.ServiceSpec{ + TaskTemplate: swarm.TaskSpec{ + ContainerSpec: &swarm.ContainerSpec{}, + }, + } + + // test that updating works if the service did not previously + // have reservations set (https://github.com/moby/moby/issues/38363) + flags = newUpdateCommand(nil).Flags() + err = flags.Set(flagReserveCPU, "2") + assert.NilError(t, err) + err = flags.Set(flagReserveMemory, "200M") + assert.NilError(t, err) + err = updateService(context.Background(), nil, flags, &spec) + assert.NilError(t, err) + + spec = swarm.ServiceSpec{ TaskTemplate: swarm.TaskSpec{ ContainerSpec: &swarm.ContainerSpec{}, Resources: &swarm.ResourceRequirements{ @@ -632,8 +664,8 @@ func TestUpdateLimitsReservations(t *testing.T) { }, } - flags := newUpdateCommand(nil).Flags() - err := flags.Set(flagLimitCPU, "2") + flags = newUpdateCommand(nil).Flags() + err = flags.Set(flagLimitCPU, "2") assert.NilError(t, err) err = flags.Set(flagReserveCPU, "2") assert.NilError(t, err)