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

fix DinD host detection when using local socket #283

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ikravets
Copy link
Contributor

When using IPC socket (unix/npipe) to communicate with local docker server docker-py implementation rewrites the base url to HTTP, see https://github.com/docker/docker-py/blob/e901eac7a8c5f29c7720eafb9f58c8356cca2324/docker/api/client.py#L143-L168

We cannot rely on URL scheme in order to detect such connections. Instead, we detect connection adapters added by docker-py api client:

As NpipeHTTPAdapter type may not be available outside of Windows we rely on adapter-specific attributes.

@codecov-commenter
Copy link

codecov-commenter commented Dec 27, 2022

Codecov Report

❗ No coverage uploaded for pull request base (main@928af5a). Click here to learn what that means.
The diff coverage is n/a.

❗ Current head afa057e differs from pull request most recent head f9d4508. Consider uploading reports for the commit f9d4508 to get more accurate results

@@           Coverage Diff           @@
##             main     #283   +/-   ##
=======================================
  Coverage        ?   87.30%           
=======================================
  Files           ?       30           
  Lines           ?      843           
  Branches        ?       57           
=======================================
  Hits            ?      736           
  Misses          ?       76           
  Partials        ?       31           

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@ikravets
Copy link
Contributor Author

ikravets commented Jan 1, 2023

Rebased to the master. @tillahoffmann, any chance for a review?

@ikravets
Copy link
Contributor Author

rebased to master

@tillahoffmann
Copy link
Collaborator

Thank you for these improvements! Are there tests that we could add that fail before the change is introduced to avoid future regressions?

Copy link
Collaborator

@tillahoffmann tillahoffmann left a comment

Choose a reason for hiding this comment

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

This is great, thank you for having such a detailed look into improving the host detection. Host detection is the challenge that seems to cause most trouble for our users so far.

Do you know whether this change would also resolve the issue of localnpipe appearing in the connection string, as discussed in #108? You're much more clued up on this than I am.

adapter = self.client.api.get_adapter(self.client.api.base_url)
is_ipc = isinstance(adapter, UnixHTTPAdapter)
is_ipc |= hasattr(adapter, "socket_path") or hasattr(adapter, "npipe_path")
is_ipc |= 'unix' in url.scheme or 'npipe' in url.scheme
Copy link
Collaborator

Choose a reason for hiding this comment

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

Even if the NpipeHTTPAdapter is only used on windows, would it still be available for an isinstance check independent of the platform? Or does the docker package not even expose NpipeHTTPAdapter if it's not on windows?

Alternatively, would checking adapter.__class__.name == 'NpipeHTTPAdapter' be easier to understand than checking the attributes of the adapter?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think that NpipeHTTPAdapter symbol is available outside of windows. Checking the class name would break for NpipeHTTPAdapter subclasses. So I went on with a classic duck-typing.

When using IPC socket (unix/npipe) to communicate with local docker
server docker-py implementation rewrites the base url to HTTP, see
https://github.com/docker/docker-py/blob/e901eac7a8c5f29c7720eafb9f58c8356cca2324/docker/api/client.py#L143-L168

We cannot rely on URL scheme in order to detect such connections.
Instead, we detect connection adapters added by docker-py api client:

- UnixHTTPAdapter
  https://github.com/docker/docker-py/blob/e901eac7a8c5f29c7720eafb9f58c8356cca2324/docker/transport/unixconn.py

- NpipeHTTPAdapter
  https://github.com/docker/docker-py/blob/e901eac7a8c5f29c7720eafb9f58c8356cca2324/docker/transport/npipeconn.py

As NpipeHTTPAdapter type may not be available outside of Windows we rely on
adapter-specific attributes.
@ikravets
Copy link
Contributor Author

ikravets commented Jul 5, 2023

@tillahoffmann Hi, it's been more than half a year since I've opened this PR. Can we maybe merge this?

@ikravets
Copy link
Contributor Author

@tillahoffmann a kind ping

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.

None yet

3 participants