2015-08-28 10:26:23 +00:00
|
|
|
package fingerprint
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
2021-06-07 15:54:33 +00:00
|
|
|
consulapi "github.com/hashicorp/consul/api"
|
2018-09-16 00:48:59 +00:00
|
|
|
log "github.com/hashicorp/go-hclog"
|
2021-06-07 15:54:33 +00:00
|
|
|
agentconsul "github.com/hashicorp/nomad/command/agent/consul"
|
2015-08-28 10:26:23 +00:00
|
|
|
)
|
|
|
|
|
2015-12-11 17:01:28 +00:00
|
|
|
const (
|
2015-12-11 21:47:35 +00:00
|
|
|
consulAvailable = "available"
|
|
|
|
consulUnavailable = "unavailable"
|
2015-12-11 17:01:28 +00:00
|
|
|
)
|
|
|
|
|
2016-09-01 20:38:31 +00:00
|
|
|
// ConsulFingerprint is used to fingerprint for Consul
|
2015-08-28 10:26:23 +00:00
|
|
|
type ConsulFingerprint struct {
|
2021-06-03 15:28:48 +00:00
|
|
|
logger log.Logger
|
2021-06-07 15:54:33 +00:00
|
|
|
client *consulapi.Client
|
2021-06-03 15:28:48 +00:00
|
|
|
lastState string
|
|
|
|
extractors map[string]consulExtractor
|
2015-08-28 10:26:23 +00:00
|
|
|
}
|
|
|
|
|
2021-06-03 15:28:48 +00:00
|
|
|
// consulExtractor is used to parse out one attribute from consulInfo. Returns
|
|
|
|
// the value of the attribute, and whether the attribute exists.
|
2021-06-07 15:54:33 +00:00
|
|
|
type consulExtractor func(agentconsul.Self) (string, bool)
|
2021-06-03 15:28:48 +00:00
|
|
|
|
2016-09-01 18:02:19 +00:00
|
|
|
// NewConsulFingerprint is used to create a Consul fingerprint
|
2018-09-16 00:48:59 +00:00
|
|
|
func NewConsulFingerprint(logger log.Logger) Fingerprint {
|
2021-06-03 15:28:48 +00:00
|
|
|
return &ConsulFingerprint{
|
|
|
|
logger: logger.Named("consul"),
|
|
|
|
lastState: consulUnavailable,
|
|
|
|
}
|
2015-08-28 10:26:23 +00:00
|
|
|
}
|
|
|
|
|
2018-12-01 16:10:39 +00:00
|
|
|
func (f *ConsulFingerprint) Fingerprint(req *FingerprintRequest, resp *FingerprintResponse) error {
|
2021-06-03 15:28:48 +00:00
|
|
|
|
|
|
|
// establish consul client if necessary
|
|
|
|
if err := f.initialize(req); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// query consul for agent self api
|
|
|
|
info := f.query(resp)
|
|
|
|
if len(info) == 0 {
|
|
|
|
// unable to reach consul, nothing to do this time
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// apply the extractor for each attribute
|
|
|
|
for attr, extractor := range f.extractors {
|
|
|
|
if s, ok := extractor(info); !ok {
|
|
|
|
f.logger.Warn("unable to fingerprint consul", "attribute", attr)
|
|
|
|
} else {
|
|
|
|
resp.AddAttribute(attr, s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// create link for consul
|
|
|
|
f.link(resp)
|
|
|
|
|
|
|
|
// indicate Consul is now available
|
|
|
|
if f.lastState == consulUnavailable {
|
|
|
|
f.logger.Info("consul agent is available")
|
|
|
|
}
|
|
|
|
|
|
|
|
f.lastState = consulAvailable
|
|
|
|
resp.Detected = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *ConsulFingerprint) Periodic() (bool, time.Duration) {
|
|
|
|
return true, 15 * time.Second
|
|
|
|
}
|
|
|
|
|
|
|
|
// clearConsulAttributes removes consul attributes and links from the passed Node.
|
|
|
|
func (f *ConsulFingerprint) clearConsulAttributes(r *FingerprintResponse) {
|
|
|
|
for attr := range f.extractors {
|
|
|
|
r.RemoveAttribute(attr)
|
|
|
|
}
|
|
|
|
r.RemoveLink("consul")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *ConsulFingerprint) initialize(req *FingerprintRequest) error {
|
|
|
|
// Only create the Consul client once to avoid creating many connections
|
2015-11-22 21:42:06 +00:00
|
|
|
if f.client == nil {
|
2018-01-24 14:09:53 +00:00
|
|
|
consulConfig, err := req.Config.ConsulConfig.ApiConfig()
|
2015-11-22 21:42:06 +00:00
|
|
|
if err != nil {
|
2021-06-03 15:28:48 +00:00
|
|
|
return fmt.Errorf("failed to initialize Consul client config: %v", err)
|
2015-11-22 21:42:06 +00:00
|
|
|
}
|
2015-08-28 10:26:23 +00:00
|
|
|
|
2021-06-07 15:54:33 +00:00
|
|
|
f.client, err = consulapi.NewClient(consulConfig)
|
2015-11-22 21:42:06 +00:00
|
|
|
if err != nil {
|
2021-06-03 15:28:48 +00:00
|
|
|
return fmt.Errorf("failed to initialize Consul client: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
f.extractors = map[string]consulExtractor{
|
2021-06-03 17:38:43 +00:00
|
|
|
"consul.server": f.server,
|
|
|
|
"consul.version": f.version,
|
|
|
|
"consul.sku": f.sku,
|
|
|
|
"consul.revision": f.revision,
|
|
|
|
"unique.consul.name": f.name,
|
|
|
|
"consul.datacenter": f.dc,
|
|
|
|
"consul.segment": f.segment,
|
|
|
|
"consul.connect": f.connect,
|
|
|
|
"consul.grpc": f.grpc,
|
|
|
|
"consul.ft.namespaces": f.namespaces,
|
2015-11-22 21:42:06 +00:00
|
|
|
}
|
2015-08-28 10:26:23 +00:00
|
|
|
}
|
|
|
|
|
2021-06-03 15:28:48 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-07 15:54:33 +00:00
|
|
|
func (f *ConsulFingerprint) query(resp *FingerprintResponse) agentconsul.Self {
|
2015-09-01 02:56:25 +00:00
|
|
|
// We'll try to detect consul by making a query to to the agent's self API.
|
|
|
|
// If we can't hit this URL consul is probably not running on this machine.
|
2015-11-22 21:42:06 +00:00
|
|
|
info, err := f.client.Agent().Self()
|
2015-08-28 10:26:23 +00:00
|
|
|
if err != nil {
|
2018-01-26 19:31:37 +00:00
|
|
|
f.clearConsulAttributes(resp)
|
2015-12-11 17:01:28 +00:00
|
|
|
|
2021-06-03 15:28:48 +00:00
|
|
|
// indicate consul no longer available
|
2015-12-11 21:47:35 +00:00
|
|
|
if f.lastState == consulAvailable {
|
2018-09-16 00:48:59 +00:00
|
|
|
f.logger.Info("consul agent is unavailable")
|
2015-12-11 17:01:28 +00:00
|
|
|
}
|
2015-12-11 21:47:35 +00:00
|
|
|
f.lastState = consulUnavailable
|
2018-01-24 14:09:53 +00:00
|
|
|
return nil
|
2015-08-28 10:26:23 +00:00
|
|
|
}
|
2021-06-03 15:28:48 +00:00
|
|
|
return info
|
|
|
|
}
|
2015-08-28 10:26:23 +00:00
|
|
|
|
2021-06-03 15:28:48 +00:00
|
|
|
func (f *ConsulFingerprint) link(resp *FingerprintResponse) {
|
2018-01-30 17:57:37 +00:00
|
|
|
if dc, ok := resp.Attributes["consul.datacenter"]; ok {
|
|
|
|
if name, ok2 := resp.Attributes["unique.consul.name"]; ok2 {
|
|
|
|
resp.AddLink("consul", fmt.Sprintf("%s.%s", dc, name))
|
|
|
|
}
|
2017-10-12 01:25:54 +00:00
|
|
|
} else {
|
2018-09-16 00:48:59 +00:00
|
|
|
f.logger.Warn("malformed Consul response prevented linking")
|
2017-10-12 01:25:54 +00:00
|
|
|
}
|
2021-06-03 15:28:48 +00:00
|
|
|
}
|
2015-08-28 10:35:44 +00:00
|
|
|
|
2021-06-07 15:54:33 +00:00
|
|
|
func (f *ConsulFingerprint) server(info agentconsul.Self) (string, bool) {
|
2021-06-03 15:28:48 +00:00
|
|
|
s, ok := info["Config"]["Server"].(bool)
|
|
|
|
return strconv.FormatBool(s), ok
|
|
|
|
}
|
|
|
|
|
2021-06-07 15:54:33 +00:00
|
|
|
func (f *ConsulFingerprint) version(info agentconsul.Self) (string, bool) {
|
2021-06-03 15:28:48 +00:00
|
|
|
v, ok := info["Config"]["Version"].(string)
|
|
|
|
return v, ok
|
|
|
|
}
|
|
|
|
|
2021-06-07 15:54:33 +00:00
|
|
|
func (f *ConsulFingerprint) sku(info agentconsul.Self) (string, bool) {
|
|
|
|
return agentconsul.SKU(info)
|
2015-08-28 10:26:23 +00:00
|
|
|
}
|
2015-11-05 21:46:02 +00:00
|
|
|
|
2021-06-07 15:54:33 +00:00
|
|
|
func (f *ConsulFingerprint) revision(info agentconsul.Self) (string, bool) {
|
2021-06-03 15:28:48 +00:00
|
|
|
r, ok := info["Config"]["Revision"].(string)
|
|
|
|
return r, ok
|
2018-01-26 19:31:37 +00:00
|
|
|
}
|
|
|
|
|
2021-06-07 15:54:33 +00:00
|
|
|
func (f *ConsulFingerprint) name(info agentconsul.Self) (string, bool) {
|
2021-06-03 15:28:48 +00:00
|
|
|
n, ok := info["Config"]["NodeName"].(string)
|
|
|
|
return n, ok
|
|
|
|
}
|
|
|
|
|
2021-06-07 15:54:33 +00:00
|
|
|
func (f *ConsulFingerprint) dc(info agentconsul.Self) (string, bool) {
|
2021-06-03 15:28:48 +00:00
|
|
|
d, ok := info["Config"]["Datacenter"].(string)
|
|
|
|
return d, ok
|
|
|
|
}
|
|
|
|
|
2021-06-07 15:54:33 +00:00
|
|
|
func (f *ConsulFingerprint) segment(info agentconsul.Self) (string, bool) {
|
2021-06-03 15:28:48 +00:00
|
|
|
tags, tagsOK := info["Member"]["Tags"].(map[string]interface{})
|
|
|
|
if !tagsOK {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
s, ok := tags["segment"].(string)
|
|
|
|
return s, ok
|
2015-11-05 21:46:02 +00:00
|
|
|
}
|
2021-06-03 17:38:43 +00:00
|
|
|
|
2021-06-07 15:54:33 +00:00
|
|
|
func (f *ConsulFingerprint) connect(info agentconsul.Self) (string, bool) {
|
2021-06-03 17:38:43 +00:00
|
|
|
c, ok := info["DebugConfig"]["ConnectEnabled"].(bool)
|
|
|
|
return strconv.FormatBool(c), ok
|
|
|
|
}
|
|
|
|
|
2021-06-07 15:54:33 +00:00
|
|
|
func (f *ConsulFingerprint) grpc(info agentconsul.Self) (string, bool) {
|
2021-06-03 17:38:43 +00:00
|
|
|
p, ok := info["DebugConfig"]["GRPCPort"].(float64)
|
|
|
|
return fmt.Sprintf("%d", int(p)), ok
|
|
|
|
}
|
|
|
|
|
2021-06-07 15:54:33 +00:00
|
|
|
func (f *ConsulFingerprint) namespaces(info agentconsul.Self) (string, bool) {
|
2021-06-07 20:22:26 +00:00
|
|
|
return strconv.FormatBool(agentconsul.Namespaces(info)), true
|
2021-06-03 17:38:43 +00:00
|
|
|
}
|