|
| 1 | +/* |
| 2 | +Copyright 2020 The Kubernetes Authors All rights reserved. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package options |
| 18 | + |
| 19 | +import ( |
| 20 | + "flag" |
| 21 | + "fmt" |
| 22 | + "time" |
| 23 | + |
| 24 | + "github.com/spf13/pflag" |
| 25 | + |
| 26 | + "k8s.io/node-problem-detector/pkg/healthchecker/types" |
| 27 | +) |
| 28 | + |
| 29 | +// NewHealthCheckerOptions returns an empty health check options struct. |
| 30 | +func NewHealthCheckerOptions() *HealthCheckerOptions { |
| 31 | + return &HealthCheckerOptions{} |
| 32 | +} |
| 33 | + |
| 34 | +// HealthCheckerOptions are the options used to configure the health checker. |
| 35 | +type HealthCheckerOptions struct { |
| 36 | + Component string |
| 37 | + SystemdService string |
| 38 | + EnableRepair bool |
| 39 | + CriCtlPath string |
| 40 | + CriSocketPath string |
| 41 | + CoolDownTime time.Duration |
| 42 | + HealthCheckTimeout time.Duration |
| 43 | +} |
| 44 | + |
| 45 | +// AddFlags adds health checker command line options to pflag. |
| 46 | +func (hco *HealthCheckerOptions) AddFlags(fs *pflag.FlagSet) { |
| 47 | + fs.StringVar(&hco.Component, "component", types.KubeletComponent, |
| 48 | + "The component to check health for. Supports kubelet, docker and cri") |
| 49 | + fs.StringVar(&hco.SystemdService, "systemd-service", "", |
| 50 | + "The underlying systemd service responsible for the component. Set to the corresponding component for docker and kubelet, containerd for cri.") |
| 51 | + fs.BoolVar(&hco.EnableRepair, "enable-repair", true, "Flag to enable/disable repair attempt for the component.") |
| 52 | + fs.StringVar(&hco.CriCtlPath, "crictl-path", types.DefaultCriCtl, |
| 53 | + "The path to the crictl binary. This is used to check health of cri component.") |
| 54 | + fs.StringVar(&hco.CriSocketPath, "cri-socket-path", types.DefaultCriSocketPath, |
| 55 | + "The path to the cri socket. Used with crictl to specify the socket path.") |
| 56 | + fs.DurationVar(&hco.CoolDownTime, "cooldown-time", types.DefaultCoolDownTime, |
| 57 | + "The duration to wait for the service to be up before attempting repair.") |
| 58 | + fs.DurationVar(&hco.HealthCheckTimeout, "health-check-timeout", types.DefaultHealthCheckTimeout, |
| 59 | + "The time to wait before marking the component as unhealthy.") |
| 60 | +} |
| 61 | + |
| 62 | +// IsValid validates health checker command line options. |
| 63 | +// Returns error if invalid, nil otherwise. |
| 64 | +func (hco *HealthCheckerOptions) IsValid() error { |
| 65 | + // Make sure the component specified is valid. |
| 66 | + if hco.Component != types.KubeletComponent && hco.Component != types.DockerComponent && hco.Component != types.CRIComponent { |
| 67 | + return fmt.Errorf("the component specified is not supported. Supported components are : <kubelet/docker/cri>") |
| 68 | + } |
| 69 | + // Make sure the systemd service is specified if repair is enabled. |
| 70 | + if hco.EnableRepair && hco.SystemdService == "" { |
| 71 | + return fmt.Errorf("systemd-service cannot be empty when repair is enabled") |
| 72 | + } |
| 73 | + // Skip checking further if the component is not cri. |
| 74 | + if hco.Component != types.CRIComponent { |
| 75 | + return nil |
| 76 | + } |
| 77 | + // Make sure the crictl path is not empty for cri component. |
| 78 | + if hco.Component == types.CRIComponent && hco.CriCtlPath == "" { |
| 79 | + return fmt.Errorf("the crictl-path cannot be empty for cri component") |
| 80 | + } |
| 81 | + // Make sure the cri socker path is not empty for cri component. |
| 82 | + if hco.Component == types.CRIComponent && hco.CriSocketPath == "" { |
| 83 | + return fmt.Errorf("the cri-socket-path cannot be empty for cri component") |
| 84 | + } |
| 85 | + return nil |
| 86 | +} |
| 87 | + |
| 88 | +// SetDefaults sets the defaults values for the dependent flags. |
| 89 | +func (hco *HealthCheckerOptions) SetDefaults() { |
| 90 | + if hco.SystemdService != "" { |
| 91 | + return |
| 92 | + } |
| 93 | + if hco.Component != types.CRIComponent { |
| 94 | + hco.SystemdService = hco.Component |
| 95 | + return |
| 96 | + } |
| 97 | + hco.SystemdService = types.ContainerdService |
| 98 | +} |
| 99 | + |
| 100 | +func init() { |
| 101 | + pflag.CommandLine.AddGoFlagSet(flag.CommandLine) |
| 102 | +} |
0 commit comments