github.com/hspak/nomad@v0.7.2-0.20180309000617-bc4ae22a39a5/client/config/config.go (about) 1 package config 2 3 import ( 4 "fmt" 5 "io" 6 "os" 7 "strconv" 8 "strings" 9 "time" 10 11 "github.com/hashicorp/nomad/helper" 12 "github.com/hashicorp/nomad/helper/tlsutil" 13 "github.com/hashicorp/nomad/nomad/structs" 14 "github.com/hashicorp/nomad/nomad/structs/config" 15 "github.com/hashicorp/nomad/version" 16 ) 17 18 var ( 19 // DefaultEnvBlacklist is the default set of environment variables that are 20 // filtered when passing the environment variables of the host to a task. 21 DefaultEnvBlacklist = strings.Join([]string{ 22 "CONSUL_TOKEN", 23 "VAULT_TOKEN", 24 "AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY", "AWS_SESSION_TOKEN", 25 "GOOGLE_APPLICATION_CREDENTIALS", 26 }, ",") 27 28 // DefaulUserBlacklist is the default set of users that tasks are not 29 // allowed to run as when using a driver in "user.checked_drivers" 30 DefaultUserBlacklist = strings.Join([]string{ 31 "root", 32 "Administrator", 33 }, ",") 34 35 // DefaultUserCheckedDrivers is the set of drivers we apply the user 36 // blacklist onto. For virtualized drivers it often doesn't make sense to 37 // make this stipulation so by default they are ignored. 38 DefaultUserCheckedDrivers = strings.Join([]string{ 39 "exec", 40 "qemu", 41 "java", 42 }, ",") 43 44 // A mapping of directories on the host OS to attempt to embed inside each 45 // task's chroot. 46 DefaultChrootEnv = map[string]string{ 47 "/bin": "/bin", 48 "/etc": "/etc", 49 "/lib": "/lib", 50 "/lib32": "/lib32", 51 "/lib64": "/lib64", 52 "/run/resolvconf": "/run/resolvconf", 53 "/sbin": "/sbin", 54 "/usr": "/usr", 55 } 56 ) 57 58 // RPCHandler can be provided to the Client if there is a local server 59 // to avoid going over the network. If not provided, the Client will 60 // maintain a connection pool to the servers 61 type RPCHandler interface { 62 RPC(method string, args interface{}, reply interface{}) error 63 } 64 65 // Config is used to parameterize and configure the behavior of the client 66 type Config struct { 67 // DevMode controls if we are in a development mode which 68 // avoids persistent storage. 69 DevMode bool 70 71 // StateDir is where we store our state 72 StateDir string 73 74 // AllocDir is where we store data for allocations 75 AllocDir string 76 77 // LogOutput is the destination for logs 78 LogOutput io.Writer 79 80 // Region is the clients region 81 Region string 82 83 // Network interface to be used in network fingerprinting 84 NetworkInterface string 85 86 // Network speed is the default speed of network interfaces if they can not 87 // be determined dynamically. 88 NetworkSpeed int 89 90 // CpuCompute is the default total CPU compute if they can not be determined 91 // dynamically. It should be given as Cores * MHz (2 Cores * 2 Ghz = 4000) 92 CpuCompute int 93 94 // MaxKillTimeout allows capping the user-specifiable KillTimeout. If the 95 // task's KillTimeout is greater than the MaxKillTimeout, MaxKillTimeout is 96 // used. 97 MaxKillTimeout time.Duration 98 99 // Servers is a list of known server addresses. These are as "host:port" 100 Servers []string 101 102 // RPCHandler can be provided to avoid network traffic if the 103 // server is running locally. 104 RPCHandler RPCHandler 105 106 // Node provides the base node 107 Node *structs.Node 108 109 // ClientMaxPort is the upper range of the ports that the client uses for 110 // communicating with plugin subsystems over loopback 111 ClientMaxPort uint 112 113 // ClientMinPort is the lower range of the ports that the client uses for 114 // communicating with plugin subsystems over loopback 115 ClientMinPort uint 116 117 // GloballyReservedPorts are ports that are reserved across all network 118 // devices and IPs. 119 GloballyReservedPorts []int 120 121 // A mapping of directories on the host OS to attempt to embed inside each 122 // task's chroot. 123 ChrootEnv map[string]string 124 125 // Options provides arbitrary key-value configuration for nomad internals, 126 // like fingerprinters and drivers. The format is: 127 // 128 // namespace.option = value 129 Options map[string]string 130 131 // Version is the version of the Nomad client 132 Version *version.VersionInfo 133 134 // ConsulConfig is this Agent's Consul configuration 135 ConsulConfig *config.ConsulConfig 136 137 // VaultConfig is this Agent's Vault configuration 138 VaultConfig *config.VaultConfig 139 140 // StatsCollectionInterval is the interval at which the Nomad client 141 // collects resource usage stats 142 StatsCollectionInterval time.Duration 143 144 // PublishNodeMetrics determines whether nomad is going to publish node 145 // level metrics to remote Telemetry sinks 146 PublishNodeMetrics bool 147 148 // PublishAllocationMetrics determines whether nomad is going to publish 149 // allocation metrics to remote Telemetry sinks 150 PublishAllocationMetrics bool 151 152 // TLSConfig holds various TLS related configurations 153 TLSConfig *config.TLSConfig 154 155 // GCInterval is the time interval at which the client triggers garbage 156 // collection 157 GCInterval time.Duration 158 159 // GCParallelDestroys is the number of parallel destroys the garbage 160 // collector will allow. 161 GCParallelDestroys int 162 163 // GCDiskUsageThreshold is the disk usage threshold given as a percent 164 // beyond which the Nomad client triggers GC of terminal allocations 165 GCDiskUsageThreshold float64 166 167 // GCInodeUsageThreshold is the inode usage threshold given as a percent 168 // beyond which the Nomad client triggers GC of the terminal allocations 169 GCInodeUsageThreshold float64 170 171 // GCMaxAllocs is the maximum number of allocations a node can have 172 // before garbage collection is triggered. 173 GCMaxAllocs int 174 175 // LogLevel is the level of the logs to putout 176 LogLevel string 177 178 // NoHostUUID disables using the host's UUID and will force generation of a 179 // random UUID. 180 NoHostUUID bool 181 182 // ACLEnabled controls if ACL enforcement and management is enabled. 183 ACLEnabled bool 184 185 // ACLTokenTTL is how long we cache token values for 186 ACLTokenTTL time.Duration 187 188 // ACLPolicyTTL is how long we cache policy values for 189 ACLPolicyTTL time.Duration 190 191 // DisableTaggedMetrics determines whether metrics will be displayed via a 192 // key/value/tag format, or simply a key/value format 193 DisableTaggedMetrics bool 194 195 // BackwardsCompatibleMetrics determines whether to show methods of 196 // displaying metrics for older verions, or to only show the new format 197 BackwardsCompatibleMetrics bool 198 199 // RPCHoldTimeout is how long an RPC can be "held" before it is errored. 200 // This is used to paper over a loss of leadership by instead holding RPCs, 201 // so that the caller experiences a slow response rather than an error. 202 // This period is meant to be long enough for a leader election to take 203 // place, and a small jitter is applied to avoid a thundering herd. 204 RPCHoldTimeout time.Duration 205 } 206 207 func (c *Config) Copy() *Config { 208 nc := new(Config) 209 *nc = *c 210 nc.Node = nc.Node.Copy() 211 nc.Servers = helper.CopySliceString(nc.Servers) 212 nc.Options = helper.CopyMapStringString(nc.Options) 213 nc.GloballyReservedPorts = helper.CopySliceInt(c.GloballyReservedPorts) 214 nc.ConsulConfig = c.ConsulConfig.Copy() 215 nc.VaultConfig = c.VaultConfig.Copy() 216 return nc 217 } 218 219 // DefaultConfig returns the default configuration 220 func DefaultConfig() *Config { 221 return &Config{ 222 Version: version.GetVersion(), 223 VaultConfig: config.DefaultVaultConfig(), 224 ConsulConfig: config.DefaultConsulConfig(), 225 LogOutput: os.Stderr, 226 Region: "global", 227 StatsCollectionInterval: 1 * time.Second, 228 TLSConfig: &config.TLSConfig{}, 229 LogLevel: "DEBUG", 230 GCInterval: 1 * time.Minute, 231 GCParallelDestroys: 2, 232 GCDiskUsageThreshold: 80, 233 GCInodeUsageThreshold: 70, 234 GCMaxAllocs: 50, 235 NoHostUUID: true, 236 DisableTaggedMetrics: false, 237 BackwardsCompatibleMetrics: false, 238 RPCHoldTimeout: 5 * time.Second, 239 } 240 } 241 242 // Read returns the specified configuration value or "". 243 func (c *Config) Read(id string) string { 244 return c.Options[id] 245 } 246 247 // ReadDefault returns the specified configuration value, or the specified 248 // default value if none is set. 249 func (c *Config) ReadDefault(id string, defaultValue string) string { 250 val, ok := c.Options[id] 251 if !ok { 252 return defaultValue 253 } 254 return val 255 } 256 257 // ReadBool parses the specified option as a boolean. 258 func (c *Config) ReadBool(id string) (bool, error) { 259 val, ok := c.Options[id] 260 if !ok { 261 return false, fmt.Errorf("Specified config is missing from options") 262 } 263 bval, err := strconv.ParseBool(val) 264 if err != nil { 265 return false, fmt.Errorf("Failed to parse %s as bool: %s", val, err) 266 } 267 return bval, nil 268 } 269 270 // ReadBoolDefault tries to parse the specified option as a boolean. If there is 271 // an error in parsing, the default option is returned. 272 func (c *Config) ReadBoolDefault(id string, defaultValue bool) bool { 273 val, err := c.ReadBool(id) 274 if err != nil { 275 return defaultValue 276 } 277 return val 278 } 279 280 // ReadInt parses the specified option as a int. 281 func (c *Config) ReadInt(id string) (int, error) { 282 val, ok := c.Options[id] 283 if !ok { 284 return 0, fmt.Errorf("Specified config is missing from options") 285 } 286 ival, err := strconv.Atoi(val) 287 if err != nil { 288 return 0, fmt.Errorf("Failed to parse %s as int: %s", val, err) 289 } 290 return ival, nil 291 } 292 293 // ReadIntDefault tries to parse the specified option as a int. If there is 294 // an error in parsing, the default option is returned. 295 func (c *Config) ReadIntDefault(id string, defaultValue int) int { 296 val, err := c.ReadInt(id) 297 if err != nil { 298 return defaultValue 299 } 300 return val 301 } 302 303 // ReadDuration parses the specified option as a duration. 304 func (c *Config) ReadDuration(id string) (time.Duration, error) { 305 val, ok := c.Options[id] 306 if !ok { 307 return time.Duration(0), fmt.Errorf("Specified config is missing from options") 308 } 309 dval, err := time.ParseDuration(val) 310 if err != nil { 311 return time.Duration(0), fmt.Errorf("Failed to parse %s as time duration: %s", val, err) 312 } 313 return dval, nil 314 } 315 316 // ReadDurationDefault tries to parse the specified option as a duration. If there is 317 // an error in parsing, the default option is returned. 318 func (c *Config) ReadDurationDefault(id string, defaultValue time.Duration) time.Duration { 319 val, err := c.ReadDuration(id) 320 if err != nil { 321 return defaultValue 322 } 323 return val 324 } 325 326 // ReadStringListToMap tries to parse the specified option as a comma separated list. 327 // If there is an error in parsing, an empty list is returned. 328 func (c *Config) ReadStringListToMap(key string) map[string]struct{} { 329 s := strings.TrimSpace(c.Read(key)) 330 list := make(map[string]struct{}) 331 if s != "" { 332 for _, e := range strings.Split(s, ",") { 333 trimmed := strings.TrimSpace(e) 334 list[trimmed] = struct{}{} 335 } 336 } 337 return list 338 } 339 340 // ReadStringListToMap tries to parse the specified option as a comma separated list. 341 // If there is an error in parsing, an empty list is returned. 342 func (c *Config) ReadStringListToMapDefault(key, defaultValue string) map[string]struct{} { 343 val, ok := c.Options[key] 344 if !ok { 345 val = defaultValue 346 } 347 348 list := make(map[string]struct{}) 349 if val != "" { 350 for _, e := range strings.Split(val, ",") { 351 trimmed := strings.TrimSpace(e) 352 list[trimmed] = struct{}{} 353 } 354 } 355 return list 356 } 357 358 // TLSConfiguration returns a TLSUtil Config based on the existing client 359 // configuration 360 func (c *Config) TLSConfiguration() *tlsutil.Config { 361 return &tlsutil.Config{ 362 VerifyIncoming: true, 363 VerifyOutgoing: true, 364 VerifyServerHostname: c.TLSConfig.VerifyServerHostname, 365 CAFile: c.TLSConfig.CAFile, 366 CertFile: c.TLSConfig.CertFile, 367 KeyFile: c.TLSConfig.KeyFile, 368 KeyLoader: c.TLSConfig.GetKeyLoader(), 369 } 370 }