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

Add "network exists" command #632

Merged

Conversation

eclark0426
Copy link
Contributor

Podman has support for a "network exists" command which checks if a network exists. This adds support for that command, although it does so by way of network inspect for compatibility with Docker, which does not have a standalone network exists command.

Closes #618

@@ -209,6 +210,26 @@ def disconnect(
full_cmd += [network, container]
run(full_cmd)

def exists(
self,
network: str,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should support passing a Network object too, i.e. this should be ValidNetwork as with other methods such as disconnect() above.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated, along with making sure the test covers that use case.

@overload
def inspect(self, x: str) -> Network: ...
def inspect(self, x: ValidNetwork) -> Network: ...
Copy link
Collaborator

Choose a reason for hiding this comment

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

inspect() doesn't make a lot of sense to take a Network object, since it's used for getting a Network object. Can you instead make exists() do self.inspect(str(network))? (or check how his is handled for other object types such as Container, Pod, ...)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It seems like things are slightly inconsistent on whether or not inspect() takes ValidSomeObject or just str. This change was based on ContainerCLI.inspect(), which takes ValidContainer as an argument. PodCLI.inspect() similarly takes ValidPod. However, VolumeCLI.inspect() and ServiceCLI.inspect() only take a str.

Given the inconsistency, sounds like str would be the preferred approach, yes?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh dear 🥲 I don't mind - the main thing I noticed is you didn't quite update the type annotations correctly in adding the support. I'd leave it as just str here to keep the scope of the changeset contained.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved back to str and used self.id in Network.exists().

You'll have to excuse any errors in my type changes, I'm not all that familiar with the codebase. Been using it a lot the last several months, but only recently dived into the actual code. For my own understanding, what was incorrect with how I'd updated the type annotations?

Copy link
Collaborator

Choose a reason for hiding this comment

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

For my own understanding, what was incorrect with how I'd updated the type annotations?

In this commit you had

    @overload
    def inspect(self, x: ValidNetwork) -> Network: ...

    @overload
    def inspect(self, x: List[str]) -> List[Network]: ...

    def inspect(self, x: Union[str, List[str]]) -> Union[Network, List[Network]]:
        if isinstance(x, str) or isinstance(x, Network):
            return Network(self.client_config, x)
        else:
            return [Network(self.client_config, reference) for reference in x]
  • The @overload methods say that the two possible invocations are a single ValidNetwork (str | Network) or a list of str. This means there's not support for a list of Network, i.e. the second overload should've had List[ValidNetwork] (although List is a bad choice of type, see Avoid using List and Dict for function argument types #584).
  • The actual method signature (without @overload) wasn't updated to support Network at all, so is inconsistent with the overloads.

If we wanted to support Network objects here I would make this:

    @overload
    def inspect(self, x: ValidNetwork) -> Network: ...

    @overload
    def inspect(self, x: Iterable[ValidNetwork]) -> List[Network]: ...

    def inspect(self, x: Union[ValidNetwork, Iterable[ValidNetwork]]) -> Union[Network, List[Network]]:
        if isinstance(x, Iterable) and not isinstance(x, str):
            return [Network(self.client_config, str(net)) for net in x]
        else:
            return Network(self.client_config, str(x))

Copy link
Collaborator

Choose a reason for hiding this comment

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

To be clear, I still think the exists() method should support Network types:

    def exists(self, network: ValidNetwork) -> bool:
        try:
            self.inspect(str(network))
        except NoSuchNetwork:
            return False
        else:
            return True

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it. Overloads are one of things I haven't touched much with type annotations so it doesn't entirely surprise me that that would be where I went awry. Thanks for the clarification!

@LewisGaul
Copy link
Collaborator

LewisGaul commented Sep 4, 2024

Thanks for doing this enhancement :) Currently all PRs are blocked on the docs CI job being broken, which I believe I have fixed in #628 but this needs confirming.

[EDIT: now fixed, merge in latest master branch]

Podman has added support for a "network exists" command
which checks if a network exists. This adds support for that
command, although it does so by way of network inspect for
cross-compatibility with Docker.
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.

Thanks again :)

@LewisGaul LewisGaul merged commit 4a518a5 into gabrieldemarmiesse:master Sep 5, 2024
21 checks passed
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.

Missing support for 'podman network exists'
3 participants