2015-08-16 20:54:49 +00:00
package agent
2015-08-16 23:40:04 +00:00
import (
2020-03-25 14:38:40 +00:00
"context"
2015-08-24 00:40:27 +00:00
"fmt"
2019-02-22 19:40:13 +00:00
"io"
2018-10-16 22:34:32 +00:00
golog "log"
2015-08-31 01:10:23 +00:00
"net"
2016-10-17 17:48:04 +00:00
"os"
2015-08-31 01:10:23 +00:00
"path/filepath"
2016-03-10 07:25:31 +00:00
"runtime"
2016-06-15 00:13:15 +00:00
"strings"
2015-08-16 23:40:04 +00:00
"sync"
2015-10-29 13:47:06 +00:00
"time"
2015-08-16 21:34:38 +00:00
2017-09-04 03:50:05 +00:00
metrics "github.com/armon/go-metrics"
2020-09-04 17:50:11 +00:00
consulapi "github.com/hashicorp/consul/api"
2018-10-16 22:34:32 +00:00
log "github.com/hashicorp/go-hclog"
uuidparse "github.com/hashicorp/go-uuid"
2015-08-23 23:53:15 +00:00
"github.com/hashicorp/nomad/client"
2018-10-16 22:34:32 +00:00
clientconfig "github.com/hashicorp/nomad/client/config"
2022-03-29 00:33:01 +00:00
"github.com/hashicorp/nomad/client/lib/cgutil"
2019-01-09 16:16:33 +00:00
"github.com/hashicorp/nomad/client/state"
2016-06-08 06:02:37 +00:00
"github.com/hashicorp/nomad/command/agent/consul"
2020-03-22 16:17:33 +00:00
"github.com/hashicorp/nomad/command/agent/event"
2022-04-06 17:17:05 +00:00
"github.com/hashicorp/nomad/helper/bufconndialer"
2022-08-24 14:56:42 +00:00
"github.com/hashicorp/nomad/helper/escapingfs"
2019-01-23 14:27:14 +00:00
"github.com/hashicorp/nomad/helper/pluginutils/loader"
2018-04-10 00:51:55 +00:00
"github.com/hashicorp/nomad/helper/uuid"
2022-03-21 09:29:57 +00:00
"github.com/hashicorp/nomad/lib/cpuset"
2015-08-16 23:40:04 +00:00
"github.com/hashicorp/nomad/nomad"
2021-06-04 19:38:46 +00:00
"github.com/hashicorp/nomad/nomad/deploymentwatcher"
2015-08-31 01:10:23 +00:00
"github.com/hashicorp/nomad/nomad/structs"
2017-02-01 00:43:57 +00:00
"github.com/hashicorp/nomad/nomad/structs/config"
2017-11-22 00:29:11 +00:00
"github.com/hashicorp/raft"
2015-08-16 23:40:04 +00:00
)
2016-06-17 00:34:25 +00:00
const (
2017-09-26 22:26:33 +00:00
agentHttpCheckInterval = 10 * time . Second
agentHttpCheckTimeout = 5 * time . Second
2016-06-17 00:34:25 +00:00
serverRpcCheckInterval = 10 * time . Second
serverRpcCheckTimeout = 3 * time . Second
serverSerfCheckInterval = 10 * time . Second
serverSerfCheckTimeout = 3 * time . Second
2017-02-01 00:43:57 +00:00
// roles used in identifying Consul entries for Nomad agents
consulRoleServer = "server"
consulRoleClient = "client"
2020-05-30 13:27:33 +00:00
// DefaultRaftMultiplier is used as a baseline Raft configuration that
// will be reliable on a very basic server.
DefaultRaftMultiplier = 1
// MaxRaftMultiplier is a fairly arbitrary upper bound that limits the
// amount of performance detuning that's possible.
MaxRaftMultiplier = 10
2016-06-17 00:34:25 +00:00
)
2015-08-16 23:40:04 +00:00
// Agent is a long running daemon that is used to run both
// clients and servers. Servers are responsible for managing
// state and making scheduling decisions. Clients can be
// scheduled to, and are responsible for interfacing with
// servers to run allocations.
2015-08-16 20:54:49 +00:00
type Agent struct {
2017-11-15 01:53:23 +00:00
config * Config
configLock sync . Mutex
2019-10-15 19:14:25 +00:00
logger log . InterceptLogger
2020-03-24 00:06:11 +00:00
auditor event . Auditor
2019-10-15 19:14:25 +00:00
httpLogger log . Logger
2018-09-13 17:43:40 +00:00
logOutput io . Writer
2015-08-16 23:40:04 +00:00
2020-05-27 17:46:52 +00:00
// EnterpriseAgent holds information and methods for enterprise functionality
EnterpriseAgent * EnterpriseAgent
2017-02-01 00:43:57 +00:00
// consulService is Nomad's custom Consul client for managing services
// and checks.
consulService * consul . ServiceClient
2020-09-04 17:50:11 +00:00
// consulProxies is the subset of Consul's Agent API Nomad uses.
consulProxies * consul . ConnectProxies
2017-02-01 00:43:57 +00:00
// consulCatalog is the subset of Consul's Catalog API Nomad uses.
consulCatalog consul . CatalogAPI
2016-05-23 18:09:31 +00:00
2020-09-04 17:50:11 +00:00
// consulConfigEntries is the subset of Consul's Configuration Entries API Nomad uses.
2020-07-28 20:12:08 +00:00
consulConfigEntries consul . ConfigAPI
2019-12-06 20:46:46 +00:00
// consulACLs is Nomad's subset of Consul's ACL API Nomad uses.
consulACLs consul . ACLsAPI
2018-09-26 20:39:09 +00:00
// client is the launched Nomad Client. Can be nil if the agent isn't
// configured to run a client.
2016-11-04 22:24:28 +00:00
client * client . Client
2016-05-11 22:24:37 +00:00
2018-09-26 20:39:09 +00:00
// server is the launched Nomad Server. Can be nil if the agent isn't
// configured to run a server.
2016-11-04 22:24:28 +00:00
server * nomad . Server
2015-08-16 23:40:04 +00:00
2018-09-26 20:39:09 +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
2015-08-16 23:40:04 +00:00
shutdown bool
2016-05-27 10:47:49 +00:00
shutdownCh chan struct { }
2015-08-16 23:40:04 +00:00
shutdownLock sync . Mutex
2017-09-04 03:50:05 +00:00
2022-04-06 17:17:05 +00:00
// builtinDialer dials the builtinListener. It is used for connecting
// consul-template to the HTTP API in process. In the event this agent is
// not running in client mode, these two fields will be nil.
builtinListener net . Listener
builtinDialer * bufconndialer . BufConnWrapper
2023-02-23 20:03:44 +00:00
// taskAPIServer is an HTTP server for attaching per-task listeners. Always
2023-02-06 19:31:22 +00:00
// requires auth.
2023-02-23 20:03:44 +00:00
taskAPIServer * builtinAPI
2023-02-06 19:31:22 +00:00
inmemSink * metrics . InmemSink
2015-08-16 20:54:49 +00:00
}
2015-08-16 23:40:04 +00:00
// NewAgent is used to create a new agent with the given configuration
2019-10-15 19:14:25 +00:00
func NewAgent ( config * Config , logger log . InterceptLogger , logOutput io . Writer , inmem * metrics . InmemSink ) ( * Agent , error ) {
2015-08-16 23:40:04 +00:00
a := & Agent {
config : config ,
logOutput : logOutput ,
2016-06-17 06:15:41 +00:00
shutdownCh : make ( chan struct { } ) ,
2023-02-06 19:31:22 +00:00
inmemSink : inmem ,
2015-08-16 23:40:04 +00:00
}
2015-08-24 00:40:27 +00:00
2018-09-13 17:43:40 +00:00
// Create the loggers
2019-02-21 18:41:54 +00:00
a . logger = logger
2019-10-15 19:14:25 +00:00
a . httpLogger = a . logger . ResetNamed ( "http" )
2018-09-13 17:43:40 +00:00
2018-06-06 18:40:08 +00:00
// Global logger should match internal logger as much as possible
2018-09-13 17:43:40 +00:00
golog . SetFlags ( golog . LstdFlags | golog . Lmicroseconds )
2018-06-06 18:40:08 +00:00
2017-02-01 00:43:57 +00:00
if err := a . setupConsul ( config . Consul ) ; err != nil {
return nil , fmt . Errorf ( "Failed to initialize Consul client: %v" , err )
2016-05-23 18:09:31 +00:00
}
2018-08-30 20:43:09 +00:00
2015-08-24 00:40:27 +00:00
if err := a . setupServer ( ) ; err != nil {
return nil , err
}
if err := a . setupClient ( ) ; err != nil {
return nil , err
}
2020-05-27 17:46:52 +00:00
2020-03-22 16:17:33 +00:00
if err := a . setupEnterpriseAgent ( logger ) ; err != nil {
return nil , err
}
2015-08-24 00:40:27 +00:00
if a . client == nil && a . server == nil {
return nil , fmt . Errorf ( "must have at least client or server mode enabled" )
}
2016-06-08 06:31:19 +00:00
2015-08-16 21:34:38 +00:00
return a , nil
}
2017-02-01 22:20:14 +00:00
// convertServerConfig takes an agent config and log output and returns a Nomad
2018-09-27 01:14:36 +00:00
// Config. There may be missing fields that must be set by the agent. To do this
// call finalizeServerConfig
func convertServerConfig ( agentConfig * Config ) ( * nomad . Config , error ) {
2017-02-01 22:20:14 +00:00
conf := agentConfig . NomadConfig
2015-09-06 01:41:00 +00:00
if conf == nil {
conf = nomad . DefaultConfig ( )
}
2017-02-01 22:20:14 +00:00
conf . DevMode = agentConfig . DevMode
2019-12-13 18:41:55 +00:00
conf . EnableDebug = agentConfig . EnableDebug
2017-08-16 22:42:15 +00:00
conf . Build = agentConfig . Version . VersionNumber ( )
2023-02-27 17:27:40 +00:00
conf . BuildDate = agentConfig . Version . BuildDate
2022-11-07 15:34:33 +00:00
conf . Revision = agentConfig . Version . Revision
2017-02-01 22:20:14 +00:00
if agentConfig . Region != "" {
conf . Region = agentConfig . Region
2015-08-31 01:14:40 +00:00
}
2017-08-13 21:11:06 +00:00
// Set the Authoritative Region if set, otherwise default to
// the same as the local region.
if agentConfig . Server . AuthoritativeRegion != "" {
conf . AuthoritativeRegion = agentConfig . Server . AuthoritativeRegion
} else if agentConfig . Region != "" {
conf . AuthoritativeRegion = agentConfig . Region
}
2017-02-01 22:20:14 +00:00
if agentConfig . Datacenter != "" {
conf . Datacenter = agentConfig . Datacenter
2015-08-31 01:14:40 +00:00
}
2017-02-01 22:20:14 +00:00
if agentConfig . NodeName != "" {
conf . NodeName = agentConfig . NodeName
2015-08-31 01:14:40 +00:00
}
2017-02-01 22:20:14 +00:00
if agentConfig . Server . BootstrapExpect > 0 {
2020-03-02 15:29:24 +00:00
conf . BootstrapExpect = agentConfig . Server . BootstrapExpect
2015-08-31 01:10:23 +00:00
}
2017-02-01 22:20:14 +00:00
if agentConfig . DataDir != "" {
conf . DataDir = filepath . Join ( agentConfig . DataDir , "server" )
2015-08-31 01:10:23 +00:00
}
2017-02-01 22:20:14 +00:00
if agentConfig . Server . DataDir != "" {
conf . DataDir = agentConfig . Server . DataDir
2015-08-31 01:10:23 +00:00
}
2017-11-22 00:29:11 +00:00
if agentConfig . Server . RaftProtocol != 0 {
conf . RaftConfig . ProtocolVersion = raft . ProtocolVersion ( agentConfig . Server . RaftProtocol )
}
2022-06-23 12:28:45 +00:00
if v := conf . RaftConfig . ProtocolVersion ; v != 3 {
return nil , fmt . Errorf ( "raft_protocol must be 3 in Nomad v1.4 and later, got %d" , v )
}
2020-05-30 13:27:33 +00:00
raftMultiplier := int ( DefaultRaftMultiplier )
if agentConfig . Server . RaftMultiplier != nil && * agentConfig . Server . RaftMultiplier != 0 {
raftMultiplier = * agentConfig . Server . RaftMultiplier
if raftMultiplier < 1 || raftMultiplier > MaxRaftMultiplier {
return nil , fmt . Errorf ( "raft_multiplier cannot be %d. Must be between 1 and %d" , * agentConfig . Server . RaftMultiplier , MaxRaftMultiplier )
}
}
2023-01-20 19:21:51 +00:00
if vPtr := agentConfig . Server . RaftTrailingLogs ; vPtr != nil {
if * vPtr < 1 {
return nil , fmt . Errorf ( "raft_trailing_logs must be non-negative, got %d" , * vPtr )
}
conf . RaftConfig . TrailingLogs = uint64 ( * vPtr )
}
if vPtr := agentConfig . Server . RaftSnapshotInterval ; vPtr != nil {
dur , err := time . ParseDuration ( * vPtr )
if err != nil {
return nil , err
}
if dur < 5 * time . Millisecond {
return nil , fmt . Errorf ( "raft_snapshot_interval must be greater than 5ms, got %q" , * vPtr )
}
conf . RaftConfig . SnapshotInterval = dur
}
if vPtr := agentConfig . Server . RaftSnapshotThreshold ; vPtr != nil {
if * vPtr < 1 {
return nil , fmt . Errorf ( "raft_snapshot_threshold must be non-negative, got %d" , * vPtr )
}
conf . RaftConfig . SnapshotThreshold = uint64 ( * vPtr )
}
2020-05-30 13:27:33 +00:00
conf . RaftConfig . ElectionTimeout *= time . Duration ( raftMultiplier )
conf . RaftConfig . HeartbeatTimeout *= time . Duration ( raftMultiplier )
conf . RaftConfig . LeaderLeaseTimeout *= time . Duration ( raftMultiplier )
conf . RaftConfig . CommitTimeout *= time . Duration ( raftMultiplier )
2018-05-31 20:11:11 +00:00
if agentConfig . Server . NumSchedulers != nil {
conf . NumSchedulers = * agentConfig . Server . NumSchedulers
2015-08-31 01:10:23 +00:00
}
2017-02-01 22:20:14 +00:00
if len ( agentConfig . Server . EnabledSchedulers ) != 0 {
2018-03-14 17:53:27 +00:00
// Convert to a set and require the core scheduler
set := make ( map [ string ] struct { } , 4 )
set [ structs . JobTypeCore ] = struct { } { }
for _ , sched := range agentConfig . Server . EnabledSchedulers {
set [ sched ] = struct { } { }
}
schedulers := make ( [ ] string , 0 , len ( set ) )
for k := range set {
schedulers = append ( schedulers , k )
}
conf . EnabledSchedulers = schedulers
2015-08-31 01:10:23 +00:00
}
2017-08-13 21:11:06 +00:00
if agentConfig . ACL . Enabled {
conf . ACLEnabled = true
}
2017-08-21 03:51:30 +00:00
if agentConfig . ACL . ReplicationToken != "" {
conf . ReplicationToken = agentConfig . ACL . ReplicationToken
}
2022-07-12 11:43:25 +00:00
if agentConfig . ACL . TokenMinExpirationTTL != 0 {
conf . ACLTokenMinExpirationTTL = agentConfig . ACL . TokenMinExpirationTTL
}
if agentConfig . ACL . TokenMaxExpirationTTL != 0 {
conf . ACLTokenMaxExpirationTTL = agentConfig . ACL . TokenMaxExpirationTTL
}
2017-09-19 14:47:10 +00:00
if agentConfig . Sentinel != nil {
conf . SentinelConfig = agentConfig . Sentinel
}
2017-12-18 21:16:23 +00:00
if agentConfig . Server . NonVotingServer {
conf . NonVoter = true
}
2018-01-30 03:53:34 +00:00
if agentConfig . Server . RedundancyZone != "" {
conf . RedundancyZone = agentConfig . Server . RedundancyZone
}
if agentConfig . Server . UpgradeVersion != "" {
conf . UpgradeVersion = agentConfig . Server . UpgradeVersion
}
2020-10-08 18:27:52 +00:00
if agentConfig . Server . EnableEventBroker != nil {
conf . EnableEventBroker = * agentConfig . Server . EnableEventBroker
2020-10-06 20:21:58 +00:00
}
2020-10-08 18:27:52 +00:00
if agentConfig . Server . EventBufferSize != nil {
2020-10-15 20:34:33 +00:00
if * agentConfig . Server . EventBufferSize < 0 {
return nil , fmt . Errorf ( "Invalid Config, event_buffer_size must be non-negative" )
}
2020-10-08 18:27:52 +00:00
conf . EventBufferSize = int64 ( * agentConfig . Server . EventBufferSize )
}
2017-12-18 21:16:23 +00:00
if agentConfig . Autopilot != nil {
if agentConfig . Autopilot . CleanupDeadServers != nil {
conf . AutopilotConfig . CleanupDeadServers = * agentConfig . Autopilot . CleanupDeadServers
}
if agentConfig . Autopilot . ServerStabilizationTime != 0 {
conf . AutopilotConfig . ServerStabilizationTime = agentConfig . Autopilot . ServerStabilizationTime
}
if agentConfig . Autopilot . LastContactThreshold != 0 {
conf . AutopilotConfig . LastContactThreshold = agentConfig . Autopilot . LastContactThreshold
}
if agentConfig . Autopilot . MaxTrailingLogs != 0 {
conf . AutopilotConfig . MaxTrailingLogs = uint64 ( agentConfig . Autopilot . MaxTrailingLogs )
}
2020-02-16 21:23:20 +00:00
if agentConfig . Autopilot . MinQuorum != 0 {
conf . AutopilotConfig . MinQuorum = uint ( agentConfig . Autopilot . MinQuorum )
}
2018-01-30 03:53:34 +00:00
if agentConfig . Autopilot . EnableRedundancyZones != nil {
conf . AutopilotConfig . EnableRedundancyZones = * agentConfig . Autopilot . EnableRedundancyZones
2017-12-18 21:16:23 +00:00
}
if agentConfig . Autopilot . DisableUpgradeMigration != nil {
conf . AutopilotConfig . DisableUpgradeMigration = * agentConfig . Autopilot . DisableUpgradeMigration
}
2018-01-30 03:53:34 +00:00
if agentConfig . Autopilot . EnableCustomUpgrades != nil {
conf . AutopilotConfig . EnableCustomUpgrades = * agentConfig . Autopilot . EnableCustomUpgrades
2017-12-18 21:16:23 +00:00
}
}
2015-09-11 01:37:42 +00:00
2023-02-17 14:23:13 +00:00
jobMaxPriority := structs . JobDefaultMaxPriority
if agentConfig . Server . JobMaxPriority != nil && * agentConfig . Server . JobMaxPriority != 0 {
jobMaxPriority = * agentConfig . Server . JobMaxPriority
if jobMaxPriority < structs . JobDefaultMaxPriority || jobMaxPriority > structs . JobMaxPriority {
return nil , fmt . Errorf ( "job_max_priority cannot be %d. Must be between %d and %d" , * agentConfig . Server . JobMaxPriority , structs . JobDefaultMaxPriority , structs . JobMaxPriority )
}
}
jobDefaultPriority := structs . JobDefaultPriority
if agentConfig . Server . JobDefaultPriority != nil && * agentConfig . Server . JobDefaultPriority != 0 {
jobDefaultPriority = * agentConfig . Server . JobDefaultPriority
if jobDefaultPriority < structs . JobDefaultPriority || jobDefaultPriority >= jobMaxPriority {
return nil , fmt . Errorf ( "job_default_priority cannot be %d. Must be between %d and %d" , * agentConfig . Server . JobDefaultPriority , structs . JobDefaultPriority , jobMaxPriority )
}
}
conf . JobMaxPriority = jobMaxPriority
conf . JobDefaultPriority = jobDefaultPriority
2015-09-11 01:37:42 +00:00
// Set up the bind addresses
2017-02-01 22:20:14 +00:00
rpcAddr , err := net . ResolveTCPAddr ( "tcp" , agentConfig . normalizedAddrs . RPC )
2016-09-02 23:23:45 +00:00
if err != nil {
2017-02-01 22:20:14 +00:00
return nil , fmt . Errorf ( "Failed to parse RPC address %q: %v" , agentConfig . normalizedAddrs . RPC , err )
2016-05-27 08:35:10 +00:00
}
2017-02-01 22:20:14 +00:00
serfAddr , err := net . ResolveTCPAddr ( "tcp" , agentConfig . normalizedAddrs . Serf )
2016-05-27 08:35:10 +00:00
if err != nil {
2017-02-01 22:20:14 +00:00
return nil , fmt . Errorf ( "Failed to parse Serf address %q: %v" , agentConfig . normalizedAddrs . Serf , err )
2016-05-27 08:35:10 +00:00
}
2016-09-02 23:23:45 +00:00
conf . RPCAddr . Port = rpcAddr . Port
conf . RPCAddr . IP = rpcAddr . IP
conf . SerfConfig . MemberlistConfig . BindPort = serfAddr . Port
conf . SerfConfig . MemberlistConfig . BindAddr = serfAddr . IP . String ( )
2023-01-11 21:39:24 +00:00
conf . SerfConfig . RejoinAfterLeave = agentConfig . Server . RejoinAfterLeave
2016-05-27 08:35:10 +00:00
2016-09-02 23:23:45 +00:00
// Set up the advertise addresses
2017-02-01 22:20:14 +00:00
rpcAddr , err = net . ResolveTCPAddr ( "tcp" , agentConfig . AdvertiseAddrs . RPC )
2016-05-27 07:42:31 +00:00
if err != nil {
2017-02-01 22:20:14 +00:00
return nil , fmt . Errorf ( "Failed to parse RPC advertise address %q: %v" , agentConfig . AdvertiseAddrs . RPC , err )
2016-05-27 07:42:31 +00:00
}
2017-02-01 22:20:14 +00:00
serfAddr , err = net . ResolveTCPAddr ( "tcp" , agentConfig . AdvertiseAddrs . Serf )
2016-09-02 23:23:45 +00:00
if err != nil {
2017-02-01 22:20:14 +00:00
return nil , fmt . Errorf ( "Failed to parse Serf advertise address %q: %v" , agentConfig . AdvertiseAddrs . Serf , err )
2016-06-01 08:08:15 +00:00
}
2018-03-16 22:24:49 +00:00
// Server address is the serf advertise address and rpc port. This is the
// address that all servers should be able to communicate over RPC with.
serverAddr , err := net . ResolveTCPAddr ( "tcp" , net . JoinHostPort ( serfAddr . IP . String ( ) , fmt . Sprintf ( "%d" , rpcAddr . Port ) ) )
if err != nil {
2018-03-16 22:53:14 +00:00
return nil , fmt . Errorf ( "Failed to resolve Serf advertise address %q: %v" , agentConfig . AdvertiseAddrs . Serf , err )
2018-03-16 22:24:49 +00:00
}
2016-09-10 17:53:53 +00:00
conf . SerfConfig . MemberlistConfig . AdvertiseAddr = serfAddr . IP . String ( )
conf . SerfConfig . MemberlistConfig . AdvertisePort = serfAddr . Port
2018-03-16 22:24:49 +00:00
conf . ClientRPCAdvertise = rpcAddr
conf . ServerRPCAdvertise = serverAddr
2016-06-01 08:08:15 +00:00
2016-09-02 23:23:45 +00:00
// Set up gc threshold and heartbeat grace period
2017-02-01 22:20:14 +00:00
if gcThreshold := agentConfig . Server . NodeGCThreshold ; gcThreshold != "" {
2015-10-29 13:47:06 +00:00
dur , err := time . ParseDuration ( gcThreshold )
if err != nil {
return nil , err
}
conf . NodeGCThreshold = dur
}
2019-07-18 12:57:47 +00:00
if gcInterval := agentConfig . Server . JobGCInterval ; gcInterval != "" {
dur , err := time . ParseDuration ( gcInterval )
if err != nil {
2019-09-05 18:20:46 +00:00
return nil , fmt . Errorf ( "failed to parse job_gc_interval: %v" , err )
} else if dur <= time . Duration ( 0 ) {
return nil , fmt . Errorf ( "job_gc_interval should be greater than 0s" )
2019-07-18 12:57:47 +00:00
}
conf . JobGCInterval = dur
}
2017-02-27 19:58:10 +00:00
if gcThreshold := agentConfig . Server . JobGCThreshold ; gcThreshold != "" {
dur , err := time . ParseDuration ( gcThreshold )
if err != nil {
return nil , err
}
conf . JobGCThreshold = dur
}
if gcThreshold := agentConfig . Server . EvalGCThreshold ; gcThreshold != "" {
dur , err := time . ParseDuration ( gcThreshold )
if err != nil {
return nil , err
}
conf . EvalGCThreshold = dur
}
2023-01-31 18:32:14 +00:00
if gcThreshold := agentConfig . Server . BatchEvalGCThreshold ; gcThreshold != "" {
dur , err := time . ParseDuration ( gcThreshold )
if err != nil {
return nil , err
}
conf . BatchEvalGCThreshold = dur
}
2017-06-29 18:29:44 +00:00
if gcThreshold := agentConfig . Server . DeploymentGCThreshold ; gcThreshold != "" {
dur , err := time . ParseDuration ( gcThreshold )
if err != nil {
return nil , err
}
conf . DeploymentGCThreshold = dur
}
2023-02-16 15:41:15 +00:00
if gcInterval := agentConfig . Server . CSIVolumeClaimGCInterval ; gcInterval != "" {
dur , err := time . ParseDuration ( gcInterval )
if err != nil {
return nil , err
} else if dur <= time . Duration ( 0 ) {
return nil , fmt . Errorf ( "csi_volume_claim_gc_interval should be greater than 0s" )
}
conf . CSIVolumeClaimGCInterval = dur
}
2020-05-11 12:20:50 +00:00
if gcThreshold := agentConfig . Server . CSIVolumeClaimGCThreshold ; gcThreshold != "" {
dur , err := time . ParseDuration ( gcThreshold )
if err != nil {
return nil , err
}
conf . CSIVolumeClaimGCThreshold = dur
}
2020-05-06 20:49:12 +00:00
if gcThreshold := agentConfig . Server . CSIPluginGCThreshold ; gcThreshold != "" {
dur , err := time . ParseDuration ( gcThreshold )
if err != nil {
return nil , err
}
conf . CSIPluginGCThreshold = dur
}
2022-07-12 11:43:25 +00:00
if gcThreshold := agentConfig . Server . ACLTokenGCThreshold ; gcThreshold != "" {
dur , err := time . ParseDuration ( gcThreshold )
if err != nil {
return nil , err
}
conf . ACLTokenExpirationGCThreshold = dur
}
2022-10-24 12:43:42 +00:00
if gcThreshold := agentConfig . Server . RootKeyGCThreshold ; gcThreshold != "" {
dur , err := time . ParseDuration ( gcThreshold )
if err != nil {
return nil , err
}
conf . RootKeyGCThreshold = dur
}
if gcInterval := agentConfig . Server . RootKeyGCInterval ; gcInterval != "" {
dur , err := time . ParseDuration ( gcInterval )
if err != nil {
return nil , err
}
conf . RootKeyGCInterval = dur
}
if rotationThreshold := agentConfig . Server . RootKeyRotationThreshold ; rotationThreshold != "" {
dur , err := time . ParseDuration ( rotationThreshold )
if err != nil {
return nil , err
}
conf . RootKeyRotationThreshold = dur
}
2015-10-29 13:47:06 +00:00
2017-07-19 16:38:35 +00:00
if heartbeatGrace := agentConfig . Server . HeartbeatGrace ; heartbeatGrace != 0 {
conf . HeartbeatGrace = heartbeatGrace
}
if min := agentConfig . Server . MinHeartbeatTTL ; min != 0 {
conf . MinHeartbeatTTL = min
}
if maxHPS := agentConfig . Server . MaxHeartbeatsPerSecond ; maxHPS != 0 {
conf . MaxHeartbeatsPerSecond = maxHPS
2016-03-04 23:44:12 +00:00
}
2021-10-06 22:48:12 +00:00
if failoverTTL := agentConfig . Server . FailoverHeartbeatTTL ; failoverTTL != 0 {
conf . FailoverHeartbeatTTL = failoverTTL
}
2016-03-04 23:44:12 +00:00
2017-02-01 22:20:14 +00:00
if * agentConfig . Consul . AutoAdvertise && agentConfig . Consul . ServerServiceName == "" {
2016-06-14 19:11:38 +00:00
return nil , fmt . Errorf ( "server_service_name must be set when auto_advertise is enabled" )
}
2020-01-12 20:50:18 +00:00
// handle system scheduler preemption default
2020-01-28 16:09:36 +00:00
if agentConfig . Server . DefaultSchedulerConfig != nil {
conf . DefaultSchedulerConfig = * agentConfig . Server . DefaultSchedulerConfig
2020-01-12 20:50:18 +00:00
}
2016-08-09 21:35:40 +00:00
// Add the Consul and Vault configs
2017-02-01 22:20:14 +00:00
conf . ConsulConfig = agentConfig . Consul
conf . VaultConfig = agentConfig . Vault
2016-06-14 19:11:38 +00:00
2016-10-25 22:57:38 +00:00
// Set the TLS config
2017-02-01 22:20:14 +00:00
conf . TLSConfig = agentConfig . TLSConfig
2016-10-24 05:22:00 +00:00
2017-10-30 19:19:11 +00:00
// Setup telemetry related config
conf . StatsCollectionInterval = agentConfig . Telemetry . collectionInterval
2018-11-14 16:13:52 +00:00
conf . DisableDispatchedJobSummaryMetrics = agentConfig . Telemetry . DisableDispatchedJobSummaryMetrics
2023-01-24 16:54:20 +00:00
conf . DisableRPCRateMetricsLabels = agentConfig . Telemetry . DisableRPCRateMetricsLabels
2017-10-30 19:19:11 +00:00
2020-01-15 15:41:59 +00:00
if d , err := time . ParseDuration ( agentConfig . Limits . RPCHandshakeTimeout ) ; err != nil {
return nil , fmt . Errorf ( "error parsing rpc_handshake_timeout: %v" , err )
} else if d < 0 {
return nil , fmt . Errorf ( "rpc_handshake_timeout must be >= 0" )
} else {
conf . RPCHandshakeTimeout = d
}
// Set max rpc conns; nil/0 == unlimited
// Leave a little room for streaming RPCs
minLimit := config . LimitsNonStreamingConnsPerClient + 5
if agentConfig . Limits . RPCMaxConnsPerClient == nil || * agentConfig . Limits . RPCMaxConnsPerClient == 0 {
conf . RPCMaxConnsPerClient = 0
} else if limit := * agentConfig . Limits . RPCMaxConnsPerClient ; limit <= minLimit {
return nil , fmt . Errorf ( "rpc_max_conns_per_client must be > %d; found: %d" , minLimit , limit )
} else {
conf . RPCMaxConnsPerClient = limit
}
2021-06-04 19:38:46 +00:00
// Set deployment rate limit
if rate := agentConfig . Server . DeploymentQueryRateLimit ; rate == 0 {
conf . DeploymentQueryRateLimit = deploymentwatcher . LimitStateQueriesPerSecond
} else if rate > 0 {
conf . DeploymentQueryRateLimit = rate
} else {
return nil , fmt . Errorf ( "deploy_query_rate_limit must be greater than 0" )
}
2022-07-12 22:40:20 +00:00
// Set plan rejection tracker configuration.
if planRejectConf := agentConfig . Server . PlanRejectionTracker ; planRejectConf != nil {
if planRejectConf . Enabled != nil {
conf . NodePlanRejectionEnabled = * planRejectConf . Enabled
}
conf . NodePlanRejectionThreshold = planRejectConf . NodeThreshold
if planRejectConf . NodeWindow == 0 {
return nil , fmt . Errorf ( "plan_rejection_tracker.node_window must be greater than 0" )
} else {
conf . NodePlanRejectionWindow = planRejectConf . NodeWindow
}
}
2021-03-23 13:08:14 +00:00
// Add Enterprise license configs
conf . LicenseEnv = agentConfig . Server . LicenseEnv
conf . LicensePath = agentConfig . Server . LicensePath
2021-04-27 20:50:07 +00:00
conf . LicenseConfig . AdditionalPubKeys = agentConfig . Server . licenseAdditionalPublicKeys
2021-03-23 13:08:14 +00:00
api: implement fuzzy search API
This PR introduces the /v1/search/fuzzy API endpoint, used for fuzzy
searching objects in Nomad. The fuzzy search endpoint routes requests
to the Nomad Server leader, which implements the Search.FuzzySearch RPC
method.
Requests to the fuzzy search API are based on the api.FuzzySearchRequest
object, e.g.
{
"Text": "ed",
"Context": "all"
}
Responses from the fuzzy search API are based on the api.FuzzySearchResponse
object, e.g.
{
"Index": 27,
"KnownLeader": true,
"LastContact": 0,
"Matches": {
"tasks": [
{
"ID": "redis",
"Scope": [
"default",
"example",
"cache"
]
}
],
"evals": [],
"deployment": [],
"volumes": [],
"scaling_policy": [],
"images": [
{
"ID": "redis:3.2",
"Scope": [
"default",
"example",
"cache",
"redis"
]
}
]
},
"Truncations": {
"volumes": false,
"scaling_policy": false,
"evals": false,
"deployment": false
}
}
The API is tunable using the new server.search stanza, e.g.
server {
search {
fuzzy_enabled = true
limit_query = 200
limit_results = 1000
min_term_length = 5
}
}
These values can be increased or decreased, so as to provide more
search results or to reduce load on the Nomad Server. The fuzzy search
API can be disabled entirely by setting `fuzzy_enabled` to `false`.
2021-02-23 20:24:52 +00:00
// Add the search configuration
if search := agentConfig . Server . Search ; search != nil {
conf . SearchConfig = & structs . SearchConfig {
FuzzyEnabled : search . FuzzyEnabled ,
LimitQuery : search . LimitQuery ,
LimitResults : search . LimitResults ,
MinTermLength : search . MinTermLength ,
}
}
2022-02-16 16:30:03 +00:00
// Set the raft bolt parameters
if bolt := agentConfig . Server . RaftBoltConfig ; bolt != nil {
conf . RaftBoltNoFreelistSync = bolt . NoFreelistSync
}
2015-09-11 19:02:22 +00:00
return conf , nil
}
2017-02-01 22:20:14 +00:00
// serverConfig is used to generate a new server configuration struct
// for initializing a nomad server.
func ( a * Agent ) serverConfig ( ) ( * nomad . Config , error ) {
2018-09-27 01:14:36 +00:00
c , err := convertServerConfig ( a . config )
if err != nil {
return nil , err
}
a . finalizeServerConfig ( c )
return c , nil
}
// finalizeServerConfig sets configuration fields on the server config that are
2023-01-20 19:21:51 +00:00
// not statically convertible and are from the agent.
2018-09-27 01:14:36 +00:00
func ( a * Agent ) finalizeServerConfig ( c * nomad . Config ) {
// Setup the logging
c . Logger = a . logger
c . LogOutput = a . logOutput
2020-07-17 15:04:57 +00:00
c . AgentShutdown = func ( ) error { return a . Shutdown ( ) }
2017-02-01 22:20:14 +00:00
}
2018-09-27 01:14:36 +00:00
// clientConfig is used to generate a new client configuration struct for
// initializing a Nomad client.
2016-02-16 21:42:48 +00:00
func ( a * Agent ) clientConfig ( ) ( * clientconfig . Config , error ) {
2018-09-27 01:14:36 +00:00
c , err := convertClientConfig ( a . config )
if err != nil {
return nil , err
2015-09-06 01:41:00 +00:00
}
2018-05-21 23:33:16 +00:00
2018-09-27 01:14:36 +00:00
if err := a . finalizeClientConfig ( c ) ; err != nil {
return nil , err
}
return c , nil
}
// finalizeClientConfig sets configuration fields on the client config that are
2023-01-20 19:21:51 +00:00
// not statically convertible and are from the agent.
2018-09-27 01:14:36 +00:00
func ( a * Agent ) finalizeClientConfig ( c * clientconfig . Config ) error {
// Setup the logging
c . Logger = a . logger
2018-05-21 23:33:16 +00:00
// If we are running a server, append both its bind and advertise address so
// we are able to at least talk to the local server even if that isn't
// configured explicitly. This handles both running server and client on one
// host and -dev mode.
2015-08-24 00:40:27 +00:00
if a . server != nil {
2020-04-07 13:27:48 +00:00
advertised := a . config . AdvertiseAddrs
normalized := a . config . normalizedAddrs
if advertised == nil || advertised . RPC == "" {
2018-09-27 01:14:36 +00:00
return fmt . Errorf ( "AdvertiseAddrs is nil or empty" )
2020-04-07 13:27:48 +00:00
} else if normalized == nil || normalized . RPC == "" {
2018-09-27 01:14:36 +00:00
return fmt . Errorf ( "normalizedAddrs is nil or empty" )
2018-05-22 21:45:15 +00:00
}
2020-04-07 13:27:48 +00:00
if normalized . RPC == advertised . RPC {
c . Servers = append ( c . Servers , normalized . RPC )
} else {
c . Servers = append ( c . Servers , normalized . RPC , advertised . RPC )
}
2015-08-24 00:40:27 +00:00
}
2018-05-21 23:33:16 +00:00
2018-09-27 01:14:36 +00:00
// Setup the plugin loaders
c . PluginLoader = a . pluginLoader
c . PluginSingletonLoader = a . pluginSingletonLoader
// Log deprecation messages about Consul related configuration in client
2016-06-15 00:13:15 +00:00
// options
var invalidConsulKeys [ ] string
2018-09-27 01:14:36 +00:00
for key := range c . Options {
2016-06-15 00:13:15 +00:00
if strings . HasPrefix ( key , "consul" ) {
invalidConsulKeys = append ( invalidConsulKeys , fmt . Sprintf ( "options.%s" , key ) )
}
}
if len ( invalidConsulKeys ) > 0 {
2018-09-13 17:43:40 +00:00
a . logger . Warn ( "invalid consul keys" , "keys" , strings . Join ( invalidConsulKeys , "," ) )
a . logger . Warn ( ` Nomad client ignores consul related configuration in client options .
2016-11-01 12:53:13 +00:00
Please refer to the guide https : //www.nomadproject.io/docs/agent/configuration/consul.html
2016-06-15 00:13:15 +00:00
to configure Nomad to work with Consul . ` )
}
2018-09-27 01:14:36 +00:00
return nil
}
// convertClientConfig takes an agent config and log output and returns a client
// Config. There may be missing fields that must be set by the agent. To do this
// call finalizeServerConfig
func convertClientConfig ( agentConfig * Config ) ( * clientconfig . Config , error ) {
2022-01-10 15:19:07 +00:00
// Set up the configuration
2018-09-27 01:14:36 +00:00
conf := agentConfig . ClientConfig
if conf == nil {
conf = clientconfig . DefaultConfig ( )
}
conf . Servers = agentConfig . Client . Servers
conf . DevMode = agentConfig . DevMode
2019-12-13 18:41:55 +00:00
conf . EnableDebug = agentConfig . EnableDebug
2018-09-27 01:14:36 +00:00
if agentConfig . Region != "" {
conf . Region = agentConfig . Region
2015-10-03 00:32:11 +00:00
}
2018-09-27 01:14:36 +00:00
if agentConfig . DataDir != "" {
conf . StateDir = filepath . Join ( agentConfig . DataDir , "client" )
conf . AllocDir = filepath . Join ( agentConfig . DataDir , "alloc" )
2017-03-14 19:56:31 +00:00
}
2018-09-27 01:14:36 +00:00
if agentConfig . Client . StateDir != "" {
conf . StateDir = agentConfig . Client . StateDir
2018-01-22 20:28:29 +00:00
}
2018-09-27 01:14:36 +00:00
if agentConfig . Client . AllocDir != "" {
conf . AllocDir = agentConfig . Client . AllocDir
}
if agentConfig . Client . NetworkInterface != "" {
conf . NetworkInterface = agentConfig . Client . NetworkInterface
}
conf . ChrootEnv = agentConfig . Client . ChrootEnv
conf . Options = agentConfig . Client . Options
if agentConfig . Client . NetworkSpeed != 0 {
conf . NetworkSpeed = agentConfig . Client . NetworkSpeed
}
if agentConfig . Client . CpuCompute != 0 {
conf . CpuCompute = agentConfig . Client . CpuCompute
}
if agentConfig . Client . MemoryMB != 0 {
conf . MemoryMB = agentConfig . Client . MemoryMB
}
2023-01-24 14:14:22 +00:00
if agentConfig . Client . DiskTotalMB != 0 {
conf . DiskTotalMB = agentConfig . Client . DiskTotalMB
}
if agentConfig . Client . DiskFreeMB != 0 {
conf . DiskFreeMB = agentConfig . Client . DiskFreeMB
}
2018-09-27 01:14:36 +00:00
if agentConfig . Client . MaxKillTimeout != "" {
dur , err := time . ParseDuration ( agentConfig . Client . MaxKillTimeout )
2015-12-23 00:10:30 +00:00
if err != nil {
2016-11-15 23:28:21 +00:00
return nil , fmt . Errorf ( "Error parsing max kill timeout: %s" , err )
2015-12-23 00:10:30 +00:00
}
conf . MaxKillTimeout = dur
}
2018-09-27 01:14:36 +00:00
conf . ClientMaxPort = uint ( agentConfig . Client . ClientMaxPort )
conf . ClientMinPort = uint ( agentConfig . Client . ClientMinPort )
2021-09-10 08:52:47 +00:00
conf . MaxDynamicPort = agentConfig . Client . MaxDynamicPort
conf . MinDynamicPort = agentConfig . Client . MinDynamicPort
2019-06-03 19:31:39 +00:00
conf . DisableRemoteExec = agentConfig . Client . DisableRemoteExec
2022-01-10 15:19:07 +00:00
if agentConfig . Client . TemplateConfig != nil {
conf . TemplateConfig = agentConfig . Client . TemplateConfig . Copy ( )
2020-10-01 18:08:49 +00:00
}
2015-08-31 01:10:23 +00:00
2019-07-25 14:45:41 +00:00
hvMap := make ( map [ string ] * structs . ClientHostVolumeConfig , len ( agentConfig . Client . HostVolumes ) )
for _ , v := range agentConfig . Client . HostVolumes {
hvMap [ v . Name ] = v
}
conf . HostVolumes = hvMap
2015-08-31 01:10:23 +00:00
// Setup the node
conf . Node = new ( structs . Node )
2018-09-27 01:14:36 +00:00
conf . Node . Datacenter = agentConfig . Datacenter
conf . Node . Name = agentConfig . NodeName
conf . Node . Meta = agentConfig . Client . Meta
conf . Node . NodeClass = agentConfig . Client . NodeClass
2016-03-19 07:54:19 +00:00
2016-09-02 23:23:45 +00:00
// Set up the HTTP advertise address
2018-09-27 01:14:36 +00:00
conf . Node . HTTPAddr = agentConfig . AdvertiseAddrs . HTTP
2016-03-14 02:05:41 +00:00
2020-02-06 21:37:42 +00:00
// Canonicalize Node struct
conf . Node . Canonicalize ( )
2016-03-14 02:05:41 +00:00
// Reserve resources on the node.
2018-09-30 01:44:55 +00:00
// COMPAT(0.10): Remove in 0.10
2016-03-14 02:05:41 +00:00
r := conf . Node . Reserved
if r == nil {
r = new ( structs . Resources )
conf . Node . Reserved = r
}
2018-09-27 01:14:36 +00:00
r . CPU = agentConfig . Client . Reserved . CPU
r . MemoryMB = agentConfig . Client . Reserved . MemoryMB
r . DiskMB = agentConfig . Client . Reserved . DiskMB
2016-03-14 02:05:41 +00:00
2018-09-30 01:44:55 +00:00
res := conf . Node . ReservedResources
if res == nil {
res = new ( structs . NodeReservedResources )
conf . Node . ReservedResources = res
}
2018-09-27 01:14:36 +00:00
res . Cpu . CpuShares = int64 ( agentConfig . Client . Reserved . CPU )
res . Memory . MemoryMB = int64 ( agentConfig . Client . Reserved . MemoryMB )
res . Disk . DiskMB = int64 ( agentConfig . Client . Reserved . DiskMB )
res . Networks . ReservedHostPorts = agentConfig . Client . Reserved . ReservedPorts
2021-03-25 02:09:37 +00:00
if agentConfig . Client . Reserved . Cores != "" {
cores , err := cpuset . Parse ( agentConfig . Client . Reserved . Cores )
if err != nil {
return nil , fmt . Errorf ( "failed to parse client > reserved > cores value %q: %v" , agentConfig . Client . Reserved . Cores , err )
}
res . Cpu . ReservedCpuCores = cores . ToSlice ( )
}
2018-09-30 01:44:55 +00:00
2018-09-27 01:14:36 +00:00
conf . Version = agentConfig . Version
2016-03-19 07:54:19 +00:00
2018-09-27 01:14:36 +00:00
if * agentConfig . Consul . AutoAdvertise && agentConfig . Consul . ClientServiceName == "" {
2016-06-14 19:11:38 +00:00
return nil , fmt . Errorf ( "client_service_name must be set when auto_advertise is enabled" )
}
2018-09-27 01:14:36 +00:00
conf . ConsulConfig = agentConfig . Consul
conf . VaultConfig = agentConfig . Vault
2017-09-06 18:23:58 +00:00
// Set up Telemetry configuration
2018-09-27 01:14:36 +00:00
conf . StatsCollectionInterval = agentConfig . Telemetry . collectionInterval
conf . PublishNodeMetrics = agentConfig . Telemetry . PublishNodeMetrics
conf . PublishAllocationMetrics = agentConfig . Telemetry . PublishAllocationMetrics
2016-10-24 05:22:00 +00:00
// Set the TLS related configs
2018-09-27 01:14:36 +00:00
conf . TLSConfig = agentConfig . TLSConfig
2016-10-26 18:13:53 +00:00
conf . Node . TLSEnabled = conf . TLSConfig . EnableHTTP
2016-10-24 05:22:00 +00:00
2017-01-31 23:32:20 +00:00
// Set the GC related configs
2018-09-27 01:14:36 +00:00
conf . GCInterval = agentConfig . Client . GCInterval
conf . GCParallelDestroys = agentConfig . Client . GCParallelDestroys
conf . GCDiskUsageThreshold = agentConfig . Client . GCDiskUsageThreshold
conf . GCInodeUsageThreshold = agentConfig . Client . GCInodeUsageThreshold
conf . GCMaxAllocs = agentConfig . Client . GCMaxAllocs
if agentConfig . Client . NoHostUUID != nil {
conf . NoHostUUID = * agentConfig . Client . NoHostUUID
2017-07-04 00:41:20 +00:00
} else {
// Default no_host_uuid to true
conf . NoHostUUID = true
}
2017-01-31 23:32:20 +00:00
2017-08-20 00:19:38 +00:00
// Setup the ACLs
2018-09-27 01:14:36 +00:00
conf . ACLEnabled = agentConfig . ACL . Enabled
conf . ACLTokenTTL = agentConfig . ACL . TokenTTL
conf . ACLPolicyTTL = agentConfig . ACL . PolicyTTL
2022-10-20 07:37:32 +00:00
conf . ACLRoleTTL = agentConfig . ACL . RoleTTL
2017-08-20 00:19:38 +00:00
2020-03-07 03:15:22 +00:00
// Setup networking configuration
2019-06-14 03:05:57 +00:00
conf . CNIPath = agentConfig . Client . CNIPath
2020-07-08 19:45:04 +00:00
conf . CNIConfigDir = agentConfig . Client . CNIConfigDir
2019-06-14 03:05:57 +00:00
conf . BridgeNetworkName = agentConfig . Client . BridgeNetworkName
conf . BridgeNetworkAllocSubnet = agentConfig . Client . BridgeNetworkSubnet
2023-02-02 15:12:15 +00:00
conf . BridgeNetworkHairpinMode = agentConfig . Client . BridgeNetworkHairpinMode
2019-06-14 03:05:57 +00:00
2020-06-16 15:53:10 +00:00
for _ , hn := range agentConfig . Client . HostNetworks {
conf . HostNetworks [ hn . Name ] = hn
}
2020-07-06 22:51:46 +00:00
conf . BindWildcardDefaultHostNetwork = agentConfig . Client . BindWildcardDefaultHostNetwork
2020-06-16 15:53:10 +00:00
2022-03-29 00:33:01 +00:00
conf . CgroupParent = cgutil . GetCgroupParent ( agentConfig . Client . CgroupParent )
2021-03-29 18:05:12 +00:00
if agentConfig . Client . ReserveableCores != "" {
cores , err := cpuset . Parse ( agentConfig . Client . ReserveableCores )
if err != nil {
return nil , fmt . Errorf ( "failed to parse 'reservable_cores': %v" , err )
}
conf . ReservableCores = cores . ToSlice ( )
}
2021-03-25 02:09:37 +00:00
2022-03-14 10:48:13 +00:00
if agentConfig . Client . NomadServiceDiscovery != nil {
conf . NomadServiceDiscovery = * agentConfig . Client . NomadServiceDiscovery
}
2022-05-03 22:38:32 +00:00
artifactConfig , err := clientconfig . ArtifactConfigFromAgent ( agentConfig . Client . Artifact )
if err != nil {
return nil , fmt . Errorf ( "invalid artifact config: %v" , err )
}
conf . Artifact = artifactConfig
2016-02-16 21:42:48 +00:00
return conf , nil
}
// setupServer is used to setup the server if enabled
func ( a * Agent ) setupServer ( ) error {
if ! a . config . Server . Enabled {
return nil
}
// Setup the configuration
conf , err := a . serverConfig ( )
if err != nil {
return fmt . Errorf ( "server config setup failed: %s" , err )
}
2018-04-10 00:51:55 +00:00
// Generate a node ID and persist it if it is the first instance, otherwise
// read the persisted node ID.
if err := a . setupNodeID ( conf ) ; err != nil {
return fmt . Errorf ( "setting up server node ID failed: %s" , err )
}
2016-10-17 17:48:04 +00:00
// Sets up the keyring for gossip encryption
if err := a . setupKeyrings ( conf ) ; err != nil {
return fmt . Errorf ( "failed to configure keyring: %v" , err )
}
2016-02-16 21:42:48 +00:00
// Create the server
2020-07-28 20:12:08 +00:00
server , err := nomad . NewServer ( conf , a . consulCatalog , a . consulConfigEntries , a . consulACLs )
2016-02-16 21:42:48 +00:00
if err != nil {
return fmt . Errorf ( "server setup failed: %v" , err )
}
a . server = server
2016-06-10 01:12:02 +00:00
2016-11-04 22:24:28 +00:00
// Consul check addresses default to bind but can be toggled to use advertise
2016-11-09 19:37:41 +00:00
rpcCheckAddr := a . config . normalizedAddrs . RPC
serfCheckAddr := a . config . normalizedAddrs . Serf
2017-01-18 23:55:14 +00:00
if * a . config . Consul . ChecksUseAdvertise {
2016-11-04 22:24:28 +00:00
rpcCheckAddr = a . config . AdvertiseAddrs . RPC
serfCheckAddr = a . config . AdvertiseAddrs . Serf
2016-09-02 23:23:45 +00:00
}
2016-06-10 01:12:02 +00:00
// Create the Nomad Server services for Consul
2017-01-18 23:55:14 +00:00
if * a . config . Consul . AutoAdvertise {
2016-06-13 23:29:07 +00:00
httpServ := & structs . Service {
Name : a . config . Consul . ServerServiceName ,
2016-11-04 22:24:28 +00:00
PortLabel : a . config . AdvertiseAddrs . HTTP ,
2019-05-14 18:37:34 +00:00
Tags : append ( [ ] string { consul . ServiceTagHTTP } , a . config . Consul . Tags ... ) ,
2016-06-13 23:29:07 +00:00
}
2017-06-21 17:41:28 +00:00
const isServer = true
if check := a . agentHTTPCheck ( isServer ) ; check != nil {
2017-05-03 00:37:09 +00:00
httpServ . Checks = [ ] * structs . ServiceCheck { check }
2017-04-19 04:28:25 +00:00
}
2016-06-13 23:29:07 +00:00
rpcServ := & structs . Service {
Name : a . config . Consul . ServerServiceName ,
2016-11-04 22:24:28 +00:00
PortLabel : a . config . AdvertiseAddrs . RPC ,
2019-05-14 18:37:34 +00:00
Tags : append ( [ ] string { consul . ServiceTagRPC } , a . config . Consul . Tags ... ) ,
2016-06-17 00:34:25 +00:00
Checks : [ ] * structs . ServiceCheck {
2017-09-26 22:26:33 +00:00
{
2018-03-20 15:03:58 +00:00
Name : a . config . Consul . ServerRPCCheckName ,
2016-09-02 23:23:45 +00:00
Type : "tcp" ,
Interval : serverRpcCheckInterval ,
Timeout : serverRpcCheckTimeout ,
PortLabel : rpcCheckAddr ,
2016-06-17 00:34:25 +00:00
} ,
} ,
2016-06-13 23:29:07 +00:00
}
serfServ := & structs . Service {
Name : a . config . Consul . ServerServiceName ,
2016-11-04 22:24:28 +00:00
PortLabel : a . config . AdvertiseAddrs . Serf ,
2019-05-14 18:37:34 +00:00
Tags : append ( [ ] string { consul . ServiceTagSerf } , a . config . Consul . Tags ... ) ,
2016-06-17 00:34:25 +00:00
Checks : [ ] * structs . ServiceCheck {
2017-09-26 22:26:33 +00:00
{
2018-03-20 15:03:58 +00:00
Name : a . config . Consul . ServerSerfCheckName ,
2016-09-02 23:23:45 +00:00
Type : "tcp" ,
Interval : serverSerfCheckInterval ,
Timeout : serverSerfCheckTimeout ,
PortLabel : serfCheckAddr ,
2016-06-17 00:34:25 +00:00
} ,
} ,
2016-06-13 23:29:07 +00:00
}
2016-09-02 23:23:45 +00:00
2016-11-04 00:33:58 +00:00
// Add the http port check if TLS isn't enabled
2017-02-01 00:43:57 +00:00
consulServices := [ ] * structs . Service {
rpcServ ,
serfServ ,
2017-04-19 04:28:25 +00:00
httpServ ,
2017-02-01 00:43:57 +00:00
}
if err := a . consulService . RegisterAgent ( consulRoleServer , consulServices ) ; err != nil {
return err
2016-11-04 00:33:58 +00:00
}
2016-06-10 01:12:02 +00:00
}
2016-02-16 21:42:48 +00:00
return nil
}
2018-04-10 00:51:55 +00:00
// setupNodeID will pull the persisted node ID, if any, or create a random one
// and persist it.
func ( a * Agent ) setupNodeID ( config * nomad . Config ) error {
2018-04-10 17:34:14 +00:00
// For dev mode we have no filesystem access so just make a node ID.
if a . config . DevMode {
config . NodeID = uuid . Generate ( )
return nil
}
2018-04-10 00:51:55 +00:00
// Load saved state, if any. Since a user could edit this, we also
2018-04-10 13:47:33 +00:00
// validate it. Saved state overwrites any configured node id
2018-04-10 00:51:55 +00:00
fileID := filepath . Join ( config . DataDir , "node-id" )
if _ , err := os . Stat ( fileID ) ; err == nil {
2023-03-08 15:20:04 +00:00
rawID , err := os . ReadFile ( fileID )
2018-04-10 00:51:55 +00:00
if err != nil {
return err
}
nodeID := strings . TrimSpace ( string ( rawID ) )
nodeID = strings . ToLower ( nodeID )
if _ , err := uuidparse . ParseUUID ( nodeID ) ; err != nil {
return err
}
config . NodeID = nodeID
2018-04-10 20:33:01 +00:00
return nil
2018-04-10 13:47:33 +00:00
}
// If they've configured a node ID manually then just use that, as
// long as it's valid.
2018-04-10 20:33:01 +00:00
if config . NodeID != "" {
2018-04-10 16:22:16 +00:00
config . NodeID = strings . ToLower ( config . NodeID )
if _ , err := uuidparse . ParseUUID ( config . NodeID ) ; err != nil {
2018-04-10 13:47:33 +00:00
return err
}
// Persist this configured nodeID to our data directory
2022-08-24 14:56:42 +00:00
if err := escapingfs . EnsurePath ( fileID , false ) ; err != nil {
2018-04-10 13:47:33 +00:00
return err
}
2023-03-08 15:20:04 +00:00
if err := os . WriteFile ( fileID , [ ] byte ( config . NodeID ) , 0600 ) ; err != nil {
2018-04-10 13:47:33 +00:00
return err
}
return nil
}
2018-04-10 00:51:55 +00:00
// If we still don't have a valid node ID, make one.
if config . NodeID == "" {
id := uuid . Generate ( )
2022-08-24 14:56:42 +00:00
if err := escapingfs . EnsurePath ( fileID , false ) ; err != nil {
2018-04-10 00:51:55 +00:00
return err
}
2023-03-08 15:20:04 +00:00
if err := os . WriteFile ( fileID , [ ] byte ( id ) , 0600 ) ; err != nil {
2018-04-10 00:51:55 +00:00
return err
}
config . NodeID = id
}
return nil
}
2016-10-17 17:48:04 +00:00
// setupKeyrings is used to initialize and load keyrings during agent startup
func ( a * Agent ) setupKeyrings ( config * nomad . Config ) error {
file := filepath . Join ( a . config . DataDir , serfKeyring )
if a . config . Server . EncryptKey == "" {
goto LOAD
}
if _ , err := os . Stat ( file ) ; err != nil {
2020-10-01 15:12:14 +00:00
if err := initKeyring ( file , a . config . Server . EncryptKey , a . logger ) ; err != nil {
2016-10-17 17:48:04 +00:00
return err
}
}
LOAD :
if _ , err := os . Stat ( file ) ; err == nil {
config . SerfConfig . KeyringFile = file
}
if err := loadKeyringFile ( config . SerfConfig ) ; err != nil {
return err
}
// Success!
return nil
}
2016-02-16 21:42:48 +00:00
// setupClient is used to setup the client if enabled
func ( a * Agent ) setupClient ( ) error {
if ! a . config . Client . Enabled {
return nil
}
2023-02-10 20:33:16 +00:00
// Plugin setup must happen before the call to clientConfig, because it
// copies the pointers to the plugin loaders from the Agent to the
// Client config.
if err := a . setupPlugins ( ) ; err != nil {
return err
}
2016-02-16 21:42:48 +00:00
// Setup the configuration
conf , err := a . clientConfig ( )
if err != nil {
return fmt . Errorf ( "client setup failed: %v" , err )
}
2015-08-31 01:10:23 +00:00
2016-03-10 07:25:31 +00:00
// Reserve some ports for the plugins if we are on Windows
if runtime . GOOS == "windows" {
if err := a . reservePortsForClient ( conf ) ; err != nil {
return err
}
2016-02-05 23:17:15 +00:00
}
2019-01-09 18:57:56 +00:00
if conf . StateDBFactory == nil {
conf . StateDBFactory = state . GetStateDBFactory ( conf . DevMode )
}
2022-04-06 17:17:05 +00:00
// Set up a custom listener and dialer. This is used by Nomad clients when
2023-01-20 19:21:51 +00:00
// running consul-template functions that utilize the Nomad API. We lazy
2022-04-06 17:17:05 +00:00
// load this into the client config, therefore this needs to happen before
// we call NewClient.
a . builtinListener , a . builtinDialer = bufconndialer . New ( )
conf . TemplateDialer = a . builtinDialer
2023-02-23 20:03:44 +00:00
// Initialize builtin Task API server here for use in the client, but it
// won't accept connections until the HTTP servers are created.
a . taskAPIServer = newBuiltinAPI ( )
conf . APIListenerRegistrar = a . taskAPIServer
2023-02-06 19:31:22 +00:00
2021-03-19 14:52:43 +00:00
nomadClient , err := client . NewClient (
conf , a . consulCatalog , a . consulProxies , a . consulService , nil )
2015-08-24 00:40:27 +00:00
if err != nil {
return fmt . Errorf ( "client setup failed: %v" , err )
}
2020-09-04 17:50:11 +00:00
a . client = nomadClient
2016-06-10 01:12:02 +00:00
2016-06-14 00:32:18 +00:00
// Create the Nomad Client services for Consul
2017-01-18 23:55:14 +00:00
if * a . config . Consul . AutoAdvertise {
2016-06-13 23:29:07 +00:00
httpServ := & structs . Service {
Name : a . config . Consul . ClientServiceName ,
2016-11-04 22:24:28 +00:00
PortLabel : a . config . AdvertiseAddrs . HTTP ,
2019-05-14 18:37:34 +00:00
Tags : append ( [ ] string { consul . ServiceTagHTTP } , a . config . Consul . Tags ... ) ,
2016-06-13 23:29:07 +00:00
}
2017-06-21 17:41:28 +00:00
const isServer = false
if check := a . agentHTTPCheck ( isServer ) ; check != nil {
2017-05-03 00:37:09 +00:00
httpServ . Checks = [ ] * structs . ServiceCheck { check }
2016-11-04 00:33:58 +00:00
}
2017-04-19 04:28:25 +00:00
if err := a . consulService . RegisterAgent ( consulRoleClient , [ ] * structs . Service { httpServ } ) ; err != nil {
return err
}
2016-06-10 01:12:02 +00:00
}
2015-08-24 00:40:27 +00:00
return nil
}
2017-05-03 00:37:09 +00:00
// agentHTTPCheck returns a health check for the agent's HTTP API if possible.
// If no HTTP health check can be supported nil is returned.
2017-06-21 17:41:28 +00:00
func ( a * Agent ) agentHTTPCheck ( server bool ) * structs . ServiceCheck {
2017-05-03 00:37:09 +00:00
// Resolve the http check address
2022-01-03 14:33:53 +00:00
httpCheckAddr := a . config . normalizedAddrs . HTTP [ 0 ]
2017-05-03 00:37:09 +00:00
if * a . config . Consul . ChecksUseAdvertise {
httpCheckAddr = a . config . AdvertiseAddrs . HTTP
}
check := structs . ServiceCheck {
2018-03-20 15:03:58 +00:00
Name : a . config . Consul . ClientHTTPCheckName ,
2017-05-03 00:37:09 +00:00
Type : "http" ,
2017-10-13 22:37:44 +00:00
Path : "/v1/agent/health?type=client" ,
2017-05-03 00:37:09 +00:00
Protocol : "http" ,
2017-09-26 22:26:33 +00:00
Interval : agentHttpCheckInterval ,
Timeout : agentHttpCheckTimeout ,
2017-05-03 00:37:09 +00:00
PortLabel : httpCheckAddr ,
}
2017-06-21 17:41:28 +00:00
// Switch to endpoint that doesn't require a leader for servers
if server {
2018-03-20 15:03:58 +00:00
check . Name = a . config . Consul . ServerHTTPCheckName
2017-10-13 22:37:44 +00:00
check . Path = "/v1/agent/health?type=server"
2017-06-21 17:41:28 +00:00
}
2017-05-03 00:37:09 +00:00
if ! a . config . TLSConfig . EnableHTTP {
// No HTTPS, return a plain http check
return & check
}
if a . config . TLSConfig . VerifyHTTPSClient {
2018-09-13 17:43:40 +00:00
a . logger . Warn ( "not registering Nomad HTTPS Health Check because verify_https_client enabled" )
2017-05-03 00:37:09 +00:00
return nil
}
// HTTPS enabled; skip verification
check . Protocol = "https"
check . TLSSkipVerify = true
return & check
}
2016-06-01 10:08:39 +00:00
// reservePortsForClient reserves a range of ports for the client to use when
2016-02-12 22:25:32 +00:00
// it creates various plugins for log collection, executors, drivers, etc
2016-02-12 21:21:56 +00:00
func ( a * Agent ) reservePortsForClient ( conf * clientconfig . Config ) error {
2018-10-03 16:47:18 +00:00
if conf . Node . ReservedResources == nil {
conf . Node . ReservedResources = & structs . NodeReservedResources { }
2016-02-12 21:21:56 +00:00
}
2016-02-12 22:25:32 +00:00
2018-10-03 16:47:18 +00:00
res := conf . Node . ReservedResources . Networks . ReservedHostPorts
if res == "" {
res = fmt . Sprintf ( "%d-%d" , conf . ClientMinPort , conf . ClientMaxPort )
} else {
res += fmt . Sprintf ( ",%d-%d" , conf . ClientMinPort , conf . ClientMaxPort )
2016-02-12 21:21:56 +00:00
}
2018-10-03 16:47:18 +00:00
conf . Node . ReservedResources . Networks . ReservedHostPorts = res
2016-02-12 21:21:56 +00:00
return nil
}
2015-08-16 23:40:04 +00:00
// Leave is used gracefully exit. Clients will inform servers
// of their departure so that allocations can be rescheduled.
2015-08-16 20:54:49 +00:00
func ( a * Agent ) Leave ( ) error {
2015-08-23 23:53:15 +00:00
if a . client != nil {
if err := a . client . Leave ( ) ; err != nil {
2018-09-13 17:43:40 +00:00
a . logger . Error ( "client leave failed" , "error" , err )
2015-08-23 23:53:15 +00:00
}
}
if a . server != nil {
if err := a . server . Leave ( ) ; err != nil {
2018-09-13 17:43:40 +00:00
a . logger . Error ( "server leave failed" , "error" , err )
2015-08-23 23:53:15 +00:00
}
}
2015-08-16 20:54:49 +00:00
return nil
}
2015-08-16 23:40:04 +00:00
// Shutdown is used to terminate the agent.
2015-08-16 20:54:49 +00:00
func ( a * Agent ) Shutdown ( ) error {
2015-08-16 23:40:04 +00:00
a . shutdownLock . Lock ( )
defer a . shutdownLock . Unlock ( )
if a . shutdown {
return nil
}
2018-09-13 17:43:40 +00:00
a . logger . Info ( "requesting shutdown" )
2015-08-23 23:53:15 +00:00
if a . client != nil {
2023-02-23 20:03:44 +00:00
// Task API must be closed separately from other HTTP servers and should
// happen before the client is shutdown
a . taskAPIServer . Shutdown ( )
2015-08-23 23:53:15 +00:00
if err := a . client . Shutdown ( ) ; err != nil {
2018-09-13 17:43:40 +00:00
a . logger . Error ( "client shutdown failed" , "error" , err )
2015-08-23 23:53:15 +00:00
}
}
2015-08-16 23:40:04 +00:00
if a . server != nil {
2015-08-23 23:53:15 +00:00
if err := a . server . Shutdown ( ) ; err != nil {
2018-09-13 17:43:40 +00:00
a . logger . Error ( "server shutdown failed" , "error" , err )
2015-08-23 23:53:15 +00:00
}
2015-08-16 23:40:04 +00:00
}
2017-02-01 00:43:57 +00:00
if err := a . consulService . Shutdown ( ) ; err != nil {
2018-09-13 17:43:40 +00:00
a . logger . Error ( "shutting down Consul client failed" , "error" , err )
2016-05-11 22:24:37 +00:00
}
2018-09-13 17:43:40 +00:00
a . logger . Info ( "shutdown complete" )
2015-08-16 23:40:04 +00:00
a . shutdown = true
close ( a . shutdownCh )
2015-08-23 23:53:15 +00:00
return nil
2015-08-16 23:40:04 +00:00
}
// RPC is used to make an RPC call to the Nomad servers
func ( a * Agent ) RPC ( method string , args interface { } , reply interface { } ) error {
if a . server != nil {
return a . server . RPC ( method , args , reply )
}
return a . client . RPC ( method , args , reply )
2015-08-16 20:54:49 +00:00
}
2015-08-24 00:40:27 +00:00
// Client returns the configured client or nil
func ( a * Agent ) Client ( ) * client . Client {
return a . client
}
// Server returns the configured server or nil
func ( a * Agent ) Server ( ) * nomad . Server {
return a . server
}
2015-08-31 01:20:00 +00:00
// Stats is used to return statistics for debugging and insight
// for various sub-systems
func ( a * Agent ) Stats ( ) map [ string ] map [ string ] string {
stats := make ( map [ string ] map [ string ] string )
if a . server != nil {
subStat := a . server . Stats ( )
for k , v := range subStat {
stats [ k ] = v
}
}
if a . client != nil {
subStat := a . client . Stats ( )
for k , v := range subStat {
stats [ k ] = v
}
}
return stats
}
2016-05-11 22:24:37 +00:00
2017-11-20 15:38:46 +00:00
// ShouldReload determines if we should reload the configuration and agent
// connections. If the TLS Configuration has not changed, we shouldn't reload.
2018-06-08 17:14:40 +00:00
func ( a * Agent ) ShouldReload ( newConfig * Config ) ( agent , http bool ) {
2017-11-30 15:50:43 +00:00
a . configLock . Lock ( )
defer a . configLock . Unlock ( )
2018-03-21 16:46:05 +00:00
2019-07-23 13:52:24 +00:00
if newConfig . LogLevel != "" && newConfig . LogLevel != a . config . LogLevel {
agent = true
}
2018-03-28 21:54:22 +00:00
isEqual , err := a . config . TLSConfig . CertificateInfoIsEqual ( newConfig . TLSConfig )
if err != nil {
2018-09-13 17:43:40 +00:00
a . logger . Error ( "parsing TLS certificate" , "error" , err )
2019-07-23 13:52:24 +00:00
return agent , false
2018-03-28 21:54:22 +00:00
} else if ! isEqual {
2018-06-08 17:14:40 +00:00
return true , true
2018-03-21 16:46:05 +00:00
}
// Allow the ability to only reload HTTP connections
2018-03-23 21:58:10 +00:00
if a . config . TLSConfig . EnableHTTP != newConfig . TLSConfig . EnableHTTP {
http = true
agent = true
2018-03-21 16:46:05 +00:00
}
// Allow the ability to only reload HTTP connections
2018-03-23 21:58:10 +00:00
if a . config . TLSConfig . EnableRPC != newConfig . TLSConfig . EnableRPC {
agent = true
2017-11-20 15:38:46 +00:00
}
2021-11-01 20:30:53 +00:00
if a . config . TLSConfig . RPCUpgradeMode != newConfig . TLSConfig . RPCUpgradeMode {
agent = true
}
2018-06-08 17:14:40 +00:00
return agent , http
2017-11-20 15:38:46 +00:00
}
2017-11-15 01:53:23 +00:00
// Reload handles configuration changes for the agent. Provides a method that
// is easier to unit test, as this action is invoked via SIGHUP.
func ( a * Agent ) Reload ( newConfig * Config ) error {
a . configLock . Lock ( )
defer a . configLock . Unlock ( )
2022-08-18 23:32:04 +00:00
current := a . config . Copy ( )
updatedLogging := newConfig != nil && ( newConfig . LogLevel != current . LogLevel )
2019-07-23 13:52:24 +00:00
if newConfig == nil || newConfig . TLSConfig == nil && ! updatedLogging {
2017-11-20 15:38:46 +00:00
return fmt . Errorf ( "cannot reload agent with nil configuration" )
}
2019-07-23 13:52:24 +00:00
if updatedLogging {
2022-08-18 23:32:04 +00:00
current . LogLevel = newConfig . LogLevel
a . logger . SetLevel ( log . LevelFromString ( current . LogLevel ) )
2019-07-23 13:52:24 +00:00
}
2020-03-22 16:17:33 +00:00
// Update eventer config
if newConfig . Audit != nil {
2020-03-25 14:57:44 +00:00
if err := a . entReloadEventer ( newConfig . Audit ) ; err != nil {
2020-03-22 16:17:33 +00:00
return err
}
}
2020-03-25 14:53:38 +00:00
// Allow auditor to call reopen regardless of config changes
// This is primarily for enterprise audit logging to allow the underlying
// file to be reopened if necessary
if err := a . auditor . Reopen ( ) ; err != nil {
return err
}
2020-03-22 16:17:33 +00:00
2019-07-23 13:52:24 +00:00
fullUpdateTLSConfig := func ( ) {
// Completely reload the agent's TLS configuration (moving from non-TLS to
// TLS, or vice versa)
// This does not handle errors in loading the new TLS configuration
2022-08-18 23:32:04 +00:00
current . TLSConfig = newConfig . TLSConfig . Copy ( )
2019-07-23 13:52:24 +00:00
}
2022-08-18 23:32:04 +00:00
if ! current . TLSConfig . IsEmpty ( ) && ! newConfig . TLSConfig . IsEmpty ( ) {
2019-07-23 13:52:24 +00:00
// This is just a TLS configuration reload, we don't need to refresh
// existing network connections
2017-11-20 15:38:46 +00:00
// Reload the certificates on the keyloader and on success store the
// updated TLS config. It is important to reuse the same keyloader
// as this allows us to dynamically reload configurations not only
// on the Agent but on the Server and Client too (they are
// referencing the same keyloader).
2022-08-18 23:32:04 +00:00
keyloader := current . TLSConfig . GetKeyLoader ( )
2017-11-20 15:38:46 +00:00
_ , err := keyloader . LoadKeyPair ( newConfig . TLSConfig . CertFile , newConfig . TLSConfig . KeyFile )
if err != nil {
return err
2017-11-15 01:53:23 +00:00
}
2022-08-18 23:32:04 +00:00
current . TLSConfig = newConfig . TLSConfig
current . TLSConfig . KeyLoader = keyloader
a . config = current
2017-11-20 15:38:46 +00:00
return nil
2022-08-18 23:32:04 +00:00
} else if newConfig . TLSConfig . IsEmpty ( ) && ! current . TLSConfig . IsEmpty ( ) {
2018-09-13 17:43:40 +00:00
a . logger . Warn ( "downgrading agent's existing TLS configuration to plaintext" )
2019-07-23 13:52:24 +00:00
fullUpdateTLSConfig ( )
2022-08-18 23:32:04 +00:00
} else if ! newConfig . TLSConfig . IsEmpty ( ) && current . TLSConfig . IsEmpty ( ) {
2018-09-13 17:43:40 +00:00
a . logger . Info ( "upgrading from plaintext configuration to TLS" )
2019-07-23 13:52:24 +00:00
fullUpdateTLSConfig ( )
2017-11-15 01:53:23 +00:00
}
2022-08-18 23:32:04 +00:00
// Set agent config to the updated config
a . config = current
2017-11-15 01:53:23 +00:00
return nil
}
2022-08-18 23:32:04 +00:00
// GetConfig returns the current agent configuration. The Config should *not*
// be mutated directly. First call Config.Copy.
2017-11-15 01:53:23 +00:00
func ( a * Agent ) GetConfig ( ) * Config {
2017-11-28 17:33:46 +00:00
a . configLock . Lock ( )
defer a . configLock . Unlock ( )
2017-11-15 01:53:23 +00:00
return a . config
}
2023-02-06 19:31:22 +00:00
// GetMetricsSink returns the metrics sink.
func ( a * Agent ) GetMetricsSink ( ) * metrics . InmemSink {
return a . inmemSink
}
2017-02-01 00:43:57 +00:00
// setupConsul creates the Consul client and starts its main Run loop.
func ( a * Agent ) setupConsul ( consulConfig * config . ConsulConfig ) error {
apiConf , err := consulConfig . ApiConfig ( )
if err != nil {
return err
}
2020-09-04 17:50:11 +00:00
consulClient , err := consulapi . NewClient ( apiConf )
2016-06-08 06:31:19 +00:00
if err != nil {
return err
}
2016-05-11 22:24:37 +00:00
2017-02-01 00:43:57 +00:00
// Create Consul Catalog client for service discovery.
2020-09-04 17:50:11 +00:00
a . consulCatalog = consulClient . Catalog ( )
2016-05-11 22:24:37 +00:00
2020-07-28 20:12:08 +00:00
// Create Consul ConfigEntries client for managing Config Entries.
2020-09-04 17:50:11 +00:00
a . consulConfigEntries = consulClient . ConfigEntries ( )
2020-07-28 20:12:08 +00:00
2019-12-06 20:46:46 +00:00
// Create Consul ACL client for managing tokens.
2020-09-04 17:50:11 +00:00
a . consulACLs = consulClient . ACL ( )
2019-12-06 20:46:46 +00:00
2017-02-01 00:43:57 +00:00
// Create Consul Service client for service advertisement and checks.
2018-06-06 00:19:52 +00:00
isClient := false
if a . config . Client != nil && a . config . Client . Enabled {
isClient = true
}
2020-09-04 17:50:11 +00:00
// Create Consul Agent client for looking info about the agent.
consulAgentClient := consulClient . Agent ( )
2021-06-07 15:54:33 +00:00
namespacesClient := consul . NewNamespacesClient ( consulClient . Namespaces ( ) , consulAgentClient )
2021-03-16 18:22:21 +00:00
a . consulService = consul . NewServiceClient ( consulAgentClient , namespacesClient , a . logger , isClient )
2020-09-04 17:50:11 +00:00
a . consulProxies = consul . NewConnectProxiesClient ( consulAgentClient )
2017-07-24 19:12:02 +00:00
// Run the Consul service client's sync'ing main loop
2017-02-01 00:43:57 +00:00
go a . consulService . Run ( )
2016-06-10 01:12:02 +00:00
return nil
2016-05-11 22:24:37 +00:00
}
2020-03-25 14:38:40 +00:00
// noOpAuditor is a no-op Auditor that fulfills the
// event.Auditor interface.
type noOpAuditor struct { }
// Ensure noOpAuditor is an Auditor
var _ event . Auditor = & noOpAuditor { }
func ( e * noOpAuditor ) Event ( ctx context . Context , eventType string , payload interface { } ) error {
return nil
}
func ( e * noOpAuditor ) Enabled ( ) bool {
return false
}
func ( e * noOpAuditor ) Reopen ( ) error {
return nil
}
func ( e * noOpAuditor ) SetEnabled ( enabled bool ) { }
func ( e * noOpAuditor ) DeliveryEnforced ( ) bool { return false }