2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2015-08-25 23:13:33 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2023-02-06 19:31:22 +00:00
|
|
|
"context"
|
2022-01-10 15:19:07 +00:00
|
|
|
"errors"
|
2015-11-17 03:30:37 +00:00
|
|
|
"fmt"
|
2023-02-06 19:31:22 +00:00
|
|
|
"net"
|
2022-01-10 15:19:07 +00:00
|
|
|
"reflect"
|
2015-11-17 03:30:37 +00:00
|
|
|
"strconv"
|
2015-11-24 15:18:49 +00:00
|
|
|
"strings"
|
2015-12-23 00:10:30 +00:00
|
|
|
"time"
|
2015-08-25 23:13:33 +00:00
|
|
|
|
2022-01-10 15:19:07 +00:00
|
|
|
"github.com/hashicorp/consul-template/config"
|
2023-05-08 18:17:10 +00:00
|
|
|
log "github.com/hashicorp/go-hclog"
|
2022-09-21 19:53:25 +00:00
|
|
|
"golang.org/x/exp/maps"
|
2022-08-18 23:32:04 +00:00
|
|
|
"golang.org/x/exp/slices"
|
2021-03-25 02:09:37 +00:00
|
|
|
|
2023-05-08 18:17:10 +00:00
|
|
|
"github.com/hashicorp/nomad/client/allocrunner/interfaces"
|
|
|
|
"github.com/hashicorp/nomad/client/lib/cgutil"
|
2019-01-09 18:57:56 +00:00
|
|
|
"github.com/hashicorp/nomad/client/state"
|
2023-05-08 18:17:10 +00:00
|
|
|
"github.com/hashicorp/nomad/command/agent/host"
|
2022-04-06 17:17:05 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/bufconndialer"
|
2019-01-23 14:27:14 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/pluginutils/loader"
|
2022-08-03 20:56:30 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/pointer"
|
2015-08-25 23:13:33 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2020-09-04 17:50:11 +00:00
|
|
|
structsc "github.com/hashicorp/nomad/nomad/structs/config"
|
2018-10-17 02:21:15 +00:00
|
|
|
"github.com/hashicorp/nomad/plugins/base"
|
2017-08-16 22:42:15 +00:00
|
|
|
"github.com/hashicorp/nomad/version"
|
2015-08-25 23:13:33 +00:00
|
|
|
)
|
|
|
|
|
2016-03-23 18:45:03 +00:00
|
|
|
var (
|
2020-10-12 12:47:05 +00:00
|
|
|
// DefaultEnvDenylist is the default set of environment variables that are
|
2016-03-23 18:45:03 +00:00
|
|
|
// filtered when passing the environment variables of the host to a task.
|
2021-09-21 19:58:51 +00:00
|
|
|
DefaultEnvDenylist = strings.Join(host.DefaultEnvDenyList, ",")
|
2016-03-24 17:55:14 +00:00
|
|
|
|
2020-10-12 12:47:05 +00:00
|
|
|
// DefaultUserDenylist is the default set of users that tasks are not
|
2016-03-24 17:55:14 +00:00
|
|
|
// allowed to run as when using a driver in "user.checked_drivers"
|
2020-10-12 12:47:05 +00:00
|
|
|
DefaultUserDenylist = strings.Join([]string{
|
2016-03-24 17:55:14 +00:00
|
|
|
"root",
|
|
|
|
"Administrator",
|
|
|
|
}, ",")
|
|
|
|
|
|
|
|
// DefaultUserCheckedDrivers is the set of drivers we apply the user
|
2020-10-12 12:47:05 +00:00
|
|
|
// denylist onto. For virtualized drivers it often doesn't make sense to
|
2016-03-24 17:55:14 +00:00
|
|
|
// make this stipulation so by default they are ignored.
|
|
|
|
DefaultUserCheckedDrivers = strings.Join([]string{
|
|
|
|
"exec",
|
2016-03-25 19:43:50 +00:00
|
|
|
"qemu",
|
2016-03-24 17:55:14 +00:00
|
|
|
"java",
|
|
|
|
}, ",")
|
2016-12-03 01:04:07 +00:00
|
|
|
|
2022-10-10 14:28:46 +00:00
|
|
|
// DefaultChrootEnv is a mapping of directories on the host OS to attempt to embed inside each
|
2016-12-03 01:04:07 +00:00
|
|
|
// task's chroot.
|
|
|
|
DefaultChrootEnv = map[string]string{
|
|
|
|
"/bin": "/bin",
|
|
|
|
"/etc": "/etc",
|
|
|
|
"/lib": "/lib",
|
|
|
|
"/lib32": "/lib32",
|
|
|
|
"/lib64": "/lib64",
|
|
|
|
"/run/resolvconf": "/run/resolvconf",
|
|
|
|
"/sbin": "/sbin",
|
|
|
|
"/usr": "/usr",
|
2020-09-29 15:21:50 +00:00
|
|
|
|
|
|
|
// embed systemd-resolved paths for systemd-resolved paths:
|
|
|
|
// /etc/resolv.conf is a symlink to /run/systemd/resolve/stub-resolv.conf in such systems.
|
|
|
|
// In non-systemd systems, this mount is a no-op and the path is ignored if not present.
|
|
|
|
"/run/systemd/resolve": "/run/systemd/resolve",
|
2016-12-03 01:04:07 +00:00
|
|
|
}
|
2022-01-10 15:19:07 +00:00
|
|
|
|
2022-06-08 18:08:25 +00:00
|
|
|
DefaultTemplateMaxStale = 87600 * time.Hour
|
2022-03-16 23:33:20 +00:00
|
|
|
|
|
|
|
DefaultTemplateFunctionDenylist = []string{"plugin", "writeToFile"}
|
2016-03-23 18:45:03 +00:00
|
|
|
)
|
|
|
|
|
2015-08-25 23:13:33 +00:00
|
|
|
// RPCHandler can be provided to the Client if there is a local server
|
|
|
|
// to avoid going over the network. If not provided, the Client will
|
|
|
|
// maintain a connection pool to the servers
|
|
|
|
type RPCHandler interface {
|
|
|
|
RPC(method string, args interface{}, reply interface{}) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Config is used to parameterize and configure the behavior of the client
|
|
|
|
type Config struct {
|
|
|
|
// DevMode controls if we are in a development mode which
|
|
|
|
// avoids persistent storage.
|
|
|
|
DevMode bool
|
|
|
|
|
2019-12-13 18:41:55 +00:00
|
|
|
// EnableDebug is used to enable debugging RPC endpoints
|
|
|
|
// in the absence of ACLs
|
|
|
|
EnableDebug bool
|
|
|
|
|
2015-08-25 23:13:33 +00:00
|
|
|
// StateDir is where we store our state
|
|
|
|
StateDir string
|
|
|
|
|
|
|
|
// AllocDir is where we store data for allocations
|
|
|
|
AllocDir string
|
|
|
|
|
2021-11-24 23:35:45 +00:00
|
|
|
// Logger provides a logger to the client
|
2019-10-15 19:14:25 +00:00
|
|
|
Logger log.InterceptLogger
|
2018-09-13 17:43:40 +00:00
|
|
|
|
2015-08-25 23:13:33 +00:00
|
|
|
// Region is the clients region
|
|
|
|
Region string
|
|
|
|
|
2015-10-01 15:31:47 +00:00
|
|
|
// Network interface to be used in network fingerprinting
|
2015-10-02 07:29:18 +00:00
|
|
|
NetworkInterface string
|
2015-10-01 15:31:47 +00:00
|
|
|
|
2015-10-03 00:32:11 +00:00
|
|
|
// Network speed is the default speed of network interfaces if they can not
|
|
|
|
// be determined dynamically.
|
|
|
|
NetworkSpeed int
|
|
|
|
|
2017-03-14 19:56:31 +00:00
|
|
|
// CpuCompute is the default total CPU compute if they can not be determined
|
|
|
|
// dynamically. It should be given as Cores * MHz (2 Cores * 2 Ghz = 4000)
|
|
|
|
CpuCompute int
|
|
|
|
|
2018-01-22 20:28:29 +00:00
|
|
|
// MemoryMB is the default node total memory in megabytes if it cannot be
|
|
|
|
// determined dynamically.
|
|
|
|
MemoryMB int
|
|
|
|
|
2023-01-24 14:14:22 +00:00
|
|
|
// DiskTotalMB is the default node total disk space in megabytes if it cannot be
|
|
|
|
// determined dynamically.
|
|
|
|
DiskTotalMB int
|
|
|
|
|
|
|
|
// DiskFreeMB is the default node free disk space in megabytes if it cannot be
|
|
|
|
// determined dynamically.
|
|
|
|
DiskFreeMB int
|
|
|
|
|
2015-12-23 00:10:30 +00:00
|
|
|
// MaxKillTimeout allows capping the user-specifiable KillTimeout. If the
|
|
|
|
// task's KillTimeout is greater than the MaxKillTimeout, MaxKillTimeout is
|
|
|
|
// used.
|
|
|
|
MaxKillTimeout time.Duration
|
|
|
|
|
2015-08-25 23:13:33 +00:00
|
|
|
// Servers is a list of known server addresses. These are as "host:port"
|
|
|
|
Servers []string
|
|
|
|
|
|
|
|
// RPCHandler can be provided to avoid network traffic if the
|
|
|
|
// server is running locally.
|
|
|
|
RPCHandler RPCHandler
|
|
|
|
|
|
|
|
// Node provides the base node
|
|
|
|
Node *structs.Node
|
2015-09-01 02:48:59 +00:00
|
|
|
|
2016-02-08 21:29:53 +00:00
|
|
|
// ClientMaxPort is the upper range of the ports that the client uses for
|
2016-03-14 02:05:41 +00:00
|
|
|
// communicating with plugin subsystems over loopback
|
2016-02-08 21:29:53 +00:00
|
|
|
ClientMaxPort uint
|
2016-02-05 23:17:15 +00:00
|
|
|
|
2016-02-08 21:29:53 +00:00
|
|
|
// ClientMinPort is the lower range of the ports that the client uses for
|
2016-03-14 02:05:41 +00:00
|
|
|
// communicating with plugin subsystems over loopback
|
2016-02-08 21:29:53 +00:00
|
|
|
ClientMinPort uint
|
2016-02-05 23:17:15 +00:00
|
|
|
|
2021-09-10 08:52:47 +00:00
|
|
|
// MaxDynamicPort is the largest dynamic port generated
|
|
|
|
MaxDynamicPort int
|
|
|
|
|
|
|
|
// MinDynamicPort is the smallest dynamic port generated
|
|
|
|
MinDynamicPort int
|
|
|
|
|
2016-08-02 02:58:32 +00:00
|
|
|
// A mapping of directories on the host OS to attempt to embed inside each
|
|
|
|
// task's chroot.
|
|
|
|
ChrootEnv map[string]string
|
|
|
|
|
2015-09-01 02:48:59 +00:00
|
|
|
// Options provides arbitrary key-value configuration for nomad internals,
|
|
|
|
// like fingerprinters and drivers. The format is:
|
|
|
|
//
|
|
|
|
// namespace.option = value
|
|
|
|
Options map[string]string
|
2016-02-25 03:06:30 +00:00
|
|
|
|
|
|
|
// Version is the version of the Nomad client
|
2017-08-16 22:42:15 +00:00
|
|
|
Version *version.VersionInfo
|
2016-05-11 22:24:37 +00:00
|
|
|
|
2016-05-24 04:28:12 +00:00
|
|
|
// ConsulConfig is this Agent's Consul configuration
|
2020-09-04 17:50:11 +00:00
|
|
|
ConsulConfig *structsc.ConsulConfig
|
2016-05-25 05:30:10 +00:00
|
|
|
|
2016-08-09 22:00:50 +00:00
|
|
|
// VaultConfig is this Agent's Vault configuration
|
2020-09-04 17:50:11 +00:00
|
|
|
VaultConfig *structsc.VaultConfig
|
2016-08-09 22:00:50 +00:00
|
|
|
|
2016-05-25 05:30:10 +00:00
|
|
|
// StatsCollectionInterval is the interval at which the Nomad client
|
|
|
|
// collects resource usage stats
|
|
|
|
StatsCollectionInterval time.Duration
|
2016-08-02 02:49:01 +00:00
|
|
|
|
|
|
|
// PublishNodeMetrics determines whether nomad is going to publish node
|
|
|
|
// level metrics to remote Telemetry sinks
|
|
|
|
PublishNodeMetrics bool
|
|
|
|
|
|
|
|
// PublishAllocationMetrics determines whether nomad is going to publish
|
|
|
|
// allocation metrics to remote Telemetry sinks
|
|
|
|
PublishAllocationMetrics bool
|
2016-10-24 05:22:00 +00:00
|
|
|
|
2016-10-25 22:57:38 +00:00
|
|
|
// TLSConfig holds various TLS related configurations
|
2020-09-04 17:50:11 +00:00
|
|
|
TLSConfig *structsc.TLSConfig
|
2017-01-09 19:21:51 +00:00
|
|
|
|
2017-01-31 23:32:20 +00:00
|
|
|
// GCInterval is the time interval at which the client triggers garbage
|
|
|
|
// collection
|
|
|
|
GCInterval time.Duration
|
|
|
|
|
2017-03-11 00:27:00 +00:00
|
|
|
// GCParallelDestroys is the number of parallel destroys the garbage
|
|
|
|
// collector will allow.
|
|
|
|
GCParallelDestroys int
|
|
|
|
|
|
|
|
// GCDiskUsageThreshold is the disk usage threshold given as a percent
|
|
|
|
// beyond which the Nomad client triggers GC of terminal allocations
|
2017-01-31 23:32:20 +00:00
|
|
|
GCDiskUsageThreshold float64
|
|
|
|
|
2017-03-11 00:27:00 +00:00
|
|
|
// GCInodeUsageThreshold is the inode usage threshold given as a percent
|
|
|
|
// beyond which the Nomad client triggers GC of the terminal allocations
|
2017-01-31 23:32:20 +00:00
|
|
|
GCInodeUsageThreshold float64
|
|
|
|
|
2017-05-11 00:39:45 +00:00
|
|
|
// GCMaxAllocs is the maximum number of allocations a node can have
|
|
|
|
// before garbage collection is triggered.
|
|
|
|
GCMaxAllocs int
|
|
|
|
|
2017-02-27 21:42:37 +00:00
|
|
|
// NoHostUUID disables using the host's UUID and will force generation of a
|
|
|
|
// random UUID.
|
|
|
|
NoHostUUID bool
|
2017-08-20 00:19:38 +00:00
|
|
|
|
|
|
|
// ACLEnabled controls if ACL enforcement and management is enabled.
|
|
|
|
ACLEnabled bool
|
|
|
|
|
|
|
|
// ACLTokenTTL is how long we cache token values for
|
|
|
|
ACLTokenTTL time.Duration
|
|
|
|
|
|
|
|
// ACLPolicyTTL is how long we cache policy values for
|
|
|
|
ACLPolicyTTL time.Duration
|
2017-08-30 21:18:42 +00:00
|
|
|
|
2022-10-20 07:37:32 +00:00
|
|
|
// ACLRoleTTL is how long we cache ACL role value for within each Nomad
|
|
|
|
// client.
|
|
|
|
ACLRoleTTL time.Duration
|
|
|
|
|
2019-06-03 19:31:39 +00:00
|
|
|
// DisableRemoteExec disables remote exec targeting tasks on this client
|
|
|
|
DisableRemoteExec bool
|
|
|
|
|
2019-08-02 19:20:14 +00:00
|
|
|
// TemplateConfig includes configuration for template rendering
|
|
|
|
TemplateConfig *ClientTemplateConfig
|
|
|
|
|
2018-01-10 19:28:44 +00:00
|
|
|
// RPCHoldTimeout is how long an RPC can be "held" before it is errored.
|
|
|
|
// This is used to paper over a loss of leadership by instead holding RPCs,
|
|
|
|
// so that the caller experiences a slow response rather than an error.
|
|
|
|
// This period is meant to be long enough for a leader election to take
|
|
|
|
// place, and a small jitter is applied to avoid a thundering herd.
|
|
|
|
RPCHoldTimeout time.Duration
|
2018-09-27 01:14:36 +00:00
|
|
|
|
|
|
|
// PluginLoader is used to load plugins.
|
|
|
|
PluginLoader loader.PluginCatalog
|
|
|
|
|
|
|
|
// PluginSingletonLoader is a plugin loader that will returns singleton
|
|
|
|
// instances of the plugins.
|
|
|
|
PluginSingletonLoader loader.PluginCatalog
|
2019-01-09 18:57:56 +00:00
|
|
|
|
|
|
|
// StateDBFactory is used to override stateDB implementations,
|
|
|
|
StateDBFactory state.NewStateDBFunc
|
2019-06-14 03:05:57 +00:00
|
|
|
|
2023-05-12 17:29:44 +00:00
|
|
|
AllocRunnerFactory AllocRunnerFactory
|
|
|
|
|
2019-06-14 03:05:57 +00:00
|
|
|
// CNIPath is the path used to search for CNI plugins. Multiple paths can
|
|
|
|
// be specified with colon delimited
|
|
|
|
CNIPath string
|
|
|
|
|
2020-05-15 15:09:01 +00:00
|
|
|
// CNIConfigDir is the directory where CNI network configuration is located. The
|
|
|
|
// client will use this path when fingerprinting CNI networks.
|
|
|
|
CNIConfigDir string
|
|
|
|
|
|
|
|
// CNIInterfacePrefix is the prefix to use when creating CNI network interfaces. This
|
|
|
|
// defaults to 'eth', therefore the first interface created by CNI inside the alloc
|
|
|
|
// network will be 'eth0'.
|
|
|
|
CNIInterfacePrefix string
|
|
|
|
|
2019-06-14 03:05:57 +00:00
|
|
|
// BridgeNetworkName is the name to use for the bridge created in bridge
|
|
|
|
// networking mode. This defaults to 'nomad' if not set
|
|
|
|
BridgeNetworkName string
|
|
|
|
|
2023-02-02 15:12:15 +00:00
|
|
|
// BridgeNetworkHairpinMode is whether or not to enable hairpin mode on the
|
|
|
|
// internal bridge network
|
|
|
|
BridgeNetworkHairpinMode bool
|
|
|
|
|
2019-06-14 03:05:57 +00:00
|
|
|
// BridgeNetworkAllocSubnet is the IP subnet to use for address allocation
|
|
|
|
// for allocations in bridge networking mode. Subnet must be in CIDR
|
|
|
|
// notation
|
|
|
|
BridgeNetworkAllocSubnet string
|
2019-07-25 14:45:41 +00:00
|
|
|
|
2019-08-12 14:22:27 +00:00
|
|
|
// HostVolumes is a map of the configured host volumes by name.
|
2019-07-25 14:45:41 +00:00
|
|
|
HostVolumes map[string]*structs.ClientHostVolumeConfig
|
2020-06-16 15:53:10 +00:00
|
|
|
|
|
|
|
// HostNetworks is a map of the conigured host networks by name.
|
|
|
|
HostNetworks map[string]*structs.ClientHostNetworkConfig
|
2020-07-06 22:51:46 +00:00
|
|
|
|
|
|
|
// BindWildcardDefaultHostNetwork toggles if the default host network should accept all
|
|
|
|
// destinations (true) or only filter on the IP of the default host network (false) when
|
|
|
|
// port mapping. This allows Nomad clients with no defined host networks to accept and
|
|
|
|
// port forward traffic only matching on the destination port. An example use of this
|
|
|
|
// is when a network loadbalancer is utilizing direct server return and the destination
|
|
|
|
// address of incomming packets does not match the IP address of the host interface.
|
|
|
|
//
|
|
|
|
// This configuration is only considered if no host networks are defined.
|
|
|
|
BindWildcardDefaultHostNetwork bool
|
2021-03-25 02:09:37 +00:00
|
|
|
|
|
|
|
// CgroupParent is the parent cgroup Nomad should use when managing any cgroup subsystems.
|
2021-04-09 17:12:53 +00:00
|
|
|
// Currently this only includes the 'cpuset' cgroup subsystem.
|
2021-03-25 02:09:37 +00:00
|
|
|
CgroupParent string
|
|
|
|
|
2021-04-09 17:12:53 +00:00
|
|
|
// ReservableCores if set overrides the set of reservable cores reported in fingerprinting.
|
2021-03-29 18:05:12 +00:00
|
|
|
ReservableCores []uint16
|
2022-03-14 10:48:13 +00:00
|
|
|
|
|
|
|
// NomadServiceDiscovery determines whether the Nomad native service
|
|
|
|
// discovery client functionality is enabled.
|
|
|
|
NomadServiceDiscovery bool
|
2022-04-06 17:17:05 +00:00
|
|
|
|
|
|
|
// TemplateDialer is our custom HTTP dialer for consul-template. This is
|
|
|
|
// used for template functions which require access to the Nomad API.
|
|
|
|
TemplateDialer *bufconndialer.BufConnWrapper
|
2022-05-03 22:38:32 +00:00
|
|
|
|
2023-02-06 19:31:22 +00:00
|
|
|
// APIListenerRegistrar allows the client to register listeners created at
|
|
|
|
// runtime (eg the Task API) with the agent's HTTP server. Since the agent
|
|
|
|
// creates the HTTP *after* the client starts, we have to use this shim to
|
|
|
|
// pass listeners back to the agent.
|
|
|
|
// This is the same design as the bufconndialer but for the
|
|
|
|
// http.Serve(listener) API instead of the net.Dial API.
|
|
|
|
APIListenerRegistrar APIListenerRegistrar
|
|
|
|
|
2022-05-03 22:38:32 +00:00
|
|
|
// Artifact configuration from the agent's config file.
|
|
|
|
Artifact *ArtifactConfig
|
2023-04-14 19:35:32 +00:00
|
|
|
|
|
|
|
// Drain configuration from the agent's config file.
|
|
|
|
Drain *DrainConfig
|
2023-05-08 18:17:10 +00:00
|
|
|
|
|
|
|
// ExtraAllocHooks are run with other allocation hooks, mainly for testing.
|
|
|
|
ExtraAllocHooks []interfaces.RunnerHook
|
2015-09-01 02:48:59 +00:00
|
|
|
}
|
|
|
|
|
2023-02-06 19:31:22 +00:00
|
|
|
type APIListenerRegistrar interface {
|
|
|
|
// Serve the HTTP API on the provided listener.
|
|
|
|
//
|
|
|
|
// The context is because Serve may be called before the HTTP server has been
|
|
|
|
// initialized. If the context is canceled before the HTTP server is
|
|
|
|
// initialized, the context's error will be returned.
|
|
|
|
Serve(context.Context, net.Listener) error
|
|
|
|
}
|
|
|
|
|
2022-01-10 15:19:07 +00:00
|
|
|
// ClientTemplateConfig is configuration on the client specific to template
|
|
|
|
// rendering
|
2019-08-02 19:20:14 +00:00
|
|
|
type ClientTemplateConfig struct {
|
2022-01-10 15:19:07 +00:00
|
|
|
// FunctionDenylist disables functions in consul-template that
|
|
|
|
// are unsafe because they expose information from the client host.
|
|
|
|
FunctionDenylist []string `hcl:"function_denylist"`
|
|
|
|
|
|
|
|
// Deprecated: COMPAT(1.0) consul-template uses inclusive language from
|
|
|
|
// v0.25.0 - function_blacklist is kept for compatibility
|
|
|
|
FunctionBlacklist []string `hcl:"function_blacklist"`
|
|
|
|
|
|
|
|
// DisableSandbox allows templates to access arbitrary files on the
|
|
|
|
// client host. By default templates can access files only within
|
|
|
|
// the task directory.
|
|
|
|
DisableSandbox bool `hcl:"disable_file_sandbox"`
|
|
|
|
|
|
|
|
// This is the maximum interval to allow "stale" data. By default, only the
|
|
|
|
// Consul leader will respond to queries; any requests to a follower will
|
|
|
|
// forward to the leader. In large clusters with many requests, this is not as
|
|
|
|
// scalable, so this option allows any follower to respond to a query, so long
|
|
|
|
// as the last-replicated data is within these bounds. Higher values result in
|
|
|
|
// less cluster load, but are more likely to have outdated data.
|
|
|
|
// NOTE: Since Consul Template uses a pointer, this field uses a pointer which
|
|
|
|
// is inconsistent with how Nomad typically works. This decision was made to
|
|
|
|
// maintain parity with the external subsystem, not to establish a new standard.
|
|
|
|
MaxStale *time.Duration `hcl:"-"`
|
|
|
|
MaxStaleHCL string `hcl:"max_stale,optional"`
|
|
|
|
|
|
|
|
// BlockQueryWaitTime is amount of time in seconds to do a blocking query for.
|
|
|
|
// Many endpoints in Consul support a feature known as "blocking queries".
|
|
|
|
// A blocking query is used to wait for a potential change using long polling.
|
|
|
|
// NOTE: Since Consul Template uses a pointer, this field uses a pointer which
|
|
|
|
// is inconsistent with how Nomad typically works. This decision was made to
|
|
|
|
// maintain parity with the external subsystem, not to establish a new standard.
|
|
|
|
BlockQueryWaitTime *time.Duration `hcl:"-"`
|
|
|
|
BlockQueryWaitTimeHCL string `hcl:"block_query_wait,optional"`
|
|
|
|
|
|
|
|
// Wait is the quiescence timers; it defines the minimum and maximum amount of
|
|
|
|
// time to wait for the Consul cluster to reach a consistent state before rendering a
|
|
|
|
// template. This is useful to enable in systems where Consul is experiencing
|
|
|
|
// a lot of flapping because it will reduce the number of times a template is rendered.
|
|
|
|
Wait *WaitConfig `hcl:"wait,optional" json:"-"`
|
|
|
|
|
|
|
|
// WaitBounds allows operators to define boundaries on individual template wait
|
|
|
|
// configuration overrides. If set, this ensures that if a job author specifies
|
|
|
|
// a wait configuration with values the cluster operator does not allow, the
|
|
|
|
// cluster operator's boundary will be applied rather than the job author's
|
|
|
|
// out of bounds configuration.
|
|
|
|
WaitBounds *WaitConfig `hcl:"wait_bounds,optional" json:"-"`
|
|
|
|
|
|
|
|
// This controls the retry behavior when an error is returned from Consul.
|
|
|
|
// Consul Template is highly fault tolerant, meaning it does not exit in the
|
|
|
|
// face of failure. Instead, it uses exponential back-off and retry functions
|
|
|
|
// to wait for the cluster to become available, as is customary in distributed
|
|
|
|
// systems.
|
|
|
|
ConsulRetry *RetryConfig `hcl:"consul_retry,optional"`
|
|
|
|
|
|
|
|
// This controls the retry behavior when an error is returned from Vault.
|
|
|
|
// Consul Template is highly fault tolerant, meaning it does not exit in the
|
|
|
|
// face of failure. Instead, it uses exponential back-off and retry functions
|
|
|
|
// to wait for the cluster to become available, as is customary in distributed
|
|
|
|
// systems.
|
|
|
|
VaultRetry *RetryConfig `hcl:"vault_retry,optional"`
|
2022-08-03 20:56:30 +00:00
|
|
|
|
|
|
|
// This controls the retry behavior when an error is returned from Nomad.
|
|
|
|
// Consul Template is highly fault tolerant, meaning it does not exit in the
|
|
|
|
// face of failure. Instead, it uses exponential back-off and retry functions
|
|
|
|
// to wait for the cluster to become available, as is customary in distributed
|
|
|
|
// systems.
|
|
|
|
NomadRetry *RetryConfig `hcl:"nomad_retry,optional"`
|
2019-08-02 19:20:14 +00:00
|
|
|
}
|
|
|
|
|
2022-01-10 15:19:07 +00:00
|
|
|
// Copy returns a deep copy of a ClientTemplateConfig
|
2019-08-16 14:03:31 +00:00
|
|
|
func (c *ClientTemplateConfig) Copy() *ClientTemplateConfig {
|
|
|
|
if c == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
nc := new(ClientTemplateConfig)
|
|
|
|
*nc = *c
|
2022-03-16 23:33:20 +00:00
|
|
|
|
|
|
|
if len(c.FunctionDenylist) > 0 {
|
2022-09-21 19:53:25 +00:00
|
|
|
nc.FunctionDenylist = slices.Clone(nc.FunctionDenylist)
|
2022-03-16 23:33:20 +00:00
|
|
|
} else if c.FunctionDenylist != nil {
|
|
|
|
// Explicitly no functions denied (which is different than nil)
|
|
|
|
nc.FunctionDenylist = []string{}
|
|
|
|
}
|
2022-01-10 15:19:07 +00:00
|
|
|
|
|
|
|
if c.BlockQueryWaitTime != nil {
|
|
|
|
nc.BlockQueryWaitTime = &*c.BlockQueryWaitTime
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.MaxStale != nil {
|
|
|
|
nc.MaxStale = &*c.MaxStale
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Wait != nil {
|
|
|
|
nc.Wait = c.Wait.Copy()
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.ConsulRetry != nil {
|
|
|
|
nc.ConsulRetry = c.ConsulRetry.Copy()
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.VaultRetry != nil {
|
|
|
|
nc.VaultRetry = c.VaultRetry.Copy()
|
|
|
|
}
|
|
|
|
|
2022-08-03 20:56:30 +00:00
|
|
|
if c.NomadRetry != nil {
|
|
|
|
nc.NomadRetry = c.NomadRetry.Copy()
|
|
|
|
}
|
|
|
|
|
2019-08-16 14:03:31 +00:00
|
|
|
return nc
|
|
|
|
}
|
|
|
|
|
2022-01-10 15:19:07 +00:00
|
|
|
func (c *ClientTemplateConfig) IsEmpty() bool {
|
|
|
|
if c == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-01-26 16:31:37 +00:00
|
|
|
return !c.DisableSandbox &&
|
2022-03-16 23:33:20 +00:00
|
|
|
c.FunctionDenylist == nil &&
|
|
|
|
c.FunctionBlacklist == nil &&
|
2022-01-26 16:31:37 +00:00
|
|
|
c.BlockQueryWaitTime == nil &&
|
2022-01-10 15:19:07 +00:00
|
|
|
c.BlockQueryWaitTimeHCL == "" &&
|
|
|
|
c.MaxStale == nil &&
|
|
|
|
c.MaxStaleHCL == "" &&
|
|
|
|
c.Wait.IsEmpty() &&
|
|
|
|
c.ConsulRetry.IsEmpty() &&
|
2022-08-03 20:56:30 +00:00
|
|
|
c.VaultRetry.IsEmpty() &&
|
|
|
|
c.NomadRetry.IsEmpty()
|
2022-01-10 15:19:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WaitConfig is mirrored from templateconfig.WaitConfig because we need to handle
|
|
|
|
// the HCL conversion which happens in agent.ParseConfigFile
|
|
|
|
// NOTE: Since Consul Template requires pointers, this type uses pointers to fields
|
|
|
|
// which is inconsistent with how Nomad typically works. This decision was made
|
|
|
|
// to maintain parity with the external subsystem, not to establish a new standard.
|
|
|
|
type WaitConfig struct {
|
|
|
|
Min *time.Duration `hcl:"-"`
|
|
|
|
MinHCL string `hcl:"min,optional" json:"-"`
|
|
|
|
Max *time.Duration `hcl:"-"`
|
|
|
|
MaxHCL string `hcl:"max,optional" json:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy returns a deep copy of the receiver.
|
|
|
|
func (wc *WaitConfig) Copy() *WaitConfig {
|
|
|
|
if wc == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
nwc := new(WaitConfig)
|
|
|
|
|
|
|
|
if wc.Min != nil {
|
|
|
|
nwc.Min = &*wc.Min
|
|
|
|
}
|
|
|
|
|
|
|
|
if wc.Max != nil {
|
|
|
|
nwc.Max = &*wc.Max
|
|
|
|
}
|
|
|
|
|
|
|
|
return wc
|
|
|
|
}
|
|
|
|
|
2022-10-10 14:28:46 +00:00
|
|
|
// Equal returns the result of reflect.DeepEqual
|
|
|
|
func (wc *WaitConfig) Equal(other *WaitConfig) bool {
|
2022-01-10 15:19:07 +00:00
|
|
|
return reflect.DeepEqual(wc, other)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsEmpty returns true if the receiver only contains an instance with no fields set.
|
|
|
|
func (wc *WaitConfig) IsEmpty() bool {
|
|
|
|
if wc == nil {
|
|
|
|
return true
|
|
|
|
}
|
2022-10-10 14:28:46 +00:00
|
|
|
return wc.Equal(&WaitConfig{})
|
2022-01-10 15:19:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate returns an error if the receiver is nil or empty or if Min is greater
|
|
|
|
// than Max the user specified Max.
|
|
|
|
func (wc *WaitConfig) Validate() error {
|
|
|
|
// If the config is nil or empty return false so that it is never assigned.
|
|
|
|
if wc == nil || wc.IsEmpty() {
|
|
|
|
return errors.New("wait config is nil or empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
// If min is nil, return
|
|
|
|
if wc.Min == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If min isn't nil, make sure Max is less than Min.
|
|
|
|
if wc.Max != nil {
|
|
|
|
if *wc.Min > *wc.Max {
|
|
|
|
return fmt.Errorf("wait config min %d is greater than max %d", *wc.Min, *wc.Max)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, return nil. Consul Template will set a Max based off of Min.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge merges two WaitConfigs. The passed instance always takes precedence.
|
|
|
|
func (wc *WaitConfig) Merge(b *WaitConfig) *WaitConfig {
|
|
|
|
if wc == nil {
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
result := *wc
|
|
|
|
if b == nil {
|
|
|
|
return &result
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.Min != nil {
|
|
|
|
result.Min = &*b.Min
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.MinHCL != "" {
|
|
|
|
result.MinHCL = b.MinHCL
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.Max != nil {
|
|
|
|
result.Max = &*b.Max
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.MaxHCL != "" {
|
|
|
|
result.MaxHCL = b.MaxHCL
|
|
|
|
}
|
|
|
|
|
|
|
|
return &result
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToConsulTemplate converts a client WaitConfig instance to a consul-template WaitConfig
|
|
|
|
func (wc *WaitConfig) ToConsulTemplate() (*config.WaitConfig, error) {
|
|
|
|
if wc.IsEmpty() {
|
|
|
|
return nil, errors.New("wait config is empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := wc.Validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-08-17 16:26:34 +00:00
|
|
|
result := &config.WaitConfig{Enabled: pointer.Of(true)}
|
2022-01-10 15:19:07 +00:00
|
|
|
|
|
|
|
if wc.Min != nil {
|
|
|
|
result.Min = wc.Min
|
|
|
|
}
|
|
|
|
|
|
|
|
if wc.Max != nil {
|
|
|
|
result.Max = wc.Max
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// RetryConfig is mirrored from templateconfig.WaitConfig because we need to handle
|
|
|
|
// the HCL indirection to support mapping in agent.ParseConfigFile.
|
|
|
|
// NOTE: Since Consul Template requires pointers, this type uses pointers to fields
|
|
|
|
// which is inconsistent with how Nomad typically works. However, since zero in
|
|
|
|
// Attempts and MaxBackoff have special meaning, it is necessary to know if the
|
|
|
|
// value was actually set rather than if it defaulted to 0. The rest of the fields
|
|
|
|
// use pointers to maintain parity with the external subystem, not to establish
|
|
|
|
// a new standard.
|
|
|
|
type RetryConfig struct {
|
|
|
|
// Attempts is the total number of maximum attempts to retry before letting
|
|
|
|
// the error fall through.
|
|
|
|
// 0 means unlimited.
|
|
|
|
Attempts *int `hcl:"attempts,optional"`
|
|
|
|
// Backoff is the base of the exponential backoff. This number will be
|
|
|
|
// multiplied by the next power of 2 on each iteration.
|
|
|
|
Backoff *time.Duration `hcl:"-"`
|
|
|
|
BackoffHCL string `hcl:"backoff,optional" json:"-"`
|
|
|
|
// MaxBackoff is an upper limit to the sleep time between retries
|
|
|
|
// A MaxBackoff of 0 means there is no limit to the exponential growth of the backoff.
|
|
|
|
MaxBackoff *time.Duration `hcl:"-"`
|
|
|
|
MaxBackoffHCL string `hcl:"max_backoff,optional" json:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rc *RetryConfig) Copy() *RetryConfig {
|
|
|
|
if rc == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
nrc := new(RetryConfig)
|
|
|
|
*nrc = *rc
|
|
|
|
|
|
|
|
// Now copy pointer values
|
|
|
|
if rc.Attempts != nil {
|
|
|
|
nrc.Attempts = &*rc.Attempts
|
|
|
|
}
|
|
|
|
if rc.Backoff != nil {
|
|
|
|
nrc.Backoff = &*rc.Backoff
|
|
|
|
}
|
|
|
|
if rc.MaxBackoff != nil {
|
|
|
|
nrc.MaxBackoff = &*rc.MaxBackoff
|
|
|
|
}
|
|
|
|
|
|
|
|
return nrc
|
|
|
|
}
|
|
|
|
|
2022-10-10 14:28:46 +00:00
|
|
|
// Equal returns the result of reflect.DeepEqual
|
|
|
|
func (rc *RetryConfig) Equal(other *RetryConfig) bool {
|
2022-01-10 15:19:07 +00:00
|
|
|
return reflect.DeepEqual(rc, other)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsEmpty returns true if the receiver only contains an instance with no fields set.
|
|
|
|
func (rc *RetryConfig) IsEmpty() bool {
|
|
|
|
if rc == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-10-10 14:28:46 +00:00
|
|
|
return rc.Equal(&RetryConfig{})
|
2022-01-10 15:19:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate returns an error if the receiver is nil or empty, or if Backoff
|
|
|
|
// is greater than MaxBackoff.
|
|
|
|
func (rc *RetryConfig) Validate() error {
|
|
|
|
// If the config is nil or empty return false so that it is never assigned.
|
|
|
|
if rc == nil || rc.IsEmpty() {
|
|
|
|
return errors.New("retry config is nil or empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
// If Backoff not set, no need to validate
|
|
|
|
if rc.Backoff == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MaxBackoff nil will end up defaulted to 1 minutes. We should validate that
|
|
|
|
// the user supplied backoff does not exceed that.
|
|
|
|
if rc.MaxBackoff == nil && *rc.Backoff > config.DefaultRetryMaxBackoff {
|
|
|
|
return fmt.Errorf("retry config backoff %d is greater than default max_backoff %d", *rc.Backoff, config.DefaultRetryMaxBackoff)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MaxBackoff == 0 means backoff is unbounded. No need to validate.
|
|
|
|
if rc.MaxBackoff != nil && *rc.MaxBackoff == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if rc.MaxBackoff != nil && *rc.Backoff > *rc.MaxBackoff {
|
|
|
|
return fmt.Errorf("retry config backoff %d is greater than max_backoff %d", *rc.Backoff, *rc.MaxBackoff)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge merges two RetryConfigs. The passed instance always takes precedence.
|
|
|
|
func (rc *RetryConfig) Merge(b *RetryConfig) *RetryConfig {
|
|
|
|
if rc == nil {
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
result := *rc
|
|
|
|
if b == nil {
|
|
|
|
return &result
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.Attempts != nil {
|
|
|
|
result.Attempts = &*b.Attempts
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.Backoff != nil {
|
|
|
|
result.Backoff = &*b.Backoff
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.BackoffHCL != "" {
|
|
|
|
result.BackoffHCL = b.BackoffHCL
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.MaxBackoff != nil {
|
|
|
|
result.MaxBackoff = &*b.MaxBackoff
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.MaxBackoffHCL != "" {
|
|
|
|
result.MaxBackoffHCL = b.MaxBackoffHCL
|
|
|
|
}
|
|
|
|
|
|
|
|
return &result
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToConsulTemplate converts a client RetryConfig instance to a consul-template RetryConfig
|
|
|
|
func (rc *RetryConfig) ToConsulTemplate() (*config.RetryConfig, error) {
|
|
|
|
if err := rc.Validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-08-17 16:26:34 +00:00
|
|
|
result := &config.RetryConfig{Enabled: pointer.Of(true)}
|
2022-01-10 15:19:07 +00:00
|
|
|
|
|
|
|
if rc.Attempts != nil {
|
|
|
|
result.Attempts = rc.Attempts
|
|
|
|
}
|
|
|
|
|
|
|
|
if rc.Backoff != nil {
|
|
|
|
result.Backoff = rc.Backoff
|
|
|
|
}
|
|
|
|
|
|
|
|
if rc.MaxBackoff != nil {
|
|
|
|
result.MaxBackoff = &*rc.MaxBackoff
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2016-02-10 21:44:53 +00:00
|
|
|
func (c *Config) Copy() *Config {
|
2022-08-18 23:32:04 +00:00
|
|
|
if c == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
nc := *c
|
2016-02-11 01:54:43 +00:00
|
|
|
nc.Node = nc.Node.Copy()
|
2022-09-21 19:53:25 +00:00
|
|
|
nc.Servers = slices.Clone(nc.Servers)
|
|
|
|
nc.Options = maps.Clone(nc.Options)
|
2019-07-25 14:45:41 +00:00
|
|
|
nc.HostVolumes = structs.CopyMapStringClientHostVolumeConfig(nc.HostVolumes)
|
2016-08-09 22:00:50 +00:00
|
|
|
nc.ConsulConfig = c.ConsulConfig.Copy()
|
|
|
|
nc.VaultConfig = c.VaultConfig.Copy()
|
2019-08-16 14:03:31 +00:00
|
|
|
nc.TemplateConfig = c.TemplateConfig.Copy()
|
2022-08-18 23:32:04 +00:00
|
|
|
nc.ReservableCores = slices.Clone(c.ReservableCores)
|
2022-05-03 22:38:32 +00:00
|
|
|
nc.Artifact = c.Artifact.Copy()
|
2022-08-18 23:32:04 +00:00
|
|
|
return &nc
|
2016-02-10 21:44:53 +00:00
|
|
|
}
|
|
|
|
|
2016-05-31 18:58:02 +00:00
|
|
|
// DefaultConfig returns the default configuration
|
|
|
|
func DefaultConfig() *Config {
|
|
|
|
return &Config{
|
2019-08-02 19:20:14 +00:00
|
|
|
Version: version.GetVersion(),
|
2020-09-04 17:50:11 +00:00
|
|
|
VaultConfig: structsc.DefaultVaultConfig(),
|
|
|
|
ConsulConfig: structsc.DefaultConsulConfig(),
|
2019-08-02 19:20:14 +00:00
|
|
|
Region: "global",
|
|
|
|
StatsCollectionInterval: 1 * time.Second,
|
2020-09-04 17:50:11 +00:00
|
|
|
TLSConfig: &structsc.TLSConfig{},
|
2019-08-02 19:20:14 +00:00
|
|
|
GCInterval: 1 * time.Minute,
|
|
|
|
GCParallelDestroys: 2,
|
|
|
|
GCDiskUsageThreshold: 80,
|
|
|
|
GCInodeUsageThreshold: 70,
|
|
|
|
GCMaxAllocs: 50,
|
|
|
|
NoHostUUID: true,
|
|
|
|
DisableRemoteExec: false,
|
|
|
|
TemplateConfig: &ClientTemplateConfig{
|
2022-06-08 18:08:25 +00:00
|
|
|
FunctionDenylist: DefaultTemplateFunctionDenylist,
|
|
|
|
DisableSandbox: false,
|
2022-08-17 16:26:34 +00:00
|
|
|
BlockQueryWaitTime: pointer.Of(5 * time.Minute), // match Consul default
|
|
|
|
MaxStale: pointer.Of(DefaultTemplateMaxStale), // match Consul default
|
2022-06-08 18:08:25 +00:00
|
|
|
Wait: &WaitConfig{
|
2022-08-17 16:26:34 +00:00
|
|
|
Min: pointer.Of(5 * time.Second),
|
|
|
|
Max: pointer.Of(4 * time.Minute),
|
2022-06-08 18:08:25 +00:00
|
|
|
},
|
|
|
|
ConsulRetry: &RetryConfig{
|
2022-08-23 21:22:18 +00:00
|
|
|
Attempts: pointer.Of(0), // unlimited
|
2022-06-08 18:08:25 +00:00
|
|
|
},
|
|
|
|
VaultRetry: &RetryConfig{
|
2022-08-23 21:22:18 +00:00
|
|
|
Attempts: pointer.Of(0), // unlimited
|
2022-08-03 20:56:30 +00:00
|
|
|
},
|
|
|
|
NomadRetry: &RetryConfig{
|
2022-08-23 21:22:18 +00:00
|
|
|
Attempts: pointer.Of(0), // unlimited
|
2022-06-08 18:08:25 +00:00
|
|
|
},
|
2019-08-02 19:20:14 +00:00
|
|
|
},
|
2020-10-13 19:56:54 +00:00
|
|
|
RPCHoldTimeout: 5 * time.Second,
|
|
|
|
CNIPath: "/opt/cni/bin",
|
|
|
|
CNIConfigDir: "/opt/cni/config",
|
|
|
|
CNIInterfacePrefix: "eth",
|
|
|
|
HostNetworks: map[string]*structs.ClientHostNetworkConfig{},
|
client: enable support for cgroups v2
This PR introduces support for using Nomad on systems with cgroups v2 [1]
enabled as the cgroups controller mounted on /sys/fs/cgroups. Newer Linux
distros like Ubuntu 21.10 are shipping with cgroups v2 only, causing problems
for Nomad users.
Nomad mostly "just works" with cgroups v2 due to the indirection via libcontainer,
but not so for managing cpuset cgroups. Before, Nomad has been making use of
a feature in v1 where a PID could be a member of more than one cgroup. In v2
this is no longer possible, and so the logic around computing cpuset values
must be modified. When Nomad detects v2, it manages cpuset values in-process,
rather than making use of cgroup heirarchy inheritence via shared/reserved
parents.
Nomad will only activate the v2 logic when it detects cgroups2 is mounted at
/sys/fs/cgroups. This means on systems running in hybrid mode with cgroups2
mounted at /sys/fs/cgroups/unified (as is typical) Nomad will continue to
use the v1 logic, and should operate as before. Systems that do not support
cgroups v2 are also not affected.
When v2 is activated, Nomad will create a parent called nomad.slice (unless
otherwise configured in Client conifg), and create cgroups for tasks using
naming convention <allocID>-<task>.scope. These follow the naming convention
set by systemd and also used by Docker when cgroups v2 is detected.
Client nodes now export a new fingerprint attribute, unique.cgroups.version
which will be set to 'v1' or 'v2' to indicate the cgroups regime in use by
Nomad.
The new cpuset management strategy fixes #11705, where docker tasks that
spawned processes on startup would "leak". In cgroups v2, the PIDs are
started in the cgroup they will always live in, and thus the cause of
the leak is eliminated.
[1] https://www.kernel.org/doc/html/latest/admin-guide/cgroup-v2.html
Closes #11289
Fixes #11705 #11773 #11933
2022-02-28 22:24:01 +00:00
|
|
|
CgroupParent: cgutil.GetCgroupParent(""),
|
2021-10-04 22:43:35 +00:00
|
|
|
MaxDynamicPort: structs.DefaultMinDynamicPort,
|
|
|
|
MinDynamicPort: structs.DefaultMaxDynamicPort,
|
2016-05-31 18:58:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-01 02:54:49 +00:00
|
|
|
// Read returns the specified configuration value or "".
|
2015-09-01 02:48:59 +00:00
|
|
|
func (c *Config) Read(id string) string {
|
2016-03-24 17:55:14 +00:00
|
|
|
return c.Options[id]
|
2015-08-25 23:13:33 +00:00
|
|
|
}
|
2015-09-01 02:54:49 +00:00
|
|
|
|
|
|
|
// ReadDefault returns the specified configuration value, or the specified
|
|
|
|
// default value if none is set.
|
|
|
|
func (c *Config) ReadDefault(id string, defaultValue string) string {
|
2020-10-12 12:47:05 +00:00
|
|
|
return c.ReadAlternativeDefault([]string{id}, defaultValue)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadAlternativeDefault returns the specified configuration value, or the
|
|
|
|
// specified value if none is set.
|
|
|
|
func (c *Config) ReadAlternativeDefault(ids []string, defaultValue string) string {
|
|
|
|
for _, id := range ids {
|
|
|
|
val, ok := c.Options[id]
|
|
|
|
if ok {
|
|
|
|
return val
|
|
|
|
}
|
2015-09-01 02:54:49 +00:00
|
|
|
}
|
2020-10-12 12:47:05 +00:00
|
|
|
|
|
|
|
return defaultValue
|
2015-09-01 02:54:49 +00:00
|
|
|
}
|
2015-11-17 03:30:37 +00:00
|
|
|
|
|
|
|
// ReadBool parses the specified option as a boolean.
|
|
|
|
func (c *Config) ReadBool(id string) (bool, error) {
|
|
|
|
val, ok := c.Options[id]
|
|
|
|
if !ok {
|
2015-11-17 03:55:08 +00:00
|
|
|
return false, fmt.Errorf("Specified config is missing from options")
|
2015-11-17 03:30:37 +00:00
|
|
|
}
|
|
|
|
bval, err := strconv.ParseBool(val)
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("Failed to parse %s as bool: %s", val, err)
|
|
|
|
}
|
|
|
|
return bval, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadBoolDefault tries to parse the specified option as a boolean. If there is
|
|
|
|
// an error in parsing, the default option is returned.
|
|
|
|
func (c *Config) ReadBoolDefault(id string, defaultValue bool) bool {
|
2015-11-17 03:55:08 +00:00
|
|
|
val, err := c.ReadBool(id)
|
2015-11-17 03:30:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return defaultValue
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|
2015-11-24 15:18:49 +00:00
|
|
|
|
2017-03-14 19:56:31 +00:00
|
|
|
// ReadInt parses the specified option as a int.
|
|
|
|
func (c *Config) ReadInt(id string) (int, error) {
|
|
|
|
val, ok := c.Options[id]
|
|
|
|
if !ok {
|
|
|
|
return 0, fmt.Errorf("Specified config is missing from options")
|
|
|
|
}
|
|
|
|
ival, err := strconv.Atoi(val)
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("Failed to parse %s as int: %s", val, err)
|
|
|
|
}
|
|
|
|
return ival, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadIntDefault tries to parse the specified option as a int. If there is
|
|
|
|
// an error in parsing, the default option is returned.
|
|
|
|
func (c *Config) ReadIntDefault(id string, defaultValue int) int {
|
|
|
|
val, err := c.ReadInt(id)
|
|
|
|
if err != nil {
|
|
|
|
return defaultValue
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
2017-02-24 21:20:40 +00:00
|
|
|
// ReadDuration parses the specified option as a duration.
|
|
|
|
func (c *Config) ReadDuration(id string) (time.Duration, error) {
|
|
|
|
val, ok := c.Options[id]
|
|
|
|
if !ok {
|
|
|
|
return time.Duration(0), fmt.Errorf("Specified config is missing from options")
|
|
|
|
}
|
|
|
|
dval, err := time.ParseDuration(val)
|
|
|
|
if err != nil {
|
|
|
|
return time.Duration(0), fmt.Errorf("Failed to parse %s as time duration: %s", val, err)
|
|
|
|
}
|
|
|
|
return dval, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadDurationDefault tries to parse the specified option as a duration. If there is
|
|
|
|
// an error in parsing, the default option is returned.
|
|
|
|
func (c *Config) ReadDurationDefault(id string, defaultValue time.Duration) time.Duration {
|
|
|
|
val, err := c.ReadDuration(id)
|
|
|
|
if err != nil {
|
|
|
|
return defaultValue
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
|
2020-10-12 12:47:05 +00:00
|
|
|
// ReadStringListToMap tries to parse the specified option(s) as a comma separated list.
|
2015-11-24 15:18:49 +00:00
|
|
|
// If there is an error in parsing, an empty list is returned.
|
2020-10-12 12:47:05 +00:00
|
|
|
func (c *Config) ReadStringListToMap(keys ...string) map[string]struct{} {
|
|
|
|
val := c.ReadAlternativeDefault(keys, "")
|
|
|
|
|
|
|
|
return splitValue(val)
|
2015-11-24 15:18:49 +00:00
|
|
|
}
|
2016-03-24 17:55:14 +00:00
|
|
|
|
2021-08-30 09:08:12 +00:00
|
|
|
// ReadStringListToMapDefault tries to parse the specified option as a comma
|
|
|
|
// separated list. If there is an error in parsing, an empty list is returned.
|
2016-03-24 17:55:14 +00:00
|
|
|
func (c *Config) ReadStringListToMapDefault(key, defaultValue string) map[string]struct{} {
|
2020-10-12 12:47:05 +00:00
|
|
|
return c.ReadStringListAlternativeToMapDefault([]string{key}, defaultValue)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadStringListAlternativeToMapDefault tries to parse the specified options as a comma sparated list.
|
|
|
|
// If there is an error in parsing, an empty list is returned.
|
|
|
|
func (c *Config) ReadStringListAlternativeToMapDefault(keys []string, defaultValue string) map[string]struct{} {
|
|
|
|
val := c.ReadAlternativeDefault(keys, defaultValue)
|
|
|
|
|
|
|
|
return splitValue(val)
|
|
|
|
}
|
2016-03-24 17:55:14 +00:00
|
|
|
|
2020-10-12 12:47:05 +00:00
|
|
|
// splitValue parses the value as a comma separated list.
|
|
|
|
func splitValue(val string) map[string]struct{} {
|
2016-03-24 17:55:14 +00:00
|
|
|
list := make(map[string]struct{})
|
|
|
|
if val != "" {
|
|
|
|
for _, e := range strings.Split(val, ",") {
|
|
|
|
trimmed := strings.TrimSpace(e)
|
|
|
|
list[trimmed] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return list
|
|
|
|
}
|
2018-10-17 02:21:15 +00:00
|
|
|
|
|
|
|
// NomadPluginConfig produces the NomadConfig struct which is sent to Nomad plugins
|
2018-12-18 00:40:58 +00:00
|
|
|
func (c *Config) NomadPluginConfig() *base.AgentConfig {
|
|
|
|
return &base.AgentConfig{
|
2018-10-30 01:34:34 +00:00
|
|
|
Driver: &base.ClientDriverConfig{
|
2018-10-19 03:31:01 +00:00
|
|
|
ClientMinPort: c.ClientMinPort,
|
|
|
|
ClientMaxPort: c.ClientMaxPort,
|
|
|
|
},
|
2018-10-17 02:21:15 +00:00
|
|
|
}
|
|
|
|
}
|