Skip to content

Conversation

@szkiba
Copy link
Contributor

@szkiba szkiba commented Nov 17, 2025

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 hosts overrides, custom DNS servers, and DNS caching.

Fixes #5161

Changes

1. Added ResolveAddr method to Dialer (lib/netext/dialer.go)

  • New public method ResolveAddr(addr string) (net.IP, int, error) that resolves hostnames to IP addresses
  • Accepts addresses in both "host:port" and "host" formats (port is optional)
  • Returns the resolved IP address, port (0 if not specified), and any error
  • Respects all k6 DNS configurations:
    • hosts overrides from options.hosts
    • Custom DNS servers from options.dns
    • IP blacklists
    • DNS caching with TTL support

2. Refactored getDialAddr internal method

  • Changed return type from string to *types.Host for better type safety and to avoid redundant string conversions
  • Updated all call sites to use .String() when the string representation is needed

3. Added AddrResolver interface (lib/vu_state.go)

  • Defines the public API contract for DNS resolution: ResolveAddr(addr string) (net.IP, error)
  • Allows extensions to type-assert the Dialer to access DNS resolution functionality

4. Added GetAddrResolver method to State

  • Provides safe, convenient access to the DNS resolver from the VU state
  • Returns nil if the Dialer doesn't implement AddrResolver (safe type assertion with comma-ok idiom)
  • Prevents potential panics when checking for resolver support

Testing

Added comprehensive test coverage:

  • TestDialerResolveAddr: 17 test cases covering:

    • IPv4 and IPv6 resolution with and without ports
    • Direct IP addresses (bypass DNS)
    • Host mappings from options.hosts
    • Custom port mappings in hosts
    • IP blacklist enforcement
    • Non-existent hosts (error handling)
  • TestGetAddrResolver: 2 test cases verifying:

    • Returns the same Dialer instance when it implements AddrResolver
    • Returns nil when Dialer doesn't implement AddrResolver

All tests pass with the existing test suite.

Usage Example

Extensions can now access the DNS resolver like this:

// In your extension code where you have access to lib.State
if resolver := state.GetAddrResolver(); resolver != nil {
    ip, err := resolver.ResolveAddr("example.com:443")
    if err != nil {
        // Handle error
    }
    // Use resolved IP...
}

Breaking Changes

None. This is a purely additive change that exposes existing functionality.

Documentation

  • Inline documentation added for new public APIs
  • Method signatures clearly document expected input/output formats

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.
@szkiba szkiba requested a review from a team as a code owner November 17, 2025 13:56
@szkiba szkiba requested review from inancgumus and oleiade and removed request for a team November 17, 2025 13:56
@szkiba szkiba linked an issue Nov 17, 2025 that may be closed by this pull request
@szkiba szkiba requested review from AgnesToulet and mstoykov and removed request for inancgumus and oleiade November 17, 2025 13:57
lib/vu_state.go Outdated
Comment on lines 103 to 104
// GetAddrResolver returns the AddrResolver implementation used by the Dialer.
// It returns nil if the Dialer doesn't implement AddrResolver.
Copy link
Contributor

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.

Copy link
Contributor Author

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
Comment on lines 31 to 34
// 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)
Copy link
Contributor

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

Copy link
Contributor Author

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
mstoykov previously approved these changes Nov 18, 2025
Copy link
Contributor

@mstoykov mstoykov left a 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.

@szkiba szkiba temporarily deployed to azure-trusted-signing November 18, 2025 10:54 — with GitHub Actions Inactive
@szkiba szkiba temporarily deployed to azure-trusted-signing November 18, 2025 10:56 — with GitHub Actions Inactive
@szkiba szkiba temporarily deployed to azure-trusted-signing November 20, 2025 18:16 — with GitHub Actions Inactive
@szkiba szkiba temporarily deployed to azure-trusted-signing November 20, 2025 18:18 — with GitHub Actions Inactive
@AgnesToulet AgnesToulet merged commit 2a8f607 into master Nov 21, 2025
58 of 62 checks passed
@AgnesToulet AgnesToulet deleted the 5161-expose-k6-dns-resolver-api-for-extensions branch November 21, 2025 08:44
@inancgumus inancgumus added this to the v1.5.0 milestone Dec 3, 2025
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.

Expose k6 DNS Resolver for Extensions

4 participants