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

config: add a option about gRPC gateway #1596

Merged
merged 3 commits into from Jun 26, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 51 additions & 0 deletions server/api/etcd_api_test.go
@@ -0,0 +1,51 @@
// Copyright 2019 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package api

import (
"encoding/json"
"net/http"
"strings"

. "github.com/pingcap/check"
)

var _ = Suite(&testEtcdAPISuite{})

type testEtcdAPISuite struct {
hc *http.Client
}

func (s *testEtcdAPISuite) SetUpSuite(c *C) {
s.hc = newHTTPClient()
}

func (s *testEtcdAPISuite) TestGRPCGateway(c *C) {
svr, clean := mustNewServer(c)
defer clean()

addr := svr.GetConfig().ClientUrls + "/v3/kv/put"
putKey := map[string]string{"key": "Zm9v", "value": "YmFy"}
v, _ := json.Marshal(putKey)
err := postJSON(addr, v)
c.Assert(err, IsNil)
addr = svr.GetConfig().ClientUrls + "/v3/kv/range"
getKey := map[string]string{"key": "Zm9v"}
v, _ = json.Marshal(getKey)
err = postJSON(addr, v, func(res []byte) bool {
c.Assert(strings.Contains(string(res), "Zm9v"), IsTrue)
return true
})
c.Assert(err, IsNil)
}
11 changes: 9 additions & 2 deletions server/api/util.go
Expand Up @@ -86,19 +86,26 @@ func readJSON(r io.ReadCloser, data interface{}) error {
return nil
}

func postJSON(url string, data []byte) error {
func postJSON(url string, data []byte, checkOpts ...func(res []byte) bool) error {
resp, err := dialClient.Post(url, "application/json", bytes.NewBuffer(data))
if err != nil {
return errors.WithStack(err)
}
res, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
defer resp.Body.Close()

if err != nil {
return err
}

if resp.StatusCode != http.StatusOK {
return errors.New(string(res))
}
for _, opt := range checkOpts {
if !opt(res) {
return errors.New("check failed")
}
}
return nil
}

Expand Down
12 changes: 9 additions & 3 deletions server/config.go
Expand Up @@ -49,9 +49,10 @@ type Config struct {
AdvertiseClientUrls string `toml:"advertise-client-urls" json:"advertise-client-urls"`
AdvertisePeerUrls string `toml:"advertise-peer-urls" json:"advertise-peer-urls"`

Name string `toml:"name" json:"name"`
DataDir string `toml:"data-dir" json:"data-dir"`
ForceNewCluster bool `json:"force-new-cluster"`
Name string `toml:"name" json:"name"`
DataDir string `toml:"data-dir" json:"data-dir"`
ForceNewCluster bool `json:"force-new-cluster"`
EnableGRPCGateway bool `json:"enable-grpc-gateway"`

InitialCluster string `toml:"initial-cluster" json:"initial-cluster"`
InitialClusterState string `toml:"initial-cluster-state" json:"initial-cluster-state"`
Expand Down Expand Up @@ -199,6 +200,7 @@ const (

defaultUseRegionStorage = true
defaultStrictlyMatchLabel = false
defaultEnableGRPCGateway = true
)

func adjustString(v *string, defValue string) {
Expand Down Expand Up @@ -427,6 +429,9 @@ func (c *Config) Adjust(meta *toml.MetaData) error {
if !configMetaData.IsDefined("enable-prevote") {
c.PreVote = true
}
if !configMetaData.IsDefined("enable-grpc-gateway") {
c.EnableGRPCGateway = defaultEnableGRPCGateway
}
return nil
}

Expand Down Expand Up @@ -876,6 +881,7 @@ func (c *Config) genEmbedEtcdConfig() (*embed.Config, error) {
cfg.PeerTLSInfo.KeyFile = c.Security.KeyPath
cfg.ForceNewCluster = c.ForceNewCluster
cfg.ZapLoggerBuilder = embed.NewZapCoreLoggerBuilder(c.logger, c.logger.Core(), c.logProps.Syncer)
cfg.EnableGRPCGateway = c.EnableGRPCGateway
cfg.Logger = "zap"
var err error

Expand Down