Skip to content

registry: isCIDRMatch: avoid performing DNS lookups if not needed#48999

Merged
thaJeztah merged 3 commits intomoby:masterfrom
thaJeztah:no_dnslookup
Dec 3, 2024
Merged

registry: isCIDRMatch: avoid performing DNS lookups if not needed#48999
thaJeztah merged 3 commits intomoby:masterfrom
thaJeztah:no_dnslookup

Conversation

@thaJeztah
Copy link
Copy Markdown
Member

A colleague found that isCIDRMatch (called indirectly through ParseRepositoryInfo), is performing DNS lookups. These lookups are related to it assuming is being run as part of the docker engine, and has to check whether the registry is marked as "insecure" in daemon config.

A consequence of this was that tests were slow as they were using foo.example.com (and similar) domains.

This is a first set of changes to avoid performing DNS lookups; the core problem lies in newRegistryInfo (which is called as part of the above) always tries to propagate all information, including whether the registry is marked "secure". This information is not used in any way for getting the key to use for storing auth, but requires some additional changes to remove (which I'll do in follow-ups);

moby/registry/config.go

Lines 386 to 405 in 321f9c2

func newIndexInfo(config *serviceConfig, indexName string) (*registry.IndexInfo, error) {
var err error
indexName, err = ValidateIndexName(indexName)
if err != nil {
return nil, err
}
// Return any configured index info, first.
if index, ok := config.IndexConfigs[indexName]; ok {
return index, nil
}
// Construct a non-configured index info.
return &registry.IndexInfo{
Name: indexName,
Mirrors: make([]string, 0),
Secure: config.isSecureIndex(indexName),
Official: false,
}, nil
}

@thaJeztah thaJeztah added area/distribution Image Distribution status/2-code-review kind/refactor PR's that refactor, or clean-up code labels Nov 29, 2024
@thaJeztah thaJeztah added this to the 28.0.0 milestone Nov 29, 2024
@thaJeztah thaJeztah self-assigned this Nov 29, 2024
@thaJeztah
Copy link
Copy Markdown
Member Author

Failure is unrelated; will be fixed by;

Skip all code if there's nothing to match against.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
isCIDRMatch defaulted to trying to resolve a hostname to get its
IP-address(es) before trying if the given host was an IP address
already.

Let's reverse the order so that we can avoid performing a DNS lookup
when it's not needed.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
Comment thread registry/config.go Outdated
Comment thread registry/config.go Outdated
Comment on lines -38 to -41
if host == "127.0.0.1" {
// I believe in future Go versions this will fail, so let's fix it later
return net.LookupIP(host)
}
Copy link
Copy Markdown
Member Author

@thaJeztah thaJeztah Dec 2, 2024

Choose a reason for hiding this comment

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

As we're now skipping lookupIP if it's an IP address, this case can be removed; let me update

@thaJeztah
Copy link
Copy Markdown
Member Author

This one's really flaky recently;

=== FAIL: amd64.integration.container TestWaitRestartedContainer/not-running (5.76s)
    wait_test.go:232: assertion failed: 5 (expectedCode int64) != 137 (waitRes.StatusCode int64)
    --- FAIL: TestWaitRestartedContainer/not-running (5.76s)

Comment thread registry/config.go
Comment on lines +292 to +294
if len(cidrs) == 0 {
return false
}
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.

Sadly this currently doesn't help, as we're unconditionally initialising RepoInfo with localhost CIDRs; while that's relevant for some uses of RepoInfo, it's not for others, and I'll look for follow-ups to split those use-cases.

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

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

Labels

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