Skip to content

feat: Use configured DNS name to lookup instance IP address#1054

Merged
hessjcg merged 2 commits intomainfrom
gh-1053-instance-dns-lookup
Dec 17, 2025
Merged

feat: Use configured DNS name to lookup instance IP address#1054
hessjcg merged 2 commits intomainfrom
gh-1053-instance-dns-lookup

Conversation

@hessjcg
Copy link
Collaborator

@hessjcg hessjcg commented Dec 16, 2025

When a custom DNS name is used to connect to a Cloud SQL instance, the dialer should first attempt to resolve the custom DNS name to an IP address and use that for the connection. If the lookup fails, the dialer should fall back to using the IP address from the instance metadata.

This change modifies the dialer to:

  • Use the configured resolver to look up the host's IP address.
  • Use the IP address from the A record of the custom DNS name if available.
  • Fall back to the IP address from the instance metadata if the A record is not available.

Fixes #1053

@hessjcg hessjcg requested a review from a team as a code owner December 16, 2025 20:40
@hessjcg hessjcg force-pushed the gh-1053-instance-dns-lookup branch from 23384f0 to 6a23ca9 Compare December 17, 2025 03:14
When a custom DNS name is used to connect to a Cloud SQL instance, the dialer should first attempt to resolve the custom DNS name to an IP address and use that for the connection. If the lookup fails, the dialer should fall back to using the IP address from the instance metadata.

This change modifies the dialer to:
- Use the configured resolver to look up the host's IP address.
- Use the IP address from the A record of the custom DNS name if available.
- Fall back to the IP address from the instance metadata if the A record is not available.
@hessjcg hessjcg force-pushed the gh-1053-instance-dns-lookup branch from 6a23ca9 to 8561613 Compare December 17, 2025 04:07
@hessjcg
Copy link
Collaborator Author

hessjcg commented Dec 17, 2025

I have fixed the test failures in TestDialerChecksSubjectAlternativeNameAndFallsBackToCN. The test was failing because it performed two dial operations (one using the instance connection name and another using the domain name), which triggered two separate metadata requests due to different cache keys. The mock, however, was configured to expect only one request. I updated the test to expect two requests, and verified that the tests now pass locally. The changes have been pushed to the branch.

@hessjcg hessjcg force-pushed the gh-1053-instance-dns-lookup branch from 8d93506 to 4673f9c Compare December 17, 2025 04:24
@hessjcg
Copy link
Collaborator Author

hessjcg commented Dec 17, 2025

I have resolved the linting issues in dialer_test.go by fixing the indentation. I verified that the tests still pass and pushed the changes. The PR should be ready for review now.

@hessjcg
Copy link
Collaborator Author

hessjcg commented Dec 17, 2025

I have verified that the linting checks pass and the tests are green. The PR is now fully updated with the fixes.

} else {
d.logger.Debugf(ctx,
"[%v] custom DNS name %q resolved to %q, using it to connect",
cn.String(), cn.DomainName(), addrs[0])
Copy link
Collaborator

Choose a reason for hiding this comment

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

I am guessing it is always in the first item of the array?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Usually it only returns 1 result. If it returns more than 1 result, that is legal, but probably a mistake.

@hessjcg hessjcg merged commit 976120e into main Dec 17, 2025
15 checks passed
@hessjcg hessjcg deleted the gh-1053-instance-dns-lookup branch December 17, 2025 22:47
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.

Use configured DNS name to lookup instance IP address

3 participants