Skip to content

Fix prefix-matching for service ps#32800

Closed
thaJeztah wants to merge 1 commit intomoby:masterfrom
thaJeztah:fix-prefix-matching
Closed

Fix prefix-matching for service ps#32800
thaJeztah wants to merge 1 commit intomoby:masterfrom
thaJeztah:fix-prefix-matching

Conversation

@thaJeztah
Copy link
Member

@thaJeztah thaJeztah commented Apr 24, 2017

The docker CLI matches objects either by ID prefix
or a full name match, but not partial name matches.

The correct order of resolution is;

  • Full ID match (a name should not be able to mask an ID)
  • Full name
  • ID-prefix

This patch changes the way services are matched.

Also change to use the first matching service, if there's a full match (by ID or Name) instead of continue looking for other possible matches.

Error handling changed;

  • Do not error early if multiple services were requested and one or more services were not found. Print the services that were not found after printing those that were found instead
  • Print an error if ID-prefix matching is ambiguous

fixes #32556

With this patch applied;

Given these services

$ docker service ls
ID                  NAME                      MODE                REPLICAS            IMAGE               PORTS
71yo8k34uxd6        db                        replicated          1/1                 busybox:latest
wt6z677fhoou        db2                       replicated          1/1                 busybox:latest
oygr4njb0qxl        71yo8k34uxd6              replicated          1/1                 busybox:latest
l6ps3579zt0u        hopeful_feynman           replicated          1/1                 busybox:latest
l9ehdvyxkyiy        xenodochial_stonebraker   replicated          1/1                 busybox:latest

Match by name no longer does a "prefix" match

$ docker service ps db
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE           ERROR                              PORTS
ygrefa0tph0c        db.1                busybox:latest      9c8f9fbf54e8        Running             Running 2 minutes ago

Full name match "trumps" partial ID match;

$ docker service ps 71yo8k34uxd6
ID                  NAME                 IMAGE               NODE                DESIRED STATE       CURRENT STATE           ERROR                              PORTS
lgq4j24xl54m        71yo8k34uxd6.1       busybox:latest      9c8f9fbf54e8        Running             Running 2 minutes ago

Ambiguous ID-prefix produces an error

$ docker service ps l
multiple services found with provided prefix: l

$ docker service ps l6
ID                  NAME                    IMAGE               NODE                DESIRED STATE       CURRENT STATE            ERROR                              PORTS
ls2ml2jkxlve        hopeful_feynman.1       busybox:latest      9c8f9fbf54e8        Running             Running 25 seconds ago

Don't error out on first non-existing service, but print errors at the end

$ docker service ps db foo bar
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE           ERROR               PORTS
4b3jlca22dho        db.1                busybox:latest      9c8f9fbf54e8        Running             Running 2 minutes ago
no such service: foo
no such service: bar

@AkihiroSuda
Copy link
Member

off-topic: I know CLI is being ripped out (#32694) from moby/moby, but what is the "current" approach to update the CLI code?

It seems we need to continue to update moby/moby/cli "for a while", and Docker employees eventually cherry-pick up them into docker/docker-cli?

@AkihiroSuda AkihiroSuda added the area/cli Client label Apr 25, 2017
@aaronlehmann
Copy link

This unfortunately isn't the first time we've had inconsistencies in how names/IDs are resolved. I'd really like to see us create a package that defines the canonical approach. It could take a set of names and IDs as input, and either match one of them or return an appropriate error.

Note that the PR description (but not the code itself) is incorrect: it says that partial name matches are part of the correct order. In fact, we don't do partial name resolution.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we're choosing the first service whose ID matches the prefix, and ignoring the others. I think we should error out if the prefix is ambiguous. There are several places in the code where we do this, and again, I think in the long term it would be a good idea to consolidate the code in a central package.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree we need to have a standard solution. I was actually considering to error out if matches > 1; this will complicate the logic a bit, because we'll have to check number of matches per keyword, and possibly de-duplicate the keywords (e.g. docker service ps foo foo)

@thaJeztah thaJeztah force-pushed the fix-prefix-matching branch 2 times, most recently from 10fc3ef to 0044194 Compare April 26, 2017 20:57
@thaJeztah
Copy link
Member Author

@aaronlehmann updated the PR. It's a bit ugly (lots of logic in there), but I think this addresses all situations

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should go above, with "strings"

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh cr*p, it was automatically inserted by my editor. thx for catching

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to ignore this, but I try to avoid using Sprintf for simple string concatenation. With the + operator, there isn't a chance of proving the wrong number or type of arguments to Sprintf.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

heh, I actually had that at first, but wasn't sure if we liked plain concatting. I'll change

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like matches could just be a boolean, since we only care about whether there was a previous match or not. But it's fine either way.

@aaronlehmann
Copy link

Overall LGTM

The failures seem unrelated

The docker CLI matches objects either by ID _prefix_
or a full name match, but not partial name matches.

The correct order of resolution is;

- Full ID match (a name should not be able to mask an ID)
- Full name
- ID-prefix

This patch changes the way services are matched.

Also change to use the first matching service, if there's a
full match (by ID or Name) instead of continue looking for
other possible matches.

Error handling changed;

- Do not error early if multiple services were requested
  and one or more services were not found. Print the
  services that were not found after printing those that
  _were_ found instead
- Print an error if ID-prefix matching is ambiguous

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
@thaJeztah thaJeztah force-pushed the fix-prefix-matching branch from 0044194 to 4242aec Compare April 27, 2017 01:06
@thaJeztah
Copy link
Member Author

Addressed nits :)

Copy link
Member

@vdemeester vdemeester left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🐯

@AkihiroSuda
Copy link
Member

Please reopen to docker/CLI?

@thaJeztah
Copy link
Member Author

opened docker/cli#72 for the CLI side, I'll bump the CLI version and remove those changes here

Copy link
Contributor

@boaz0 boaz0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@aaronlehmann
Copy link

@thaJeztah: Is this needed to fix the test now that the CLI behavior has changed?

@thaJeztah
Copy link
Member Author

@aaronlehmann the CLI in this repo still uses an older version, but we need to update it. Problem is currently that I don't know how to get the 17.06 CLI in. I think the plan is to fixate the CLI to 17.06-GA for integration tests, but given that https://github.com/docker/cli does not have a tag or branch for 17.06, we cannot get it from there; it needs to be fetched from https://github.com/docker/docker-ce, but that means getting the entire "docker" repository (which is big), so not idea.

@thaJeztah
Copy link
Member Author

Actually, thinking if #33684 will pass, because it will be running against the old CLI

@thaJeztah
Copy link
Member Author

Nevermind, it doesn't assume the CLI changes yet

@aaronlehmann
Copy link

I think the plan is to fixate the CLI to 17.06-GA for integration tests, but given that https://github.com/docker/cli does not have a tag or branch for 17.06, we cannot get it from there; it needs to be fetched from https://github.com/docker/docker-ce, but that means getting the entire "docker" repository (which is big), so not idea.

That is mind-boggling.

  • Why doesn't docker/cli have a tag or branch for 17.06? That seems very wrong.
  • If moby is supposed to be independent of released docker versions, why must the CLI be fixed to any particular released version?

@thaJeztah
Copy link
Member Author

Why doesn't docker/cli have a tag or branch for 17.06? That seems very wrong.

All cherry-picking is done in docker/docker-ce, so that no branches had to be maintained for the individual repos

If moby is supposed to be independent of released docker versions, why must the CLI be fixed to any particular released version?

After 17.06-GA we won't accept new integration tests that use the Docker CLI (at least, not in this repository), the 17.06 CLI will be used for the existing tests, to catch regressions. Newer versions may be used in a e2e test-suite managed by Docker, Inc., but that's not a responsibility in this repo

@aaronlehmann
Copy link

All cherry-picking is done in docker/docker-ce, so that no branches had to be maintained for the individual repos

What is wrong with having branches in docker/cli?

I think cherry-picking in docker-ce is a very bad idea because means the unit test coverage is completely lost.

It also has the disadvantage that you pointed out where there is no revision in docker/cli corresponding to what was actually released, and one has to refer to another repository which is less convenient.

After 17.06-GA we won't accept new integration tests that use the Docker CLI (at least, not in this repository), the 17.06 CLI will be used for the existing tests, to catch regressions. Newer versions may be used in a e2e test-suite managed by Docker, Inc., but that's not a responsibility in this repo

This doesn't answer my question of why the cli must be fixed at that particular version, instead of, say, master from docker/cli on the date of the release.

@dnephin
Copy link
Member

dnephin commented Jun 15, 2017

This was merged in docker/cli so closing it here.

We'll need to port this cli test to an API test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

docker servcie ps <service_name> lists all instances of services with names starting with service_name

7 participants