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)