Add health-check-monitor

This commit is contained in:
Archit Bansal
2020-05-27 14:08:42 -07:00
parent 1d03b66f15
commit 44dc4aa6c1
9 changed files with 630 additions and 2 deletions
+163
View File
@@ -0,0 +1,163 @@
/*
Copyright 2020 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package healthchecker
import (
"context"
"errors"
"net/http"
"os/exec"
"strings"
"time"
"github.com/golang/glog"
"k8s.io/node-problem-detector/cmd/healthchecker/options"
"k8s.io/node-problem-detector/pkg/healthchecker/types"
)
type healthChecker struct {
enableRepair bool
healthCheckFunc func() bool
// The repair is "best-effort" and ignores the error from the underlying actions.
// The bash commands to kill the process will fail if the service is down and hence ignore.
repairFunc func()
uptimeFunc func() (time.Duration, error)
crictlPath string
healthCheckTimeout time.Duration
coolDownTime time.Duration
}
// NewHealthChecker returns a new health checker configured with the given options.
func NewHealthChecker(hco *options.HealthCheckerOptions) (types.HealthChecker, error) {
hc := &healthChecker{
enableRepair: hco.EnableRepair,
crictlPath: hco.CriCtlPath,
healthCheckTimeout: hco.HealthCheckTimeout,
coolDownTime: hco.CoolDownTime,
}
hc.healthCheckFunc = getHealthCheckFunc(hco)
hc.repairFunc = getRepairFunc(hco)
hc.uptimeFunc = getUptimeFunc(hco.SystemdService)
return hc, nil
}
// getUptimeFunc returns the time for which the given service has been running.
func getUptimeFunc(service string) func() (time.Duration, error) {
return func() (time.Duration, error) {
out, err := execCommand(types.CmdTimeout, "systemctl", "show", service, "--property=ActiveEnterTimestamp")
if err != nil {
return time.Duration(0), err
}
val := strings.Split(out, "=")
if len(val) < 2 {
return time.Duration(0), errors.New("could not parse the service uptime time correctly")
}
t, err := time.Parse(types.UptimeTimeLayout, val[1])
if err != nil {
return time.Duration(0), err
}
return time.Since(t), nil
}
}
// getRepairFunc returns the repair function based on the component.
func getRepairFunc(hco *options.HealthCheckerOptions) func() {
switch hco.Component {
case types.DockerComponent:
// Use "docker ps" for docker health check. Not using crictl for docker to remove
// dependency on the kubelet.
return func() {
execCommand(types.CmdTimeout, "pkill", "-SIGUSR1", "dockerd")
execCommand(types.CmdTimeout, "systemctl", "kill", "--kill-who=main", hco.SystemdService)
}
default:
// Just kill the service for all other components
return func() {
execCommand(types.CmdTimeout, "systemctl", "kill", "--kill-who=main", hco.SystemdService)
}
}
}
// getHealthCheckFunc returns the health check function based on the component.
func getHealthCheckFunc(hco *options.HealthCheckerOptions) func() bool {
switch hco.Component {
case types.KubeletComponent:
return func() bool {
httpClient := http.Client{Timeout: hco.HealthCheckTimeout}
response, err := httpClient.Get(types.KubeletHealthCheckEndpoint)
if err != nil || response.StatusCode != http.StatusOK {
return false
}
return true
}
case types.DockerComponent:
return func() bool {
if _, err := execCommand(hco.HealthCheckTimeout, "docker", "ps"); err != nil {
return false
}
return true
}
case types.CRIComponent:
return func() bool {
if _, err := execCommand(hco.HealthCheckTimeout, hco.CriCtlPath, "--runtime-endpoint="+hco.CriSocketPath, "--image-endpoint="+hco.CriSocketPath, "pods"); err != nil {
return false
}
return true
}
}
return nil
}
// CheckHealth checks for the health of the component and tries to repair if enabled.
// Returns true if healthy, false otherwise.
func (hc *healthChecker) CheckHealth() bool {
healthy := hc.healthCheckFunc()
if healthy {
return true
}
// The service is unhealthy.
// Attempt repair based on flag.
if hc.enableRepair {
glog.Infof("health-checker: component is unhealthy, proceeding to repair")
// repair if the service has been up for the cool down period.
uptime, err := hc.uptimeFunc()
if err != nil {
glog.Infof("health-checker: %v\n", err.Error())
}
glog.Infof("health-checker: component uptime: %v\n", uptime)
if uptime > hc.coolDownTime {
hc.repairFunc()
}
}
return false
}
// execCommand executes the bash command and returns the (output, error) from command, error if timeout occurs.
func execCommand(timeout time.Duration, command string, args ...string) (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
cmd := exec.CommandContext(ctx, command, args...)
glog.Infof("health-checker: executing command : %v\n", cmd)
out, err := cmd.Output()
if err != nil {
glog.Infof("health-checker: command failed : %v, %v\n", err.Error(), out)
return "", err
}
return strings.TrimSuffix(string(out), "\n"), nil
}
+118
View File
@@ -0,0 +1,118 @@
/*
Copyright 2020 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package healthchecker
import (
"testing"
"time"
"k8s.io/node-problem-detector/pkg/healthchecker/types"
)
var repairCalled bool
func NewTestHealthChecker(repairFunc func(), healthCheckFunc func() bool, uptimeFunc func() (time.Duration, error), enableRepair bool) types.HealthChecker {
repairCalled = false
return &healthChecker{
enableRepair: enableRepair,
healthCheckFunc: healthCheckFunc,
repairFunc: repairFunc,
uptimeFunc: uptimeFunc,
healthCheckTimeout: time.Second,
coolDownTime: 2 * time.Second,
}
}
func healthyFunc() bool {
return true
}
func unhealthyFunc() bool {
return false
}
func repairFunc() {
repairCalled = true
}
func longServiceUptimeFunc() (time.Duration, error) {
return 1 * time.Hour, nil
}
func shortServiceUptimeFunc() (time.Duration, error) {
return 1 * time.Second, nil
}
func TestHealthCheck(t *testing.T) {
for _, tc := range []struct {
description string
enableRepair bool
healthy bool
healthCheckFunc func() bool
uptimeFunc func() (time.Duration, error)
repairFunc func()
repairCalled bool
}{
{
description: "healthy component",
enableRepair: true,
healthy: true,
healthCheckFunc: healthyFunc,
repairFunc: repairFunc,
uptimeFunc: shortServiceUptimeFunc,
repairCalled: false,
},
{
description: "unhealthy component and disabled repair",
enableRepair: false,
healthy: false,
healthCheckFunc: unhealthyFunc,
repairFunc: repairFunc,
uptimeFunc: shortServiceUptimeFunc,
repairCalled: false,
},
{
description: "unhealthy component, enabled repair and component in cool dowm",
enableRepair: true,
healthy: false,
healthCheckFunc: unhealthyFunc,
repairFunc: repairFunc,
uptimeFunc: shortServiceUptimeFunc,
repairCalled: false,
},
{
description: "unhealthy component, enabled repair and component out of cool dowm",
enableRepair: true,
healthy: false,
healthCheckFunc: unhealthyFunc,
repairFunc: repairFunc,
uptimeFunc: longServiceUptimeFunc,
repairCalled: true,
},
} {
t.Run(tc.description, func(t *testing.T) {
hc := NewTestHealthChecker(tc.repairFunc, tc.healthCheckFunc, tc.uptimeFunc, tc.enableRepair)
healthy := hc.CheckHealth()
if healthy != tc.healthy {
t.Errorf("incorrect health returned got %t; expected %t", healthy, tc.healthy)
}
if repairCalled != tc.repairCalled {
t.Errorf("incorrect repairCalled got %t; expected %t", repairCalled, tc.repairCalled)
}
})
}
}
+37
View File
@@ -0,0 +1,37 @@
/*
Copyright 2020 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package types
import "time"
const (
DefaultCoolDownTime = 2 * time.Minute
DefaultHealthCheckTimeout = 10 * time.Second
CmdTimeout = 10 * time.Second
DefaultCriCtl = "/usr/bin/crictl"
DefaultCriSocketPath = "unix:///var/run/containerd/containerd.sock"
KubeletComponent = "kubelet"
CRIComponent = "cri"
DockerComponent = "docker"
ContainerdService = "containerd"
KubeletHealthCheckEndpoint = "http://127.0.0.1:10248/healthz"
UptimeTimeLayout = "Mon 2006-01-02 15:04:05 UTC"
)
type HealthChecker interface {
CheckHealth() bool
}