Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow List[str] in DockerContainerListFilters['filters'] and format_dict_for_cli() #590

Conversation

LooLzzz
Copy link

@LooLzzz LooLzzz commented May 21, 2024

The docker cli allows for multiple --filter args to be passed,
this PR aims to imitate that.

https://docs.docker.com/reference/cli/docker/container/ls/#filter

Copy link
Collaborator

@LewisGaul LewisGaul left a comment

Choose a reason for hiding this comment

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

Hey @LooLzzz thanks for the contribution :) you've got some failures in the CI, but there's also a comment I had about the approach which it'd be good to get thoughts from @gabrieldemarmiesse on.

@@ -58,7 +58,7 @@
{
"id": str,
"name": str,
"label": str,
"label": Union[str, List[str]],
Copy link
Collaborator

@LewisGaul LewisGaul May 22, 2024

Choose a reason for hiding this comment

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

The problem I have with this is it special-cases label, when actually it's presumably valid to specify any of the filter types more than once? It seems like conceptually what we want is to allow specifying the same key twice, but that's not possible with normal dicts in python of course.

Any thoughts @gabrieldemarmiesse on how to resolve this? One option would be to accept a list of 2-tuples instead of a dict, i.e. something like docker.container.list(filters: Sequence[DockerContainerListFilters]) where DockerContainerListFilters = tuple[Literal["id"], str] | tuple[Literal["name"], str] | ....

Choose a reason for hiding this comment

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

I think to be backward compatible (and also because it's easy to write), we can apply the Union[str, List[str]] to all values in the TypedDict. So what's done in this PR, but for all elements. What do you think?

Copy link
Collaborator

@LewisGaul LewisGaul May 23, 2024

Choose a reason for hiding this comment

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

What I don't like about that is it's not truly mirroring the CLI (you don't specify --filter label=foo,bar), so it feels like sacrificing the quality of the API for minor backwards compatibility (could easily enough continue to support the old form until v1.0 if we wanted). Perhaps not all fields would make sense to be specified more than once, is that something we'd need to think about in your suggested approach?

A couple of other notes on this API:

[EDIT: realised I was looking at the image list APIs rather than container list, oops]

Choose a reason for hiding this comment

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

That would be worth a redesign from scratch possibly... As we're pre-1.0 I believe we can afford it

Copy link
Author

@LooLzzz LooLzzz May 23, 2024

Choose a reason for hiding this comment

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

Maybe chained calls would suite this API better?
Many ORMs are using this style of API

docker.container.list()
                .filter({'label': foo})
                .filter({'label': bar})

The actual evaluation is only performed when some magic method is called,
like __repr__ / __str__ / __list__

Copy link
Collaborator

@LewisGaul LewisGaul Sep 1, 2024

Choose a reason for hiding this comment

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

The lazy evaluation approach has downsides that make me hesitant:

  • implementation complexity
  • cognitive overhead caused by the fact that under certain APIs the actual command gets run at expected times

I still think we should go for:

docker.container.list(filters=[
    ("label", "foo"),
    ("label", "bar=1"),
    ("label!", "foo=0"),
    ("status", "created"),
    ("status", "running"),
    ("name", "prefix-*"),
])

(Note that the CLI is a bit confusing here in that label filters are ANDed together whereas status filters are ORed together).

@LewisGaul
Copy link
Collaborator

Tracking the core of the issue at #629

@LewisGaul
Copy link
Collaborator

Closing in favour of #635 - thanks for bringing this issue up, it'll be good to have this solved properly.

@LewisGaul LewisGaul closed this Sep 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants