Skip to content

Commit 5674770

Browse files
committed
Address PR GoogleCloudPlatform#1198 comments: add host normalization for IPv6
- Implement NormalizeHost to strip brackets from IPv6 addresses. - Use NormalizeHost in controller and kubelet clients. - Add unit tests for normalization and regression tests for clients.
1 parent 5cf847c commit 5674770

6 files changed

Lines changed: 117 additions & 2 deletions

File tree

kubelet-to-gcm/monitor/controller/client.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"net/url"
2626
"strings"
2727

28+
"github.com/GoogleCloudPlatform/k8s-stackdriver/kubelet-to-gcm/monitor/util"
2829
"github.com/prometheus/common/expfmt"
2930
"github.com/prometheus/common/model"
3031
)
@@ -82,7 +83,7 @@ type Client struct {
8283
// NewClient generates a client to hit the given controller.
8384
func NewClient(host string, port uint, client *http.Client) (*Client, error) {
8485
// Parse our URL upfront, so we can fail fast.
85-
urlStr := fmt.Sprintf("http://%s/metrics", net.JoinHostPort(host, fmt.Sprint(port)))
86+
urlStr := fmt.Sprintf("http://%s/metrics", net.JoinHostPort(util.NormalizeHost(host), fmt.Sprint(port)))
8687
metricsURL, err := url.Parse(urlStr)
8788
if err != nil {
8889
return nil, err

kubelet-to-gcm/monitor/controller/client_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ func TestNewClient(t *testing.T) {
4040
port: 10250,
4141
expectedURL: "http://[2001:db8::1]:10250/metrics",
4242
},
43+
{
44+
name: "Bracketed IPv6",
45+
host: "[2001:db8::1]",
46+
port: 10250,
47+
expectedURL: "http://[2001:db8::1]:10250/metrics",
48+
},
4349
{
4450
name: "Hostname",
4551
host: "localhost",

kubelet-to-gcm/monitor/kubelet/client.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"net/http"
2525
"net/url"
2626

27+
"github.com/GoogleCloudPlatform/k8s-stackdriver/kubelet-to-gcm/monitor/util"
2728
stats "k8s.io/kubelet/pkg/apis/stats/v1alpha1"
2829
)
2930

@@ -41,7 +42,7 @@ func NewClient(host string, port uint, client *http.Client, useAuthPort bool) (*
4142
if useAuthPort {
4243
protocol = "https"
4344
}
44-
urlStr := fmt.Sprintf("%s://%s/stats/summary", protocol, net.JoinHostPort(host, fmt.Sprint(port)))
45+
urlStr := fmt.Sprintf("%s://%s/stats/summary", protocol, net.JoinHostPort(util.NormalizeHost(host), fmt.Sprint(port)))
4546
summaryURL, err := url.Parse(urlStr)
4647
if err != nil {
4748
return nil, err

kubelet-to-gcm/monitor/kubelet/client_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,20 @@ func TestNewClient(t *testing.T) {
5757
useAuthPort: true,
5858
expectedURL: "https://[2001:db8::1]:10250/stats/summary",
5959
},
60+
{
61+
name: "Bracketed IPv6 HTTP",
62+
host: "[2001:db8::1]",
63+
port: 10255,
64+
useAuthPort: false,
65+
expectedURL: "http://[2001:db8::1]:10255/stats/summary",
66+
},
67+
{
68+
name: "Bracketed IPv6 HTTPS",
69+
host: "[2001:db8::1]",
70+
port: 10250,
71+
useAuthPort: true,
72+
expectedURL: "https://[2001:db8::1]:10250/stats/summary",
73+
},
6074
}
6175

6276
for _, tc := range testCases {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
Copyright 2026 Google Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package util
18+
19+
import (
20+
"strings"
21+
)
22+
23+
// NormalizeHost handles host strings that may already be bracketed (like IPv6).
24+
// net.JoinHostPort expects a raw IP or hostname; if it receives a bracketed IPv6,
25+
// it will double-bracket it.
26+
func NormalizeHost(host string) string {
27+
host = strings.TrimSpace(host)
28+
if strings.HasPrefix(host, "[") && strings.HasSuffix(host, "]") {
29+
return host[1 : len(host)-1]
30+
}
31+
return host
32+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
Copyright 2026 Google Inc.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package util
18+
19+
import (
20+
"testing"
21+
)
22+
23+
func TestNormalizeHost(t *testing.T) {
24+
testCases := []struct {
25+
desc string
26+
input string
27+
expected string
28+
}{
29+
{
30+
desc: "IPv4",
31+
input: "127.0.0.1",
32+
expected: "127.0.0.1",
33+
},
34+
{
35+
desc: "hostname",
36+
input: "localhost",
37+
expected: "localhost",
38+
},
39+
{
40+
desc: "raw IPv6",
41+
input: "2001:db8::1",
42+
expected: "2001:db8::1",
43+
},
44+
{
45+
desc: "bracketed IPv6",
46+
input: "[2001:db8::1]",
47+
expected: "2001:db8::1",
48+
},
49+
{
50+
desc: "bracketed IPv6 with spaces",
51+
input: " [2001:db8::1] ",
52+
expected: "2001:db8::1",
53+
},
54+
}
55+
56+
for _, tc := range testCases {
57+
if got := NormalizeHost(tc.input); got != tc.expected {
58+
t.Errorf("%s: NormalizeHost(%q) = %q, want %q", tc.desc, tc.input, got, tc.expected)
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)