-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expose DNS resolver functionality for extensions #5421
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
Expose DNS resolver functionality for extensions #5421
Conversation
This simplifies the internal API by returning the structured Host type directly rather than converting it to a string. The caller can calls .String() on the returned Host when needed. This change makes it easier for future code to access the IP and port components separately without needing to parse the string representation.
lib/vu_state.go
Outdated
| // GetAddrResolver returns the AddrResolver implementation used by the Dialer. | ||
| // It returns nil if the Dialer doesn't implement AddrResolver. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would skip the implementation details - and just go with it will return the addresolver or not if it is not available.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, I modified it according to the suggestion.
lib/vu_state.go
Outdated
| // ResolveAddr looks up the IP address for the given host and optionally port. | ||
| // The address is expected in the form "host:port" or just "host". | ||
| // It returns the resolved IP, the port (0 if not specified), and an error if any. | ||
| ResolveAddr(addr string) (net.IP, error) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would say that it follows the settings from the options and maybe list those. Or at least a note it does it in a special way that mgiht nto be just what dig hostname will return on the cli
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, I modified it according to the suggestion.
mstoykov
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! I have left some comments on the godoc but it is not blocking.
Summary
This PR exposes k6's internal DNS resolver to extensions, enabling them to perform DNS lookups that respect the user's k6 configuration including
hostsoverrides, custom DNS servers, and DNS caching.Fixes #5161
Changes
1. Added
ResolveAddrmethod toDialer(lib/netext/dialer.go)ResolveAddr(addr string) (net.IP, int, error)that resolves hostnames to IP addresseshostsoverrides fromoptions.hostsoptions.dns2. Refactored
getDialAddrinternal methodstringto*types.Hostfor better type safety and to avoid redundant string conversions.String()when the string representation is needed3. Added
AddrResolverinterface (lib/vu_state.go)ResolveAddr(addr string) (net.IP, error)4. Added
GetAddrResolvermethod toStatenilif the Dialer doesn't implementAddrResolver(safe type assertion with comma-ok idiom)Testing
Added comprehensive test coverage:
TestDialerResolveAddr: 17 test cases covering:options.hostsTestGetAddrResolver: 2 test cases verifying:AddrResolvernilwhen Dialer doesn't implementAddrResolverAll tests pass with the existing test suite.
Usage Example
Extensions can now access the DNS resolver like this:
Breaking Changes
None. This is a purely additive change that exposes existing functionality.
Documentation