1. make vars private

2. expose endpoints via functions
3. add test cases
4. rename host addr var
This commit is contained in:
corneredrat
2023-02-09 15:02:52 +05:30
parent 83e520784b
commit a117c0c056
3 changed files with 99 additions and 14 deletions

View File

@@ -138,9 +138,9 @@ func healthCheckEndpointOKFunc(endpoint string, timeout time.Duration) func() (b
func getHealthCheckFunc(hco *options.HealthCheckerOptions) func() (bool, error) {
switch hco.Component {
case types.KubeletComponent:
return healthCheckEndpointOKFunc(types.KubeletHealthCheckEndpoint, hco.HealthCheckTimeout)
return healthCheckEndpointOKFunc(types.KubeletHealthCheckEndpoint(), hco.HealthCheckTimeout)
case types.KubeProxyComponent:
return healthCheckEndpointOKFunc(types.KubeProxyHealthCheckEndpoint, hco.HealthCheckTimeout)
return healthCheckEndpointOKFunc(types.KubeProxyHealthCheckEndpoint(), hco.HealthCheckTimeout)
case types.DockerComponent:
return func() (bool, error) {
if _, err := execCommand(hco.HealthCheckTimeout, getDockerPath(), "ps"); err != nil {

View File

@@ -40,19 +40,18 @@ const (
KubeProxyComponent = "kube-proxy"
LogPatternFlagSeparator = ":"
hostAddressKey = "HOST_ADDRESS"
kubeletPortKey = "KUBELET_PORT"
kubeProxyPortKey = "KUBEPROXY_PORT"
nodeEnvKey = "HOST_IP"
kubeletPortKey = "KUBELET_PORT"
kubeProxyPortKey = "KUBEPROXY_PORT"
defaultHostIP = "127.0.0.1"
defaultHost = "127.0.0.1"
defaultKubeletPort = "10248"
defaultKubeproxyPort = "10256"
)
var (
KubeletHealthCheckEndpoint string
KubeProxyHealthCheckEndpoint string
kubeletHealthCheckEndpoint string
kubeProxyHealthCheckEndpoint string
)
func init() {
@@ -62,13 +61,13 @@ func init() {
func setKubeEndpoints() {
var o string
hostIP := defaultHostIP
hostAddress := defaultHost
kubeletPort := defaultKubeletPort
kubeProxyPort := defaultKubeproxyPort
o = os.Getenv(nodeEnvKey)
o = os.Getenv(hostAddressKey)
if o != "" {
hostIP = o
hostAddress = o
}
o = os.Getenv(kubeletPortKey)
if o != "" {
@@ -79,11 +78,18 @@ func setKubeEndpoints() {
kubeProxyPort = o
}
KubeletHealthCheckEndpoint = fmt.Sprintf("http://%s:%s/healthz", hostIP, kubeletPort)
KubeProxyHealthCheckEndpoint = fmt.Sprintf("http://%s:%s/healthz", hostIP, kubeProxyPort)
kubeletHealthCheckEndpoint = fmt.Sprintf("http://%s:%s/healthz", hostAddress, kubeletPort)
kubeProxyHealthCheckEndpoint = fmt.Sprintf("http://%s:%s/healthz", hostAddress, kubeProxyPort)
}
func KubeProxyHealthCheckEndpoint() string {
return kubeProxyHealthCheckEndpoint
}
func KubeletHealthCheckEndpoint() string {
return kubeletHealthCheckEndpoint
}
type HealthChecker interface {
CheckHealth() (bool, error)
}

View File

@@ -98,3 +98,82 @@ func TestLogPatternFlag(t *testing.T) {
})
}
}
func TestKubeEndpointConfiguration(t *testing.T) {
testCases := []struct {
name string
envConfig map[string]string
expectedKubeletEndpoint string
expectedKubeProxyEndpoint string
}{
{
name: "no overrides supplied",
envConfig: map[string]string{},
expectedKubeletEndpoint: "127.0.0.1:10248",
expectedKubeProxyEndpoint: "127.0.0.1:10256",
}, {
name: "HOST_ADDRESS override supplied",
envConfig: map[string]string{
"HOST_ADDRESS": "samplehost.testdomain.com",
},
expectedKubeletEndpoint: "samplehost.testdomain.com:10248",
expectedKubeProxyEndpoint: "samplehost.testdomain.com:10256",
},
{
name: "KUBELET_PORT override supplied",
envConfig: map[string]string{
"KUBELET_PORT": "12345",
},
expectedKubeletEndpoint: "127.0.0.1:12345",
expectedKubeProxyEndpoint: "127.0.0.1:10256",
},
{
name: "KUBEPROXY_PORT override supplied",
envConfig: map[string]string{
"KUBEPROXY_PORT": "12345",
},
expectedKubeletEndpoint: "127.0.0.1:10248",
expectedKubeProxyEndpoint: "127.0.0.1:12345",
},
{
name: "HOST_ADDRESS and KUBELET_PORT override supplied",
envConfig: map[string]string{
"HOST_ADDRESS": "samplehost.testdomain.com",
"KUBELET_PORT": "12345",
},
expectedKubeletEndpoint: "samplehost.testdomain.com:12345",
expectedKubeProxyEndpoint: "samplehost.testdomain.com:10256",
},
{
name: "HOST_ADDRESS and KUBEPROXY_PORT override supplied",
envConfig: map[string]string{
"HOST_ADDRESS": "samplehost.testdomain.com",
"KUBEPROXY_PORT": "12345",
},
expectedKubeletEndpoint: "samplehost.testdomain.com:10248",
expectedKubeProxyEndpoint: "samplehost.testdomain.com:12345",
},
{
name: "HOST_ADDRESS, KUBELET_PORT and KUBEPROXY_PORT override supplied",
envConfig: map[string]string{
"HOST_ADDRESS": "10.0.10.1",
"KUBELET_PROXY": "12345",
"KUBEPROXY_PORT": "12346",
},
expectedKubeletEndpoint: "10.0.10.1:12345",
expectedKubeProxyEndpoint: "10.0.10.1:12346",
},
}
for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
for key, val := range test.envConfig {
t.Setenv(key, val)
}
kubeProxyHCEndpoint := KubeProxyHealthCheckEndpoint()
kubeletHCEndpoint := KubeletHealthCheckEndpoint()
assert.Equal(t, kubeProxyHCEndpoint, test.expectedKubeProxyEndpoint)
assert.Equal(t, kubeletHCEndpoint, test.expectedKubeletEndpoint)
})
}
}