|
| 1 | +/* |
| 2 | +Copyright 2017 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 | + "os" |
| 23 | + |
| 24 | + "net/url" |
| 25 | + |
| 26 | + "github.com/spf13/pflag" |
| 27 | +) |
| 28 | + |
| 29 | +// NodeProblemDetectorOptions contains node problem detector command line and application options. |
| 30 | +type NodeProblemDetectorOptions struct { |
| 31 | + // command line options |
| 32 | + |
| 33 | + // KernelMonitorConfigPath specifies the path to kernel monitor configuration file. |
| 34 | + KernelMonitorConfigPath string |
| 35 | + // ApiServerOverride is the custom URI used to connect to Kubernetes ApiServer. |
| 36 | + ApiServerOverride string |
| 37 | + // PrintVersion is the flag determining whether version information is printed. |
| 38 | + PrintVersion bool |
| 39 | + // HostnameOverride specifies custom node name used to override hostname. |
| 40 | + HostnameOverride string |
| 41 | + // ServerPort is the port to bind the node problem detector server. Use 0 to disable. |
| 42 | + ServerPort int |
| 43 | + // ServerAddress is the address to bind the node problem detector server. |
| 44 | + ServerAddress string |
| 45 | + |
| 46 | + // application options |
| 47 | + |
| 48 | + // NodeName is the node name used to communicate with Kubernetes ApiServer. |
| 49 | + NodeName string |
| 50 | +} |
| 51 | + |
| 52 | +func NewNodeProblemDetectorOptions() *NodeProblemDetectorOptions { |
| 53 | + return &NodeProblemDetectorOptions{} |
| 54 | +} |
| 55 | + |
| 56 | +// AddFlags adds node problem detector command line options to pflag. |
| 57 | +func (npdo *NodeProblemDetectorOptions) AddFlags(fs *pflag.FlagSet) { |
| 58 | + fs.StringVar(&npdo.KernelMonitorConfigPath, "kernel-monitor", |
| 59 | + "/config/kernel-monitor.json", "The path to the kernel monitor config file") |
| 60 | + fs.StringVar(&npdo.ApiServerOverride, "apiserver-override", |
| 61 | + "", "Custom URI used to connect to Kubernetes ApiServer") |
| 62 | + fs.BoolVar(&npdo.PrintVersion, "version", false, "Print version information and quit") |
| 63 | + fs.StringVar(&npdo.HostnameOverride, "hostname-override", |
| 64 | + "", "Custom node name used to override hostname") |
| 65 | + fs.IntVar(&npdo.ServerPort, "port", |
| 66 | + 10256, "The port to bind the node problem detector server. Use 0 to disable.") |
| 67 | + fs.StringVar(&npdo.ServerAddress, "address", |
| 68 | + "127.0.0.1", "The address to bind the node problem detector server.") |
| 69 | +} |
| 70 | + |
| 71 | +// ValidOrDie validates node problem detector command line options. |
| 72 | +func (npdo *NodeProblemDetectorOptions) ValidOrDie() { |
| 73 | + if _, err := url.Parse(npdo.ApiServerOverride); err != nil { |
| 74 | + panic(fmt.Sprintf("apiserver-override %q is not a valid HTTP URI: %v", |
| 75 | + npdo.ApiServerOverride, err)) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +// SetNodeNameOrDie sets `NodeName` field with valid value. |
| 80 | +func (npdo *NodeProblemDetectorOptions) SetNodeNameOrDie() { |
| 81 | + // Check hostname override first for customized node name. |
| 82 | + if npdo.HostnameOverride != "" { |
| 83 | + npdo.NodeName = npdo.HostnameOverride |
| 84 | + return |
| 85 | + } |
| 86 | + |
| 87 | + // Get node name from environment variable NODE_NAME |
| 88 | + // By default, assume that the NODE_NAME env should have been set with |
| 89 | + // downward api or user defined exported environment variable. We prefer it because sometimes |
| 90 | + // the hostname returned by os.Hostname is not right because: |
| 91 | + // 1. User may override the hostname. |
| 92 | + // 2. For some cloud providers, os.Hostname is different from the real hostname. |
| 93 | + npdo.NodeName = os.Getenv("NODE_NAME") |
| 94 | + if npdo.NodeName != "" { |
| 95 | + return |
| 96 | + } |
| 97 | + |
| 98 | + // For backward compatibility. If the env is not set, get the hostname |
| 99 | + // from os.Hostname(). This may not work for all configurations and |
| 100 | + // environments. |
| 101 | + nodeName, err := os.Hostname() |
| 102 | + if err != nil { |
| 103 | + panic(fmt.Sprintf("Failed to get host name: %v", err)) |
| 104 | + } |
| 105 | + |
| 106 | + npdo.NodeName = nodeName |
| 107 | +} |
| 108 | + |
| 109 | +func init() { |
| 110 | + pflag.CommandLine.AddGoFlagSet(flag.CommandLine) |
| 111 | +} |
0 commit comments