-
Notifications
You must be signed in to change notification settings - Fork 635
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1d03b66
commit 6d6b5a5
Showing
9 changed files
with
636 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
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 main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/spf13/pflag" | ||
|
||
"k8s.io/node-problem-detector/cmd/healthchecker/options" | ||
"k8s.io/node-problem-detector/pkg/custompluginmonitor/types" | ||
"k8s.io/node-problem-detector/pkg/healthchecker" | ||
) | ||
|
||
func main() { | ||
// Set glog flag so that it does not log to files. | ||
if err := flag.Set("logtostderr", "true"); err != nil { | ||
fmt.Printf("Failed to set logtostderr=true: %v", err) | ||
os.Exit(int(types.Unknown)) | ||
} | ||
|
||
hco := options.NewHealthCheckerOptions() | ||
hco.AddFlags(pflag.CommandLine) | ||
pflag.Parse() | ||
hco.SetDefaults() | ||
if err := hco.IsValid(); err != nil { | ||
fmt.Println(err) | ||
os.Exit(int(types.Unknown)) | ||
} | ||
|
||
hc, err := healthchecker.NewHealthChecker(hco) | ||
if err != nil { | ||
fmt.Println(err) | ||
os.Exit(int(types.Unknown)) | ||
} | ||
if !hc.CheckHealth() { | ||
fmt.Printf("%v:%v was found unhealthy; repair flag : %v\n", hco.Component, hco.SystemService, hco.EnableRepair) | ||
os.Exit(int(types.NonOK)) | ||
} | ||
os.Exit(int(types.OK)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
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 options | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/spf13/pflag" | ||
|
||
"k8s.io/node-problem-detector/pkg/healthchecker/types" | ||
) | ||
|
||
// NewHealthCheckerOptions returns an empty health check options struct. | ||
func NewHealthCheckerOptions() *HealthCheckerOptions { | ||
return &HealthCheckerOptions{} | ||
} | ||
|
||
// HealthCheckerOptions are the options used to configure the health checker. | ||
type HealthCheckerOptions struct { | ||
Component string | ||
SystemService string | ||
EnableRepair bool | ||
CriCtlPath string | ||
CriSocketPath string | ||
CoolDownTime time.Duration | ||
HealthCheckTimeout time.Duration | ||
CmdTimeout time.Duration | ||
} | ||
|
||
// AddFlags adds health checker command line options to pflag. | ||
func (hco *HealthCheckerOptions) AddFlags(fs *pflag.FlagSet) { | ||
fs.StringVar(&hco.Component, "component", types.KubeletComponent, | ||
"The component to check health for. Supports kubelet, docker and cri") | ||
fs.StringVar(&hco.SystemService, "system-service", "", | ||
"The underlying system service responsible for the component. Set to the corresponding component for docker and kubelet, containerd for cri.") | ||
fs.BoolVar(&hco.EnableRepair, "enable-repair", true, "Flag to enable/disable repair attempt for the component.") | ||
fs.StringVar(&hco.CriCtlPath, "crictl-path", types.DefaultCriCtl, | ||
"The path to the crictl binary. This is used to check health of cri component.") | ||
fs.StringVar(&hco.CriSocketPath, "cri-socket-path", types.DefaultCricSocketPath, | ||
"The path to the cri socket. Used with crictl to specify the socket path.") | ||
fs.DurationVar(&hco.CoolDownTime, "cooldown-time", types.DefaultCoolDownTime, | ||
"The duration to wait for the service to be up before attempting repair.") | ||
fs.DurationVar(&hco.HealthCheckTimeout, "health-check-timeout", types.DefaultHealthCheckTimeout, | ||
"The time to wait before marking the component as unhealthy.") | ||
fs.DurationVar(&hco.CmdTimeout, "cmd-timeout", types.DefaultCmdTimeout, | ||
"The time to wait for the exec commands to complete.") | ||
} | ||
|
||
// ValidOrDie validates health checker command line options. | ||
func (hco *HealthCheckerOptions) IsValid() error { | ||
// Make sure the component specified is valid. | ||
if hco.Component != types.KubeletComponent && hco.Component != types.DockerComponent && hco.Component != types.CRIComponent { | ||
return fmt.Errorf("the component specified is not supported. Supported components are : <kubelet/docker/cri>") | ||
} | ||
// Make sure the system service is specified if repair is enabled. | ||
if hco.EnableRepair && hco.SystemService == "" { | ||
return fmt.Errorf("system-service cannot be empty when repair is enabled") | ||
} | ||
// Skip checking further if the component is not cri. | ||
if hco.Component != types.CRIComponent { | ||
return nil | ||
} | ||
// Make sure the crictl path is not empty for cri component. | ||
if hco.Component == types.CRIComponent && hco.CriCtlPath == "" { | ||
return fmt.Errorf("the crictl-path cannot be empty for cri component") | ||
} | ||
// Make sure the cri socker path is not empty for cri component. | ||
if hco.Component == types.CRIComponent && hco.CriSocketPath == "" { | ||
return fmt.Errorf("the cri-socket-path cannot be empty for cri component") | ||
} | ||
return nil | ||
} | ||
|
||
// SetDefaults sets the defaults values for the dependent flags. | ||
func (hco *HealthCheckerOptions) SetDefaults() { | ||
if hco.SystemService != "" { | ||
return | ||
} | ||
if hco.Component != types.CRIComponent { | ||
hco.SystemService = hco.Component | ||
return | ||
} | ||
hco.SystemService = types.ContainerdService | ||
} | ||
|
||
func init() { | ||
pflag.CommandLine.AddGoFlagSet(flag.CommandLine) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
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 options | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"k8s.io/node-problem-detector/pkg/healthchecker/types" | ||
) | ||
|
||
func TestValidOrDie(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
hco HealthCheckerOptions | ||
expectError bool | ||
}{ | ||
{ | ||
name: "valid component", | ||
hco: HealthCheckerOptions{ | ||
Component: types.KubeletComponent, | ||
}, | ||
expectError: false, | ||
}, | ||
{ | ||
name: "invalid component", | ||
hco: HealthCheckerOptions{ | ||
Component: "wrongComponent", | ||
}, | ||
expectError: true, | ||
}, | ||
{ | ||
name: "empty crictl-path with cri", | ||
hco: HealthCheckerOptions{ | ||
Component: types.CRIComponent, | ||
CriCtlPath: "", | ||
EnableRepair: false, | ||
}, | ||
expectError: true, | ||
}, | ||
{ | ||
name: "empty system-service and repair enabled", | ||
hco: HealthCheckerOptions{ | ||
Component: types.KubeletComponent, | ||
EnableRepair: true, | ||
SystemService: "", | ||
}, | ||
expectError: true, | ||
}, | ||
} | ||
|
||
for _, test := range testCases { | ||
t.Run(test.name, func(t *testing.T) { | ||
if test.expectError { | ||
assert.Error(t, test.hco.IsValid(), "HealthChecker option %+v is invalid. Expected IsValid to return error.", test.hco) | ||
} else { | ||
assert.NoError(t, test.hco.IsValid(), "HealthChecker option %+v is valid. Expected IsValid to return nil.", test.hco) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"plugin": "custom", | ||
"pluginConfig": { | ||
"invoke_interval": "10s", | ||
"timeout": "3m", | ||
"max_output_length": 80, | ||
"concurrency": 1 | ||
}, | ||
"source": "health-checker", | ||
"metricsReporting": true, | ||
"conditions": [ | ||
{ | ||
"type": "ContainerRuntimeUnhealthy", | ||
"reason": "ContainerRuntimeIsHealthy", | ||
"message": "Container runtime on the node is functioning properly" | ||
} | ||
], | ||
"rules": [ | ||
{ | ||
"type": "permanent", | ||
"condition": "ContainerRuntimeUnhealthy", | ||
"reason": "DockerUnhealthy", | ||
"path": "/home/kubernetes/bin/health-checker", | ||
"args": [ | ||
"--component=docker", | ||
"--enable-repair=false", | ||
"--cooldown-time=2m", | ||
"--health-check-timeout=60s" | ||
], | ||
"timeout": "3m" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"plugin": "custom", | ||
"pluginConfig": { | ||
"invoke_interval": "10s", | ||
"timeout": "3m", | ||
"max_output_length": 80, | ||
"concurrency": 1 | ||
}, | ||
"source": "health-checker", | ||
"metricsReporting": true, | ||
"conditions": [ | ||
{ | ||
"type": "KubeletUnhealthy", | ||
"reason": "KubeletIsHealthy", | ||
"message": "kubelet on the node is functioning properly" | ||
} | ||
], | ||
"rules": [ | ||
{ | ||
"type": "permanent", | ||
"condition": "KubeletUnhealthy", | ||
"reason": "KubeletUnhealthy", | ||
"path": "/home/kubernetes/bin/health-checker", | ||
"args": [ | ||
"--component=kubelet", | ||
"--enable-repair=false", | ||
"--cooldown-time=1m", | ||
"--health-check-timeout=10s" | ||
], | ||
"timeout": "3m" | ||
} | ||
] | ||
} |
Oops, something went wrong.