diff --git a/pkg/healthchecker/health_checker.go b/pkg/healthchecker/health_checker.go index c1ff2bda..06d94108 100644 --- a/pkg/healthchecker/health_checker.go +++ b/pkg/healthchecker/health_checker.go @@ -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 { diff --git a/pkg/healthchecker/types/types.go b/pkg/healthchecker/types/types.go index 38f4536a..f17af30b 100644 --- a/pkg/healthchecker/types/types.go +++ b/pkg/healthchecker/types/types.go @@ -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) } diff --git a/pkg/healthchecker/types/types_test.go b/pkg/healthchecker/types/types_test.go index b5da17f5..36e35872 100644 --- a/pkg/healthchecker/types/types_test.go +++ b/pkg/healthchecker/types/types_test.go @@ -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) + }) + } +}