2014-01-08 21:52:09 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
2014-01-08 21:56:34 +00:00
|
|
|
"fmt"
|
2018-10-19 16:04:07 +00:00
|
|
|
"sort"
|
2017-05-05 10:36:35 +00:00
|
|
|
|
2014-04-04 21:55:44 +00:00
|
|
|
"github.com/armon/go-metrics"
|
2019-10-15 20:58:50 +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"
|
2019-04-16 16:00:15 +00:00
|
|
|
bexpr "github.com/hashicorp/go-bexpr"
|
2017-01-24 07:37:21 +00:00
|
|
|
"github.com/hashicorp/go-memdb"
|
2014-01-08 21:52:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Health endpoint is used to query the health information
|
|
|
|
type Health struct {
|
|
|
|
srv *Server
|
|
|
|
}
|
|
|
|
|
|
|
|
// ChecksInState is used to get all the checks in a given state
|
|
|
|
func (h *Health) ChecksInState(args *structs.ChecksInStateRequest,
|
2014-02-05 21:30:18 +00:00
|
|
|
reply *structs.IndexedHealthChecks) error {
|
2014-04-19 00:17:12 +00:00
|
|
|
if done, err := h.srv.forward("Health.ChecksInState", args, args, reply); done {
|
2014-01-08 21:52:09 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-16 16:00:15 +00:00
|
|
|
filter, err := bexpr.CreateFilter(args.Filter, nil, reply.HealthChecks)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-01-24 07:37:21 +00:00
|
|
|
return h.srv.blockingQuery(
|
2015-10-12 07:42:09 +00:00
|
|
|
&args.QueryOptions,
|
2014-04-21 18:04:52 +00:00
|
|
|
&reply.QueryMeta,
|
2017-04-21 00:46:29 +00:00
|
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
2017-01-14 01:08:43 +00:00
|
|
|
var index uint64
|
|
|
|
var checks structs.HealthChecks
|
|
|
|
var err error
|
|
|
|
if len(args.NodeMetaFilters) > 0 {
|
2017-01-24 07:37:21 +00:00
|
|
|
index, checks, err = state.ChecksInStateByNodeMeta(ws, args.State, args.NodeMetaFilters)
|
2017-01-14 01:08:43 +00:00
|
|
|
} else {
|
2017-01-24 07:37:21 +00:00
|
|
|
index, checks, err = state.ChecksInState(ws, args.State)
|
2017-01-14 01:08:43 +00:00
|
|
|
}
|
2015-10-12 07:42:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
reply.Index, reply.HealthChecks = index, checks
|
2015-07-27 21:41:46 +00:00
|
|
|
if err := h.srv.filterACL(args.Token, reply); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-04-16 16:00:15 +00:00
|
|
|
|
|
|
|
raw, err := filter.Execute(reply.HealthChecks)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
reply.HealthChecks = raw.(structs.HealthChecks)
|
|
|
|
|
2015-07-27 21:41:46 +00:00
|
|
|
return h.srv.sortNodesByDistanceFrom(args.Source, reply.HealthChecks)
|
2014-02-05 21:30:18 +00:00
|
|
|
})
|
2014-01-08 21:52:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NodeChecks is used to get all the checks for a node
|
|
|
|
func (h *Health) NodeChecks(args *structs.NodeSpecificRequest,
|
2014-02-05 21:30:18 +00:00
|
|
|
reply *structs.IndexedHealthChecks) error {
|
2014-04-19 00:17:12 +00:00
|
|
|
if done, err := h.srv.forward("Health.NodeChecks", args, args, reply); done {
|
2014-01-08 21:52:09 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-16 16:00:15 +00:00
|
|
|
filter, err := bexpr.CreateFilter(args.Filter, nil, reply.HealthChecks)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-01-24 07:37:21 +00:00
|
|
|
return h.srv.blockingQuery(
|
2015-10-12 07:42:09 +00:00
|
|
|
&args.QueryOptions,
|
2014-04-21 18:04:52 +00:00
|
|
|
&reply.QueryMeta,
|
2017-04-21 00:46:29 +00:00
|
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
2017-01-24 07:37:21 +00:00
|
|
|
index, checks, err := state.NodeChecks(ws, args.Node)
|
2015-10-12 07:42:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
reply.Index, reply.HealthChecks = index, checks
|
2019-04-16 16:00:15 +00:00
|
|
|
if err := h.srv.filterACL(args.Token, reply); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
raw, err := filter.Execute(reply.HealthChecks)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
reply.HealthChecks = raw.(structs.HealthChecks)
|
|
|
|
return nil
|
2014-02-05 21:30:18 +00:00
|
|
|
})
|
2014-01-08 21:52:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ServiceChecks is used to get all the checks for a service
|
|
|
|
func (h *Health) ServiceChecks(args *structs.ServiceSpecificRequest,
|
2014-02-05 21:30:18 +00:00
|
|
|
reply *structs.IndexedHealthChecks) error {
|
2014-01-08 21:56:34 +00:00
|
|
|
// Reject if tag filtering is on
|
|
|
|
if args.TagFilter {
|
|
|
|
return fmt.Errorf("Tag filtering is not supported")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Potentially forward
|
2014-04-19 00:17:12 +00:00
|
|
|
if done, err := h.srv.forward("Health.ServiceChecks", args, args, reply); done {
|
2014-01-08 21:52:09 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-16 16:00:15 +00:00
|
|
|
filter, err := bexpr.CreateFilter(args.Filter, nil, reply.HealthChecks)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-01-24 07:37:21 +00:00
|
|
|
return h.srv.blockingQuery(
|
2015-10-12 07:42:09 +00:00
|
|
|
&args.QueryOptions,
|
2014-04-21 18:04:52 +00:00
|
|
|
&reply.QueryMeta,
|
2017-04-21 00:46:29 +00:00
|
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
2017-01-14 01:08:43 +00:00
|
|
|
var index uint64
|
|
|
|
var checks structs.HealthChecks
|
|
|
|
var err error
|
|
|
|
if len(args.NodeMetaFilters) > 0 {
|
2017-01-24 07:37:21 +00:00
|
|
|
index, checks, err = state.ServiceChecksByNodeMeta(ws, args.ServiceName, args.NodeMetaFilters)
|
2017-01-14 01:08:43 +00:00
|
|
|
} else {
|
2017-01-24 07:37:21 +00:00
|
|
|
index, checks, err = state.ServiceChecks(ws, args.ServiceName)
|
2017-01-14 01:08:43 +00:00
|
|
|
}
|
2015-10-12 07:42:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
reply.Index, reply.HealthChecks = index, checks
|
2015-07-27 21:41:46 +00:00
|
|
|
if err := h.srv.filterACL(args.Token, reply); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-04-16 16:00:15 +00:00
|
|
|
|
|
|
|
raw, err := filter.Execute(reply.HealthChecks)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
reply.HealthChecks = raw.(structs.HealthChecks)
|
|
|
|
|
2015-07-27 21:41:46 +00:00
|
|
|
return h.srv.sortNodesByDistanceFrom(args.Source, reply.HealthChecks)
|
2014-02-05 21:30:18 +00:00
|
|
|
})
|
2014-01-08 21:52:09 +00:00
|
|
|
}
|
2014-01-08 23:06:13 +00:00
|
|
|
|
|
|
|
// ServiceNodes returns all the nodes registered as part of a service including health info
|
2014-02-05 21:30:18 +00:00
|
|
|
func (h *Health) ServiceNodes(args *structs.ServiceSpecificRequest, reply *structs.IndexedCheckServiceNodes) error {
|
2014-04-19 00:17:12 +00:00
|
|
|
if done, err := h.srv.forward("Health.ServiceNodes", args, args, reply); done {
|
2014-01-08 23:06:13 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the arguments
|
|
|
|
if args.ServiceName == "" {
|
|
|
|
return fmt.Errorf("Must provide service name")
|
|
|
|
}
|
|
|
|
|
2018-03-09 17:32:22 +00:00
|
|
|
// Determine the function we'll call
|
2018-03-22 02:54:44 +00:00
|
|
|
var f func(memdb.WatchSet, *state.Store, *structs.ServiceSpecificRequest) (uint64, structs.CheckServiceNodes, error)
|
2018-03-09 17:32:22 +00:00
|
|
|
switch {
|
|
|
|
case args.Connect:
|
2018-03-22 02:54:44 +00:00
|
|
|
f = h.serviceNodesConnect
|
2018-03-09 17:32:22 +00:00
|
|
|
case args.TagFilter:
|
2018-03-22 02:54:44 +00:00
|
|
|
f = h.serviceNodesTagFilter
|
2018-03-09 17:32:22 +00:00
|
|
|
default:
|
2018-03-22 02:54:44 +00:00
|
|
|
f = h.serviceNodesDefault
|
2018-03-09 17:32:22 +00:00
|
|
|
}
|
|
|
|
|
2018-03-11 18:49:12 +00:00
|
|
|
// If we're doing a connect query, we need read access to the service
|
|
|
|
// we're trying to find proxies for, so check that.
|
|
|
|
if args.Connect {
|
|
|
|
// Fetch the ACL token, if any.
|
2018-10-19 16:04:07 +00:00
|
|
|
rule, err := h.srv.ResolveToken(args.Token)
|
2018-03-11 18:49:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-15 20:58:50 +00:00
|
|
|
if rule != nil && rule.ServiceRead(args.ServiceName, nil) != acl.Allow {
|
2018-03-11 18:49:12 +00:00
|
|
|
// Just return nil, which will return an empty response (tested)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-16 16:00:15 +00:00
|
|
|
filter, err := bexpr.CreateFilter(args.Filter, nil, reply.Nodes)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = h.srv.blockingQuery(
|
2015-10-12 07:42:09 +00:00
|
|
|
&args.QueryOptions,
|
2014-04-21 18:04:52 +00:00
|
|
|
&reply.QueryMeta,
|
2017-04-21 00:46:29 +00:00
|
|
|
func(ws memdb.WatchSet, state *state.Store) error {
|
2018-03-22 02:54:44 +00:00
|
|
|
index, nodes, err := f(ws, state, args)
|
2015-10-12 07:42:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-02-05 21:30:18 +00:00
|
|
|
}
|
2015-07-27 21:41:46 +00:00
|
|
|
|
2015-10-12 07:42:09 +00:00
|
|
|
reply.Index, reply.Nodes = index, nodes
|
2017-01-14 01:08:43 +00:00
|
|
|
if len(args.NodeMetaFilters) > 0 {
|
2017-01-18 21:23:33 +00:00
|
|
|
reply.Nodes = nodeMetaFilter(args.NodeMetaFilters, reply.Nodes)
|
2017-01-14 01:08:43 +00:00
|
|
|
}
|
2019-04-16 16:00:15 +00:00
|
|
|
|
2015-07-27 21:41:46 +00:00
|
|
|
if err := h.srv.filterACL(args.Token, reply); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-04-16 16:00:15 +00:00
|
|
|
|
|
|
|
raw, err := filter.Execute(reply.Nodes)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
reply.Nodes = raw.(structs.CheckServiceNodes)
|
|
|
|
|
2015-07-27 21:41:46 +00:00
|
|
|
return h.srv.sortNodesByDistanceFrom(args.Source, reply.Nodes)
|
2014-02-05 21:30:18 +00:00
|
|
|
})
|
2014-04-04 21:55:44 +00:00
|
|
|
|
|
|
|
// Provide some metrics
|
|
|
|
if err == nil {
|
2018-03-09 17:32:22 +00:00
|
|
|
// For metrics, we separate Connect-based lookups from non-Connect
|
|
|
|
key := "service"
|
|
|
|
if args.Connect {
|
|
|
|
key = "connect"
|
|
|
|
}
|
|
|
|
|
|
|
|
metrics.IncrCounterWithLabels([]string{"health", key, "query"}, 1,
|
2017-10-04 23:43:27 +00:00
|
|
|
[]metrics.Label{{Name: "service", Value: args.ServiceName}})
|
2019-01-07 21:30:47 +00:00
|
|
|
// DEPRECATED (singular-service-tag) - remove this when backwards RPC compat
|
|
|
|
// with 1.2.x is not required.
|
2014-04-04 21:55:44 +00:00
|
|
|
if args.ServiceTag != "" {
|
2018-03-09 17:32:22 +00:00
|
|
|
metrics.IncrCounterWithLabels([]string{"health", key, "query-tag"}, 1,
|
2017-10-04 23:43:27 +00:00
|
|
|
[]metrics.Label{{Name: "service", Value: args.ServiceName}, {Name: "tag", Value: args.ServiceTag}})
|
2014-04-04 21:55:44 +00:00
|
|
|
}
|
2018-10-11 11:50:05 +00:00
|
|
|
if len(args.ServiceTags) > 0 {
|
2018-10-19 16:04:07 +00:00
|
|
|
// Sort tags so that the metric is the same even if the request
|
|
|
|
// tags are in a different order
|
|
|
|
sort.Strings(args.ServiceTags)
|
|
|
|
|
2018-10-11 11:50:05 +00:00
|
|
|
labels := []metrics.Label{{Name: "service", Value: args.ServiceName}}
|
|
|
|
for _, tag := range args.ServiceTags {
|
|
|
|
labels = append(labels, metrics.Label{Name: "tag", Value: tag})
|
|
|
|
}
|
|
|
|
metrics.IncrCounterWithLabels([]string{"health", key, "query-tags"}, 1, labels)
|
|
|
|
}
|
2014-04-04 21:55:44 +00:00
|
|
|
if len(reply.Nodes) == 0 {
|
2018-03-09 17:32:22 +00:00
|
|
|
metrics.IncrCounterWithLabels([]string{"health", key, "not-found"}, 1,
|
2017-10-04 23:43:27 +00:00
|
|
|
[]metrics.Label{{Name: "service", Value: args.ServiceName}})
|
2014-04-04 21:55:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
2014-01-08 23:06:13 +00:00
|
|
|
}
|
2018-03-22 02:54:44 +00:00
|
|
|
|
|
|
|
// The serviceNodes* functions below are the various lookup methods that
|
|
|
|
// can be used by the ServiceNodes endpoint.
|
|
|
|
|
|
|
|
func (h *Health) serviceNodesConnect(ws memdb.WatchSet, s *state.Store, args *structs.ServiceSpecificRequest) (uint64, structs.CheckServiceNodes, error) {
|
|
|
|
return s.CheckConnectServiceNodes(ws, args.ServiceName)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Health) serviceNodesTagFilter(ws memdb.WatchSet, s *state.Store, args *structs.ServiceSpecificRequest) (uint64, structs.CheckServiceNodes, error) {
|
2019-01-07 21:30:47 +00:00
|
|
|
// DEPRECATED (singular-service-tag) - remove this when backwards RPC compat
|
|
|
|
// with 1.2.x is not required.
|
|
|
|
// Agents < v1.3.0 populate the ServiceTag field. In this case,
|
2018-10-11 11:50:05 +00:00
|
|
|
// use ServiceTag instead of the ServiceTags field.
|
|
|
|
if args.ServiceTag != "" {
|
|
|
|
return s.CheckServiceTagNodes(ws, args.ServiceName, []string{args.ServiceTag})
|
|
|
|
}
|
|
|
|
return s.CheckServiceTagNodes(ws, args.ServiceName, args.ServiceTags)
|
2018-03-22 02:54:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Health) serviceNodesDefault(ws memdb.WatchSet, s *state.Store, args *structs.ServiceSpecificRequest) (uint64, structs.CheckServiceNodes, error) {
|
|
|
|
return s.CheckServiceNodes(ws, args.ServiceName)
|
|
|
|
}
|