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
Replace use of Sprintf with net.JoinHostPort #109344
Conversation
Hi @stbenjam. Thanks for your PR. I'm waiting for a kubernetes member to verify that this patch is reasonable to test. If it is, they should reply with Once the patch is verified, the new status will be reflected by the I understand the commands that are listed here. Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
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.
/ok-to-test
/priority backlog
/triage accepted
/test pull-kubernetes-node-e2e-containerd |
/test pull-kubernetes-node-e2e-containerd |
/retest we should have a linter for these things 😄 I can't remember the number of bugs we had because of this 🤷 |
/approve |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: aojea, dims, stbenjam The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
working on it :) golangci/golangci-lint#2749 |
@@ -82,7 +85,7 @@ func getNodeSummary() (*stats.Summary, error) { | |||
if err != nil { | |||
return nil, fmt.Errorf("failed to get current kubelet config") | |||
} | |||
req, err := http.NewRequest("GET", fmt.Sprintf("http://%s:%d/stats/summary", kubeletConfig.Address, kubeletConfig.ReadOnlyPort), nil) | |||
req, err := http.NewRequest("GET", fmt.Sprintf("http://%s/stats/summary", net.JoinHostPort(kubeletConfig.Address, strconv.Itoa(int(kubeletConfig.ReadOnlyPort)))), nil) |
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.
why so many conversions from int32 to string? It looks like JoinHostPort can take int32 as an argument and kubeletConfig.ReadOnlyPort
is int32
already
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.
You're looking at a version of JoinHostPort that is not part of the standard library.
This way of doing things is already common in this codebase (and many others):
$ grep -r JoinHostPort . | grep strconv | wc -l
89
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.
got it. Indeed my google search failed me.
On IPv6 clusters, one of the most frequent problems I encounter is assumptions that one can build a URL with a host and port simply by using Sprintf, like this: ```go fmt.Sprintf("http://%s:%d/foo", host, port) ``` When `host` is an IPv6 address, this produces an invalid URL as it must be bracketed, like this: ``` http://[2001:4860:4860::8888]:9443 ``` This change fixes the occurences of joining a host and port with the purpose built `net.JoinHostPort` function. I encounter this problem often enough that I started to [write a linter for it](https://github.com/stbenjam/go-sprintf-host-port). I don't think the linter is quite ready for wide use yet, but I did run it against the Kube codebase and found these. While the host portion in some of these changes may always be an FQDN or IPv4 IP today, it's an easy thing that can break later on.
/lgtm |
The Kubernetes project has merge-blocking tests that are currently too flaky to consistently pass. This bot retests PRs for certain kubernetes repos according to the following rules:
You can:
/retest |
1 similar comment
The Kubernetes project has merge-blocking tests that are currently too flaky to consistently pass. This bot retests PRs for certain kubernetes repos according to the following rules:
You can:
/retest |
What type of PR is this?
/kind cleanup
What this PR does / why we need it:
On IPv6 clusters, one of the most frequent problems I encounter is
assumptions that one can build a URL with a host and port simply by
using Sprintf, like this:
When
host
is an IPv6 address, this produces an invalid URL as it mustbe bracketed, like this:
This change fixes the occurrences of joining a host and port with the
purpose built
net.JoinHostPort
function.I encounter this problem often enough that I started to write a linter
for it. I don't
think the linter is quite ready for wide use yet, but I did run it
against the Kube codebase and found these. While the host portion in
some of these changes may always be an FQDN or IPv4 IP today, it's an
easy thing that can break later on.
Which issue(s) this PR fixes:
N/A
Special notes for your reviewer:
Does this PR introduce a user-facing change?
Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.: