2020-12-08 00:58:12 +03:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-12-13 19:08:09 +03:00
|
|
|
"fmt"
|
2021-08-04 01:38:44 +03:00
|
|
|
"reflect"
|
2020-12-08 00:58:12 +03:00
|
|
|
|
2020-12-09 13:15:36 +03:00
|
|
|
"github.com/hashicorp/consul/api"
|
2020-12-20 00:31:02 +03:00
|
|
|
"github.com/imdario/mergo"
|
2020-12-08 00:58:12 +03:00
|
|
|
"github.com/unistack-org/micro/v3/config"
|
2021-08-04 01:38:44 +03:00
|
|
|
"github.com/unistack-org/micro/v3/util/jitter"
|
2021-01-19 03:13:45 +03:00
|
|
|
rutil "github.com/unistack-org/micro/v3/util/reflect"
|
2020-12-08 00:58:12 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
DefaultStructTag = "consul"
|
|
|
|
)
|
|
|
|
|
|
|
|
type consulConfig struct {
|
|
|
|
opts config.Options
|
|
|
|
cli *api.Client
|
|
|
|
path string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *consulConfig) Options() config.Options {
|
|
|
|
return c.opts
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *consulConfig) Init(opts ...config.Option) error {
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&c.opts)
|
|
|
|
}
|
|
|
|
|
2020-12-13 13:30:00 +03:00
|
|
|
if c.opts.Codec == nil {
|
|
|
|
return config.ErrCodecMissing
|
|
|
|
}
|
|
|
|
|
2020-12-09 13:15:36 +03:00
|
|
|
cfg := api.DefaultConfigWithLogger(&consulLogger{logger: c.opts.Logger})
|
2020-12-08 00:58:12 +03:00
|
|
|
path := ""
|
|
|
|
|
|
|
|
if c.opts.Context != nil {
|
|
|
|
if v, ok := c.opts.Context.Value(configKey{}).(*api.Config); ok {
|
2020-12-09 13:15:36 +03:00
|
|
|
cfg.Address = v.Address
|
|
|
|
cfg.Scheme = v.Scheme
|
|
|
|
cfg.Datacenter = v.Datacenter
|
|
|
|
cfg.Transport = v.Transport
|
|
|
|
cfg.HttpClient = v.HttpClient
|
|
|
|
cfg.HttpAuth = v.HttpAuth
|
|
|
|
cfg.WaitTime = v.WaitTime
|
|
|
|
cfg.Token = v.Token
|
|
|
|
cfg.TokenFile = v.TokenFile
|
|
|
|
cfg.Namespace = v.Namespace
|
|
|
|
cfg.TLSConfig = v.TLSConfig
|
2020-12-08 00:58:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := c.opts.Context.Value(addrKey{}).(string); ok {
|
|
|
|
cfg.Address = v
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := c.opts.Context.Value(tokenKey{}).(string); ok {
|
|
|
|
cfg.Token = v
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := c.opts.Context.Value(pathKey{}).(string); ok {
|
|
|
|
path = v
|
|
|
|
}
|
2020-12-09 13:21:48 +03:00
|
|
|
/*
|
|
|
|
if v, ok := c.opts.Context.Value(tlsConfigKey{}).(*tls.Config); ok {
|
|
|
|
cfg.TLSConfig = *v
|
|
|
|
}
|
|
|
|
*/
|
2020-12-08 00:58:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
cli, err := api.NewClient(cfg)
|
2021-03-05 16:57:37 +03:00
|
|
|
if err != nil && !c.opts.AllowFail {
|
2020-12-08 00:58:12 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.cli = cli
|
|
|
|
c.path = path
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-21 16:03:21 +03:00
|
|
|
func (c *consulConfig) Load(ctx context.Context, opts ...config.LoadOption) error {
|
2020-12-18 03:39:35 +03:00
|
|
|
for _, fn := range c.opts.BeforeLoad {
|
2020-12-19 23:24:25 +03:00
|
|
|
if err := fn(ctx, c); err != nil && !c.opts.AllowFail {
|
2020-12-18 03:39:35 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-08 00:58:12 +03:00
|
|
|
pair, _, err := c.cli.KV().Get(c.path, nil)
|
2020-12-19 23:24:25 +03:00
|
|
|
if err != nil && !c.opts.AllowFail {
|
2021-07-29 11:14:21 +03:00
|
|
|
return fmt.Errorf("consul path %s load error: %v", c.path, err)
|
2020-12-19 23:24:25 +03:00
|
|
|
} else if pair == nil && !c.opts.AllowFail {
|
2021-07-29 11:14:21 +03:00
|
|
|
return fmt.Errorf("consul path %s not found", c.path)
|
2020-12-08 00:58:12 +03:00
|
|
|
}
|
|
|
|
|
2020-12-19 23:24:25 +03:00
|
|
|
if err == nil && pair != nil {
|
2021-06-21 16:03:21 +03:00
|
|
|
options := config.NewLoadOptions(opts...)
|
|
|
|
mopts := []func(*mergo.Config){mergo.WithTypeCheck}
|
|
|
|
if options.Override {
|
|
|
|
mopts = append(mopts, mergo.WithOverride)
|
|
|
|
}
|
|
|
|
if options.Append {
|
|
|
|
mopts = append(mopts, mergo.WithAppendSlice)
|
|
|
|
}
|
|
|
|
|
2021-08-04 01:38:44 +03:00
|
|
|
dst := c.opts.Struct
|
|
|
|
if options.Struct != nil {
|
|
|
|
dst = options.Struct
|
|
|
|
}
|
|
|
|
|
|
|
|
src, err := rutil.Zero(dst)
|
2020-12-20 00:31:02 +03:00
|
|
|
if err == nil {
|
2020-12-20 00:33:52 +03:00
|
|
|
err = c.opts.Codec.Unmarshal(pair.Value, src)
|
2020-12-20 00:31:02 +03:00
|
|
|
if err == nil {
|
2021-08-04 01:38:44 +03:00
|
|
|
err = mergo.Merge(dst, src, mopts...)
|
2020-12-20 00:31:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil && !c.opts.AllowFail {
|
2020-12-19 23:24:25 +03:00
|
|
|
return err
|
|
|
|
}
|
2020-12-20 00:31:02 +03:00
|
|
|
|
2020-12-18 03:39:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, fn := range c.opts.AfterLoad {
|
2020-12-19 23:24:25 +03:00
|
|
|
if err := fn(ctx, c); err != nil && !c.opts.AllowFail {
|
2020-12-18 03:39:35 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-12-08 00:58:12 +03:00
|
|
|
}
|
|
|
|
|
2021-06-21 16:03:21 +03:00
|
|
|
func (c *consulConfig) Save(ctx context.Context, opts ...config.SaveOption) error {
|
2020-12-18 03:39:35 +03:00
|
|
|
for _, fn := range c.opts.BeforeSave {
|
2020-12-19 23:24:25 +03:00
|
|
|
if err := fn(ctx, c); err != nil && !c.opts.AllowFail {
|
2020-12-18 03:39:35 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-08 17:07:32 +03:00
|
|
|
buf, err := c.opts.Codec.Marshal(c.opts.Struct)
|
|
|
|
if err == nil {
|
|
|
|
_, err = c.cli.KV().Put(&api.KVPair{Key: c.path, Value: buf}, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil && !c.opts.AllowFail {
|
2021-07-29 11:14:21 +03:00
|
|
|
return fmt.Errorf("consul path %s save error: %v", c.path, err)
|
2021-05-08 17:07:32 +03:00
|
|
|
}
|
|
|
|
|
2020-12-18 03:39:35 +03:00
|
|
|
for _, fn := range c.opts.AfterSave {
|
2020-12-19 23:24:25 +03:00
|
|
|
if err := fn(ctx, c); err != nil && !c.opts.AllowFail {
|
2020-12-18 03:39:35 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-08 00:58:12 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *consulConfig) String() string {
|
|
|
|
return "consul"
|
|
|
|
}
|
|
|
|
|
2021-01-29 16:20:57 +03:00
|
|
|
func (c *consulConfig) Name() string {
|
|
|
|
return c.opts.Name
|
|
|
|
}
|
|
|
|
|
2021-08-04 01:38:44 +03:00
|
|
|
func (c *consulConfig) Watch(ctx context.Context, opts ...config.WatchOption) (config.Watcher, error) {
|
|
|
|
w := &consulWatcher{
|
|
|
|
cli: c.cli,
|
|
|
|
path: c.path,
|
|
|
|
opts: c.opts,
|
|
|
|
wopts: config.NewWatchOptions(opts...),
|
|
|
|
done: make(chan struct{}),
|
|
|
|
vchan: make(chan map[string]interface{}),
|
|
|
|
echan: make(chan error),
|
|
|
|
}
|
|
|
|
|
|
|
|
go w.run()
|
|
|
|
|
|
|
|
return w, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type consulWatcher struct {
|
|
|
|
cli *api.Client
|
|
|
|
path string
|
|
|
|
opts config.Options
|
|
|
|
wopts config.WatchOptions
|
|
|
|
done chan struct{}
|
|
|
|
vchan chan map[string]interface{}
|
|
|
|
echan chan error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *consulWatcher) run() {
|
|
|
|
ticker := jitter.NewTicker(w.wopts.MinInterval, w.wopts.MaxInterval)
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
|
src := w.opts.Struct
|
|
|
|
if w.wopts.Struct != nil {
|
|
|
|
src = w.wopts.Struct
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-w.done:
|
|
|
|
return
|
|
|
|
case <-ticker.C:
|
|
|
|
dst, err := rutil.Zero(src)
|
|
|
|
if err != nil {
|
|
|
|
w.echan <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pair, _, err := w.cli.KV().Get(w.path, nil)
|
|
|
|
if err != nil {
|
|
|
|
w.echan <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if pair == nil {
|
|
|
|
w.echan <- fmt.Errorf("consul path %s not found", w.path)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = w.opts.Codec.Unmarshal(pair.Value, dst)
|
|
|
|
if err != nil {
|
|
|
|
w.echan <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
srcmp, err := rutil.StructFieldsMap(src)
|
|
|
|
if err != nil {
|
|
|
|
w.echan <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
dstmp, err := rutil.StructFieldsMap(dst)
|
|
|
|
if err != nil {
|
|
|
|
w.echan <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for sk, sv := range srcmp {
|
|
|
|
if reflect.DeepEqual(dstmp[sk], sv) {
|
|
|
|
delete(dstmp, sk)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
w.vchan <- dstmp
|
|
|
|
src = dst
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *consulWatcher) Next() (map[string]interface{}, error) {
|
|
|
|
select {
|
|
|
|
case <-w.done:
|
|
|
|
break
|
|
|
|
case err := <-w.echan:
|
|
|
|
return nil, err
|
|
|
|
case v, ok := <-w.vchan:
|
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
return nil, config.ErrWatcherStopped
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *consulWatcher) Stop() error {
|
|
|
|
close(w.done)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-08 00:58:12 +03:00
|
|
|
func NewConfig(opts ...config.Option) config.Config {
|
|
|
|
options := config.NewOptions(opts...)
|
|
|
|
if len(options.StructTag) == 0 {
|
|
|
|
options.StructTag = DefaultStructTag
|
|
|
|
}
|
|
|
|
return &consulConfig{opts: options}
|
|
|
|
}
|