Skip to content

registry: some optimizations to reduce network connections and DNS lookups if not needed#49050

Merged
thaJeztah merged 4 commits intomoby:masterfrom
thaJeztah:registry_cleanups
Dec 10, 2024
Merged

registry: some optimizations to reduce network connections and DNS lookups if not needed#49050
thaJeztah merged 4 commits intomoby:masterfrom
thaJeztah:registry_cleanups

Conversation

@thaJeztah
Copy link
Copy Markdown
Member

registry: loginV2: don't contact registry when failing to construct request

Reverse the order in which we call v2AuthHTTPClient and http.NewRequest.
This is mostly theoretical, but v2AuthHTTPClient makes a network connection
to ping the registry, but loginV2 may fail after this if http.NewRequest
fails. Put the (lightweight) http.NewRequest first, so that we can return
early before trying to contact the registry.

registry: loginV2: move variables closer to where they're used

Also rename a variable that shadowed a package type.

registry: Service.lookupV2Endpoints: add arg to skip mirrors

This function unconditionally constructed endpoints for mirrors when
requesting endpoints for the default (Docker Hub) registry. Doing so
involves validating the config, which involves;

  • parsing the hostname
  • constructing TLS config
  • performing a DNS lookup to resolve the host's IP address and matching
    it against CIDR masks for insecure registries.

When looking up push endpoints or endpoints to consider for authentication,
mirror endpoints were discarded to prevent sending credentials of the upstream
registry to a mirror.

This patch adds a "includeMirrors" argument to skip constructing endpoints
for mirrors when not needed.

registry: ConvertToHostname: use strings.Cut to reduce allocations

Slight refactor to use strings.Cut, which doesn't do allocations

- A picture of a cute animal (not mandatory but encouraged)

@thaJeztah thaJeztah added area/distribution Image Distribution status/2-code-review kind/refactor PR's that refactor, or clean-up code area/authentication labels Dec 8, 2024
@thaJeztah thaJeztah added this to the 28.0.0 milestone Dec 8, 2024
@thaJeztah thaJeztah self-assigned this Dec 8, 2024
Comment thread registry/service_v2.go
for _, mirror := range s.config.Mirrors {
if !strings.HasPrefix(mirror, "http://") && !strings.HasPrefix(mirror, "https://") {
mirror = "https://" + mirror
if includeMirrors {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This diff is best reviewed with whitespaces ignored; use ?w=1 for the diff view; https://github.com/moby/moby/pull/49050/files?w=1

@thaJeztah thaJeztah changed the title registry: some optimisations to reduce DNS lookups if not needed registry: some optimisations to reduce network connections and DNS lookups if not needed Dec 8, 2024
@thaJeztah thaJeztah changed the title registry: some optimisations to reduce network connections and DNS lookups if not needed registry: some optimizations to reduce network connections and DNS lookups if not needed Dec 8, 2024
@thaJeztah thaJeztah force-pushed the registry_cleanups branch 2 times, most recently from 182cf8b to aaee597 Compare December 9, 2024 13:48
@thaJeztah thaJeztah requested a review from laurazard December 9, 2024 13:49
Copy link
Copy Markdown
Contributor

@robmry robmry left a comment

Choose a reason for hiding this comment

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

LGTM apart from the old comment.

Comment thread registry/service.go
Comment on lines 71 to +74
// Lookup endpoints for authentication using "LookupPushEndpoints", which
// excludes mirrors to prevent sending credentials of the upstream registry
// to a mirror.
endpoints, err := s.LookupPushEndpoints(registryHostName)
s.mu.RLock()
endpoints, err := s.lookupV2Endpoints(registryHostName, false)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The comment needs a matching update.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Oh! Good catch; I initially kept it to use the "push endpoints", but then thought it may be clearer to remove that extra indirect and make it match the "except for mirrors" part.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Updated 👍

…equest

Reverse the order in which we call v2AuthHTTPClient and http.NewRequest.
This is mostly theoretical, but v2AuthHTTPClient makes a network connection
to ping the registry, but loginV2 may fail after this if http.NewRequest
fails. Put the (lightweight) http.NewRequest first, so that we can return
early before trying to contact the registry.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
Also rename a variable that shadowed a package type.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
This function unconditionally constructed endpoints for mirrors when
requesting endpoints for the default (Docker Hub) registry. Doing so
involves validating the config, which involves;

- parsing the hostname
- constructing TLS config
- performing a DNS lookup to resolve the host's IP address and matching
  it against CIDR masks for insecure registries.

When looking up push endpoints or endpoints to consider for authentication,
mirror endpoints were discarded to prevent sending credentials of the upstream
registry to a mirror.

This patch adds a "includeMirrors" argument to skip constructing endpoints
for mirrors when not needed. While at it, also removing named output variables,
as they didn't add much.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
Slight refactor to use strings.Cut, which doesn't do allocations

Signed-off-by: Sebastiaan van Stijn <[email protected]>
@thaJeztah
Copy link
Copy Markdown
Member Author

All green; let me bring this one in 👍

@thaJeztah thaJeztah merged commit 4ba7860 into moby:master Dec 10, 2024
@thaJeztah thaJeztah deleted the registry_cleanups branch December 10, 2024 13:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/authentication area/distribution Image Distribution kind/refactor PR's that refactor, or clean-up code status/2-code-review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants