Skip to content

Commit

Permalink
refactor: etcd != remote.
Browse files Browse the repository at this point in the history
  • Loading branch information
Reasno committed Aug 6, 2021
1 parent 7d7f997 commit d95c325
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 34 deletions.
9 changes: 0 additions & 9 deletions c.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (

"github.com/DoNewsCode/core/codec/yaml"
"github.com/DoNewsCode/core/config"
"github.com/DoNewsCode/core/config/remote"
"github.com/DoNewsCode/core/config/watcher"
"github.com/DoNewsCode/core/container"
"github.com/DoNewsCode/core/contract"
Expand All @@ -24,7 +23,6 @@ import (
"github.com/go-kit/kit/log"
"github.com/knadh/koanf/providers/confmap"
"github.com/knadh/koanf/providers/file"
clientv3 "go.etcd.io/etcd/client/v3"
)

// C stands for the core of the application. It contains service definitions and
Expand Down Expand Up @@ -93,13 +91,6 @@ func WithYamlFile(path string) (CoreOption, CoreOption) {
WithConfigWatcher(watcher.File{Path: path})
}

// WithRemoteYamlFile is a two-in-one coreOption. It uses the remote key on etcd as the
// source of configuration, and watches the change of that key for hot reloading.
func WithRemoteYamlFile(key string, cfg clientv3.Config) (CoreOption, CoreOption) {
r := remote.Provider(key, &cfg)
return WithConfigStack(r, config.CodecParser{Codec: yaml.Codec{}}), WithConfigWatcher(r)
}

// WithInline is a CoreOption that creates a inline config in the configuration stack.
func WithInline(key string, entry interface{}) CoreOption {
return WithConfigStack(confmap.Provider(map[string]interface{}{
Expand Down
24 changes: 12 additions & 12 deletions config/remote/remote.go → config/remote/etcd/etcd.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package remote
// Package etcd allows the core package to bootstrap its configuration from an etcd server.
package etcd

import (
"context"
Expand All @@ -8,23 +9,23 @@ import (
"go.etcd.io/etcd/client/v3"
)

// Remote is a core.ConfProvider and contract.ConfigWatcher implementation to read and watch remote config key.
// ETCD is a core.ConfProvider and contract.ConfigWatcher implementation to read and watch remote config key.
// The remote client uses etcd.
type Remote struct {
key string
type ETCD struct {
key string
clientConfig *clientv3.Config
}

// Provider create a *Remote
func Provider(key string, clientConfig *clientv3.Config) *Remote {
return &Remote{
key: key,
// Provider create a *ETCD
func Provider(key string, clientConfig *clientv3.Config) *ETCD {
return &ETCD{
key: key,
clientConfig: clientConfig,
}
}

// ReadBytes reads the contents of a key from etcd and returns the bytes.
func (r *Remote) ReadBytes() ([]byte, error) {
func (r *ETCD) ReadBytes() ([]byte, error) {
client, err := clientv3.New(*r.clientConfig)
if err != nil {
return nil, err
Expand All @@ -43,15 +44,15 @@ func (r *Remote) ReadBytes() ([]byte, error) {
}

// Read is not supported by the remote provider.
func (r *Remote) Read() (map[string]interface{}, error) {
func (r *ETCD) Read() (map[string]interface{}, error) {
return nil, errors.New("remote provider does not support this method")
}

// Watch watches the change to the remote key from etcd. If the key is edited or created, the reload function
// will be called. note the reload function should not just load the changes made within this key, but rather
// it should reload the whole config stack. For example, if the flag or env takes precedence over the config
// key, they should remain to be so after the key changes.
func (r *Remote) Watch(ctx context.Context, reload func() error) error {
func (r *ETCD) Watch(ctx context.Context, reload func() error) error {
client, err := clientv3.New(*r.clientConfig)
if err != nil {
return err
Expand All @@ -74,4 +75,3 @@ func (r *Remote) Watch(ctx context.Context, reload func() error) error {
}
}
}

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package remote
package etcd

import (
"context"
Expand Down Expand Up @@ -67,7 +67,7 @@ func TestError(t *testing.T) {
}
addrs := strings.Split(os.Getenv("ETCD_ADDR"), ",")
var (
r *Remote
r *ETCD
err error
)

Expand Down Expand Up @@ -126,7 +126,7 @@ func TestError(t *testing.T) {
g.Wait()
}

func put(r *Remote, val string) error {
func put(r *ETCD, val string) error {
client, err := clientv3.New(*r.clientConfig)
if err != nil {
return err
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package remote_test
package etcd_test

import (
"context"
"fmt"
"github.com/DoNewsCode/core"
"github.com/stretchr/testify/assert"
"github.com/DoNewsCode/core/codec/yaml"
"github.com/DoNewsCode/core/config"
"github.com/DoNewsCode/core/config/remote/etcd"
clientv3 "go.etcd.io/etcd/client/v3"
"os"
"strings"
"testing"
"time"
)

func Test_integration(t *testing.T) {
func Example() {
addr := os.Getenv("ETCD_ADDR")
if addr == "" {
t.Skip("set ETCD_ADDR for run remote test")
fmt.Println("set ETCD_ADDR for run example")
return
}
key := "core.yaml"
Expand All @@ -23,13 +25,15 @@ func Test_integration(t *testing.T) {
Endpoints: envEtcdAddrs,
DialTimeout: time.Second,
}
if err := put(cfg, key, "name: remote"); err != nil {
t.Fatal(err)
}
_ = put(cfg, key, "name: etcd")

c := core.New(core.WithRemoteYamlFile(key, cfg))
provider := etcd.Provider(key, &cfg)
c := core.New(core.WithConfigStack(provider, config.CodecParser{Codec: yaml.Codec{}}), core.WithConfigWatcher(provider))
c.ProvideEssentials()
assert.Equal(t, "remote", c.String("name"))
fmt.Println(c.String("name"))

// Output:
// etcd
}

func put(cfg clientv3.Config, key, val string) error {
Expand Down

0 comments on commit d95c325

Please sign in to comment.