Skip to content

Commit a117c0c

Browse files
author
corneredrat
committed
1. make vars private
2. expose endpoints via functions 3. add test cases 4. rename host addr var
1 parent 83e5207 commit a117c0c

File tree

3 files changed

+99
-14
lines changed

3 files changed

+99
-14
lines changed

pkg/healthchecker/health_checker.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,9 @@ func healthCheckEndpointOKFunc(endpoint string, timeout time.Duration) func() (b
138138
func getHealthCheckFunc(hco *options.HealthCheckerOptions) func() (bool, error) {
139139
switch hco.Component {
140140
case types.KubeletComponent:
141-
return healthCheckEndpointOKFunc(types.KubeletHealthCheckEndpoint, hco.HealthCheckTimeout)
141+
return healthCheckEndpointOKFunc(types.KubeletHealthCheckEndpoint(), hco.HealthCheckTimeout)
142142
case types.KubeProxyComponent:
143-
return healthCheckEndpointOKFunc(types.KubeProxyHealthCheckEndpoint, hco.HealthCheckTimeout)
143+
return healthCheckEndpointOKFunc(types.KubeProxyHealthCheckEndpoint(), hco.HealthCheckTimeout)
144144
case types.DockerComponent:
145145
return func() (bool, error) {
146146
if _, err := execCommand(hco.HealthCheckTimeout, getDockerPath(), "ps"); err != nil {

pkg/healthchecker/types/types.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,18 @@ const (
4040
KubeProxyComponent = "kube-proxy"
4141

4242
LogPatternFlagSeparator = ":"
43+
hostAddressKey = "HOST_ADDRESS"
44+
kubeletPortKey = "KUBELET_PORT"
45+
kubeProxyPortKey = "KUBEPROXY_PORT"
4346

44-
nodeEnvKey = "HOST_IP"
45-
kubeletPortKey = "KUBELET_PORT"
46-
kubeProxyPortKey = "KUBEPROXY_PORT"
47-
48-
defaultHostIP = "127.0.0.1"
47+
defaultHost = "127.0.0.1"
4948
defaultKubeletPort = "10248"
5049
defaultKubeproxyPort = "10256"
5150
)
5251

5352
var (
54-
KubeletHealthCheckEndpoint string
55-
KubeProxyHealthCheckEndpoint string
53+
kubeletHealthCheckEndpoint string
54+
kubeProxyHealthCheckEndpoint string
5655
)
5756

5857
func init() {
@@ -62,13 +61,13 @@ func init() {
6261
func setKubeEndpoints() {
6362
var o string
6463

65-
hostIP := defaultHostIP
64+
hostAddress := defaultHost
6665
kubeletPort := defaultKubeletPort
6766
kubeProxyPort := defaultKubeproxyPort
6867

69-
o = os.Getenv(nodeEnvKey)
68+
o = os.Getenv(hostAddressKey)
7069
if o != "" {
71-
hostIP = o
70+
hostAddress = o
7271
}
7372
o = os.Getenv(kubeletPortKey)
7473
if o != "" {
@@ -79,9 +78,16 @@ func setKubeEndpoints() {
7978
kubeProxyPort = o
8079
}
8180

82-
KubeletHealthCheckEndpoint = fmt.Sprintf("http://%s:%s/healthz", hostIP, kubeletPort)
83-
KubeProxyHealthCheckEndpoint = fmt.Sprintf("http://%s:%s/healthz", hostIP, kubeProxyPort)
81+
kubeletHealthCheckEndpoint = fmt.Sprintf("http://%s:%s/healthz", hostAddress, kubeletPort)
82+
kubeProxyHealthCheckEndpoint = fmt.Sprintf("http://%s:%s/healthz", hostAddress, kubeProxyPort)
83+
84+
}
8485

86+
func KubeProxyHealthCheckEndpoint() string {
87+
return kubeProxyHealthCheckEndpoint
88+
}
89+
func KubeletHealthCheckEndpoint() string {
90+
return kubeletHealthCheckEndpoint
8591
}
8692

8793
type HealthChecker interface {

pkg/healthchecker/types/types_test.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,82 @@ func TestLogPatternFlag(t *testing.T) {
9898
})
9999
}
100100
}
101+
102+
func TestKubeEndpointConfiguration(t *testing.T) {
103+
testCases := []struct {
104+
name string
105+
envConfig map[string]string
106+
expectedKubeletEndpoint string
107+
expectedKubeProxyEndpoint string
108+
}{
109+
{
110+
name: "no overrides supplied",
111+
envConfig: map[string]string{},
112+
expectedKubeletEndpoint: "127.0.0.1:10248",
113+
expectedKubeProxyEndpoint: "127.0.0.1:10256",
114+
}, {
115+
name: "HOST_ADDRESS override supplied",
116+
envConfig: map[string]string{
117+
"HOST_ADDRESS": "samplehost.testdomain.com",
118+
},
119+
expectedKubeletEndpoint: "samplehost.testdomain.com:10248",
120+
expectedKubeProxyEndpoint: "samplehost.testdomain.com:10256",
121+
},
122+
{
123+
name: "KUBELET_PORT override supplied",
124+
envConfig: map[string]string{
125+
"KUBELET_PORT": "12345",
126+
},
127+
expectedKubeletEndpoint: "127.0.0.1:12345",
128+
expectedKubeProxyEndpoint: "127.0.0.1:10256",
129+
},
130+
{
131+
name: "KUBEPROXY_PORT override supplied",
132+
envConfig: map[string]string{
133+
"KUBEPROXY_PORT": "12345",
134+
},
135+
expectedKubeletEndpoint: "127.0.0.1:10248",
136+
expectedKubeProxyEndpoint: "127.0.0.1:12345",
137+
},
138+
{
139+
name: "HOST_ADDRESS and KUBELET_PORT override supplied",
140+
envConfig: map[string]string{
141+
"HOST_ADDRESS": "samplehost.testdomain.com",
142+
"KUBELET_PORT": "12345",
143+
},
144+
expectedKubeletEndpoint: "samplehost.testdomain.com:12345",
145+
expectedKubeProxyEndpoint: "samplehost.testdomain.com:10256",
146+
},
147+
{
148+
name: "HOST_ADDRESS and KUBEPROXY_PORT override supplied",
149+
envConfig: map[string]string{
150+
"HOST_ADDRESS": "samplehost.testdomain.com",
151+
"KUBEPROXY_PORT": "12345",
152+
},
153+
expectedKubeletEndpoint: "samplehost.testdomain.com:10248",
154+
expectedKubeProxyEndpoint: "samplehost.testdomain.com:12345",
155+
},
156+
{
157+
name: "HOST_ADDRESS, KUBELET_PORT and KUBEPROXY_PORT override supplied",
158+
envConfig: map[string]string{
159+
"HOST_ADDRESS": "10.0.10.1",
160+
"KUBELET_PROXY": "12345",
161+
"KUBEPROXY_PORT": "12346",
162+
},
163+
expectedKubeletEndpoint: "10.0.10.1:12345",
164+
expectedKubeProxyEndpoint: "10.0.10.1:12346",
165+
},
166+
}
167+
for _, test := range testCases {
168+
t.Run(test.name, func(t *testing.T) {
169+
for key, val := range test.envConfig {
170+
t.Setenv(key, val)
171+
}
172+
kubeProxyHCEndpoint := KubeProxyHealthCheckEndpoint()
173+
kubeletHCEndpoint := KubeletHealthCheckEndpoint()
174+
175+
assert.Equal(t, kubeProxyHCEndpoint, test.expectedKubeProxyEndpoint)
176+
assert.Equal(t, kubeletHCEndpoint, test.expectedKubeletEndpoint)
177+
})
178+
}
179+
}

0 commit comments

Comments
 (0)