Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

server: use json for API and internal storage #66

Merged
merged 1 commit into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 34 additions & 6 deletions cmd/weirctl/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@
package main

import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"os"
"strings"

"github.com/pingcap/TiProxy/pkg/config"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
)

const (
Expand All @@ -43,7 +47,15 @@ func GetNamespaceCmd(ctx *Context) *cobra.Command {
return err
}

cmd.Println(resp)
var nscs []config.Namespace
if err := json.Unmarshal([]byte(resp), &nscs); err != nil {
return err
}
nscsbytes, err := yaml.Marshal(&nscs)
if err != nil {
return err
}
cmd.Print(string(nscsbytes))
return nil
},
},
Expand All @@ -60,7 +72,7 @@ func GetNamespaceCmd(ctx *Context) *cobra.Command {
return err
}

cmd.Println(resp)
cmd.Print(resp)
return nil
}
rootCmd.AddCommand(commitNamespaces)
Expand All @@ -81,7 +93,15 @@ func GetNamespaceCmd(ctx *Context) *cobra.Command {
return err
}

cmd.Println(resp)
var nsc config.Namespace
if err := json.Unmarshal([]byte(resp), &nsc); err != nil {
return err
}
nscbytes, err := yaml.Marshal(&nsc)
if err != nil {
return err
}
cmd.Print(string(nscbytes))
return nil
}
rootCmd.AddCommand(getNamespace)
Expand All @@ -107,13 +127,21 @@ func GetNamespaceCmd(ctx *Context) *cobra.Command {
defer f.Close()
in = f
}
var nsc config.Namespace
if err := yaml.NewDecoder(in).Decode(&nsc); err != nil {
return err
}
nscbytes, err := json.Marshal(&nsc)
if err != nil {
return err
}

resp, err := doRequest(cmd.Context(), ctx, http.MethodPut, fmt.Sprintf("%s/%s", namespacePrefix, args[0]), in)
resp, err := doRequest(cmd.Context(), ctx, http.MethodPut, fmt.Sprintf("%s/%s", namespacePrefix, args[0]), bytes.NewReader(nscbytes))
if err != nil {
return err
}

cmd.Println(resp)
cmd.Print(resp)
return nil
}
rootCmd.AddCommand(putNamespace)
Expand All @@ -134,7 +162,7 @@ func GetNamespaceCmd(ctx *Context) *cobra.Command {
return err
}

cmd.Println(resp)
cmd.Print(resp)
return nil
}
rootCmd.AddCommand(delNamespace)
Expand Down
7 changes: 4 additions & 3 deletions pkg/config/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"path/filepath"

"github.com/pingcap/TiProxy/pkg/util/errors"
"gopkg.in/yaml.v3"
)

var (
Expand Down Expand Up @@ -51,11 +52,11 @@ func NewNamespacesFromDir(nsdir string) (*NamespaceDir, error) {
if err != nil {
return nil, err
}
cfg, err := NewNamespaceConfig(fileData)
if err != nil {
var cfg Namespace
if err := yaml.Unmarshal(fileData, &cfg); err != nil {
return nil, err
}
c.cfgs[cfg.Namespace] = cfg
c.cfgs[cfg.Namespace] = &cfg
c.nspath[cfg.Namespace] = yamlFile
}

Expand Down
40 changes: 7 additions & 33 deletions pkg/config/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,44 +15,18 @@

package config

import (
"io/ioutil"

"github.com/goccy/go-yaml"
)

type Namespace struct {
Namespace string `yaml:"namespace"`
Frontend FrontendNamespace `yaml:"frontend"`
Backend BackendNamespace `yaml:"backend"`
Namespace string `yaml:"namespace" json:"namespace" toml:"namespace"`
Frontend FrontendNamespace `yaml:"frontend" json:"frontend" toml:"frontend"`
Backend BackendNamespace `yaml:"backend" json:"backend" toml:"backend"`
}

type FrontendNamespace struct {
Security TLSCert `yaml:"security"`
Security TLSCert `yaml:"security" json:"security" toml:"security"`
}

type BackendNamespace struct {
Instances []string `yaml:"instances"`
SelectorType string `yaml:"selector_type"`
Security TLSCert `yaml:"security"`
}

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

func NewNamespaceConfigFile(path string) (*Namespace, error) {
fileData, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
return NewNamespaceConfig(fileData)
}

func (cfg *Namespace) ToBytes() ([]byte, error) {
return yaml.Marshal(cfg)
Instances []string `yaml:"instances" json:"instances" toml:"instances"`
SelectorType string `yaml:"selector_type" json:"selector_type" toml:"selector_type"`
Security TLSCert `yaml:"security" json:"security" toml:"security"`
}
9 changes: 5 additions & 4 deletions pkg/config/namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

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

var testNamespaceConfig = Namespace{
Expand All @@ -27,9 +28,9 @@ var testNamespaceConfig = Namespace{
}

func TestNamespaceConfig(t *testing.T) {
data, err := testNamespaceConfig.ToBytes()
data, err := yaml.Marshal(testNamespaceConfig)
require.NoError(t, err)
cfg, err := NewNamespaceConfig(data)
require.NoError(t, err)
require.Equal(t, testNamespaceConfig, *cfg)
var cfg Namespace
require.NoError(t, yaml.Unmarshal(data, &cfg))
require.Equal(t, testNamespaceConfig, cfg)
}
20 changes: 12 additions & 8 deletions pkg/manager/config/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,23 @@ package config

import (
"context"
"encoding/json"
"io/ioutil"
"path/filepath"

"github.com/pingcap/TiProxy/pkg/config"
"go.uber.org/zap"
"gopkg.in/yaml.v3"
)

func (e *ConfigManager) GetNamespace(ctx context.Context, ns string) (*config.Namespace, error) {
etcdKeyValue, err := e.get(ctx, PathPrefixNamespace, ns)
if err != nil {
return nil, err
}
return config.NewNamespaceConfig(etcdKeyValue.Value)
var cfg config.Namespace
err = json.Unmarshal(etcdKeyValue.Value, &cfg)
return &cfg, err
}

func (e *ConfigManager) ListAllNamespace(ctx context.Context) ([]*config.Namespace, error) {
Expand All @@ -39,23 +43,23 @@ func (e *ConfigManager) ListAllNamespace(ctx context.Context) ([]*config.Namespa

var ret []*config.Namespace
for _, kv := range etcdKeyValues {
nsCfg, err := config.NewNamespaceConfig(kv.Value)
if err != nil {
var nsCfg config.Namespace
if err := json.Unmarshal(kv.Value, &nsCfg); err != nil {
if e.ignoreWrongNamespace {
e.logger.Warn("parse namespace config error", zap.Error(err), zap.ByteString("namespace", kv.Key))
continue
} else {
return nil, err
}
}
ret = append(ret, nsCfg)
ret = append(ret, &nsCfg)
}

return ret, nil
}

func (e *ConfigManager) SetNamespace(ctx context.Context, ns string, nsc *config.Namespace) error {
r, err := nsc.ToBytes()
r, err := json.Marshal(nsc)
if err != nil {
return err
}
Expand All @@ -78,11 +82,11 @@ func (e *ConfigManager) ImportNamespaceFromDir(ctx context.Context, dir string)
if err != nil {
return err
}
cfg, err := config.NewNamespaceConfig(fileData)
if err != nil {
var cfg config.Namespace
if err := yaml.Unmarshal(fileData, &cfg); err != nil {
return err
}
if err := e.SetNamespace(ctx, cfg.Namespace, cfg); err != nil {
if err := e.SetNamespace(ctx, cfg.Namespace, &cfg); err != nil {
return err
}
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/server/api/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ type configHttpHandler struct {
func (h *configHttpHandler) HandleSetProxyConfig(c *gin.Context) {
pco := &config.ProxyServerOnline{}
if c.ShouldBindJSON(pco) != nil {
c.String(http.StatusBadRequest, "bad proxy config json")
c.JSON(http.StatusBadRequest, "bad proxy config json")
return
}

if err := h.cfgmgr.SetProxyConfig(c, pco); err != nil {
c.String(http.StatusInternalServerError, "can not update proxy config")
c.JSON(http.StatusInternalServerError, "can not update proxy config")
return
}

c.String(http.StatusOK, "")
c.JSON(http.StatusOK, "")
}

func registerConfig(group *gin.RouterGroup, logger *zap.Logger, mgrcfg *mgrcfg.ConfigManager) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/server/api/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ func (h *debugHttpHandler) Redirect(c *gin.Context) {
}
h.logger.Error(errMsg, err_fields...)

c.String(http.StatusInternalServerError, errMsg)
c.JSON(http.StatusInternalServerError, errMsg)
} else {
c.String(http.StatusOK, "")
c.JSON(http.StatusOK, "")
}
}

Expand Down
30 changes: 15 additions & 15 deletions pkg/server/api/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,55 +33,55 @@ type namespaceHttpHandler struct {
func (h *namespaceHttpHandler) HandleGetNamespace(c *gin.Context) {
ns := c.Param("namespace")
if ns == "" {
c.String(http.StatusBadRequest, "bad namespace parameter")
c.JSON(http.StatusBadRequest, "bad namespace parameter")
return
}

nsc, err := h.cfgmgr.GetNamespace(c, ns)
if err != nil {
h.logger.Error("can not get namespace", zap.String("namespace", ns), zap.Error(err))
c.String(http.StatusInternalServerError, "can not get namespace")
c.JSON(http.StatusInternalServerError, "can not get namespace")
return
}

c.YAML(http.StatusOK, nsc)
c.JSON(http.StatusOK, nsc)
}

func (h *namespaceHttpHandler) HandleUpsertNamesapce(c *gin.Context) {
ns := c.Param("namespace")
if ns == "" {
c.String(http.StatusBadRequest, "bad namespace parameter")
c.JSON(http.StatusBadRequest, "bad namespace parameter")
return
}

nsc := &config.Namespace{}

if c.ShouldBindYAML(nsc) != nil {
c.String(http.StatusBadRequest, "bad namespace json")
if c.ShouldBindJSON(nsc) != nil {
c.JSON(http.StatusBadRequest, "bad namespace json")
return
}

if err := h.cfgmgr.SetNamespace(c, nsc.Namespace, nsc); err != nil {
c.String(http.StatusInternalServerError, "can not update config")
c.JSON(http.StatusInternalServerError, "can not update config")
return
}

c.String(http.StatusOK, "")
c.JSON(http.StatusOK, "")
}

func (h *namespaceHttpHandler) HandleRemoveNamespace(c *gin.Context) {
ns := c.Param("namespace")
if ns == "" {
c.String(http.StatusBadRequest, "bad namespace parameter")
c.JSON(http.StatusBadRequest, "bad namespace parameter")
return
}

if err := h.cfgmgr.DelNamespace(c, ns); err != nil {
c.String(http.StatusInternalServerError, "can not update config")
c.JSON(http.StatusInternalServerError, "can not update config")
return
}

c.String(http.StatusOK, "")
c.JSON(http.StatusOK, "")
}

func (h *namespaceHttpHandler) HandleCommit(c *gin.Context) {
Expand Down Expand Up @@ -109,23 +109,23 @@ func (h *namespaceHttpHandler) HandleCommit(c *gin.Context) {
if err := h.nsmgr.CommitNamespaces(nss, nss_delete); err != nil {
errMsg := "commit reload namespace error"
h.logger.Error(errMsg, zap.Error(err), zap.Any("namespaces", nss))
c.String(http.StatusInternalServerError, errMsg)
c.JSON(http.StatusInternalServerError, errMsg)
return
}

c.String(http.StatusOK, "")
c.JSON(http.StatusOK, "")
}

func (h *namespaceHttpHandler) HandleList(c *gin.Context) {
nscs, err := h.cfgmgr.ListAllNamespace(c)
if err != nil {
errMsg := "failed to list namespaces"
h.logger.Error(errMsg, zap.Error(err))
c.String(http.StatusInternalServerError, errMsg)
c.JSON(http.StatusInternalServerError, errMsg)
return
}

c.YAML(http.StatusOK, nscs)
c.JSON(http.StatusOK, nscs)
}

func registerNamespace(group *gin.RouterGroup, logger *zap.Logger, mgrcfg *mgrcfg.ConfigManager, mgrns *mgrns.NamespaceManager) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func NewServer(ctx context.Context, cfg *config.Config, logger *zap.Logger, name
func(c *gin.Context) {
if !ready.Load() {
c.Abort()
c.String(http.StatusInternalServerError, "service not ready")
c.JSON(http.StatusInternalServerError, "service not ready")
}
},
)
Expand Down