2015-03-28 18:52:04 +00:00
|
|
|
package consul
|
|
|
|
|
2015-04-09 20:23:14 +00:00
|
|
|
import (
|
2015-06-23 02:14:02 +00:00
|
|
|
"fmt"
|
2017-03-14 05:56:24 +00:00
|
|
|
"strings"
|
2015-06-29 22:53:29 +00:00
|
|
|
"sync"
|
2015-05-08 08:31:34 +00:00
|
|
|
"time"
|
|
|
|
|
2017-08-23 14:52:48 +00:00
|
|
|
"github.com/hashicorp/consul/acl"
|
pkg refactor
command/agent/* -> agent/*
command/consul/* -> agent/consul/*
command/agent/command{,_test}.go -> command/agent{,_test}.go
command/base/command.go -> command/base.go
command/base/* -> command/*
commands.go -> command/commands.go
The script which did the refactor is:
(
cd $GOPATH/src/github.com/hashicorp/consul
git mv command/agent/command.go command/agent.go
git mv command/agent/command_test.go command/agent_test.go
git mv command/agent/flag_slice_value{,_test}.go command/
git mv command/agent .
git mv command/base/command.go command/base.go
git mv command/base/config_util{,_test}.go command/
git mv commands.go command/
git mv consul agent
rmdir command/base/
gsed -i -e 's|package agent|package command|' command/agent{,_test}.go
gsed -i -e 's|package agent|package command|' command/flag_slice_value{,_test}.go
gsed -i -e 's|package base|package command|' command/base.go command/config_util{,_test}.go
gsed -i -e 's|package main|package command|' command/commands.go
gsed -i -e 's|base.Command|BaseCommand|' command/commands.go
gsed -i -e 's|agent.Command|AgentCommand|' command/commands.go
gsed -i -e 's|\tCommand:|\tBaseCommand:|' command/commands.go
gsed -i -e 's|base\.||' command/commands.go
gsed -i -e 's|command\.||' command/commands.go
gsed -i -e 's|command|c|' main.go
gsed -i -e 's|range Commands|range command.Commands|' main.go
gsed -i -e 's|Commands: Commands|Commands: command.Commands|' main.go
gsed -i -e 's|base\.BoolValue|BoolValue|' command/operator_autopilot_set.go
gsed -i -e 's|base\.DurationValue|DurationValue|' command/operator_autopilot_set.go
gsed -i -e 's|base\.StringValue|StringValue|' command/operator_autopilot_set.go
gsed -i -e 's|base\.UintValue|UintValue|' command/operator_autopilot_set.go
gsed -i -e 's|\bCommand\b|BaseCommand|' command/base.go
gsed -i -e 's|BaseCommand Options|Command Options|' command/base.go
gsed -i -e 's|base.Command|BaseCommand|' command/*.go
gsed -i -e 's|c\.Command|c.BaseCommand|g' command/*.go
gsed -i -e 's|\tCommand:|\tBaseCommand:|' command/*_test.go
gsed -i -e 's|base\.||' command/*_test.go
gsed -i -e 's|\bCommand\b|AgentCommand|' command/agent{,_test}.go
gsed -i -e 's|cmd.AgentCommand|cmd.BaseCommand|' command/agent.go
gsed -i -e 's|cli.AgentCommand = new(Command)|cli.Command = new(AgentCommand)|' command/agent_test.go
gsed -i -e 's|exec.AgentCommand|exec.Command|' command/agent_test.go
gsed -i -e 's|exec.BaseCommand|exec.Command|' command/agent_test.go
gsed -i -e 's|NewTestAgent|agent.NewTestAgent|' command/agent_test.go
gsed -i -e 's|= TestConfig|= agent.TestConfig|' command/agent_test.go
gsed -i -e 's|: RetryJoin|: agent.RetryJoin|' command/agent_test.go
gsed -i -e 's|\.\./\.\./|../|' command/config_util_test.go
gsed -i -e 's|\bverifyUniqueListeners|VerifyUniqueListeners|' agent/config{,_test}.go command/agent.go
gsed -i -e 's|\bserfLANKeyring\b|SerfLANKeyring|g' agent/{agent,keyring,testagent}.go command/agent.go
gsed -i -e 's|\bserfWANKeyring\b|SerfWANKeyring|g' agent/{agent,keyring,testagent}.go command/agent.go
gsed -i -e 's|\bNewAgent\b|agent.New|g' command/agent{,_test}.go
gsed -i -e 's|\bNewAgent|New|' agent/{acl_test,agent,testagent}.go
gsed -i -e 's|\bAgent\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bBool\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bDefaultConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bDevConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bMergeConfig\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bReadConfigPaths\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bParseMetaPair\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bSerfLANKeyring\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|\bSerfWANKeyring\b|agent.&|g' command/agent{,_test}.go
gsed -i -e 's|circonus\.agent|circonus|g' command/agent{,_test}.go
gsed -i -e 's|logger\.agent|logger|g' command/agent{,_test}.go
gsed -i -e 's|metrics\.agent|metrics|g' command/agent{,_test}.go
gsed -i -e 's|// agent.Agent|// agent|' command/agent{,_test}.go
gsed -i -e 's|a\.agent\.Config|a.Config|' command/agent{,_test}.go
gsed -i -e 's|agent\.AppendSliceValue|AppendSliceValue|' command/{configtest,validate}.go
gsed -i -e 's|consul/consul|agent/consul|' GNUmakefile
gsed -i -e 's|\.\./test|../../test|' agent/consul/server_test.go
# fix imports
f=$(grep -rl 'github.com/hashicorp/consul/command/agent' * | grep '\.go')
gsed -i -e 's|github.com/hashicorp/consul/command/agent|github.com/hashicorp/consul/agent|' $f
goimports -w $f
f=$(grep -rl 'github.com/hashicorp/consul/consul' * | grep '\.go')
gsed -i -e 's|github.com/hashicorp/consul/consul|github.com/hashicorp/consul/agent/consul|' $f
goimports -w $f
goimports -w command/*.go main.go
)
2017-06-09 22:28:28 +00:00
|
|
|
"github.com/hashicorp/consul/agent/consul/state"
|
2017-07-06 10:34:00 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2020-01-28 23:50:41 +00:00
|
|
|
"github.com/hashicorp/consul/logging"
|
|
|
|
"github.com/hashicorp/go-hclog"
|
2017-01-24 16:13:48 +00:00
|
|
|
"github.com/hashicorp/go-memdb"
|
2015-04-09 20:23:14 +00:00
|
|
|
)
|
|
|
|
|
2015-06-06 03:31:33 +00:00
|
|
|
// Coordinate manages queries and updates for network coordinates.
|
2015-03-28 18:52:04 +00:00
|
|
|
type Coordinate struct {
|
2015-06-06 03:31:33 +00:00
|
|
|
// srv is a pointer back to the server.
|
|
|
|
srv *Server
|
2015-05-08 08:31:34 +00:00
|
|
|
|
2020-01-28 23:50:41 +00:00
|
|
|
logger hclog.Logger
|
|
|
|
|
2017-08-14 14:36:07 +00:00
|
|
|
// updates holds pending coordinate updates for the given nodes. This is
|
|
|
|
// keyed by node:segment so we can get a coordinate for each segment for
|
|
|
|
// servers, and we only track the latest update per node:segment.
|
|
|
|
updates map[string]*structs.CoordinateUpdateRequest
|
2015-06-29 22:53:29 +00:00
|
|
|
|
|
|
|
// updatesLock synchronizes access to the updates map.
|
|
|
|
updatesLock sync.Mutex
|
2015-05-08 08:31:34 +00:00
|
|
|
}
|
|
|
|
|
2015-06-06 03:31:33 +00:00
|
|
|
// NewCoordinate returns a new Coordinate endpoint.
|
2020-01-28 23:50:41 +00:00
|
|
|
func NewCoordinate(srv *Server, logger hclog.Logger) *Coordinate {
|
2015-06-23 02:14:02 +00:00
|
|
|
c := &Coordinate{
|
2015-06-30 21:25:40 +00:00
|
|
|
srv: srv,
|
2020-01-28 23:50:41 +00:00
|
|
|
logger: logger.Named(logging.Coordinate),
|
2017-08-14 14:36:07 +00:00
|
|
|
updates: make(map[string]*structs.CoordinateUpdateRequest),
|
2015-05-14 21:37:13 +00:00
|
|
|
}
|
2015-06-23 02:14:02 +00:00
|
|
|
|
2015-06-29 22:53:29 +00:00
|
|
|
go c.batchUpdate()
|
2015-06-23 02:14:02 +00:00
|
|
|
return c
|
2015-05-14 21:37:13 +00:00
|
|
|
}
|
|
|
|
|
2015-06-29 22:53:29 +00:00
|
|
|
// batchUpdate is a long-running routine that flushes pending coordinates to the
|
|
|
|
// Raft log in batches.
|
|
|
|
func (c *Coordinate) batchUpdate() {
|
|
|
|
for {
|
2015-06-23 02:14:02 +00:00
|
|
|
select {
|
2015-06-29 22:53:29 +00:00
|
|
|
case <-time.After(c.srv.config.CoordinateUpdatePeriod):
|
|
|
|
if err := c.batchApplyUpdates(); err != nil {
|
2020-01-28 23:50:41 +00:00
|
|
|
c.logger.Warn("Batch update failed", "error", err)
|
2015-06-29 22:53:29 +00:00
|
|
|
}
|
|
|
|
case <-c.srv.shutdownCh:
|
|
|
|
return
|
2015-06-23 02:14:02 +00:00
|
|
|
}
|
|
|
|
}
|
2015-06-29 22:53:29 +00:00
|
|
|
}
|
|
|
|
|
2015-10-23 22:19:14 +00:00
|
|
|
// batchApplyUpdates applies all pending updates to the Raft log in a series of
|
|
|
|
// batches.
|
2015-06-29 22:53:29 +00:00
|
|
|
func (c *Coordinate) batchApplyUpdates() error {
|
2015-06-30 01:26:04 +00:00
|
|
|
// Grab the pending updates and release the lock so we can still handle
|
|
|
|
// incoming messages.
|
2015-06-29 22:53:29 +00:00
|
|
|
c.updatesLock.Lock()
|
2015-06-30 01:26:04 +00:00
|
|
|
pending := c.updates
|
2017-08-14 14:36:07 +00:00
|
|
|
c.updates = make(map[string]*structs.CoordinateUpdateRequest)
|
2015-06-30 01:26:04 +00:00
|
|
|
c.updatesLock.Unlock()
|
|
|
|
|
|
|
|
// Enforce the rate limit.
|
|
|
|
limit := c.srv.config.CoordinateUpdateBatchSize * c.srv.config.CoordinateUpdateMaxBatches
|
|
|
|
size := len(pending)
|
|
|
|
if size > limit {
|
2020-01-28 23:50:41 +00:00
|
|
|
c.logger.Warn("Discarded coordinate updates", "number_discarded", size-limit)
|
2015-06-30 01:26:04 +00:00
|
|
|
size = limit
|
2015-06-23 02:14:02 +00:00
|
|
|
}
|
2015-06-29 22:53:29 +00:00
|
|
|
|
2015-06-30 01:26:04 +00:00
|
|
|
// Transform the map into a slice that we can feed to the Raft log in
|
|
|
|
// batches.
|
|
|
|
i := 0
|
2015-10-23 22:19:14 +00:00
|
|
|
updates := make(structs.Coordinates, size)
|
2017-08-14 14:36:07 +00:00
|
|
|
for _, update := range pending {
|
2015-06-30 01:26:04 +00:00
|
|
|
if !(i < size) {
|
2015-06-29 22:53:29 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2017-08-14 14:36:07 +00:00
|
|
|
updates[i] = &structs.Coordinate{
|
|
|
|
Node: update.Node,
|
|
|
|
Segment: update.Segment,
|
|
|
|
Coord: update.Coord,
|
|
|
|
}
|
2015-06-30 01:26:04 +00:00
|
|
|
i++
|
|
|
|
}
|
2015-06-29 22:53:29 +00:00
|
|
|
|
2015-06-30 01:26:04 +00:00
|
|
|
// Apply the updates to the Raft log in batches.
|
|
|
|
for start := 0; start < size; start += c.srv.config.CoordinateUpdateBatchSize {
|
|
|
|
end := start + c.srv.config.CoordinateUpdateBatchSize
|
|
|
|
if end > size {
|
|
|
|
end = size
|
2015-06-29 22:53:29 +00:00
|
|
|
}
|
|
|
|
|
2015-10-27 21:41:33 +00:00
|
|
|
// We set the "safe to ignore" flag on this update type so old
|
|
|
|
// servers don't crash if they see one of these.
|
|
|
|
t := structs.CoordinateBatchUpdateType | structs.IgnoreUnknownTypeFlag
|
|
|
|
|
2015-06-30 01:26:04 +00:00
|
|
|
slice := updates[start:end]
|
2015-11-14 23:44:26 +00:00
|
|
|
resp, err := c.srv.raftApply(t, slice)
|
|
|
|
if err != nil {
|
2015-06-30 01:26:04 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-11-14 23:44:26 +00:00
|
|
|
if respErr, ok := resp.(error); ok {
|
|
|
|
return respErr
|
|
|
|
}
|
2015-06-29 22:53:29 +00:00
|
|
|
}
|
2015-06-27 20:26:41 +00:00
|
|
|
return nil
|
2015-06-23 02:14:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update inserts or updates the LAN coordinate of a node.
|
2015-06-27 20:26:41 +00:00
|
|
|
func (c *Coordinate) Update(args *structs.CoordinateUpdateRequest, reply *struct{}) (err error) {
|
2020-07-07 19:45:08 +00:00
|
|
|
if done, err := c.srv.ForwardRPC("Coordinate.Update", args, args, reply); done {
|
2015-04-09 20:23:14 +00:00
|
|
|
return err
|
|
|
|
}
|
2015-05-08 08:31:34 +00:00
|
|
|
|
2017-05-26 00:37:16 +00:00
|
|
|
// Older clients can send coordinates with invalid numeric values like
|
|
|
|
// NaN and Inf. We guard against these coming in, though newer clients
|
|
|
|
// should never send these.
|
|
|
|
if !args.Coord.IsValid() {
|
|
|
|
return fmt.Errorf("invalid coordinate")
|
|
|
|
}
|
|
|
|
|
2015-06-27 20:26:41 +00:00
|
|
|
// Since this is a coordinate coming from some place else we harden this
|
|
|
|
// and look for dimensionality problems proactively.
|
2015-06-29 22:53:29 +00:00
|
|
|
coord, err := c.srv.serfLAN.GetCoordinate()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-06-27 20:26:41 +00:00
|
|
|
}
|
2015-06-29 22:53:29 +00:00
|
|
|
if !coord.IsCompatibleWith(args.Coord) {
|
2017-05-26 00:37:16 +00:00
|
|
|
return fmt.Errorf("incompatible coordinate")
|
2015-04-09 20:23:14 +00:00
|
|
|
}
|
2015-05-08 08:31:34 +00:00
|
|
|
|
2016-12-12 19:58:31 +00:00
|
|
|
// Fetch the ACL token, if any, and enforce the node policy if enabled.
|
2020-02-10 15:40:44 +00:00
|
|
|
authz, err := c.srv.ResolveToken(args.Token)
|
2016-12-12 19:58:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-29 21:16:03 +00:00
|
|
|
if authz != nil {
|
2020-02-10 15:40:44 +00:00
|
|
|
var authzContext acl.AuthorizerContext
|
|
|
|
structs.DefaultEnterpriseMeta().FillAuthzContext(&authzContext)
|
|
|
|
if authz.NodeWrite(args.Node, &authzContext) != acl.Allow {
|
2017-08-23 14:52:48 +00:00
|
|
|
return acl.ErrPermissionDenied
|
2016-12-12 19:58:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-29 22:53:29 +00:00
|
|
|
// Add the coordinate to the map of pending updates.
|
2017-08-14 14:36:07 +00:00
|
|
|
key := fmt.Sprintf("%s:%s", args.Node, args.Segment)
|
2015-06-29 22:53:29 +00:00
|
|
|
c.updatesLock.Lock()
|
2017-08-14 14:36:07 +00:00
|
|
|
c.updates[key] = args
|
2015-06-29 22:53:29 +00:00
|
|
|
c.updatesLock.Unlock()
|
2015-04-09 20:23:14 +00:00
|
|
|
return nil
|
|
|
|
}
|
2015-06-06 03:31:33 +00:00
|
|
|
|
2015-07-29 23:33:25 +00:00
|
|
|
// ListDatacenters returns the list of datacenters and their respective nodes
|
|
|
|
// and the raw coordinates of those nodes (if no coordinates are available for
|
|
|
|
// any of the nodes, the node list may be empty).
|
|
|
|
func (c *Coordinate) ListDatacenters(args *struct{}, reply *[]structs.DatacenterMap) error {
|
2017-03-14 01:54:34 +00:00
|
|
|
maps, err := c.srv.router.GetDatacenterMaps()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-07-29 23:33:25 +00:00
|
|
|
}
|
|
|
|
|
2017-03-14 05:56:24 +00:00
|
|
|
// Strip the datacenter suffixes from all the node names.
|
|
|
|
for i := range maps {
|
|
|
|
suffix := fmt.Sprintf(".%s", maps[i].Datacenter)
|
|
|
|
for j := range maps[i].Coordinates {
|
|
|
|
node := maps[i].Coordinates[j].Node
|
|
|
|
maps[i].Coordinates[j].Node = strings.TrimSuffix(node, suffix)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-29 23:33:25 +00:00
|
|
|
*reply = maps
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListNodes returns the list of nodes with their raw network coordinates (if no
|
|
|
|
// coordinates are available for a node it won't appear in this list).
|
|
|
|
func (c *Coordinate) ListNodes(args *structs.DCSpecificRequest, reply *structs.IndexedCoordinates) error {
|
2020-07-07 19:45:08 +00:00
|
|
|
if done, err := c.srv.ForwardRPC("Coordinate.ListNodes", args, args, reply); done {
|
2015-07-29 23:33:25 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-01-24 16:13:48 +00:00
|
|
|
return c.srv.blockingQuery(&args.QueryOptions,
|
2015-07-29 23:33:25 +00:00
|
|
|
&reply.QueryMeta,
|
2017-04-21 00:46:29 +00:00
|
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
2017-01-24 16:13:48 +00:00
|
|
|
index, coords, err := state.Coordinates(ws)
|
2015-10-23 22:19:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
reply.Index, reply.Coordinates = index, coords
|
2016-12-12 19:58:31 +00:00
|
|
|
if err := c.srv.filterACL(args.Token, reply); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-08-14 14:36:07 +00:00
|
|
|
|
2015-10-23 22:19:14 +00:00
|
|
|
return nil
|
2015-07-29 23:33:25 +00:00
|
|
|
})
|
|
|
|
}
|
2017-10-27 02:16:40 +00:00
|
|
|
|
2017-10-31 20:34:49 +00:00
|
|
|
// Node returns the raw coordinates for a single node.
|
2017-10-27 02:16:40 +00:00
|
|
|
func (c *Coordinate) Node(args *structs.NodeSpecificRequest, reply *structs.IndexedCoordinates) error {
|
2020-07-07 19:45:08 +00:00
|
|
|
if done, err := c.srv.ForwardRPC("Coordinate.Node", args, args, reply); done {
|
2017-10-27 02:16:40 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the ACL token, if any, and enforce the node policy if enabled.
|
2020-02-10 15:40:44 +00:00
|
|
|
|
|
|
|
authz, err := c.srv.ResolveToken(args.Token)
|
2017-10-27 02:16:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-29 21:16:03 +00:00
|
|
|
if authz != nil {
|
2020-02-10 15:40:44 +00:00
|
|
|
var authzContext acl.AuthorizerContext
|
|
|
|
structs.WildcardEnterpriseMeta().FillAuthzContext(&authzContext)
|
|
|
|
if authz.NodeRead(args.Node, &authzContext) != acl.Allow {
|
2017-10-27 02:16:40 +00:00
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.srv.blockingQuery(&args.QueryOptions,
|
|
|
|
&reply.QueryMeta,
|
|
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
|
|
|
index, nodeCoords, err := state.Coordinate(args.Node, ws)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var coords structs.Coordinates
|
|
|
|
for segment, coord := range nodeCoords {
|
|
|
|
coords = append(coords, &structs.Coordinate{
|
|
|
|
Node: args.Node,
|
|
|
|
Segment: segment,
|
|
|
|
Coord: coord,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
reply.Index, reply.Coordinates = index, coords
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|