Skip to content

Commit 6e30b17

Browse files
authored
Merge pull request #733 from raghu-nandan-bs/variablize-kube-endpoints
optionally read node and port information from env variables for kube* services
2 parents e992542 + 706bf35 commit 6e30b17

File tree

3 files changed

+132
-5
lines changed

3 files changed

+132
-5
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: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package types
1818

1919
import (
2020
"fmt"
21+
"os"
2122
"sort"
2223
"strconv"
2324
"strings"
@@ -38,12 +39,57 @@ const (
3839
ContainerdService = "containerd"
3940
KubeProxyComponent = "kube-proxy"
4041

41-
KubeletHealthCheckEndpoint = "http://127.0.0.1:10248/healthz"
42-
KubeProxyHealthCheckEndpoint = "http://127.0.0.1:10256/healthz"
43-
4442
LogPatternFlagSeparator = ":"
43+
hostAddressKey = "HOST_ADDRESS"
44+
kubeletPortKey = "KUBELET_PORT"
45+
kubeProxyPortKey = "KUBEPROXY_PORT"
46+
47+
defaultHostAddress = "127.0.0.1"
48+
defaultKubeletPort = "10248"
49+
defaultKubeproxyPort = "10256"
4550
)
4651

52+
var (
53+
kubeletHealthCheckEndpoint string
54+
kubeProxyHealthCheckEndpoint string
55+
)
56+
57+
func init() {
58+
setKubeEndpoints()
59+
}
60+
61+
func setKubeEndpoints() {
62+
var o string
63+
64+
hostAddress := defaultHostAddress
65+
kubeletPort := defaultKubeletPort
66+
kubeProxyPort := defaultKubeproxyPort
67+
68+
o = os.Getenv(hostAddressKey)
69+
if o != "" {
70+
hostAddress = o
71+
}
72+
o = os.Getenv(kubeletPortKey)
73+
if o != "" {
74+
kubeletPort = o
75+
}
76+
o = os.Getenv(kubeProxyPortKey)
77+
if o != "" {
78+
kubeProxyPort = o
79+
}
80+
81+
kubeletHealthCheckEndpoint = fmt.Sprintf("http://%s:%s/healthz", hostAddress, kubeletPort)
82+
kubeProxyHealthCheckEndpoint = fmt.Sprintf("http://%s:%s/healthz", hostAddress, kubeProxyPort)
83+
84+
}
85+
86+
func KubeProxyHealthCheckEndpoint() string {
87+
return kubeProxyHealthCheckEndpoint
88+
}
89+
func KubeletHealthCheckEndpoint() string {
90+
return kubeletHealthCheckEndpoint
91+
}
92+
4793
type HealthChecker interface {
4894
CheckHealth() (bool, error)
4995
}

pkg/healthchecker/types/types_test.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,84 @@ 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: "http://127.0.0.1:10248/healthz",
113+
expectedKubeProxyEndpoint: "http://127.0.0.1:10256/healthz",
114+
}, {
115+
name: "HOST_ADDRESS override supplied",
116+
envConfig: map[string]string{
117+
"HOST_ADDRESS": "samplehost.testdomain.com",
118+
},
119+
expectedKubeletEndpoint: "http://samplehost.testdomain.com:10248/healthz",
120+
expectedKubeProxyEndpoint: "http://samplehost.testdomain.com:10256/healthz",
121+
},
122+
{
123+
name: "KUBELET_PORT override supplied",
124+
envConfig: map[string]string{
125+
"KUBELET_PORT": "12345",
126+
},
127+
expectedKubeletEndpoint: "http://127.0.0.1:12345/healthz",
128+
expectedKubeProxyEndpoint: "http://127.0.0.1:10256/healthz",
129+
},
130+
{
131+
name: "KUBEPROXY_PORT override supplied",
132+
envConfig: map[string]string{
133+
"KUBEPROXY_PORT": "12345",
134+
},
135+
expectedKubeletEndpoint: "http://127.0.0.1:10248/healthz",
136+
expectedKubeProxyEndpoint: "http://127.0.0.1:12345/healthz",
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: "http://samplehost.testdomain.com:12345/healthz",
145+
expectedKubeProxyEndpoint: "http://samplehost.testdomain.com:10256/healthz",
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: "http://samplehost.testdomain.com:10248/healthz",
154+
expectedKubeProxyEndpoint: "http://samplehost.testdomain.com:12345/healthz",
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_PORT": "12345",
161+
"KUBEPROXY_PORT": "12346",
162+
},
163+
expectedKubeletEndpoint: "http://10.0.10.1:12345/healthz",
164+
expectedKubeProxyEndpoint: "http://10.0.10.1:12346/healthz",
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+
setKubeEndpoints()
173+
174+
kubeProxyHCEndpoint := KubeProxyHealthCheckEndpoint()
175+
kubeletHCEndpoint := KubeletHealthCheckEndpoint()
176+
177+
assert.Equal(t, kubeProxyHCEndpoint, test.expectedKubeProxyEndpoint)
178+
assert.Equal(t, kubeletHCEndpoint, test.expectedKubeletEndpoint)
179+
})
180+
}
181+
}

0 commit comments

Comments
 (0)