Skip to content

Commit

Permalink
*: adapt to toml (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
xhebox authored Dec 27, 2022
1 parent 644009c commit d642637
Show file tree
Hide file tree
Showing 17 changed files with 159 additions and 115 deletions.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,17 @@ cmd_%:
golangci-lint:
GOBIN=$(GOBIN) go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest

lint: golangci-lint
lint: golangci-lint tidy
$(GOBIN)/golangci-lint run
cd lib && $(GOBIN)/golangci-lint run

gocovmerge:
GOBIN=$(GOBIN) go install github.com/wadey/gocovmerge@master

tidy:
go mod tidy
cd lib && go mod tidy

test: gocovmerge
rm -f .cover.*
go test -coverprofile=.cover.pkg ./...
Expand Down
2 changes: 1 addition & 1 deletion cmd/tiproxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func main() {
Short: "start the proxy server",
}

configFile := rootCmd.PersistentFlags().String("config", "conf/proxy.yaml", "proxy config file path")
configFile := rootCmd.PersistentFlags().String("config", "conf/proxy.toml", "proxy config file path")
logEncoder := rootCmd.PersistentFlags().String("log_encoder", "tidb", "log in format of tidb, console, or json")
logLevel := rootCmd.PersistentFlags().String("log_level", "", "log level")
_ = rootCmd.PersistentFlags().String("cluster_name", "tiproxy", "default cluster name, used to generate node name and differential clusters in dns discovery")
Expand Down
7 changes: 7 additions & 0 deletions conf/namespace/example.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace = "default"

[frontend]

[backend]
instances = [ "127.0.0.1:4000" ]
selector-type = "random"
8 changes: 0 additions & 8 deletions conf/namespace/example.yaml

This file was deleted.

8 changes: 8 additions & 0 deletions conf/namespace/test.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace = "ns2"

[frontend]
user = "xhe"

[backend]
instances = [ "127.0.0.1:7000" ]
selector-type = "random"
9 changes: 0 additions & 9 deletions conf/namespace/test.yaml

This file was deleted.

74 changes: 74 additions & 0 deletions conf/proxy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
workdir = "./work"

[proxy]
addr = "0.0.0.0:6000"
tcp-keep-alive = true
max-connections = 1000
pd-addrs = "127.0.0.1:2379"
# require-backend-tls = true
# proxy-protocol = "v2"
# graceful-wait-before-shutdown = 10

[metrics]

[api]
addr = "0.0.0.0:3080"
enable-basic-auth = false
user = ""
password = ""

[log]
level = "info"
encoder = "tidb"

[log.log-file]
filename = ""
max-size = 300
max-days = 1
max-backups = 1

[security]
rsa-key-size = 4_096
# tls object is either of type server, client, or peer
# [xxxx]
# ca = "ca.pem"
# cert = "c.pem"
# key = "k.pem"
# auto-certs = true
# skip-ca = trure
# client object:
# 1. requires: ca or skip-ca(skip verify server certs)
# 2. optionally: cert/key will be used if server asks
# 3. useless/forbid: auto-certs
# server object:
# 1. requires: cert/key or auto-certs(generate a temporary cert, mostly for testing)
# 2. optionally: ca will enable server-side client verification.
# 3. useless/forbid: skip-ca
# peer object:
# 1. requires: cert/key/ca or auto-certs
# 2. useless/forbid: skip-ca

# client object
[security.cluster-tls]
# access to other components like TiDB or PD, will use this
# skip-ca = true

# client object
[security.sql-tls]
# access to other components like TiDB or PD, will use this
skip-ca = true

# server object
[security.server-tls]
# proxy SQL or HTTP port will use this
# auto-certs: true

# peer object
[security.peer-tls]
# internal communication between proxies
# auto-certs: true

[advance]
# ignore-wrong-namespace = true
# peer-port = "3081"
# watch-interval = "10m"
59 changes: 0 additions & 59 deletions conf/proxy.yaml

This file was deleted.

2 changes: 1 addition & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ EXPOSE 3080
EXPOSE 3081
EXPOSE 6000

ENTRYPOINT ["/bin/tiproxy", "-conf", "/etc/proxy/proxy.yaml"]
ENTRYPOINT ["/bin/tiproxy", "-conf", "/etc/proxy/proxy.toml"]
2 changes: 1 addition & 1 deletion lib/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func GetRootCmd(tlsConfig *tls.Config) *cobra.Command {

curls := rootCmd.PersistentFlags().StringArray("curls", []string{"localhost:3080"}, "API gateway addresses")
logEncoder := rootCmd.PersistentFlags().String("log_encoder", "tidb", "log in format of tidb, console, or json")
logLevel := rootCmd.PersistentFlags().String("log_level", "info", "log level")
logLevel := rootCmd.PersistentFlags().String("log_level", "warn", "log level")
insecure := rootCmd.PersistentFlags().BoolP("insecure", "k", false, "enable TLS without CA, useful for testing, or for expired certs")
caPath := rootCmd.PersistentFlags().String("ca", "", "CA to verify server certificates, set to 'skip' if want to enable SSL without verification")
certPath := rootCmd.PersistentFlags().String("cert", "", "cert for server-side client authentication")
Expand Down
32 changes: 18 additions & 14 deletions lib/cli/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"

"github.com/BurntSushi/toml"
"github.com/pingcap/TiProxy/lib/config"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
)

const (
Expand Down Expand Up @@ -69,12 +70,11 @@ func GetNamespaceCmd(ctx *Context) *cobra.Command {
if err := json.Unmarshal([]byte(resp), &nscs); err != nil {
return err
}
nscsbytes, err := yaml.Marshal(&nscs)
if err != nil {
return err
nscsmap := make(map[string]config.Namespace, len(nscs))
for _, nsc := range nscs {
nscsmap[nsc.Namespace] = nsc
}
cmd.Println(string(nscsbytes))
return nil
return toml.NewEncoder(cmd.OutOrStdout()).Encode(nscsmap)
},
},
)
Expand Down Expand Up @@ -115,7 +115,7 @@ func GetNamespaceCmd(ctx *Context) *cobra.Command {
if err := json.Unmarshal([]byte(resp), &nsc); err != nil {
return err
}
nscbytes, err := yaml.Marshal(&nsc)
nscbytes, err := nsc.ToBytes()
if err != nil {
return err
}
Expand All @@ -141,11 +141,15 @@ func GetNamespaceCmd(ctx *Context) *cobra.Command {
defer f.Close()
in = f
}
var nsc config.Namespace
if err := yaml.NewDecoder(in).Decode(&nsc); err != nil {
nscbytes, err := io.ReadAll(in)
if err != nil {
return err
}
nscbytes, err := json.Marshal(&nsc)
nsc, err := config.NewNamespace(nscbytes)
if err != nil {
return err
}
nscbytes, err = json.Marshal(nsc)
if err != nil {
return err
}
Expand All @@ -171,7 +175,7 @@ func GetNamespaceCmd(ctx *Context) *cobra.Command {
return cmd.Help()
}

nFiles, err := listAllFiles(args[0], ".yaml")
nFiles, err := listAllFiles(args[0], ".toml")
if err != nil {
return err
}
Expand All @@ -181,11 +185,11 @@ func GetNamespaceCmd(ctx *Context) *cobra.Command {
if err != nil {
return err
}
var nsc config.Namespace
if err := yaml.Unmarshal(fileData, &nsc); err != nil {
nsc, err := config.NewNamespace(fileData)
if err != nil {
return err
}
nscbytes, err := json.Marshal(&nsc)
nscbytes, err := json.Marshal(nsc)
if err != nil {
return err
}
Expand Down
20 changes: 20 additions & 0 deletions lib/config/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@

package config

import (
"bytes"

"github.com/BurntSushi/toml"
)

type Namespace struct {
Namespace string `yaml:"namespace" json:"namespace" toml:"namespace"`
Frontend FrontendNamespace `yaml:"frontend" json:"frontend" toml:"frontend"`
Expand All @@ -31,3 +37,17 @@ type BackendNamespace struct {
SelectorType string `yaml:"selector-type" json:"selector-type" toml:"selector-type"`
Security TLSConfig `yaml:"security" json:"security" toml:"security"`
}

func NewNamespace(data []byte) (*Namespace, error) {
var cfg Namespace
if err := toml.Unmarshal(data, &cfg); err != nil {
return nil, err
}
return &cfg, nil
}

func (cfg *Namespace) ToBytes() ([]byte, error) {
b := new(bytes.Buffer)
err := toml.NewEncoder(b).Encode(cfg)
return b.Bytes(), err
}
11 changes: 6 additions & 5 deletions lib/config/namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"testing"

"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
)

var testNamespaceConfig = Namespace{
Expand All @@ -45,9 +44,11 @@ var testNamespaceConfig = Namespace{
}

func TestNamespaceConfig(t *testing.T) {
data, err := yaml.Marshal(testNamespaceConfig)
data1, err := testNamespaceConfig.ToBytes()
require.NoError(t, err)
var cfg Namespace
require.NoError(t, yaml.Unmarshal(data, &cfg))
require.Equal(t, testNamespaceConfig, cfg)
cfg, err := NewNamespace(data1)
require.NoError(t, err)
data2, err := cfg.ToBytes()
require.NoError(t, err)
require.Equal(t, data1, data2)
}
9 changes: 6 additions & 3 deletions lib/config/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
package config

import (
"bytes"
"os"
"path/filepath"

"github.com/BurntSushi/toml"
"github.com/pingcap/TiProxy/lib/util/errors"
"gopkg.in/yaml.v3"
)

var (
Expand Down Expand Up @@ -116,7 +117,7 @@ func NewConfig(data []byte) (*Config, error) {
var cfg Config
cfg.Advance.IgnoreWrongNamespace = true
cfg.Proxy.RequireBackendTLS = true
if err := yaml.Unmarshal(data, &cfg); err != nil {
if err := toml.Unmarshal(data, &cfg); err != nil {
return nil, err
}
if err := cfg.Check(); err != nil {
Expand All @@ -143,5 +144,7 @@ func (cfg *Config) Check() error {
}

func (cfg *Config) ToBytes() ([]byte, error) {
return yaml.Marshal(cfg)
b := new(bytes.Buffer)
err := toml.NewEncoder(b).Encode(cfg)
return b.Bytes(), err
}
3 changes: 1 addition & 2 deletions lib/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ require (
go.etcd.io/etcd/client/pkg/v3 v3.5.4
go.uber.org/atomic v1.9.0
go.uber.org/zap v1.23.0
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/BurntSushi/toml v0.3.1 // indirect
github.com/BurntSushi/toml v0.3.1
github.com/kr/text v0.2.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
go.uber.org/multierr v1.7.0 // indirect
Expand Down
Loading

0 comments on commit d642637

Please sign in to comment.