2015-01-06 18:40:00 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2016-11-16 21:45:26 +00:00
|
|
|
"bufio"
|
2019-03-21 20:04:40 +00:00
|
|
|
"bytes"
|
2020-09-03 16:22:03 +00:00
|
|
|
"context"
|
2015-01-06 18:40:00 +00:00
|
|
|
"fmt"
|
2019-03-21 20:04:40 +00:00
|
|
|
"io"
|
2019-01-07 14:39:23 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2015-01-06 18:40:00 +00:00
|
|
|
)
|
|
|
|
|
2018-03-26 15:51:43 +00:00
|
|
|
// ServiceKind is the kind of service being registered.
|
|
|
|
type ServiceKind string
|
|
|
|
|
|
|
|
const (
|
|
|
|
// ServiceKindTypical is a typical, classic Consul service. This is
|
|
|
|
// represented by the absence of a value. This was chosen for ease of
|
|
|
|
// backwards compatibility: existing services in the catalog would
|
|
|
|
// default to the typical service.
|
|
|
|
ServiceKindTypical ServiceKind = ""
|
|
|
|
|
|
|
|
// ServiceKindConnectProxy is a proxy for the Connect feature. This
|
|
|
|
// service proxies another service within Consul and speaks the connect
|
|
|
|
// protocol.
|
|
|
|
ServiceKindConnectProxy ServiceKind = "connect-proxy"
|
2019-06-18 00:52:01 +00:00
|
|
|
|
|
|
|
// ServiceKindMeshGateway is a Mesh Gateway for the Connect feature. This
|
|
|
|
// service will proxy connections based off the SNI header set by other
|
|
|
|
// connect proxies
|
|
|
|
ServiceKindMeshGateway ServiceKind = "mesh-gateway"
|
2020-03-26 16:20:56 +00:00
|
|
|
|
|
|
|
// ServiceKindTerminatingGateway is a Terminating Gateway for the Connect
|
|
|
|
// feature. This service will proxy connections to services outside the mesh.
|
|
|
|
ServiceKindTerminatingGateway ServiceKind = "terminating-gateway"
|
2020-04-14 14:48:02 +00:00
|
|
|
|
|
|
|
// ServiceKindIngressGateway is an Ingress Gateway for the Connect feature.
|
|
|
|
// This service will ingress connections based of configuration defined in
|
|
|
|
// the ingress-gateway config entry.
|
|
|
|
ServiceKindIngressGateway ServiceKind = "ingress-gateway"
|
2018-03-26 15:51:43 +00:00
|
|
|
)
|
|
|
|
|
2018-09-12 16:07:47 +00:00
|
|
|
// UpstreamDestType is the type of upstream discovery mechanism.
|
|
|
|
type UpstreamDestType string
|
|
|
|
|
|
|
|
const (
|
|
|
|
// UpstreamDestTypeService discovers instances via healthy service lookup.
|
|
|
|
UpstreamDestTypeService UpstreamDestType = "service"
|
|
|
|
|
|
|
|
// UpstreamDestTypePreparedQuery discovers instances via prepared query
|
|
|
|
// execution.
|
|
|
|
UpstreamDestTypePreparedQuery UpstreamDestType = "prepared_query"
|
|
|
|
)
|
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
// AgentCheck represents a check known to the agent
|
|
|
|
type AgentCheck struct {
|
2017-11-01 22:30:29 +00:00
|
|
|
Node string
|
|
|
|
CheckID string
|
|
|
|
Name string
|
|
|
|
Status string
|
|
|
|
Notes string
|
|
|
|
Output string
|
|
|
|
ServiceID string
|
|
|
|
ServiceName string
|
2019-10-17 18:33:11 +00:00
|
|
|
Type string
|
2017-11-01 22:30:29 +00:00
|
|
|
Definition HealthCheckDefinition
|
2020-01-17 03:34:04 +00:00
|
|
|
Namespace string `json:",omitempty"`
|
2015-01-06 18:40:00 +00:00
|
|
|
}
|
|
|
|
|
2018-09-07 14:30:47 +00:00
|
|
|
// AgentWeights represent optional weights for a service
|
|
|
|
type AgentWeights struct {
|
|
|
|
Passing int
|
|
|
|
Warning int
|
|
|
|
}
|
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
// AgentService represents a service known to the agent
|
|
|
|
type AgentService struct {
|
2018-09-12 16:07:47 +00:00
|
|
|
Kind ServiceKind `json:",omitempty"`
|
2016-02-16 19:45:29 +00:00
|
|
|
ID string
|
|
|
|
Service string
|
|
|
|
Tags []string
|
2018-04-21 15:34:29 +00:00
|
|
|
Meta map[string]string
|
2016-02-16 19:45:29 +00:00
|
|
|
Port int
|
|
|
|
Address string
|
2019-06-17 14:51:50 +00:00
|
|
|
TaggedAddresses map[string]ServiceAddress `json:",omitempty"`
|
2018-09-07 14:30:47 +00:00
|
|
|
Weights AgentWeights
|
2016-02-16 19:45:29 +00:00
|
|
|
EnableTagOverride bool
|
2019-08-09 19:19:30 +00:00
|
|
|
CreateIndex uint64 `json:",omitempty" bexpr:"-"`
|
|
|
|
ModifyIndex uint64 `json:",omitempty" bexpr:"-"`
|
|
|
|
ContentHash string `json:",omitempty" bexpr:"-"`
|
|
|
|
Proxy *AgentServiceConnectProxyConfig `json:",omitempty"`
|
|
|
|
Connect *AgentServiceConnect `json:",omitempty"`
|
2019-12-10 02:26:41 +00:00
|
|
|
// NOTE: If we ever set the ContentHash outside of singular service lookup then we may need
|
|
|
|
// to include the Namespace in the hash. When we do, then we are in for lots of fun with tests.
|
|
|
|
// For now though, ignoring it works well enough.
|
|
|
|
Namespace string `json:",omitempty" bexpr:"-" hash:"ignore"`
|
2020-11-19 21:27:31 +00:00
|
|
|
// Datacenter is only ever returned and is ignored if presented.
|
|
|
|
Datacenter string `json:",omitempty" bexpr:"-" hash:"ignore"`
|
2018-04-20 13:24:24 +00:00
|
|
|
}
|
|
|
|
|
2019-01-07 14:39:23 +00:00
|
|
|
// AgentServiceChecksInfo returns information about a Service and its checks
|
|
|
|
type AgentServiceChecksInfo struct {
|
|
|
|
AggregatedStatus string
|
|
|
|
Service *AgentService
|
|
|
|
Checks HealthChecks
|
|
|
|
}
|
|
|
|
|
2018-04-20 13:24:24 +00:00
|
|
|
// AgentServiceConnect represents the Connect configuration of a service.
|
|
|
|
type AgentServiceConnect struct {
|
2018-09-27 13:33:12 +00:00
|
|
|
Native bool `json:",omitempty"`
|
2019-04-16 16:00:15 +00:00
|
|
|
SidecarService *AgentServiceRegistration `json:",omitempty" bexpr:"-"`
|
2018-04-20 13:24:24 +00:00
|
|
|
}
|
|
|
|
|
2018-09-12 16:07:47 +00:00
|
|
|
// AgentServiceConnectProxyConfig is the proxy configuration in a connect-proxy
|
|
|
|
// ServiceDefinition or response.
|
|
|
|
type AgentServiceConnectProxyConfig struct {
|
2019-06-18 00:52:01 +00:00
|
|
|
DestinationServiceName string `json:",omitempty"`
|
2018-09-12 16:07:47 +00:00
|
|
|
DestinationServiceID string `json:",omitempty"`
|
|
|
|
LocalServiceAddress string `json:",omitempty"`
|
|
|
|
LocalServicePort int `json:",omitempty"`
|
2019-04-16 16:00:15 +00:00
|
|
|
Config map[string]interface{} `json:",omitempty" bexpr:"-"`
|
2019-06-18 00:52:01 +00:00
|
|
|
Upstreams []Upstream `json:",omitempty"`
|
2019-07-01 23:47:58 +00:00
|
|
|
MeshGateway MeshGatewayConfig `json:",omitempty"`
|
2019-09-26 02:55:52 +00:00
|
|
|
Expose ExposeConfig `json:",omitempty"`
|
2015-01-06 18:40:00 +00:00
|
|
|
}
|
|
|
|
|
2020-08-27 15:00:48 +00:00
|
|
|
const (
|
|
|
|
// MemberTagKeyACLMode is the key used to indicate what ACL mode the agent is
|
|
|
|
// operating in. The values of this key will be one of the MemberACLMode constants
|
|
|
|
// with the key not being present indicating ACLModeUnknown.
|
|
|
|
MemberTagKeyACLMode = "acls"
|
|
|
|
|
|
|
|
// MemberTagRole is the key used to indicate that the member is a server or not.
|
|
|
|
MemberTagKeyRole = "role"
|
|
|
|
|
|
|
|
// MemberTagValueRoleServer is the value of the MemberTagKeyRole used to indicate
|
|
|
|
// that the member represents a Consul server.
|
|
|
|
MemberTagValueRoleServer = "consul"
|
|
|
|
|
|
|
|
// MemberTagKeySegment is the key name of the tag used to indicate which network
|
|
|
|
// segment this member is in.
|
|
|
|
// Network Segments are a Consul Enterprise feature.
|
|
|
|
MemberTagKeySegment = "segment"
|
|
|
|
|
|
|
|
// MemberTagKeyBootstrap is the key name of the tag used to indicate whether this
|
|
|
|
// agent was started with the "bootstrap" configuration enabled
|
|
|
|
MemberTagKeyBootstrap = "bootstrap"
|
|
|
|
// MemberTagValueBootstrap is the value of the MemberTagKeyBootstrap key when the
|
|
|
|
// agent was started with the "bootstrap" configuration enabled.
|
|
|
|
MemberTagValueBootstrap = "1"
|
|
|
|
|
|
|
|
// MemberTagKeyBootstrapExpect is the key name of the tag used to indicate whether
|
|
|
|
// this agent was started with the "bootstrap_expect" configuration set to a non-zero
|
|
|
|
// value. The value of this key will be the string for of that configuration value.
|
|
|
|
MemberTagKeyBootstrapExpect = "expect"
|
|
|
|
|
|
|
|
// MemberTagKeyUseTLS is the key name of the tag used to indicate whther this agent
|
|
|
|
// was configured to use TLS.
|
|
|
|
MemberTagKeyUseTLS = "use_tls"
|
|
|
|
// MemberTagValueUseTLS is the value of the MemberTagKeyUseTLS when the agent was
|
|
|
|
// configured to use TLS. Any other value indicates that it was not setup in
|
|
|
|
// that manner.
|
|
|
|
MemberTagValueUseTLS = "1"
|
2020-11-17 15:53:57 +00:00
|
|
|
|
|
|
|
// MemberTagKeyReadReplica is the key used to indicate that the member is a read
|
|
|
|
// replica server (will remain a Raft non-voter).
|
|
|
|
// Read Replicas are a Consul Enterprise feature.
|
|
|
|
MemberTagKeyReadReplica = "read_replica"
|
|
|
|
// MemberTagValueReadReplica is the value of the MemberTagKeyReadReplica key when
|
|
|
|
// the member is in fact a read-replica. Any other value indicates that it is not.
|
|
|
|
// Read Replicas are a Consul Enterprise feature.
|
|
|
|
MemberTagValueReadReplica = "1"
|
2020-08-27 15:00:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type MemberACLMode string
|
|
|
|
|
|
|
|
const (
|
|
|
|
// ACLModeDisables indicates that ACLs are disabled for this agent
|
|
|
|
ACLModeDisabled MemberACLMode = "0"
|
|
|
|
// ACLModeEnabled indicates that ACLs are enabled and operating in new ACL
|
|
|
|
// mode (v1.4.0+ ACLs)
|
|
|
|
ACLModeEnabled MemberACLMode = "1"
|
|
|
|
// ACLModeLegacy indicates that ACLs are enabled and operating in legacy mode.
|
|
|
|
ACLModeLegacy MemberACLMode = "2"
|
|
|
|
// ACLModeUnkown is used to indicate that the AgentMember.Tags didn't advertise
|
|
|
|
// an ACL mode at all. This is the case for Consul versions before v1.4.0 and
|
|
|
|
// should be treated similarly to ACLModeLegacy.
|
|
|
|
ACLModeUnknown MemberACLMode = "3"
|
|
|
|
)
|
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
// AgentMember represents a cluster member known to the agent
|
|
|
|
type AgentMember struct {
|
2020-06-22 17:18:45 +00:00
|
|
|
Name string
|
|
|
|
Addr string
|
|
|
|
Port uint16
|
|
|
|
Tags map[string]string
|
|
|
|
// Status of the Member which corresponds to github.com/hashicorp/serf/serf.MemberStatus
|
|
|
|
// Value is one of:
|
|
|
|
//
|
|
|
|
// AgentMemberNone = 0
|
|
|
|
// AgentMemberAlive = 1
|
|
|
|
// AgentMemberLeaving = 2
|
|
|
|
// AgentMemberLeft = 3
|
|
|
|
// AgentMemberFailed = 4
|
2015-01-06 18:40:00 +00:00
|
|
|
Status int
|
|
|
|
ProtocolMin uint8
|
|
|
|
ProtocolMax uint8
|
|
|
|
ProtocolCur uint8
|
|
|
|
DelegateMin uint8
|
|
|
|
DelegateMax uint8
|
|
|
|
DelegateCur uint8
|
|
|
|
}
|
|
|
|
|
2020-08-27 15:00:48 +00:00
|
|
|
// ACLMode returns the ACL mode this agent is operating in.
|
|
|
|
func (m *AgentMember) ACLMode() MemberACLMode {
|
|
|
|
mode := m.Tags[MemberTagKeyACLMode]
|
|
|
|
|
|
|
|
// the key may not have existed but then an
|
|
|
|
// empty string will be returned and we will
|
|
|
|
// handle that in the default case of the switch
|
|
|
|
switch MemberACLMode(mode) {
|
|
|
|
case ACLModeDisabled:
|
|
|
|
return ACLModeDisabled
|
|
|
|
case ACLModeEnabled:
|
|
|
|
return ACLModeEnabled
|
|
|
|
case ACLModeLegacy:
|
|
|
|
return ACLModeLegacy
|
|
|
|
default:
|
|
|
|
return ACLModeUnknown
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsConsulServer returns true when this member is a Consul server.
|
|
|
|
func (m *AgentMember) IsConsulServer() bool {
|
|
|
|
return m.Tags[MemberTagKeyRole] == MemberTagValueRoleServer
|
|
|
|
}
|
|
|
|
|
2017-09-05 19:22:20 +00:00
|
|
|
// AllSegments is used to select for all segments in MembersOpts.
|
|
|
|
const AllSegments = "_all"
|
|
|
|
|
2017-08-30 02:30:46 +00:00
|
|
|
// MembersOpts is used for querying member information.
|
|
|
|
type MembersOpts struct {
|
2017-09-01 00:39:46 +00:00
|
|
|
// WAN is whether to show members from the WAN.
|
|
|
|
WAN bool
|
2017-08-14 14:36:07 +00:00
|
|
|
|
2017-09-05 19:22:20 +00:00
|
|
|
// Segment is the LAN segment to show members for. Setting this to the
|
|
|
|
// AllSegments value above will show members in all segments.
|
2017-08-14 14:36:07 +00:00
|
|
|
Segment string
|
|
|
|
}
|
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
// AgentServiceRegistration is used to register a new service
|
|
|
|
type AgentServiceRegistration struct {
|
2019-06-17 14:51:50 +00:00
|
|
|
Kind ServiceKind `json:",omitempty"`
|
|
|
|
ID string `json:",omitempty"`
|
|
|
|
Name string `json:",omitempty"`
|
|
|
|
Tags []string `json:",omitempty"`
|
|
|
|
Port int `json:",omitempty"`
|
|
|
|
Address string `json:",omitempty"`
|
|
|
|
TaggedAddresses map[string]ServiceAddress `json:",omitempty"`
|
|
|
|
EnableTagOverride bool `json:",omitempty"`
|
|
|
|
Meta map[string]string `json:",omitempty"`
|
|
|
|
Weights *AgentWeights `json:",omitempty"`
|
2016-02-16 19:45:29 +00:00
|
|
|
Check *AgentServiceCheck
|
|
|
|
Checks AgentServiceChecks
|
2019-08-09 19:19:30 +00:00
|
|
|
Proxy *AgentServiceConnectProxyConfig `json:",omitempty"`
|
|
|
|
Connect *AgentServiceConnect `json:",omitempty"`
|
2019-12-10 02:26:41 +00:00
|
|
|
Namespace string `json:",omitempty" bexpr:"-" hash:"ignore"`
|
2015-01-06 18:40:00 +00:00
|
|
|
}
|
|
|
|
|
2020-09-03 16:22:03 +00:00
|
|
|
// ServiceRegisterOpts is used to pass extra options to the service register.
|
2020-02-03 09:11:40 +00:00
|
|
|
type ServiceRegisterOpts struct {
|
|
|
|
//Missing healthchecks will be deleted from the agent.
|
|
|
|
//Using this parameter allows to idempotently register a service and its checks without
|
|
|
|
//having to manually deregister checks.
|
|
|
|
ReplaceExistingChecks bool
|
2020-09-03 16:22:03 +00:00
|
|
|
|
|
|
|
// ctx is an optional context pass through to the underlying HTTP
|
|
|
|
// request layer. Use WithContext() to set the context.
|
|
|
|
ctx context.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithContext sets the context to be used for the request on a new ServiceRegisterOpts,
|
|
|
|
// and returns the opts.
|
|
|
|
func (o ServiceRegisterOpts) WithContext(ctx context.Context) ServiceRegisterOpts {
|
|
|
|
o.ctx = ctx
|
|
|
|
return o
|
2020-02-03 09:11:40 +00:00
|
|
|
}
|
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
// AgentCheckRegistration is used to register a new check
|
|
|
|
type AgentCheckRegistration struct {
|
2015-01-14 03:18:46 +00:00
|
|
|
ID string `json:",omitempty"`
|
|
|
|
Name string `json:",omitempty"`
|
|
|
|
Notes string `json:",omitempty"`
|
|
|
|
ServiceID string `json:",omitempty"`
|
2015-01-06 18:40:00 +00:00
|
|
|
AgentServiceCheck
|
2019-12-10 02:26:41 +00:00
|
|
|
Namespace string `json:",omitempty"`
|
2015-01-06 18:40:00 +00:00
|
|
|
}
|
|
|
|
|
2016-08-16 07:05:55 +00:00
|
|
|
// AgentServiceCheck is used to define a node or service level check
|
2015-01-06 18:40:00 +00:00
|
|
|
type AgentServiceCheck struct {
|
2020-06-29 12:52:35 +00:00
|
|
|
CheckID string `json:",omitempty"`
|
|
|
|
Name string `json:",omitempty"`
|
|
|
|
Args []string `json:"ScriptArgs,omitempty"`
|
|
|
|
DockerContainerID string `json:",omitempty"`
|
|
|
|
Shell string `json:",omitempty"` // Only supported for Docker.
|
|
|
|
Interval string `json:",omitempty"`
|
|
|
|
Timeout string `json:",omitempty"`
|
|
|
|
TTL string `json:",omitempty"`
|
|
|
|
HTTP string `json:",omitempty"`
|
|
|
|
Header map[string][]string `json:",omitempty"`
|
|
|
|
Method string `json:",omitempty"`
|
|
|
|
Body string `json:",omitempty"`
|
|
|
|
TCP string `json:",omitempty"`
|
|
|
|
Status string `json:",omitempty"`
|
|
|
|
Notes string `json:",omitempty"`
|
|
|
|
TLSSkipVerify bool `json:",omitempty"`
|
|
|
|
GRPC string `json:",omitempty"`
|
|
|
|
GRPCUseTLS bool `json:",omitempty"`
|
|
|
|
AliasNode string `json:",omitempty"`
|
|
|
|
AliasService string `json:",omitempty"`
|
|
|
|
SuccessBeforePassing int `json:",omitempty"`
|
|
|
|
FailuresBeforeCritical int `json:",omitempty"`
|
2016-08-16 07:05:55 +00:00
|
|
|
|
2016-08-16 16:27:20 +00:00
|
|
|
// In Consul 0.7 and later, checks that are associated with a service
|
|
|
|
// may also contain this optional DeregisterCriticalServiceAfter field,
|
|
|
|
// which is a timeout in the same Go time format as Interval and TTL. If
|
|
|
|
// a check is in the critical state for more than this configured value,
|
|
|
|
// then its associated service (and all of its associated checks) will
|
2016-08-16 07:05:55 +00:00
|
|
|
// automatically be deregistered.
|
|
|
|
DeregisterCriticalServiceAfter string `json:",omitempty"`
|
2015-01-06 18:40:00 +00:00
|
|
|
}
|
2015-01-14 03:18:46 +00:00
|
|
|
type AgentServiceChecks []*AgentServiceCheck
|
2015-01-06 18:40:00 +00:00
|
|
|
|
2017-07-26 18:03:43 +00:00
|
|
|
// AgentToken is used when updating ACL tokens for an agent.
|
|
|
|
type AgentToken struct {
|
|
|
|
Token string
|
|
|
|
}
|
|
|
|
|
2017-08-08 08:31:38 +00:00
|
|
|
// Metrics info is used to store different types of metric values from the agent.
|
|
|
|
type MetricsInfo struct {
|
|
|
|
Timestamp string
|
2017-08-08 19:33:59 +00:00
|
|
|
Gauges []GaugeValue
|
|
|
|
Points []PointValue
|
|
|
|
Counters []SampledValue
|
|
|
|
Samples []SampledValue
|
|
|
|
}
|
|
|
|
|
|
|
|
// GaugeValue stores one value that is updated as time goes on, such as
|
|
|
|
// the amount of memory allocated.
|
|
|
|
type GaugeValue struct {
|
|
|
|
Name string
|
|
|
|
Value float32
|
|
|
|
Labels map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
// PointValue holds a series of points for a metric.
|
|
|
|
type PointValue struct {
|
|
|
|
Name string
|
|
|
|
Points []float32
|
|
|
|
}
|
|
|
|
|
|
|
|
// SampledValue stores info about a metric that is incremented over time,
|
|
|
|
// such as the number of requests to an HTTP endpoint.
|
|
|
|
type SampledValue struct {
|
|
|
|
Name string
|
|
|
|
Count int
|
|
|
|
Sum float64
|
|
|
|
Min float64
|
|
|
|
Max float64
|
|
|
|
Mean float64
|
|
|
|
Stddev float64
|
|
|
|
Labels map[string]string
|
2017-08-08 08:31:38 +00:00
|
|
|
}
|
|
|
|
|
2018-03-27 23:50:17 +00:00
|
|
|
// AgentAuthorizeParams are the request parameters for authorizing a request.
|
|
|
|
type AgentAuthorizeParams struct {
|
|
|
|
Target string
|
2018-03-28 21:20:25 +00:00
|
|
|
ClientCertURI string
|
2018-03-27 23:50:17 +00:00
|
|
|
ClientCertSerial string
|
|
|
|
}
|
|
|
|
|
|
|
|
// AgentAuthorize is the response structure for Connect authorization.
|
|
|
|
type AgentAuthorize struct {
|
|
|
|
Authorized bool
|
|
|
|
Reason string
|
|
|
|
}
|
|
|
|
|
2018-04-20 13:24:24 +00:00
|
|
|
// ConnectProxyConfig is the response structure for agent-local proxy
|
|
|
|
// configuration.
|
|
|
|
type ConnectProxyConfig struct {
|
|
|
|
ProxyServiceID string
|
|
|
|
TargetServiceID string
|
|
|
|
TargetServiceName string
|
|
|
|
ContentHash string
|
2019-08-09 19:19:30 +00:00
|
|
|
Config map[string]interface{} `bexpr:"-"`
|
|
|
|
Upstreams []Upstream
|
2018-09-12 16:07:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Upstream is the response structure for a proxy upstream configuration.
|
|
|
|
type Upstream struct {
|
|
|
|
DestinationType UpstreamDestType `json:",omitempty"`
|
|
|
|
DestinationNamespace string `json:",omitempty"`
|
|
|
|
DestinationName string
|
|
|
|
Datacenter string `json:",omitempty"`
|
|
|
|
LocalBindAddress string `json:",omitempty"`
|
|
|
|
LocalBindPort int `json:",omitempty"`
|
2019-04-16 16:00:15 +00:00
|
|
|
Config map[string]interface{} `json:",omitempty" bexpr:"-"`
|
2019-07-01 23:47:58 +00:00
|
|
|
MeshGateway MeshGatewayConfig `json:",omitempty"`
|
2018-04-20 13:24:24 +00:00
|
|
|
}
|
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
// Agent can be used to query the Agent endpoints
|
|
|
|
type Agent struct {
|
|
|
|
c *Client
|
|
|
|
|
|
|
|
// cache the node name
|
|
|
|
nodeName string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Agent returns a handle to the agent endpoints
|
|
|
|
func (c *Client) Agent() *Agent {
|
|
|
|
return &Agent{c: c}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Self is used to query the agent we are speaking to for
|
|
|
|
// information about itself
|
|
|
|
func (a *Agent) Self() (map[string]map[string]interface{}, error) {
|
|
|
|
r := a.c.newRequest("GET", "/v1/agent/self")
|
|
|
|
_, resp, err := requireOK(a.c.doRequest(r))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
var out map[string]map[string]interface{}
|
|
|
|
if err := decodeBody(resp, &out); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2018-10-17 20:20:35 +00:00
|
|
|
// Host is used to retrieve information about the host the
|
|
|
|
// agent is running on such as CPU, memory, and disk. Requires
|
|
|
|
// a operator:read ACL token.
|
|
|
|
func (a *Agent) Host() (map[string]interface{}, error) {
|
|
|
|
r := a.c.newRequest("GET", "/v1/agent/host")
|
|
|
|
_, resp, err := requireOK(a.c.doRequest(r))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
var out map[string]interface{}
|
|
|
|
if err := decodeBody(resp, &out); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2017-08-08 08:31:38 +00:00
|
|
|
// Metrics is used to query the agent we are speaking to for
|
|
|
|
// its current internal metric data
|
|
|
|
func (a *Agent) Metrics() (*MetricsInfo, error) {
|
|
|
|
r := a.c.newRequest("GET", "/v1/agent/metrics")
|
|
|
|
_, resp, err := requireOK(a.c.doRequest(r))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
var out *MetricsInfo
|
|
|
|
if err := decodeBody(resp, &out); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2016-11-30 18:29:42 +00:00
|
|
|
// Reload triggers a configuration reload for the agent we are connected to.
|
|
|
|
func (a *Agent) Reload() error {
|
|
|
|
r := a.c.newRequest("PUT", "/v1/agent/reload")
|
|
|
|
_, resp, err := requireOK(a.c.doRequest(r))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resp.Body.Close()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
// NodeName is used to get the node name of the agent
|
|
|
|
func (a *Agent) NodeName() (string, error) {
|
|
|
|
if a.nodeName != "" {
|
|
|
|
return a.nodeName, nil
|
|
|
|
}
|
|
|
|
info, err := a.Self()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
name := info["Config"]["NodeName"].(string)
|
|
|
|
a.nodeName = name
|
|
|
|
return name, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checks returns the locally registered checks
|
|
|
|
func (a *Agent) Checks() (map[string]*AgentCheck, error) {
|
2019-04-16 16:00:15 +00:00
|
|
|
return a.ChecksWithFilter("")
|
|
|
|
}
|
|
|
|
|
|
|
|
// ChecksWithFilter returns a subset of the locally registered checks that match
|
|
|
|
// the given filter expression
|
|
|
|
func (a *Agent) ChecksWithFilter(filter string) (map[string]*AgentCheck, error) {
|
2015-01-06 18:40:00 +00:00
|
|
|
r := a.c.newRequest("GET", "/v1/agent/checks")
|
2019-04-16 16:00:15 +00:00
|
|
|
r.filterQuery(filter)
|
2015-01-06 18:40:00 +00:00
|
|
|
_, resp, err := requireOK(a.c.doRequest(r))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
var out map[string]*AgentCheck
|
|
|
|
if err := decodeBody(resp, &out); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Services returns the locally registered services
|
|
|
|
func (a *Agent) Services() (map[string]*AgentService, error) {
|
2019-04-16 16:00:15 +00:00
|
|
|
return a.ServicesWithFilter("")
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServicesWithFilter returns a subset of the locally registered services that match
|
|
|
|
// the given filter expression
|
|
|
|
func (a *Agent) ServicesWithFilter(filter string) (map[string]*AgentService, error) {
|
2015-01-06 18:40:00 +00:00
|
|
|
r := a.c.newRequest("GET", "/v1/agent/services")
|
2019-04-16 16:00:15 +00:00
|
|
|
r.filterQuery(filter)
|
2015-01-06 18:40:00 +00:00
|
|
|
_, resp, err := requireOK(a.c.doRequest(r))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
var out map[string]*AgentService
|
|
|
|
if err := decodeBody(resp, &out); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-04-20 13:24:24 +00:00
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2019-01-07 14:39:23 +00:00
|
|
|
// AgentHealthServiceByID returns for a given serviceID: the aggregated health status, the service definition or an error if any
|
|
|
|
// - If the service is not found, will return status (critical, nil, nil)
|
|
|
|
// - If the service is found, will return (critical|passing|warning), AgentServiceChecksInfo, nil)
|
|
|
|
// - In all other cases, will return an error
|
|
|
|
func (a *Agent) AgentHealthServiceByID(serviceID string) (string, *AgentServiceChecksInfo, error) {
|
|
|
|
path := fmt.Sprintf("/v1/agent/health/service/id/%v", url.PathEscape(serviceID))
|
|
|
|
r := a.c.newRequest("GET", path)
|
|
|
|
r.params.Add("format", "json")
|
|
|
|
r.header.Set("Accept", "application/json")
|
|
|
|
_, resp, err := a.c.doRequest(r)
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
// Service not Found
|
|
|
|
if resp.StatusCode == http.StatusNotFound {
|
|
|
|
return HealthCritical, nil, nil
|
|
|
|
}
|
|
|
|
var out *AgentServiceChecksInfo
|
|
|
|
if err := decodeBody(resp, &out); err != nil {
|
|
|
|
return HealthCritical, out, err
|
|
|
|
}
|
|
|
|
switch resp.StatusCode {
|
|
|
|
case http.StatusOK:
|
|
|
|
return HealthPassing, out, nil
|
|
|
|
case http.StatusTooManyRequests:
|
|
|
|
return HealthWarning, out, nil
|
|
|
|
case http.StatusServiceUnavailable:
|
|
|
|
return HealthCritical, out, nil
|
|
|
|
}
|
|
|
|
return HealthCritical, out, fmt.Errorf("Unexpected Error Code %v for %s", resp.StatusCode, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// AgentHealthServiceByName returns for a given service name: the aggregated health status for all services
|
|
|
|
// having the specified name.
|
|
|
|
// - If no service is not found, will return status (critical, [], nil)
|
|
|
|
// - If the service is found, will return (critical|passing|warning), []api.AgentServiceChecksInfo, nil)
|
|
|
|
// - In all other cases, will return an error
|
|
|
|
func (a *Agent) AgentHealthServiceByName(service string) (string, []AgentServiceChecksInfo, error) {
|
|
|
|
path := fmt.Sprintf("/v1/agent/health/service/name/%v", url.PathEscape(service))
|
|
|
|
r := a.c.newRequest("GET", path)
|
|
|
|
r.params.Add("format", "json")
|
|
|
|
r.header.Set("Accept", "application/json")
|
|
|
|
_, resp, err := a.c.doRequest(r)
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
// Service not Found
|
|
|
|
if resp.StatusCode == http.StatusNotFound {
|
|
|
|
return HealthCritical, nil, nil
|
|
|
|
}
|
|
|
|
var out []AgentServiceChecksInfo
|
|
|
|
if err := decodeBody(resp, &out); err != nil {
|
|
|
|
return HealthCritical, out, err
|
|
|
|
}
|
|
|
|
switch resp.StatusCode {
|
|
|
|
case http.StatusOK:
|
|
|
|
return HealthPassing, out, nil
|
|
|
|
case http.StatusTooManyRequests:
|
|
|
|
return HealthWarning, out, nil
|
|
|
|
case http.StatusServiceUnavailable:
|
|
|
|
return HealthCritical, out, nil
|
|
|
|
}
|
|
|
|
return HealthCritical, out, fmt.Errorf("Unexpected Error Code %v for %s", resp.StatusCode, path)
|
|
|
|
}
|
|
|
|
|
2018-09-27 14:00:51 +00:00
|
|
|
// Service returns a locally registered service instance and allows for
|
|
|
|
// hash-based blocking.
|
|
|
|
//
|
|
|
|
// Note that this uses an unconventional blocking mechanism since it's
|
|
|
|
// agent-local state. That means there is no persistent raft index so we block
|
|
|
|
// based on object hash instead.
|
|
|
|
func (a *Agent) Service(serviceID string, q *QueryOptions) (*AgentService, *QueryMeta, error) {
|
|
|
|
r := a.c.newRequest("GET", "/v1/agent/service/"+serviceID)
|
|
|
|
r.setQueryOptions(q)
|
|
|
|
rtt, resp, err := requireOK(a.c.doRequest(r))
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
qm := &QueryMeta{}
|
|
|
|
parseQueryMeta(resp, qm)
|
|
|
|
qm.RequestTime = rtt
|
|
|
|
|
|
|
|
var out *AgentService
|
|
|
|
if err := decodeBody(resp, &out); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, qm, nil
|
|
|
|
}
|
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
// Members returns the known gossip members. The WAN
|
|
|
|
// flag can be used to query a server for WAN members.
|
|
|
|
func (a *Agent) Members(wan bool) ([]*AgentMember, error) {
|
|
|
|
r := a.c.newRequest("GET", "/v1/agent/members")
|
|
|
|
if wan {
|
|
|
|
r.params.Set("wan", "1")
|
|
|
|
}
|
|
|
|
_, resp, err := requireOK(a.c.doRequest(r))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
var out []*AgentMember
|
|
|
|
if err := decodeBody(resp, &out); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2017-08-30 02:30:46 +00:00
|
|
|
// MembersOpts returns the known gossip members and can be passed
|
|
|
|
// additional options for WAN/segment filtering.
|
|
|
|
func (a *Agent) MembersOpts(opts MembersOpts) ([]*AgentMember, error) {
|
2017-08-14 14:36:07 +00:00
|
|
|
r := a.c.newRequest("GET", "/v1/agent/members")
|
2017-08-30 02:30:46 +00:00
|
|
|
r.params.Set("segment", opts.Segment)
|
2017-09-01 00:39:46 +00:00
|
|
|
if opts.WAN {
|
2017-08-14 14:36:07 +00:00
|
|
|
r.params.Set("wan", "1")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, resp, err := requireOK(a.c.doRequest(r))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
var out []*AgentMember
|
|
|
|
if err := decodeBody(resp, &out); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
// ServiceRegister is used to register a new service with
|
|
|
|
// the local agent
|
|
|
|
func (a *Agent) ServiceRegister(service *AgentServiceRegistration) error {
|
2020-02-03 09:11:40 +00:00
|
|
|
opts := ServiceRegisterOpts{
|
|
|
|
ReplaceExistingChecks: false,
|
|
|
|
}
|
|
|
|
|
|
|
|
return a.serviceRegister(service, opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServiceRegister is used to register a new service with
|
|
|
|
// the local agent and can be passed additional options.
|
|
|
|
func (a *Agent) ServiceRegisterOpts(service *AgentServiceRegistration, opts ServiceRegisterOpts) error {
|
|
|
|
return a.serviceRegister(service, opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Agent) serviceRegister(service *AgentServiceRegistration, opts ServiceRegisterOpts) error {
|
2015-01-06 18:40:00 +00:00
|
|
|
r := a.c.newRequest("PUT", "/v1/agent/service/register")
|
|
|
|
r.obj = service
|
2020-09-03 16:22:03 +00:00
|
|
|
r.ctx = opts.ctx
|
2020-02-03 09:11:40 +00:00
|
|
|
if opts.ReplaceExistingChecks {
|
|
|
|
r.params.Set("replace-existing-checks", "true")
|
|
|
|
}
|
2015-01-06 18:40:00 +00:00
|
|
|
_, resp, err := requireOK(a.c.doRequest(r))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resp.Body.Close()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServiceDeregister is used to deregister a service with
|
|
|
|
// the local agent
|
|
|
|
func (a *Agent) ServiceDeregister(serviceID string) error {
|
|
|
|
r := a.c.newRequest("PUT", "/v1/agent/service/deregister/"+serviceID)
|
|
|
|
_, resp, err := requireOK(a.c.doRequest(r))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resp.Body.Close()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-23 23:01:59 +00:00
|
|
|
// PassTTL is used to set a TTL check to the passing state.
|
|
|
|
//
|
|
|
|
// DEPRECATION NOTICE: This interface is deprecated in favor of UpdateTTL().
|
|
|
|
// The client interface will be removed in 0.8 or changed to use
|
|
|
|
// UpdateTTL()'s endpoint and the server endpoints will be removed in 0.9.
|
2015-01-06 18:40:00 +00:00
|
|
|
func (a *Agent) PassTTL(checkID, note string) error {
|
2016-03-04 23:18:25 +00:00
|
|
|
return a.updateTTL(checkID, note, "pass")
|
2015-01-06 18:40:00 +00:00
|
|
|
}
|
|
|
|
|
2016-04-23 23:01:59 +00:00
|
|
|
// WarnTTL is used to set a TTL check to the warning state.
|
|
|
|
//
|
|
|
|
// DEPRECATION NOTICE: This interface is deprecated in favor of UpdateTTL().
|
|
|
|
// The client interface will be removed in 0.8 or changed to use
|
|
|
|
// UpdateTTL()'s endpoint and the server endpoints will be removed in 0.9.
|
2015-01-06 18:40:00 +00:00
|
|
|
func (a *Agent) WarnTTL(checkID, note string) error {
|
2016-03-04 23:18:25 +00:00
|
|
|
return a.updateTTL(checkID, note, "warn")
|
2015-01-06 18:40:00 +00:00
|
|
|
}
|
|
|
|
|
2016-04-23 23:01:59 +00:00
|
|
|
// FailTTL is used to set a TTL check to the failing state.
|
|
|
|
//
|
|
|
|
// DEPRECATION NOTICE: This interface is deprecated in favor of UpdateTTL().
|
|
|
|
// The client interface will be removed in 0.8 or changed to use
|
|
|
|
// UpdateTTL()'s endpoint and the server endpoints will be removed in 0.9.
|
2015-01-06 18:40:00 +00:00
|
|
|
func (a *Agent) FailTTL(checkID, note string) error {
|
2016-03-04 23:18:25 +00:00
|
|
|
return a.updateTTL(checkID, note, "fail")
|
2015-01-06 18:40:00 +00:00
|
|
|
}
|
|
|
|
|
2016-03-04 23:18:25 +00:00
|
|
|
// updateTTL is used to update the TTL of a check. This is the internal
|
2016-04-23 23:01:59 +00:00
|
|
|
// method that uses the old API that's present in Consul versions prior to
|
|
|
|
// 0.6.4. Since Consul didn't have an analogous "update" API before it seemed
|
|
|
|
// ok to break this (former) UpdateTTL in favor of the new UpdateTTL below,
|
|
|
|
// but keep the old Pass/Warn/Fail methods using the old API under the hood.
|
|
|
|
//
|
|
|
|
// DEPRECATION NOTICE: This interface is deprecated in favor of UpdateTTL().
|
|
|
|
// The client interface will be removed in 0.8 and the server endpoints will
|
|
|
|
// be removed in 0.9.
|
2016-03-04 23:18:25 +00:00
|
|
|
func (a *Agent) updateTTL(checkID, note, status string) error {
|
2015-01-06 18:40:00 +00:00
|
|
|
switch status {
|
|
|
|
case "pass":
|
|
|
|
case "warn":
|
|
|
|
case "fail":
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("Invalid status: %s", status)
|
|
|
|
}
|
|
|
|
endpoint := fmt.Sprintf("/v1/agent/check/%s/%s", status, checkID)
|
|
|
|
r := a.c.newRequest("PUT", endpoint)
|
|
|
|
r.params.Set("note", note)
|
|
|
|
_, resp, err := requireOK(a.c.doRequest(r))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resp.Body.Close()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-03-04 23:18:25 +00:00
|
|
|
// checkUpdate is the payload for a PUT for a check update.
|
|
|
|
type checkUpdate struct {
|
2016-04-24 03:18:19 +00:00
|
|
|
// Status is one of the api.Health* states: HealthPassing
|
2016-04-23 23:01:59 +00:00
|
|
|
// ("passing"), HealthWarning ("warning"), or HealthCritical
|
|
|
|
// ("critical").
|
2016-03-04 23:18:25 +00:00
|
|
|
Status string
|
|
|
|
|
|
|
|
// Output is the information to post to the UI for operators as the
|
|
|
|
// output of the process that decided to hit the TTL check. This is
|
|
|
|
// different from the note field that's associated with the check
|
|
|
|
// itself.
|
|
|
|
Output string
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateTTL is used to update the TTL of a check. This uses the newer API
|
|
|
|
// that was introduced in Consul 0.6.4 and later. We translate the old status
|
|
|
|
// strings for compatibility (though a newer version of Consul will still be
|
|
|
|
// required to use this API).
|
|
|
|
func (a *Agent) UpdateTTL(checkID, output, status string) error {
|
|
|
|
switch status {
|
2016-03-24 18:26:07 +00:00
|
|
|
case "pass", HealthPassing:
|
|
|
|
status = HealthPassing
|
|
|
|
case "warn", HealthWarning:
|
|
|
|
status = HealthWarning
|
|
|
|
case "fail", HealthCritical:
|
|
|
|
status = HealthCritical
|
2016-03-04 23:18:25 +00:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("Invalid status: %s", status)
|
|
|
|
}
|
|
|
|
|
|
|
|
endpoint := fmt.Sprintf("/v1/agent/check/update/%s", checkID)
|
|
|
|
r := a.c.newRequest("PUT", endpoint)
|
|
|
|
r.obj = &checkUpdate{
|
|
|
|
Status: status,
|
|
|
|
Output: output,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, resp, err := requireOK(a.c.doRequest(r))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resp.Body.Close()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
// CheckRegister is used to register a new check with
|
|
|
|
// the local agent
|
|
|
|
func (a *Agent) CheckRegister(check *AgentCheckRegistration) error {
|
|
|
|
r := a.c.newRequest("PUT", "/v1/agent/check/register")
|
|
|
|
r.obj = check
|
|
|
|
_, resp, err := requireOK(a.c.doRequest(r))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resp.Body.Close()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CheckDeregister is used to deregister a check with
|
|
|
|
// the local agent
|
|
|
|
func (a *Agent) CheckDeregister(checkID string) error {
|
|
|
|
r := a.c.newRequest("PUT", "/v1/agent/check/deregister/"+checkID)
|
|
|
|
_, resp, err := requireOK(a.c.doRequest(r))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resp.Body.Close()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Join is used to instruct the agent to attempt a join to
|
|
|
|
// another cluster member
|
|
|
|
func (a *Agent) Join(addr string, wan bool) error {
|
|
|
|
r := a.c.newRequest("PUT", "/v1/agent/join/"+addr)
|
|
|
|
if wan {
|
|
|
|
r.params.Set("wan", "1")
|
|
|
|
}
|
|
|
|
_, resp, err := requireOK(a.c.doRequest(r))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resp.Body.Close()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-11-30 18:29:42 +00:00
|
|
|
// Leave is used to have the agent gracefully leave the cluster and shutdown
|
|
|
|
func (a *Agent) Leave() error {
|
|
|
|
r := a.c.newRequest("PUT", "/v1/agent/leave")
|
|
|
|
_, resp, err := requireOK(a.c.doRequest(r))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resp.Body.Close()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
// ForceLeave is used to have the agent eject a failed node
|
|
|
|
func (a *Agent) ForceLeave(node string) error {
|
|
|
|
r := a.c.newRequest("PUT", "/v1/agent/force-leave/"+node)
|
|
|
|
_, resp, err := requireOK(a.c.doRequest(r))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resp.Body.Close()
|
|
|
|
return nil
|
|
|
|
}
|
2015-01-21 18:51:26 +00:00
|
|
|
|
2019-10-04 21:10:02 +00:00
|
|
|
//ForceLeavePrune is used to have an a failed agent removed
|
|
|
|
//from the list of members
|
|
|
|
func (a *Agent) ForceLeavePrune(node string) error {
|
|
|
|
r := a.c.newRequest("PUT", "/v1/agent/force-leave/"+node)
|
|
|
|
r.params.Set("prune", "1")
|
|
|
|
_, resp, err := requireOK(a.c.doRequest(r))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resp.Body.Close()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-27 23:50:17 +00:00
|
|
|
// ConnectAuthorize is used to authorize an incoming connection
|
|
|
|
// to a natively integrated Connect service.
|
|
|
|
func (a *Agent) ConnectAuthorize(auth *AgentAuthorizeParams) (*AgentAuthorize, error) {
|
|
|
|
r := a.c.newRequest("POST", "/v1/agent/connect/authorize")
|
|
|
|
r.obj = auth
|
|
|
|
_, resp, err := requireOK(a.c.doRequest(r))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-04-05 11:53:42 +00:00
|
|
|
defer resp.Body.Close()
|
2018-03-27 23:50:17 +00:00
|
|
|
|
|
|
|
var out AgentAuthorize
|
|
|
|
if err := decodeBody(resp, &out); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConnectCARoots returns the list of roots.
|
|
|
|
func (a *Agent) ConnectCARoots(q *QueryOptions) (*CARootList, *QueryMeta, error) {
|
|
|
|
r := a.c.newRequest("GET", "/v1/agent/connect/ca/roots")
|
|
|
|
r.setQueryOptions(q)
|
|
|
|
rtt, resp, err := requireOK(a.c.doRequest(r))
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
qm := &QueryMeta{}
|
|
|
|
parseQueryMeta(resp, qm)
|
|
|
|
qm.RequestTime = rtt
|
|
|
|
|
|
|
|
var out CARootList
|
|
|
|
if err := decodeBody(resp, &out); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return &out, qm, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConnectCALeaf gets the leaf certificate for the given service ID.
|
2018-03-28 21:16:41 +00:00
|
|
|
func (a *Agent) ConnectCALeaf(serviceID string, q *QueryOptions) (*LeafCert, *QueryMeta, error) {
|
2018-03-27 23:50:17 +00:00
|
|
|
r := a.c.newRequest("GET", "/v1/agent/connect/ca/leaf/"+serviceID)
|
|
|
|
r.setQueryOptions(q)
|
|
|
|
rtt, resp, err := requireOK(a.c.doRequest(r))
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
qm := &QueryMeta{}
|
|
|
|
parseQueryMeta(resp, qm)
|
|
|
|
qm.RequestTime = rtt
|
|
|
|
|
2018-03-28 21:16:41 +00:00
|
|
|
var out LeafCert
|
2018-03-27 23:50:17 +00:00
|
|
|
if err := decodeBody(resp, &out); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return &out, qm, nil
|
|
|
|
}
|
|
|
|
|
2015-01-21 18:51:26 +00:00
|
|
|
// EnableServiceMaintenance toggles service maintenance mode on
|
|
|
|
// for the given service ID.
|
2015-01-21 21:02:47 +00:00
|
|
|
func (a *Agent) EnableServiceMaintenance(serviceID, reason string) error {
|
2015-01-21 18:51:26 +00:00
|
|
|
r := a.c.newRequest("PUT", "/v1/agent/service/maintenance/"+serviceID)
|
|
|
|
r.params.Set("enable", "true")
|
2015-01-21 21:02:47 +00:00
|
|
|
r.params.Set("reason", reason)
|
2015-01-21 18:51:26 +00:00
|
|
|
_, resp, err := requireOK(a.c.doRequest(r))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resp.Body.Close()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DisableServiceMaintenance toggles service maintenance mode off
|
|
|
|
// for the given service ID.
|
|
|
|
func (a *Agent) DisableServiceMaintenance(serviceID string) error {
|
|
|
|
r := a.c.newRequest("PUT", "/v1/agent/service/maintenance/"+serviceID)
|
|
|
|
r.params.Set("enable", "false")
|
|
|
|
_, resp, err := requireOK(a.c.doRequest(r))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resp.Body.Close()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// EnableNodeMaintenance toggles node maintenance mode on for the
|
|
|
|
// agent we are connected to.
|
2015-01-21 21:02:47 +00:00
|
|
|
func (a *Agent) EnableNodeMaintenance(reason string) error {
|
2015-01-21 18:51:26 +00:00
|
|
|
r := a.c.newRequest("PUT", "/v1/agent/maintenance")
|
|
|
|
r.params.Set("enable", "true")
|
2015-01-21 21:02:47 +00:00
|
|
|
r.params.Set("reason", reason)
|
2015-01-21 18:51:26 +00:00
|
|
|
_, resp, err := requireOK(a.c.doRequest(r))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resp.Body.Close()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DisableNodeMaintenance toggles node maintenance mode off for the
|
|
|
|
// agent we are connected to.
|
|
|
|
func (a *Agent) DisableNodeMaintenance() error {
|
|
|
|
r := a.c.newRequest("PUT", "/v1/agent/maintenance")
|
|
|
|
r.params.Set("enable", "false")
|
|
|
|
_, resp, err := requireOK(a.c.doRequest(r))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resp.Body.Close()
|
|
|
|
return nil
|
|
|
|
}
|
2016-11-16 21:45:26 +00:00
|
|
|
|
|
|
|
// Monitor returns a channel which will receive streaming logs from the agent
|
|
|
|
// Providing a non-nil stopCh can be used to close the connection and stop the
|
2017-08-08 22:52:55 +00:00
|
|
|
// log stream. An empty string will be sent down the given channel when there's
|
|
|
|
// nothing left to stream, after which the caller should close the stopCh.
|
2017-07-14 23:31:44 +00:00
|
|
|
func (a *Agent) Monitor(loglevel string, stopCh <-chan struct{}, q *QueryOptions) (chan string, error) {
|
2020-01-28 23:50:41 +00:00
|
|
|
return a.monitor(loglevel, false, stopCh, q)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MonitorJSON is like Monitor except it returns logs in JSON format.
|
|
|
|
func (a *Agent) MonitorJSON(loglevel string, stopCh <-chan struct{}, q *QueryOptions) (chan string, error) {
|
|
|
|
return a.monitor(loglevel, true, stopCh, q)
|
|
|
|
}
|
|
|
|
func (a *Agent) monitor(loglevel string, logJSON bool, stopCh <-chan struct{}, q *QueryOptions) (chan string, error) {
|
2016-11-16 21:45:26 +00:00
|
|
|
r := a.c.newRequest("GET", "/v1/agent/monitor")
|
2016-11-28 21:13:49 +00:00
|
|
|
r.setQueryOptions(q)
|
2016-11-16 21:45:26 +00:00
|
|
|
if loglevel != "" {
|
|
|
|
r.params.Add("loglevel", loglevel)
|
|
|
|
}
|
2020-01-28 23:50:41 +00:00
|
|
|
if logJSON {
|
|
|
|
r.params.Set("logjson", "true")
|
|
|
|
}
|
2016-11-16 21:45:26 +00:00
|
|
|
_, resp, err := requireOK(a.c.doRequest(r))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
logCh := make(chan string, 64)
|
|
|
|
go func() {
|
|
|
|
defer resp.Body.Close()
|
|
|
|
scanner := bufio.NewScanner(resp.Body)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-stopCh:
|
|
|
|
close(logCh)
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
if scanner.Scan() {
|
2017-08-08 22:52:55 +00:00
|
|
|
// An empty string signals to the caller that
|
|
|
|
// the scan is done, so make sure we only emit
|
|
|
|
// that when the scanner says it's done, not if
|
|
|
|
// we happen to ingest an empty line.
|
|
|
|
if text := scanner.Text(); text != "" {
|
|
|
|
logCh <- text
|
|
|
|
} else {
|
|
|
|
logCh <- " "
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logCh <- ""
|
2016-11-16 21:45:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return logCh, nil
|
|
|
|
}
|
2017-07-26 18:03:43 +00:00
|
|
|
|
|
|
|
// UpdateACLToken updates the agent's "acl_token". See updateToken for more
|
|
|
|
// details.
|
2019-02-27 19:28:31 +00:00
|
|
|
//
|
|
|
|
// DEPRECATED (ACL-Legacy-Compat) - Prefer UpdateDefaultACLToken for v1.4.3 and above
|
2018-01-28 18:40:13 +00:00
|
|
|
func (a *Agent) UpdateACLToken(token string, q *WriteOptions) (*WriteMeta, error) {
|
|
|
|
return a.updateToken("acl_token", token, q)
|
2017-07-26 18:03:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateACLAgentToken updates the agent's "acl_agent_token". See updateToken
|
|
|
|
// for more details.
|
2019-02-27 19:28:31 +00:00
|
|
|
//
|
|
|
|
// DEPRECATED (ACL-Legacy-Compat) - Prefer UpdateAgentACLToken for v1.4.3 and above
|
2018-01-28 18:40:13 +00:00
|
|
|
func (a *Agent) UpdateACLAgentToken(token string, q *WriteOptions) (*WriteMeta, error) {
|
|
|
|
return a.updateToken("acl_agent_token", token, q)
|
2017-07-26 18:03:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateACLAgentMasterToken updates the agent's "acl_agent_master_token". See
|
|
|
|
// updateToken for more details.
|
2019-02-27 19:28:31 +00:00
|
|
|
//
|
|
|
|
// DEPRECATED (ACL-Legacy-Compat) - Prefer UpdateAgentMasterACLToken for v1.4.3 and above
|
2018-01-28 18:40:13 +00:00
|
|
|
func (a *Agent) UpdateACLAgentMasterToken(token string, q *WriteOptions) (*WriteMeta, error) {
|
|
|
|
return a.updateToken("acl_agent_master_token", token, q)
|
2017-07-26 18:03:43 +00:00
|
|
|
}
|
|
|
|
|
2017-08-03 22:39:31 +00:00
|
|
|
// UpdateACLReplicationToken updates the agent's "acl_replication_token". See
|
|
|
|
// updateToken for more details.
|
2019-02-27 19:28:31 +00:00
|
|
|
//
|
|
|
|
// DEPRECATED (ACL-Legacy-Compat) - Prefer UpdateReplicationACLToken for v1.4.3 and above
|
2018-01-28 18:40:13 +00:00
|
|
|
func (a *Agent) UpdateACLReplicationToken(token string, q *WriteOptions) (*WriteMeta, error) {
|
|
|
|
return a.updateToken("acl_replication_token", token, q)
|
2017-08-03 22:39:31 +00:00
|
|
|
}
|
|
|
|
|
2019-02-27 19:28:31 +00:00
|
|
|
// UpdateDefaultACLToken updates the agent's "default" token. See updateToken
|
|
|
|
// for more details
|
|
|
|
func (a *Agent) UpdateDefaultACLToken(token string, q *WriteOptions) (*WriteMeta, error) {
|
|
|
|
return a.updateTokenFallback("default", "acl_token", token, q)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateAgentACLToken updates the agent's "agent" token. See updateToken
|
|
|
|
// for more details
|
|
|
|
func (a *Agent) UpdateAgentACLToken(token string, q *WriteOptions) (*WriteMeta, error) {
|
|
|
|
return a.updateTokenFallback("agent", "acl_agent_token", token, q)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateAgentMasterACLToken updates the agent's "agent_master" token. See updateToken
|
|
|
|
// for more details
|
|
|
|
func (a *Agent) UpdateAgentMasterACLToken(token string, q *WriteOptions) (*WriteMeta, error) {
|
|
|
|
return a.updateTokenFallback("agent_master", "acl_agent_master_token", token, q)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateReplicationACLToken updates the agent's "replication" token. See updateToken
|
|
|
|
// for more details
|
|
|
|
func (a *Agent) UpdateReplicationACLToken(token string, q *WriteOptions) (*WriteMeta, error) {
|
|
|
|
return a.updateTokenFallback("replication", "acl_replication_token", token, q)
|
|
|
|
}
|
|
|
|
|
|
|
|
// updateToken can be used to update one of an agent's ACL tokens after the agent has
|
|
|
|
// started. The tokens are may not be persisted, so will need to be updated again if
|
|
|
|
// the agent is restarted unless the agent is configured to persist them.
|
2018-01-28 18:40:13 +00:00
|
|
|
func (a *Agent) updateToken(target, token string, q *WriteOptions) (*WriteMeta, error) {
|
2019-02-27 19:28:31 +00:00
|
|
|
meta, _, err := a.updateTokenOnce(target, token, q)
|
|
|
|
return meta, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Agent) updateTokenFallback(target, fallback, token string, q *WriteOptions) (*WriteMeta, error) {
|
|
|
|
meta, status, err := a.updateTokenOnce(target, token, q)
|
|
|
|
if err != nil && status == 404 {
|
|
|
|
meta, _, err = a.updateTokenOnce(fallback, token, q)
|
|
|
|
}
|
|
|
|
return meta, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Agent) updateTokenOnce(target, token string, q *WriteOptions) (*WriteMeta, int, error) {
|
2018-01-28 18:40:13 +00:00
|
|
|
r := a.c.newRequest("PUT", fmt.Sprintf("/v1/agent/token/%s", target))
|
2017-07-26 18:03:43 +00:00
|
|
|
r.setWriteOptions(q)
|
|
|
|
r.obj = &AgentToken{Token: token}
|
2019-03-21 20:04:40 +00:00
|
|
|
|
|
|
|
rtt, resp, err := a.c.doRequest(r)
|
2017-07-26 18:03:43 +00:00
|
|
|
if err != nil {
|
2019-03-21 20:04:40 +00:00
|
|
|
return nil, 0, err
|
2017-07-26 18:03:43 +00:00
|
|
|
}
|
2019-03-21 20:04:40 +00:00
|
|
|
defer resp.Body.Close()
|
2017-07-26 18:03:43 +00:00
|
|
|
|
|
|
|
wm := &WriteMeta{RequestTime: rtt}
|
2019-03-21 20:04:40 +00:00
|
|
|
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
io.Copy(&buf, resp.Body)
|
|
|
|
return wm, resp.StatusCode, fmt.Errorf("Unexpected response code: %d (%s)", resp.StatusCode, buf.Bytes())
|
|
|
|
}
|
|
|
|
|
2019-02-27 19:28:31 +00:00
|
|
|
return wm, resp.StatusCode, nil
|
2017-07-26 18:03:43 +00:00
|
|
|
}
|