feat(contrib/config): use key file extension as format & config load log (#1619)

* add format
pull/1622/head
Windfarer 3 years ago committed by GitHub
parent fcd9351717
commit a8692e7dde
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      config/config.go
  2. 1
      config/config_test.go
  3. 8
      contrib/config/consul/config.go
  4. 32
      contrib/config/consul/config_test.go
  5. 1
      contrib/config/consul/go.mod
  6. 9
      contrib/config/etcd/config.go
  7. 37
      contrib/config/etcd/config_test.go
  8. 2
      contrib/config/etcd/go.mod
  9. 9
      contrib/config/kubernetes/config.go
  10. 15
      contrib/config/nacos/config.go
  11. 5
      examples/config/t/a.yaml
  12. 6
      examples/config/t/b.yaml

@ -103,6 +103,9 @@ func (c *config) Load() error {
if err != nil { if err != nil {
return err return err
} }
for _, v := range kvs {
c.log.Infof("config loaded: %s format: %s", v.Key, v.Format)
}
if err = c.reader.Merge(kvs...); err != nil { if err = c.reader.Merge(kvs...); err != nil {
c.log.Errorf("failed to merge config source: %v", err) c.log.Errorf("failed to merge config source: %v", err)
return err return err

@ -138,6 +138,7 @@ func TestConfig(t *testing.T) {
cf := &config{} cf := &config{}
cf.opts = opts cf.opts = opts
cf.reader = newReader(opts) cf.reader = newReader(opts)
cf.log = log.NewHelper(opts.logger)
err = cf.Load() err = cf.Load()
assert.Nil(t, err) assert.Nil(t, err)

@ -3,6 +3,7 @@ package consul
import ( import (
"context" "context"
"errors" "errors"
"path/filepath"
"strings" "strings"
"github.com/go-kratos/kratos/v2/config" "github.com/go-kratos/kratos/v2/config"
@ -67,12 +68,13 @@ func (s *source) Load() ([]*config.KeyValue, error) {
if !strings.HasSuffix(s.options.path, "/") { if !strings.HasSuffix(s.options.path, "/") {
pathPrefix = pathPrefix + "/" pathPrefix = pathPrefix + "/"
} }
kvs := make([]*config.KeyValue, 0) kvs := make([]*config.KeyValue, 0)
for _, item := range kv { for _, item := range kv {
k := strings.TrimPrefix(item.Key, pathPrefix)
kvs = append(kvs, &config.KeyValue{ kvs = append(kvs, &config.KeyValue{
Key: item.Key[len(pathPrefix):], Key: k,
Value: item.Value, Value: item.Value,
Format: strings.TrimPrefix(filepath.Ext(k), "."),
}) })
} }
return kvs, nil return kvs, nil

@ -4,6 +4,7 @@ import (
"testing" "testing"
"github.com/hashicorp/consul/api" "github.com/hashicorp/consul/api"
"github.com/stretchr/testify/assert"
) )
const testPath = "kratos/test/config" const testPath = "kratos/test/config"
@ -60,3 +61,34 @@ func TestConfig(t *testing.T) {
t.Error(err) t.Error(err)
} }
} }
func TestExtToFormat(t *testing.T) {
client, err := api.NewClient(&api.Config{
Address: "127.0.0.1:8500",
})
if err != nil {
t.Fatal(err)
}
tp := "kratos/test/ext"
tn := "a.bird.json"
tk := tp + "/" + tn
tc := `{"a":1}`
if _, err = client.KV().Put(&api.KVPair{Key: tk, Value: []byte(tc)}, nil); err != nil {
t.Fatal(err)
}
source, err := New(client, WithPath(tp))
if err != nil {
t.Fatal(err)
}
kvs, err := source.Load()
if err != nil {
t.Fatal(err)
}
assert.Equal(t, 1, len(kvs))
assert.Equal(t, tn, kvs[0].Key)
assert.Equal(t, tc, string(kvs[0].Value))
assert.Equal(t, "json", kvs[0].Format)
}

@ -5,6 +5,7 @@ go 1.15
require ( require (
github.com/go-kratos/kratos/v2 v2.0.5 github.com/go-kratos/kratos/v2 v2.0.5
github.com/hashicorp/consul/api v1.10.0 github.com/hashicorp/consul/api v1.10.0
github.com/stretchr/testify v1.7.0
) )
replace github.com/go-kratos/kratos/v2 => ../../../ replace github.com/go-kratos/kratos/v2 => ../../../

@ -3,6 +3,8 @@ package etcd
import ( import (
"context" "context"
"errors" "errors"
"path/filepath"
"strings"
"github.com/go-kratos/kratos/v2/config" "github.com/go-kratos/kratos/v2/config"
clientv3 "go.etcd.io/etcd/client/v3" clientv3 "go.etcd.io/etcd/client/v3"
@ -75,12 +77,13 @@ func (s *source) Load() ([]*config.KeyValue, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
kvs := make([]*config.KeyValue, 0) kvs := make([]*config.KeyValue, 0)
for _, item := range rsp.Kvs { for _, item := range rsp.Kvs {
k := string(item.Key)
kvs = append(kvs, &config.KeyValue{ kvs = append(kvs, &config.KeyValue{
Key: string(item.Key), Key: k,
Value: item.Value, Value: item.Value,
Format: strings.TrimPrefix(filepath.Ext(k), "."),
}) })
} }
return kvs, nil return kvs, nil

@ -5,6 +5,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/stretchr/testify/assert"
clientv3 "go.etcd.io/etcd/client/v3" clientv3 "go.etcd.io/etcd/client/v3"
"google.golang.org/grpc" "google.golang.org/grpc"
) )
@ -64,3 +65,39 @@ func TestConfig(t *testing.T) {
t.Error(err) t.Error(err)
} }
} }
func TestExtToFormat(t *testing.T) {
client, err := clientv3.New(clientv3.Config{
Endpoints: []string{"127.0.0.1:2379"},
DialTimeout: time.Second, DialOptions: []grpc.DialOption{grpc.WithBlock()},
})
if err != nil {
t.Fatal(err)
}
defer func() {
_ = client.Close()
}()
tp := "/kratos/test/ext"
tn := "a.bird.json"
tk := tp + "/" + tn
tc := `{"a":1}`
if _, err = client.Put(context.Background(), tk, tc); err != nil {
t.Fatal(err)
}
source, err := New(client, WithPath(tp), WithPrefix(true))
if err != nil {
t.Fatal(err)
}
kvs, err := source.Load()
if err != nil {
t.Fatal(err)
}
assert.Equal(t, 1, len(kvs))
assert.Equal(t, tk, kvs[0].Key)
assert.Equal(t, tc, string(kvs[0].Value))
assert.Equal(t, "json", kvs[0].Format)
}

@ -3,7 +3,9 @@ module github.com/go-kratos/kratos/contrib/config/etcd/v2
go 1.16 go 1.16
require ( require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-kratos/kratos/v2 v2.1.0 github.com/go-kratos/kratos/v2 v2.1.0
github.com/stretchr/testify v1.7.0
go.etcd.io/etcd/client/v3 v3.5.0 go.etcd.io/etcd/client/v3 v3.5.0
google.golang.org/grpc v1.40.0 google.golang.org/grpc v1.40.0
) )

@ -4,6 +4,8 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"path/filepath"
"strings"
"github.com/go-kratos/kratos/v2/config" "github.com/go-kratos/kratos/v2/config"
v1 "k8s.io/api/core/v1" v1 "k8s.io/api/core/v1"
@ -116,9 +118,12 @@ func (k *kube) load() (kvs []*config.KeyValue, err error) {
func (k *kube) configMap(cm v1.ConfigMap) (kvs []*config.KeyValue) { func (k *kube) configMap(cm v1.ConfigMap) (kvs []*config.KeyValue) {
for name, val := range cm.Data { for name, val := range cm.Data {
k := fmt.Sprintf("%s/%s/%s", k.opts.Namespace, cm.Name, name)
kvs = append(kvs, &config.KeyValue{ kvs = append(kvs, &config.KeyValue{
Key: fmt.Sprintf("%s/%s/%s", k.opts.Namespace, cm.Name, name), Key: k,
Value: []byte(val), Value: []byte(val),
Format: strings.TrimPrefix(filepath.Ext(k), "."),
}) })
} }
return kvs return kvs

@ -2,6 +2,8 @@ package config
import ( import (
"context" "context"
"path/filepath"
"strings"
"time" "time"
"github.com/go-kratos/kratos/v2/config" "github.com/go-kratos/kratos/v2/config"
@ -89,11 +91,12 @@ func (c *Config) Load() ([]*config.KeyValue, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
k := c.opts.dataID
return []*config.KeyValue{ return []*config.KeyValue{
{ {
Key: c.opts.dataID, Key: k,
Value: []byte(content), Value: []byte(content),
Format: strings.TrimPrefix(filepath.Ext(k), "."),
}, },
}, nil }, nil
} }
@ -144,10 +147,12 @@ func (w *Watcher) Next() ([]*config.KeyValue, error) {
case <-w.Context.Done(): case <-w.Context.Done():
return nil, nil return nil, nil
case content := <-w.content: case content := <-w.content:
k := w.dataID
return []*config.KeyValue{ return []*config.KeyValue{
{ {
Key: w.dataID, Key: k,
Value: []byte(content), Value: []byte(content),
Format: strings.TrimPrefix(filepath.Ext(k), "."),
}, },
}, nil }, nil
} }

@ -0,0 +1,5 @@
foo:
baz: "2"
biu: "example"
hello:
a: b

@ -0,0 +1,6 @@
foo:
bar: 3
baz: aaaa
hey:
good: bad
qux: quux
Loading…
Cancel
Save