2019-04-09 18:01:51 +03:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"github.com/hashicorp/consul/api"
|
|
|
|
"github.com/hashicorp/consul/watch"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
"io"
|
2019-04-10 11:28:10 +03:00
|
|
|
"sync"
|
2019-04-09 18:01:51 +03:00
|
|
|
)
|
|
|
|
|
2019-04-10 11:28:10 +03:00
|
|
|
type consulConfigProvider struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
idxMap map[string]uint64
|
|
|
|
}
|
2019-04-09 18:01:51 +03:00
|
|
|
|
2019-04-10 11:28:10 +03:00
|
|
|
func (rc *consulConfigProvider) Get(rp viper.RemoteProvider) (io.Reader, error) {
|
2019-04-09 18:01:51 +03:00
|
|
|
config := api.DefaultConfig()
|
|
|
|
config.Address = rp.Endpoint()
|
|
|
|
client, err := api.NewClient(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-10 11:28:10 +03:00
|
|
|
kv, meta, err := client.KV().Get(rp.Path(), nil)
|
2019-04-09 18:01:51 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-10 11:28:10 +03:00
|
|
|
rc.updateIndex(rp, meta.LastIndex)
|
2019-04-09 18:01:51 +03:00
|
|
|
return bytes.NewReader(kv.Value), nil
|
|
|
|
}
|
|
|
|
|
2019-04-10 11:28:10 +03:00
|
|
|
func (rc *consulConfigProvider) Watch(rp viper.RemoteProvider) (io.Reader, error) {
|
|
|
|
return rc.watch(rp)
|
2019-04-09 18:01:51 +03:00
|
|
|
}
|
|
|
|
|
2019-04-10 11:28:10 +03:00
|
|
|
func (rc *consulConfigProvider) watch(rp viper.RemoteProvider) (r io.Reader, err error) {
|
|
|
|
p, err := watch.Parse(map[string]interface{}{"type": "key", "key": rp.Path()})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// handler
|
|
|
|
p.Handler = func(index uint64, data interface{}) {
|
|
|
|
if data == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
kv, ok := data.(*api.KVPair)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !rc.updateIndex(rp, index) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
r = bytes.NewReader(kv.Value)
|
|
|
|
p.Stop()
|
|
|
|
}
|
|
|
|
// start watch
|
|
|
|
p.Run(rp.Endpoint())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rc *consulConfigProvider) WatchChannel(
|
2019-04-09 18:01:51 +03:00
|
|
|
rp viper.RemoteProvider) (resp <-chan *viper.RemoteResponse, quit chan bool) {
|
2019-04-10 11:28:10 +03:00
|
|
|
return rc.watchChannel(rp)
|
2019-04-09 18:01:51 +03:00
|
|
|
}
|
|
|
|
|
2019-04-10 11:28:10 +03:00
|
|
|
func (rc *consulConfigProvider) watchChannel(rp viper.RemoteProvider) (<-chan *viper.RemoteResponse, chan bool) {
|
|
|
|
p, err := watch.Parse(map[string]interface{}{"type": "key", "key": rp.Path()})
|
2019-04-09 18:01:51 +03:00
|
|
|
if err != nil {
|
2019-04-10 11:28:10 +03:00
|
|
|
// this should not happen
|
2019-04-09 18:01:51 +03:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
quit := make(chan bool)
|
|
|
|
viperResponseCh := make(chan *viper.RemoteResponse)
|
2019-04-10 11:28:10 +03:00
|
|
|
// handler
|
2019-04-09 18:01:51 +03:00
|
|
|
p.Handler = func(index uint64, data interface{}) {
|
|
|
|
if data == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
kv, ok := data.(*api.KVPair)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2019-04-10 11:28:10 +03:00
|
|
|
if !rc.updateIndex(rp, index) {
|
|
|
|
return
|
|
|
|
}
|
2019-04-09 18:01:51 +03:00
|
|
|
select {
|
|
|
|
case viperResponseCh <- &viper.RemoteResponse{Value: kv.Value}:
|
|
|
|
case <-quit:
|
|
|
|
}
|
|
|
|
}
|
2019-04-10 11:28:10 +03:00
|
|
|
// start watcher
|
|
|
|
go p.Run(rp.Endpoint())
|
2019-04-09 18:01:51 +03:00
|
|
|
// wait quit
|
|
|
|
go func() {
|
|
|
|
<-quit
|
|
|
|
p.Stop()
|
|
|
|
}()
|
|
|
|
return viperResponseCh, quit
|
|
|
|
}
|
|
|
|
|
2019-04-10 11:28:10 +03:00
|
|
|
func makeIndexKey(rp viper.RemoteProvider) string {
|
|
|
|
return rp.Endpoint() + "_" + rp.Path()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rc *consulConfigProvider) updateIndex(
|
|
|
|
rp viper.RemoteProvider, lastIndex uint64) (updated bool) {
|
|
|
|
rc.mu.Lock()
|
|
|
|
oldLastIndex := rc.idxMap[makeIndexKey(rp)]
|
|
|
|
if oldLastIndex < lastIndex {
|
|
|
|
rc.idxMap[makeIndexKey(rp)] = lastIndex
|
|
|
|
updated = true
|
|
|
|
}
|
|
|
|
rc.mu.Unlock()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-09 18:01:51 +03:00
|
|
|
func init() {
|
2019-04-10 11:28:10 +03:00
|
|
|
viper.RemoteConfig = &consulConfigProvider{idxMap: make(map[string]uint64)}
|
2019-04-09 18:01:51 +03:00
|
|
|
}
|