From ec41f4bdc897cfd860de2149818688c23f6eb8f6 Mon Sep 17 00:00:00 2001 From: xhe Date: Sat, 17 Sep 2022 15:50:15 +0800 Subject: [PATCH] *: another operator adapt PR (#86) --- cmd/{weirproxy => tiproxy}/main.go | 7 +++-- cmd/{weirctl => tiproxyctl}/main.go | 4 +++ conf/{weirproxy.yaml => proxy.yaml} | 6 ++-- docker/Dockerfile | 2 +- lib/cli/config.go | 43 +++++++++++++++++++++++------ lib/cli/main.go | 2 +- lib/go.mod | 9 +----- lib/go.sum | 18 ++++++++++++ lib/util/errors/error.go | 2 +- lib/util/errors/errors.go | 4 +-- lib/util/errors/werror.go | 2 +- pkg/manager/config/manager.go | 10 +++---- pkg/manager/config/manager_test.go | 23 +++++---------- pkg/manager/config/proxy.go | 26 +++++++++++++++-- pkg/manager/config/proxy_test.go | 10 ++++--- pkg/server/api/config.go | 13 +++++++++ pkg/server/server.go | 4 +-- 17 files changed, 126 insertions(+), 59 deletions(-) rename cmd/{weirproxy => tiproxy}/main.go (97%) rename cmd/{weirctl => tiproxyctl}/main.go (89%) rename conf/{weirproxy.yaml => proxy.yaml} (95%) diff --git a/cmd/weirproxy/main.go b/cmd/tiproxy/main.go similarity index 97% rename from cmd/weirproxy/main.go rename to cmd/tiproxy/main.go index 80f29441..6ce17045 100644 --- a/cmd/weirproxy/main.go +++ b/cmd/tiproxy/main.go @@ -17,23 +17,24 @@ package main import ( "io/ioutil" + "os" "github.com/pingcap/TiProxy/lib/config" - "github.com/pingcap/TiProxy/pkg/server" "github.com/pingcap/TiProxy/lib/util/cmd" "github.com/pingcap/TiProxy/lib/util/errors" "github.com/pingcap/TiProxy/lib/util/waitgroup" + "github.com/pingcap/TiProxy/pkg/server" "github.com/spf13/cobra" "go.uber.org/zap" ) func main() { rootCmd := &cobra.Command{ - Use: "weirproxy", + Use: os.Args[0], Short: "start the proxy server", } - configFile := rootCmd.PersistentFlags().String("config", "conf/weirproxy.yaml", "weir proxy config file path") + configFile := rootCmd.PersistentFlags().String("config", "conf/proxy.yaml", "proxy config file path") pubAddr := rootCmd.PersistentFlags().String("pub_addr", "127.0.0.1", "IP or domain, will be used as the accessible addr for other clients") logEncoder := rootCmd.PersistentFlags().String("log_encoder", "", "log in format of tidb, console, or json") logLevel := rootCmd.PersistentFlags().String("log_level", "", "log level") diff --git a/cmd/weirctl/main.go b/cmd/tiproxyctl/main.go similarity index 89% rename from cmd/weirctl/main.go rename to cmd/tiproxyctl/main.go index 6f1b11f6..7b7ede49 100644 --- a/cmd/weirctl/main.go +++ b/cmd/tiproxyctl/main.go @@ -15,11 +15,15 @@ package main import ( + "os" + "strings" + "github.com/pingcap/TiProxy/lib/cli" "github.com/pingcap/TiProxy/lib/util/cmd" ) func main() { rootCmd := cli.GetRootCmd() + rootCmd.Use = strings.Replace(rootCmd.Use, "tiproxyctl", os.Args[0], 1) cmd.RunRootCommand(rootCmd) } diff --git a/conf/weirproxy.yaml b/conf/proxy.yaml similarity index 95% rename from conf/weirproxy.yaml rename to conf/proxy.yaml index 36398a77..ed76b190 100644 --- a/conf/weirproxy.yaml +++ b/conf/proxy.yaml @@ -40,16 +40,16 @@ security: # 2. useless/forbid: skip-ca cluster-tls: # client object # access to other components like TiDB or PD, will use this - skip-ca: true + # skip-ca: true sql-tls: # client object # access to TiDB sql port, it has a standalone TLS configuration skip-ca: true server-tls: # server object # proxy SQL or HTTP port will use this - auto-certs: true + # auto-certs: true peer-tls: # peer object # internal communication between proxies - auto-certs: true + # auto-certs: true advance: # ignore-wrong-namespace: true # peer-port: "3081" diff --git a/docker/Dockerfile b/docker/Dockerfile index 1de29978..6d5f5b32 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -15,4 +15,4 @@ ARG BUILDFLAGS ARG GOPROXY RUN export BUILDFLAGS=${BUILDFLAGS} && export GOPROXY=${GOPROXY} && cd proxy && make cmd && cp bin/* /bin/ && cp -a conf /etc/proxy && cd .. && rm -rf proxy RUN rm -rf $(go env GOMODCACHE GOCACHE) && apk del git make go -ENTRYPOINT ["/bin/weirproxy", "-conf", "/etc/proxy/weirproxy.yaml"] +ENTRYPOINT ["/bin/tiproxy", "-conf", "/etc/proxy/proxy.yaml"] diff --git a/lib/cli/config.go b/lib/cli/config.go index 6e063969..94fe3ff6 100644 --- a/lib/cli/config.go +++ b/lib/cli/config.go @@ -27,19 +27,18 @@ const ( configPrefix = "/api/admin/config" ) -func GetConfigCmd(ctx *Context) *cobra.Command { +func GetConfigProxyCmd(ctx *Context) *cobra.Command { rootCmd := &cobra.Command{ - Use: "config", - Short: "", + Use: "proxy", } - // config proxy + // set config proxy { - configProxy := &cobra.Command{ - Use: "proxy", + setProxy := &cobra.Command{ + Use: "set", } - input := configProxy.Flags().String("input", "", "specify the input json file for proxy config") - configProxy.RunE = func(cmd *cobra.Command, args []string) error { + input := setProxy.Flags().String("input", "", "specify the input json file for proxy config") + setProxy.RunE = func(cmd *cobra.Command, args []string) error { var b io.Reader if *input != "" { f, err := os.Open(*input) @@ -59,8 +58,34 @@ func GetConfigCmd(ctx *Context) *cobra.Command { cmd.Println(resp) return nil } - rootCmd.AddCommand(configProxy) + rootCmd.AddCommand(setProxy) + } + + // get config proxy + { + getProxy := &cobra.Command{ + Use: "get", + } + getProxy.RunE = func(cmd *cobra.Command, args []string) error { + resp, err := doRequest(cmd.Context(), ctx, http.MethodGet, fmt.Sprintf("%s/proxy", configPrefix), nil) + if err != nil { + return err + } + + cmd.Println(resp) + return nil + } + rootCmd.AddCommand(getProxy) } return rootCmd } + +func GetConfigCmd(ctx *Context) *cobra.Command { + rootCmd := &cobra.Command{ + Use: "config", + Short: "", + } + rootCmd.AddCommand(GetConfigProxyCmd(ctx)) + return rootCmd +} diff --git a/lib/cli/main.go b/lib/cli/main.go index b0ef7b27..b1cd7784 100644 --- a/lib/cli/main.go +++ b/lib/cli/main.go @@ -23,7 +23,7 @@ import ( func GetRootCmd() *cobra.Command { rootCmd := &cobra.Command{ - Use: "weirctl", + Use: "tiproxyctl", Short: "cli", } diff --git a/lib/go.mod b/lib/go.mod index 090c9036..a5366d08 100644 --- a/lib/go.mod +++ b/lib/go.mod @@ -1,6 +1,6 @@ module github.com/pingcap/TiProxy/lib -go 1.19 +go 1.16 require ( github.com/pingcap/log v1.1.0 @@ -13,16 +13,9 @@ require ( ) require ( - github.com/benbjohnson/clock v1.1.0 // indirect - github.com/davecgh/go-spew v1.1.1 // indirect - github.com/inconshreveable/mousetrap v1.0.0 // indirect github.com/kr/text v0.2.0 // indirect github.com/pingcap/errors v0.11.4 // indirect github.com/pkg/errors v0.9.1 // indirect - github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/spf13/pflag v1.0.5 // indirect - go.uber.org/multierr v1.7.0 // indirect golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 // indirect gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect - gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect ) diff --git a/lib/go.sum b/lib/go.sum index e5d4c5a6..2b88ad15 100644 --- a/lib/go.sum +++ b/lib/go.sum @@ -41,6 +41,7 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= go.etcd.io/etcd/client/pkg/v3 v3.5.4 h1:lrneYvz923dvC14R54XcA7FXoZ3mlGZAgmwhfm7HqOg= go.etcd.io/etcd/client/pkg/v3 v3.5.4/go.mod h1:IJHfcCEKxYu1Os13ZdwCwIUTUVGYTSAM3YSwc9/Ac1g= go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= @@ -48,6 +49,7 @@ go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE= go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= go.uber.org/goleak v1.1.10/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A= go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI= +go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ= go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU= go.uber.org/multierr v1.7.0 h1:zaiO/rmgFjbmCXdSYJWQcdvOCsthmdaHfr3Gm2Kx4Ec= go.uber.org/multierr v1.7.0/go.mod h1:7EAYxJLBy9rStEaz58O2t4Uvip6FSURkq8/ppBp95ak= @@ -56,18 +58,34 @@ go.uber.org/zap v1.19.0/go.mod h1:xg/QME4nWcxGxrpdeYfq7UvYrLh66cuVKdrbD1XF/NI= go.uber.org/zap v1.23.0 h1:OjGQ5KQDEUawVHxNwQgPpiypGHOxo2mNZsOqTak4fFY= go.uber.org/zap v1.23.0/go.mod h1:D+nX8jyLsMHMYrln8A0rJjFt/T/9/bGgIhAqxv5URuY= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20191108193012-7d206e10da11/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= diff --git a/lib/util/errors/error.go b/lib/util/errors/error.go index 4b8ac024..076775ab 100644 --- a/lib/util/errors/error.go +++ b/lib/util/errors/error.go @@ -81,7 +81,7 @@ func (e *Error) Is(target error) bool { return errors.Is(e.err, target) } -func (e *Error) As(target any) bool { +func (e *Error) As(target interface{}) bool { return errors.As(e.err, target) } diff --git a/lib/util/errors/errors.go b/lib/util/errors/errors.go index 999ea4c6..bc612043 100644 --- a/lib/util/errors/errors.go +++ b/lib/util/errors/errors.go @@ -23,7 +23,7 @@ func New(text string) error { return errors.New(text) } -func Errorf(format string, args ...any) error { +func Errorf(format string, args ...interface{}) error { return fmt.Errorf(format, args...) } @@ -31,7 +31,7 @@ func Is(err, target error) bool { return errors.Is(err, target) } -func As(err error, target any) bool { +func As(err error, target interface{}) bool { return errors.As(err, target) } diff --git a/lib/util/errors/werror.go b/lib/util/errors/werror.go index 730b0a02..39fd6ba0 100644 --- a/lib/util/errors/werror.go +++ b/lib/util/errors/werror.go @@ -74,7 +74,7 @@ func Wrap(cerr error, uerr error) error { } // Wrapf is like Wrap, with the underlying error being the result of `fmt.Errorf()` -func Wrapf(cerr error, msg string, args ...any) error { +func Wrapf(cerr error, msg string, args ...interface{}) error { if cerr == nil { return nil } diff --git a/pkg/manager/config/manager.go b/pkg/manager/config/manager.go index b6692e73..ed84af9c 100644 --- a/pkg/manager/config/manager.go +++ b/pkg/manager/config/manager.go @@ -61,9 +61,9 @@ func NewConfigManager() *ConfigManager { } } -func (srv *ConfigManager) Init(ctx context.Context, kv mvcc.WatchableKV, cfg config.Advance, logger *zap.Logger) error { +func (srv *ConfigManager) Init(ctx context.Context, kv mvcc.WatchableKV, cfg *config.Config, logger *zap.Logger) error { srv.logger = logger - srv.ignoreWrongNamespace = cfg.IgnoreWrongNamespace + srv.ignoreWrongNamespace = cfg.Advance.IgnoreWrongNamespace // slash appended to distinguish '/dir'(file) and '/dir/'(directory) srv.basePath = appendSlashToDirPath(DefaultEtcdPath) @@ -72,9 +72,7 @@ func (srv *ConfigManager) Init(ctx context.Context, kv mvcc.WatchableKV, cfg con ctx, cancel := context.WithCancel(ctx) srv.cancel = cancel - srv.initProxyConfig(ctx) - - return nil + return srv.watchCfgProxy(ctx, cfg) } func (e *ConfigManager) watch(ctx context.Context, ns, key string, f func(*zap.Logger, mvccpb.Event)) { @@ -85,7 +83,7 @@ func (e *ConfigManager) watch(ctx context.Context, ns, key string, f func(*zap.L wch := e.kv.NewWatchStream() defer wch.Close() for { - if _, err := wch.Watch(mvcc.AutoWatchID, wkey, getPrefix(wkey), wch.Rev()-1); err == nil { + if _, err := wch.Watch(mvcc.AutoWatchID, wkey, getPrefix(wkey), wch.Rev()); err == nil { break } if k := retryInterval * 2; k < e.watchInterval { diff --git a/pkg/manager/config/manager_test.go b/pkg/manager/config/manager_test.go index fd99f794..267c7b4a 100644 --- a/pkg/manager/config/manager_test.go +++ b/pkg/manager/config/manager_test.go @@ -33,7 +33,7 @@ import ( "go.uber.org/zap" ) -func testConfigManager(t *testing.T, cfg config.Advance) (*ConfigManager, context.Context) { +func testConfigManager(t *testing.T, cfg *config.Config) (*ConfigManager, context.Context) { addr, err := url.Parse("http://127.0.0.1:0") require.NoError(t, err) @@ -68,9 +68,7 @@ func testConfigManager(t *testing.T, cfg config.Advance) (*ConfigManager, contex } func TestBase(t *testing.T) { - cfgmgr, ctx := testConfigManager(t, config.Advance{ - IgnoreWrongNamespace: true, - }) + cfgmgr, ctx := testConfigManager(t, &config.Config{}) nsNum := 10 valNum := 30 @@ -130,9 +128,7 @@ func TestBase(t *testing.T) { } func TestBaseConcurrency(t *testing.T) { - cfgmgr, ctx := testConfigManager(t, config.Advance{ - IgnoreWrongNamespace: true, - }) + cfgmgr, ctx := testConfigManager(t, &config.Config{}) var wg waitgroup.WaitGroup batchNum := 16 @@ -171,28 +167,23 @@ func TestBaseConcurrency(t *testing.T) { } func TestBaseWatch(t *testing.T) { - cfgmgr, ctx := testConfigManager(t, config.Advance{ - IgnoreWrongNamespace: true, - }) + cfgmgr, ctx := testConfigManager(t, &config.Config{}) ch := make(chan string, 1) cfgmgr.watch(ctx, "test", "t", func(l *zap.Logger, e mvccpb.Event) { ch <- string(e.Kv.Value) }) - // clear the channel first - for len(ch) > 0 { - <-ch - } - // set it require.NoError(t, cfgmgr.set(ctx, "test", "t", "1")) - // now the only way to check watch is to wait select { case <-time.After(5 * time.Second): t.Fatal("timeout waiting chan") case tg := <-ch: + for len(ch) > 0 { + tg = <-ch + } require.Equal(t, "1", tg) } } diff --git a/pkg/manager/config/proxy.go b/pkg/manager/config/proxy.go index 59a3ffd3..ee46d432 100644 --- a/pkg/manager/config/proxy.go +++ b/pkg/manager/config/proxy.go @@ -19,11 +19,20 @@ import ( "encoding/json" "github.com/pingcap/TiProxy/lib/config" + "github.com/pingcap/TiProxy/lib/util/errors" "go.etcd.io/etcd/api/v3/mvccpb" "go.uber.org/zap" ) -func (e *ConfigManager) initProxyConfig(ctx context.Context) { +func (e *ConfigManager) watchCfgProxy(ctx context.Context, cfg *config.Config) error { + if _, err := e.GetProxyConfig(ctx); err != nil && errors.Is(err, ErrNoOrMultiResults) { + if err := e.SetProxyConfig(ctx, &config.ProxyServerOnline{ + MaxConnections: cfg.Proxy.MaxConnections, + TCPKeepAlive: cfg.Proxy.TCPKeepAlive, + }); err != nil { + return err + } + } e.watch(ctx, PathPrefixProxy, "config", func(logger *zap.Logger, evt mvccpb.Event) { var proxy config.ProxyServerOnline if err := json.Unmarshal(evt.Kv.Value, &proxy); err != nil { @@ -32,12 +41,25 @@ func (e *ConfigManager) initProxyConfig(ctx context.Context) { } e.chProxy <- &proxy }) + return nil } -func (e *ConfigManager) GetProxyConfig() <-chan *config.ProxyServerOnline { +func (e *ConfigManager) GetProxyConfigWatch() <-chan *config.ProxyServerOnline { return e.chProxy } +func (e *ConfigManager) GetProxyConfig(ctx context.Context) (*config.ProxyServerOnline, error) { + val, err := e.get(ctx, PathPrefixProxy, "config") + if err != nil { + return nil, err + } + ret := &config.ProxyServerOnline{} + if err := json.Unmarshal(val.Value, ret); err != nil { + return nil, err + } + return ret, nil +} + func (e *ConfigManager) SetProxyConfig(ctx context.Context, proxy *config.ProxyServerOnline) error { value, err := json.Marshal(proxy) if err != nil { diff --git a/pkg/manager/config/proxy_test.go b/pkg/manager/config/proxy_test.go index b0615215..05a0ba35 100644 --- a/pkg/manager/config/proxy_test.go +++ b/pkg/manager/config/proxy_test.go @@ -23,9 +23,7 @@ import ( ) func TestProxyConfig(t *testing.T) { - cfgmgr, ctx := testConfigManager(t, config.Advance{ - IgnoreWrongNamespace: true, - }) + cfgmgr, ctx := testConfigManager(t, &config.Config{}) cases := []*config.ProxyServerOnline{ { @@ -46,13 +44,17 @@ func TestProxyConfig(t *testing.T) { }, } - ch := cfgmgr.GetProxyConfig() + ch := cfgmgr.GetProxyConfigWatch() + for _, tc := range cases { require.NoError(t, cfgmgr.SetProxyConfig(ctx, tc)) select { case <-time.After(5 * time.Second): t.Fatal("timeout waiting chan") case tg := <-ch: + for len(ch) > 0 { + tg = <-ch + } require.Equal(t, tc, tg) } } diff --git a/pkg/server/api/config.go b/pkg/server/api/config.go index 80216870..8f7a2927 100644 --- a/pkg/server/api/config.go +++ b/pkg/server/api/config.go @@ -36,6 +36,7 @@ func (h *configHttpHandler) HandleSetProxyConfig(c *gin.Context) { } if err := h.cfgmgr.SetProxyConfig(c, pco); err != nil { + h.logger.Error("can not update proxy config", zap.Error(err)) c.JSON(http.StatusInternalServerError, "can not update proxy config") return } @@ -43,7 +44,19 @@ func (h *configHttpHandler) HandleSetProxyConfig(c *gin.Context) { c.JSON(http.StatusOK, "") } +func (h *configHttpHandler) HandleGetProxyConfig(c *gin.Context) { + pco, err := h.cfgmgr.GetProxyConfig(c) + if err != nil { + h.logger.Error("can not get proxy config", zap.Error(err)) + c.JSON(http.StatusInternalServerError, "can not get proxy config") + return + } + + c.JSON(http.StatusOK, pco) +} + func registerConfig(group *gin.RouterGroup, logger *zap.Logger, mgrcfg *mgrcfg.ConfigManager) { h := &configHttpHandler{logger, mgrcfg} group.PUT("/proxy", h.HandleSetProxyConfig) + group.GET("/proxy", h.HandleGetProxyConfig) } diff --git a/pkg/server/server.go b/pkg/server/server.go index eaaf5bc2..d37a9ef4 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -139,7 +139,7 @@ func NewServer(ctx context.Context, cfg *config.Config, logger *zap.Logger, pubA // setup config manager { - err = srv.ConfigManager.Init(ctx, srv.Etcd.Server.KV(), cfg.Advance, logger.Named("config")) + err = srv.ConfigManager.Init(ctx, srv.Etcd.Server.KV(), cfg, logger.Named("config")) if err != nil { err = errors.WithStack(err) return @@ -201,7 +201,7 @@ func NewServer(ctx context.Context, cfg *config.Config, logger *zap.Logger, pubA } func (s *Server) Run(ctx context.Context) { - s.Proxy.Run(ctx, s.ConfigManager.GetProxyConfig()) + s.Proxy.Run(ctx, s.ConfigManager.GetProxyConfigWatch()) } func (s *Server) Close() error {