Merge pull request #1566 from thaJeztah/fix_panic_on_update

Fix panic (npe) when updating service limits/reservations
This commit is contained in:
Silvin Lubecki 2018-12-13 09:47:31 +01:00 committed by GitHub
commit b9f150b17e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 2 deletions

View File

@ -302,6 +302,12 @@ func updateService(ctx context.Context, apiClient client.NetworkAPIClient, flags
if task.Resources == nil { if task.Resources == nil {
task.Resources = &swarm.ResourceRequirements{} 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 return task.Resources
} }

View File

@ -617,6 +617,38 @@ func TestUpdateIsolationValid(t *testing.T) {
// and that values are not updated are not reset to their default value // and that values are not updated are not reset to their default value
func TestUpdateLimitsReservations(t *testing.T) { func TestUpdateLimitsReservations(t *testing.T) {
spec := swarm.ServiceSpec{ 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{ TaskTemplate: swarm.TaskSpec{
ContainerSpec: &swarm.ContainerSpec{}, ContainerSpec: &swarm.ContainerSpec{},
Resources: &swarm.ResourceRequirements{ Resources: &swarm.ResourceRequirements{
@ -632,8 +664,8 @@ func TestUpdateLimitsReservations(t *testing.T) {
}, },
} }
flags := newUpdateCommand(nil).Flags() flags = newUpdateCommand(nil).Flags()
err := flags.Set(flagLimitCPU, "2") err = flags.Set(flagLimitCPU, "2")
assert.NilError(t, err) assert.NilError(t, err)
err = flags.Set(flagReserveCPU, "2") err = flags.Set(flagReserveCPU, "2")
assert.NilError(t, err) assert.NilError(t, err)