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 97039f03..e7e7266c 100644 --- a/pkg/healthchecker/types/types.go +++ b/pkg/healthchecker/types/types.go @@ -18,6 +18,7 @@ package types import ( "fmt" + "os" "sort" "strconv" "strings" @@ -38,12 +39,57 @@ const ( ContainerdService = "containerd" KubeProxyComponent = "kube-proxy" - KubeletHealthCheckEndpoint = "http://127.0.0.1:10248/healthz" - KubeProxyHealthCheckEndpoint = "http://127.0.0.1:10256/healthz" - LogPatternFlagSeparator = ":" + hostAddressKey = "HOST_ADDRESS" + kubeletPortKey = "KUBELET_PORT" + kubeProxyPortKey = "KUBEPROXY_PORT" + + defaultHostAddress = "127.0.0.1" + defaultKubeletPort = "10248" + defaultKubeproxyPort = "10256" ) +var ( + kubeletHealthCheckEndpoint string + kubeProxyHealthCheckEndpoint string +) + +func init() { + setKubeEndpoints() +} + +func setKubeEndpoints() { + var o string + + hostAddress := defaultHostAddress + kubeletPort := defaultKubeletPort + kubeProxyPort := defaultKubeproxyPort + + o = os.Getenv(hostAddressKey) + if o != "" { + hostAddress = o + } + o = os.Getenv(kubeletPortKey) + if o != "" { + kubeletPort = o + } + o = os.Getenv(kubeProxyPortKey) + if o != "" { + kubeProxyPort = o + } + + 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..007f79c5 100644 --- a/pkg/healthchecker/types/types_test.go +++ b/pkg/healthchecker/types/types_test.go @@ -98,3 +98,84 @@ 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: "http://127.0.0.1:10248/healthz", + expectedKubeProxyEndpoint: "http://127.0.0.1:10256/healthz", + }, { + name: "HOST_ADDRESS override supplied", + envConfig: map[string]string{ + "HOST_ADDRESS": "samplehost.testdomain.com", + }, + expectedKubeletEndpoint: "http://samplehost.testdomain.com:10248/healthz", + expectedKubeProxyEndpoint: "http://samplehost.testdomain.com:10256/healthz", + }, + { + name: "KUBELET_PORT override supplied", + envConfig: map[string]string{ + "KUBELET_PORT": "12345", + }, + expectedKubeletEndpoint: "http://127.0.0.1:12345/healthz", + expectedKubeProxyEndpoint: "http://127.0.0.1:10256/healthz", + }, + { + name: "KUBEPROXY_PORT override supplied", + envConfig: map[string]string{ + "KUBEPROXY_PORT": "12345", + }, + expectedKubeletEndpoint: "http://127.0.0.1:10248/healthz", + expectedKubeProxyEndpoint: "http://127.0.0.1:12345/healthz", + }, + { + name: "HOST_ADDRESS and KUBELET_PORT override supplied", + envConfig: map[string]string{ + "HOST_ADDRESS": "samplehost.testdomain.com", + "KUBELET_PORT": "12345", + }, + expectedKubeletEndpoint: "http://samplehost.testdomain.com:12345/healthz", + expectedKubeProxyEndpoint: "http://samplehost.testdomain.com:10256/healthz", + }, + { + name: "HOST_ADDRESS and KUBEPROXY_PORT override supplied", + envConfig: map[string]string{ + "HOST_ADDRESS": "samplehost.testdomain.com", + "KUBEPROXY_PORT": "12345", + }, + expectedKubeletEndpoint: "http://samplehost.testdomain.com:10248/healthz", + expectedKubeProxyEndpoint: "http://samplehost.testdomain.com:12345/healthz", + }, + { + name: "HOST_ADDRESS, KUBELET_PORT and KUBEPROXY_PORT override supplied", + envConfig: map[string]string{ + "HOST_ADDRESS": "10.0.10.1", + "KUBELET_PORT": "12345", + "KUBEPROXY_PORT": "12346", + }, + expectedKubeletEndpoint: "http://10.0.10.1:12345/healthz", + expectedKubeProxyEndpoint: "http://10.0.10.1:12346/healthz", + }, + } + for _, test := range testCases { + t.Run(test.name, func(t *testing.T) { + for key, val := range test.envConfig { + t.Setenv(key, val) + } + setKubeEndpoints() + + kubeProxyHCEndpoint := KubeProxyHealthCheckEndpoint() + kubeletHCEndpoint := KubeletHealthCheckEndpoint() + + assert.Equal(t, kubeProxyHCEndpoint, test.expectedKubeProxyEndpoint) + assert.Equal(t, kubeletHCEndpoint, test.expectedKubeletEndpoint) + }) + } +}