DockerCLI/cli/command/service
Sebastiaan van Stijn 2fc608cea6
Fix order of processing of some xx-add/xx-rm service update flags
Combining `-add` and `-rm` flags on `docker service update` should
be usable to explicitly replace existing options. The current order
of processing did not allow this, causing the `-rm` flag to remove
properties that were specified in `-add`. This behavior was inconsistent
with (for example) `--host-add` and `--host-rm`.

This patch updates the behavior to first remove properties, then
add new properties.

Note that there's still some improvements to make, to make the removal
more granulas (e.g. to make `--label-rm label=some-value` only remove
the label if value matches `some-value`); these changes are left for
a follow-up.

Before this change:
-----------------------------

Create a service with two env-vars

```bash
docker service create --env FOO=bar --env BAR=baz  --name=test nginx:alpine
docker service inspect --format '{{json .Spec.TaskTemplate.ContainerSpec.Env }}' test | jq .
[
  "FOO=bar",
  "BAR=baz"
]
```

Update the service, with the intent to replace the value of `FOO` for a new value

```bash
docker service update  --env-rm FOO --env-add FOO=updated-foo test
docker service inspect --format '{{json .Spec.TaskTemplate.ContainerSpec.Env }}' test | jq .
[
  "BAR=baz"
]
```

Create a service with two labels

```bash
docker service create --label FOO=bar --label BAR=baz  --name=test nginx:alpine
docker service inspect --format '{{json .Spec.Labels }}' test | jq .
{
  "BAR": "baz",
  "FOO": "bar"
}
```

Update the service, with the intent to replace the value of `FOO` for a new value

```bash
docker service update  --label-rm FOO --label-add FOO=updated-foo test
docker service inspect --format '{{json .Spec.Labels }}' test | jq .
{
  "BAR": "baz"
}
```

Create a service with two container labels

```bash
docker service create --container-label FOO=bar --container-label BAR=baz  --name=test nginx:alpine
docker service inspect --format '{{json .Spec.TaskTemplate.ContainerSpec.Labels }}' test | jq .
{
  "BAR": "baz",
  "FOO": "bar"
}
```

Update the service, with the intent to replace the value of `FOO` for a new value

```bash
docker service update  --container-label-rm FOO --container-label-add FOO=updated-foo test
docker service inspect --format '{{json .Spec.TaskTemplate.ContainerSpec.Labels }}' test | jq .
{
  "BAR": "baz",
}
```

With this patch applied:
--------------------------------

Create a service with two env-vars

```bash
docker service create --env FOO=bar --env BAR=baz  --name=test nginx:alpine
docker service inspect --format '{{json .Spec.TaskTemplate.ContainerSpec.Env }}' test | jq .
[
  "FOO=bar",
  "BAR=baz"
]
```

Update the service, and replace the value of `FOO` for a new value

```bash
docker service update  --env-rm FOO --env-add FOO=updated-foo test
docker service inspect --format '{{json .Spec.TaskTemplate.ContainerSpec.Env }}' test | jq .
[
  "BAR=baz",
  "FOO=updated-foo"
]
```

Create a service with two labels

```bash
docker service create --label FOO=bar --label BAR=baz  --name=test nginx:alpine
docker service inspect --format '{{json .Spec.Labels }}' test | jq .
{
  "BAR": "baz",
  "FOO": "bar"
}
```

Update the service, and replace the value of `FOO` for a new value

```bash
docker service update  --label-rm FOO --label-add FOO=updated-foo test
docker service inspect --format '{{json .Spec.Labels }}' test | jq .
{
  "BAR": "baz",
  "FOO": "updated-foo"
}
```

Create a service with two container labels

```bash
docker service create --container-label FOO=bar --container-label BAR=baz  --name=test nginx:alpine
docker service inspect --format '{{json .Spec.TaskTemplate.ContainerSpec.Labels }}' test | jq .
{
  "BAR": "baz",
  "FOO": "bar"
}
```

Update the service, and replace the value of `FOO` for a new value

```bash
docker service update  --container-label-rm FOO --container-label-add FOO=updated-foo test
docker service inspect --format '{{json .Spec.TaskTemplate.ContainerSpec.Labels }}' test | jq .
{
  "BAR": "baz",
  "FOO": "updated-foo"
}
```

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2020-08-04 19:09:59 +02:00
..
progress Add jobs support to CLI 2020-04-24 11:22:10 -05:00
testdata Add support for --limit-pids on service create / update (swarm) 2020-06-18 21:25:02 +02:00
client_test.go service: remove unused opts from newService() (unparam) 2019-10-31 19:22:43 +01:00
cmd.go Error out on orchestrator command that don't support k8s yet 2017-12-26 11:46:59 +01:00
create.go Add jobs support to CLI 2020-04-24 11:22:10 -05:00
create_test.go bump gotest.tools v3.0.1 for compatibility with Go 1.14 2020-02-23 00:28:55 +01:00
formatter.go docker service inspect fails when TaskTemplate.Resources is nil 2020-07-28 22:38:33 +02:00
formatter_test.go Add jobs support to CLI 2020-04-24 11:22:10 -05:00
generic_resource_opts.go Added support for generic resource update 2017-11-28 18:03:10 +01:00
generic_resource_opts_test.go bump gotest.tools v3.0.1 for compatibility with Go 1.14 2020-02-23 00:28:55 +01:00
helpers.go Switch from x/net/context to context 2018-05-11 16:49:43 -07:00
inspect.go formatter package heavy refactoring 2018-10-23 17:05:44 +02:00
inspect_test.go Add support for --limit-pids on service create / update (swarm) 2020-06-18 21:25:02 +02:00
list.go Add jobs support to CLI 2020-04-24 11:22:10 -05:00
list_test.go bump gotest.tools v3.0.1 for compatibility with Go 1.14 2020-02-23 00:28:55 +01:00
logs.go Explicit Z on logs timestamp examples 2020-05-28 15:24:07 +03:00
opts.go Add support for --limit-pids on service create / update (swarm) 2020-06-18 21:25:02 +02:00
opts_test.go Add support for --limit-pids on service create / update (swarm) 2020-06-18 21:25:02 +02:00
parse.go Add CredentialSpec tests 2019-04-12 11:17:34 -05:00
ps.go Migrate to non-deprecated functions of `api/types/filters` 2018-07-27 15:37:43 +02:00
ps_test.go bump gotest.tools v3.0.1 for compatibility with Go 1.14 2020-02-23 00:28:55 +01:00
remove.go Switch from x/net/context to context 2018-05-11 16:49:43 -07:00
rollback.go Update code for upstream cobra 2017-10-25 14:49:26 -04:00
rollback_test.go Replace deprecated Cobra command.SetOutput() with command.SetOut() 2020-05-07 14:25:59 +02:00
scale.go Add jobs support to CLI 2020-04-24 11:22:10 -05:00
trust.go Switch from x/net/context to context 2018-05-11 16:49:43 -07:00
update.go Fix order of processing of some xx-add/xx-rm service update flags 2020-08-04 19:09:59 +02:00
update_test.go Fix order of processing of some xx-add/xx-rm service update flags 2020-08-04 19:09:59 +02:00