2023-03-28 20:12:41 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2014-08-20 20:45:34 +00:00
|
|
|
package watch
|
|
|
|
|
|
|
|
import (
|
2017-06-26 20:52:03 +00:00
|
|
|
"context"
|
2014-08-20 20:45:34 +00:00
|
|
|
"fmt"
|
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
consulapi "github.com/hashicorp/consul/api"
|
2014-08-20 20:45:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// watchFactory is a function that can create a new WatchFunc
|
|
|
|
// from a parameter configuration
|
2017-04-21 00:46:29 +00:00
|
|
|
type watchFactory func(params map[string]interface{}) (WatcherFunc, error)
|
2014-08-20 20:45:34 +00:00
|
|
|
|
|
|
|
// watchFuncFactory maps each type to a factory function
|
|
|
|
var watchFuncFactory map[string]watchFactory
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
watchFuncFactory = map[string]watchFactory{
|
2019-08-09 19:19:30 +00:00
|
|
|
"key": keyWatch,
|
|
|
|
"keyprefix": keyPrefixWatch,
|
|
|
|
"services": servicesWatch,
|
|
|
|
"nodes": nodesWatch,
|
|
|
|
"service": serviceWatch,
|
|
|
|
"checks": checksWatch,
|
|
|
|
"event": eventWatch,
|
|
|
|
"connect_roots": connectRootsWatch,
|
|
|
|
"connect_leaf": connectLeafWatch,
|
|
|
|
"agent_service": agentServiceWatch,
|
2014-08-20 20:45:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// keyWatch is used to return a key watching function
|
2017-04-21 00:46:29 +00:00
|
|
|
func keyWatch(params map[string]interface{}) (WatcherFunc, error) {
|
2016-08-10 22:49:51 +00:00
|
|
|
stale := false
|
|
|
|
if err := assignValueBool(params, "stale", &stale); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-08-20 22:50:32 +00:00
|
|
|
var key string
|
|
|
|
if err := assignValue(params, "key", &key); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if key == "" {
|
2014-08-20 20:45:34 +00:00
|
|
|
return nil, fmt.Errorf("Must specify a single key to watch")
|
|
|
|
}
|
2018-04-25 19:41:26 +00:00
|
|
|
fn := func(p *Plan) (BlockingParamVal, interface{}, error) {
|
2014-08-20 20:45:34 +00:00
|
|
|
kv := p.client.KV()
|
2017-06-26 21:11:14 +00:00
|
|
|
opts := makeQueryOptionsWithContext(p, stale)
|
2017-06-27 21:22:57 +00:00
|
|
|
defer p.cancelFunc()
|
2014-08-20 20:45:34 +00:00
|
|
|
pair, meta, err := kv.Get(key, &opts)
|
|
|
|
if err != nil {
|
2018-04-05 16:15:43 +00:00
|
|
|
return nil, nil, err
|
2014-08-20 20:45:34 +00:00
|
|
|
}
|
2014-08-20 22:18:08 +00:00
|
|
|
if pair == nil {
|
2018-04-05 16:15:43 +00:00
|
|
|
return WaitIndexVal(meta.LastIndex), nil, err
|
2014-08-20 22:18:08 +00:00
|
|
|
}
|
2018-04-05 16:15:43 +00:00
|
|
|
return WaitIndexVal(meta.LastIndex), pair, err
|
2014-08-20 20:45:34 +00:00
|
|
|
}
|
|
|
|
return fn, nil
|
|
|
|
}
|
2014-08-20 22:22:22 +00:00
|
|
|
|
|
|
|
// keyPrefixWatch is used to return a key prefix watching function
|
2017-04-21 00:46:29 +00:00
|
|
|
func keyPrefixWatch(params map[string]interface{}) (WatcherFunc, error) {
|
2016-08-10 22:49:51 +00:00
|
|
|
stale := false
|
|
|
|
if err := assignValueBool(params, "stale", &stale); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-08-20 22:50:32 +00:00
|
|
|
var prefix string
|
|
|
|
if err := assignValue(params, "prefix", &prefix); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if prefix == "" {
|
2014-08-20 22:22:22 +00:00
|
|
|
return nil, fmt.Errorf("Must specify a single prefix to watch")
|
|
|
|
}
|
2018-04-25 19:41:26 +00:00
|
|
|
fn := func(p *Plan) (BlockingParamVal, interface{}, error) {
|
2014-08-20 22:22:22 +00:00
|
|
|
kv := p.client.KV()
|
2017-06-26 21:11:14 +00:00
|
|
|
opts := makeQueryOptionsWithContext(p, stale)
|
2017-06-27 21:22:57 +00:00
|
|
|
defer p.cancelFunc()
|
2014-08-20 22:22:22 +00:00
|
|
|
pairs, meta, err := kv.List(prefix, &opts)
|
|
|
|
if err != nil {
|
2018-04-05 16:15:43 +00:00
|
|
|
return nil, nil, err
|
2014-08-20 22:22:22 +00:00
|
|
|
}
|
2018-04-05 16:15:43 +00:00
|
|
|
return WaitIndexVal(meta.LastIndex), pairs, err
|
2014-08-20 22:22:22 +00:00
|
|
|
}
|
|
|
|
return fn, nil
|
|
|
|
}
|
2014-08-20 22:29:31 +00:00
|
|
|
|
|
|
|
// servicesWatch is used to watch the list of available services
|
2017-04-21 00:46:29 +00:00
|
|
|
func servicesWatch(params map[string]interface{}) (WatcherFunc, error) {
|
2016-05-10 07:09:15 +00:00
|
|
|
stale := false
|
|
|
|
if err := assignValueBool(params, "stale", &stale); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-04-25 19:41:26 +00:00
|
|
|
fn := func(p *Plan) (BlockingParamVal, interface{}, error) {
|
2014-08-20 22:29:31 +00:00
|
|
|
catalog := p.client.Catalog()
|
2017-06-26 21:11:14 +00:00
|
|
|
opts := makeQueryOptionsWithContext(p, stale)
|
2017-06-27 21:22:57 +00:00
|
|
|
defer p.cancelFunc()
|
2014-08-20 22:29:31 +00:00
|
|
|
services, meta, err := catalog.Services(&opts)
|
|
|
|
if err != nil {
|
2018-04-05 16:15:43 +00:00
|
|
|
return nil, nil, err
|
2014-08-20 22:29:31 +00:00
|
|
|
}
|
2018-04-05 16:15:43 +00:00
|
|
|
return WaitIndexVal(meta.LastIndex), services, err
|
2014-08-20 22:29:31 +00:00
|
|
|
}
|
|
|
|
return fn, nil
|
|
|
|
}
|
2014-08-20 22:33:13 +00:00
|
|
|
|
|
|
|
// nodesWatch is used to watch the list of available nodes
|
2017-04-21 00:46:29 +00:00
|
|
|
func nodesWatch(params map[string]interface{}) (WatcherFunc, error) {
|
2016-05-10 07:09:15 +00:00
|
|
|
stale := false
|
|
|
|
if err := assignValueBool(params, "stale", &stale); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-04-25 19:41:26 +00:00
|
|
|
fn := func(p *Plan) (BlockingParamVal, interface{}, error) {
|
2014-08-20 22:33:13 +00:00
|
|
|
catalog := p.client.Catalog()
|
2017-06-26 21:11:14 +00:00
|
|
|
opts := makeQueryOptionsWithContext(p, stale)
|
2017-06-27 21:22:57 +00:00
|
|
|
defer p.cancelFunc()
|
2014-08-20 22:33:13 +00:00
|
|
|
nodes, meta, err := catalog.Nodes(&opts)
|
|
|
|
if err != nil {
|
2018-04-05 16:15:43 +00:00
|
|
|
return nil, nil, err
|
2014-08-20 22:33:13 +00:00
|
|
|
}
|
2018-04-05 16:15:43 +00:00
|
|
|
return WaitIndexVal(meta.LastIndex), nodes, err
|
2014-08-20 22:33:13 +00:00
|
|
|
}
|
|
|
|
return fn, nil
|
|
|
|
}
|
2014-08-20 22:50:32 +00:00
|
|
|
|
|
|
|
// serviceWatch is used to watch a specific service for changes
|
2017-04-21 00:46:29 +00:00
|
|
|
func serviceWatch(params map[string]interface{}) (WatcherFunc, error) {
|
2016-08-10 22:49:51 +00:00
|
|
|
stale := false
|
|
|
|
if err := assignValueBool(params, "stale", &stale); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-04-29 19:28:01 +00:00
|
|
|
var (
|
|
|
|
service string
|
|
|
|
tags []string
|
|
|
|
)
|
2014-08-20 22:50:32 +00:00
|
|
|
if err := assignValue(params, "service", &service); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if service == "" {
|
|
|
|
return nil, fmt.Errorf("Must specify a single service to watch")
|
|
|
|
}
|
2019-04-29 19:28:01 +00:00
|
|
|
if err := assignValueStringSlice(params, "tag", &tags); err != nil {
|
2014-08-20 22:50:32 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
passingOnly := false
|
2014-08-21 18:38:44 +00:00
|
|
|
if err := assignValueBool(params, "passingonly", &passingOnly); err != nil {
|
|
|
|
return nil, err
|
2014-08-20 22:50:32 +00:00
|
|
|
}
|
|
|
|
|
2018-04-25 19:41:26 +00:00
|
|
|
fn := func(p *Plan) (BlockingParamVal, interface{}, error) {
|
2014-08-20 22:50:32 +00:00
|
|
|
health := p.client.Health()
|
2017-06-26 21:11:14 +00:00
|
|
|
opts := makeQueryOptionsWithContext(p, stale)
|
2017-06-27 21:22:57 +00:00
|
|
|
defer p.cancelFunc()
|
2019-04-29 19:28:01 +00:00
|
|
|
nodes, meta, err := health.ServiceMultipleTags(service, tags, passingOnly, &opts)
|
2014-08-20 22:50:32 +00:00
|
|
|
if err != nil {
|
2018-04-05 16:15:43 +00:00
|
|
|
return nil, nil, err
|
2014-08-20 22:50:32 +00:00
|
|
|
}
|
2018-04-05 16:15:43 +00:00
|
|
|
return WaitIndexVal(meta.LastIndex), nodes, err
|
2014-08-20 22:50:32 +00:00
|
|
|
}
|
|
|
|
return fn, nil
|
|
|
|
}
|
2014-08-20 23:32:12 +00:00
|
|
|
|
|
|
|
// checksWatch is used to watch a specific checks in a given state
|
2017-04-21 00:46:29 +00:00
|
|
|
func checksWatch(params map[string]interface{}) (WatcherFunc, error) {
|
2016-08-10 22:49:51 +00:00
|
|
|
stale := false
|
|
|
|
if err := assignValueBool(params, "stale", &stale); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-08-20 23:32:12 +00:00
|
|
|
var service, state string
|
|
|
|
if err := assignValue(params, "service", &service); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := assignValue(params, "state", &state); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if service != "" && state != "" {
|
|
|
|
return nil, fmt.Errorf("Cannot specify service and state")
|
|
|
|
}
|
|
|
|
if service == "" && state == "" {
|
|
|
|
state = "any"
|
|
|
|
}
|
|
|
|
|
2018-04-25 19:41:26 +00:00
|
|
|
fn := func(p *Plan) (BlockingParamVal, interface{}, error) {
|
2014-08-20 23:32:12 +00:00
|
|
|
health := p.client.Health()
|
2017-06-26 21:11:14 +00:00
|
|
|
opts := makeQueryOptionsWithContext(p, stale)
|
2017-06-27 21:22:57 +00:00
|
|
|
defer p.cancelFunc()
|
2014-08-20 23:32:12 +00:00
|
|
|
var checks []*consulapi.HealthCheck
|
|
|
|
var meta *consulapi.QueryMeta
|
|
|
|
var err error
|
|
|
|
if state != "" {
|
|
|
|
checks, meta, err = health.State(state, &opts)
|
|
|
|
} else {
|
|
|
|
checks, meta, err = health.Checks(service, &opts)
|
|
|
|
}
|
|
|
|
if err != nil {
|
2018-04-05 16:15:43 +00:00
|
|
|
return nil, nil, err
|
2014-08-20 23:32:12 +00:00
|
|
|
}
|
2018-04-05 16:15:43 +00:00
|
|
|
return WaitIndexVal(meta.LastIndex), checks, err
|
2014-08-20 23:32:12 +00:00
|
|
|
}
|
|
|
|
return fn, nil
|
|
|
|
}
|
2014-08-28 22:41:06 +00:00
|
|
|
|
|
|
|
// eventWatch is used to watch for events, optionally filtering on name
|
2017-04-21 00:46:29 +00:00
|
|
|
func eventWatch(params map[string]interface{}) (WatcherFunc, error) {
|
2016-08-10 22:49:51 +00:00
|
|
|
// The stale setting doesn't apply to events.
|
|
|
|
|
2014-08-28 22:41:06 +00:00
|
|
|
var name string
|
|
|
|
if err := assignValue(params, "name", &name); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-04-25 19:41:26 +00:00
|
|
|
fn := func(p *Plan) (BlockingParamVal, interface{}, error) {
|
2014-08-28 22:41:06 +00:00
|
|
|
event := p.client.Event()
|
2017-06-26 21:11:14 +00:00
|
|
|
opts := makeQueryOptionsWithContext(p, false)
|
2017-06-27 21:22:57 +00:00
|
|
|
defer p.cancelFunc()
|
2014-08-28 22:41:06 +00:00
|
|
|
events, meta, err := event.List(name, &opts)
|
|
|
|
if err != nil {
|
2018-04-05 16:15:43 +00:00
|
|
|
return nil, nil, err
|
2014-08-28 22:41:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Prune to only the new events
|
|
|
|
for i := 0; i < len(events); i++ {
|
2018-04-05 16:15:43 +00:00
|
|
|
if WaitIndexVal(event.IDToIndex(events[i].ID)).Equal(p.lastParamVal) {
|
2014-08-28 22:41:06 +00:00
|
|
|
events = events[i+1:]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2018-04-05 16:15:43 +00:00
|
|
|
return WaitIndexVal(meta.LastIndex), events, err
|
|
|
|
}
|
|
|
|
return fn, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// connectRootsWatch is used to watch for changes to Connect Root certificates.
|
|
|
|
func connectRootsWatch(params map[string]interface{}) (WatcherFunc, error) {
|
2018-04-26 13:01:20 +00:00
|
|
|
// We don't support stale since roots are cached locally in the agent.
|
2018-04-05 16:15:43 +00:00
|
|
|
|
2018-04-25 19:41:26 +00:00
|
|
|
fn := func(p *Plan) (BlockingParamVal, interface{}, error) {
|
2018-04-05 16:15:43 +00:00
|
|
|
agent := p.client.Agent()
|
|
|
|
opts := makeQueryOptionsWithContext(p, false)
|
|
|
|
defer p.cancelFunc()
|
|
|
|
|
|
|
|
roots, meta, err := agent.ConnectCARoots(&opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return WaitIndexVal(meta.LastIndex), roots, err
|
|
|
|
}
|
|
|
|
return fn, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// connectLeafWatch is used to watch for changes to Connect Leaf certificates
|
|
|
|
// for given local service id.
|
|
|
|
func connectLeafWatch(params map[string]interface{}) (WatcherFunc, error) {
|
2018-04-26 13:01:20 +00:00
|
|
|
// We don't support stale since certs are cached locally in the agent.
|
2018-04-05 16:15:43 +00:00
|
|
|
|
2018-05-19 07:11:51 +00:00
|
|
|
var serviceName string
|
|
|
|
if err := assignValue(params, "service", &serviceName); err != nil {
|
2018-04-05 16:15:43 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-04-25 19:41:26 +00:00
|
|
|
fn := func(p *Plan) (BlockingParamVal, interface{}, error) {
|
2018-04-05 16:15:43 +00:00
|
|
|
agent := p.client.Agent()
|
|
|
|
opts := makeQueryOptionsWithContext(p, false)
|
|
|
|
defer p.cancelFunc()
|
|
|
|
|
2018-05-19 07:11:51 +00:00
|
|
|
leaf, meta, err := agent.ConnectCALeaf(serviceName, &opts)
|
2018-04-05 16:15:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return WaitIndexVal(meta.LastIndex), leaf, err
|
|
|
|
}
|
|
|
|
return fn, nil
|
|
|
|
}
|
|
|
|
|
2018-09-27 14:00:51 +00:00
|
|
|
// agentServiceWatch is used to watch for changes to a single service instance
|
|
|
|
// on the local agent. Note that this state is agent-local so the watch
|
|
|
|
// mechanism uses `hash` rather than `index` for deciding whether to block.
|
|
|
|
func agentServiceWatch(params map[string]interface{}) (WatcherFunc, error) {
|
|
|
|
// We don't support consistency modes since it's agent local data
|
|
|
|
|
|
|
|
var serviceID string
|
|
|
|
if err := assignValue(params, "service_id", &serviceID); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
fn := func(p *Plan) (BlockingParamVal, interface{}, error) {
|
|
|
|
agent := p.client.Agent()
|
|
|
|
opts := makeQueryOptionsWithContext(p, false)
|
|
|
|
defer p.cancelFunc()
|
|
|
|
|
|
|
|
svc, _, err := agent.Service(serviceID, &opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return string ContentHash since we don't have Raft indexes to block on.
|
|
|
|
return WaitHashVal(svc.ContentHash), svc, err
|
|
|
|
}
|
|
|
|
return fn, nil
|
|
|
|
}
|
|
|
|
|
2017-06-26 21:11:14 +00:00
|
|
|
func makeQueryOptionsWithContext(p *Plan, stale bool) consulapi.QueryOptions {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2019-01-08 16:43:14 +00:00
|
|
|
p.setCancelFunc(cancel)
|
2018-04-05 16:15:43 +00:00
|
|
|
opts := consulapi.QueryOptions{AllowStale: stale}
|
|
|
|
switch param := p.lastParamVal.(type) {
|
|
|
|
case WaitIndexVal:
|
|
|
|
opts.WaitIndex = uint64(param)
|
|
|
|
case WaitHashVal:
|
|
|
|
opts.WaitHash = string(param)
|
|
|
|
}
|
2017-07-15 00:30:08 +00:00
|
|
|
return *opts.WithContext(ctx)
|
2017-06-26 21:11:14 +00:00
|
|
|
}
|