go beetle: increase consul request timeout
MODIFY
Makefile
Makefile
@@ -41,7 +41,7 @@ install: $(GO_INSTALL_TARGETS)
uninstall:
cd $(BIN_DIR) && rm -f $(GO_INSTALL_TARGETS) $(SCRIPTS)
-GO_MODULES = $(patsubst %,$(GO_SRC)/%, client.go server.go datatypes.go server_state.go failover_state.go redis.go redis_shim.go redis_server_info.go logging.go version.go garbage_collect_keys.go notification_mailer.go config.go delete_keys.go copy_keys.go dump_expiries.go)
+GO_MODULES = $(patsubst %,$(GO_SRC)/%, client.go server.go datatypes.go server_state.go failover_state.go redis.go redis_shim.go redis_server_info.go logging.go version.go garbage_collect_keys.go notification_mailer.go config.go delete_keys.go copy_keys.go dump_expiries.go consul/consul.go)
.packr:
go get github.com/gobuffalo/packr/packr

MODIFY
go/beetle.go
go/beetle.go
@@ -35,6 +35,7 @@ var opts struct {
Server string `long:"server" description:"Specifies config server address."`
Port int `long:"port" description:"Port to use for web socket connections. Defaults to 9650."`
ConsulUrl string `long:"consul" optional:"t" optional-value:"http://127.0.0.1:8500" description:"Specifies consul server url to use for retrieving config values. If given without argument, tries to contact local consul agent."`
+ ConsulToken string `long:"consul-token" env:"BEETLE_CONSUL_TOKEN" description:"Specifies consul authentication token."`
GcThreshold int `long:"redis-gc-threshold" description:"Number of seconds to wait until considering an expired redis key eligible for garbage collection. Defaults to 3600 (1 hour)."`
GcDatabases string `long:"redis-gc-databases" description:"Database numbers to collect keys from (e.g. 0,4). Defaults to 4."`
GcSystem string `long:"redis-gc-system" default:"system" description:"Redis system from which to collect keys."`
@@ -320,7 +321,7 @@ var consulClient *consul.Client
func getConsulClient() *consul.Client {
if opts.ConsulUrl != "" && consulClient == nil {
- consulClient = consul.NewClient(opts.ConsulUrl, "beetle")
+ consulClient = consul.NewClient(opts.ConsulUrl, opts.ConsulToken, "beetle")
}
return consulClient
}

MODIFY
go/consul/consul.go
go/consul/consul.go
@@ -65,6 +65,7 @@ type Space struct {
// Client is used to access consul
type Client struct {
consulUrl string
+ consulToken string
appName string
appConfig Space
sharedConfig Space
@@ -74,9 +75,10 @@ type Client struct {
}
// NewClient creates a new consul client
-func NewClient(consulUrl string, appName string) *Client {
+func NewClient(consulUrl string, consulToken string, appName string) *Client {
client := Client{
consulUrl: consulUrl,
+ consulToken: consulToken,
appName: appName,
appConfig: Space{prefix: "apps/" + appName + "/config/", upcase: true},
sharedConfig: Space{prefix: "shared/config/", upcase: true},
@@ -144,6 +146,7 @@ func (c *Client) GetDataCenters() error {
// GetData loads a key/value space from consul
func (c *Client) GetData(space *Space, useIndex bool) error {
+ client := &http.Client{Timeout: time.Second * 60}
fullUrl := c.kvUrl(space.prefix) + "?recurse"
if useIndex {
fullUrl += "&index=" + strconv.Itoa(space.modifyIndex)
@@ -151,7 +154,14 @@ func (c *Client) GetData(space *Space, useIndex bool) error {
if Verbose {
log.Printf("GET %s\n", fullUrl)
}
- response, err := http.Get(fullUrl)
+ req, err := http.NewRequest(http.MethodGet, fullUrl, nil)
+ if err != nil {
+ return errors.Wrapf(err, "PUT %q failed", fullUrl)
+ }
+ if c.consulToken != "" {
+ req.Header.Set("X-Consul-Token", c.consulToken)
+ }
+ response, err := client.Do(req)
if err != nil {
return errors.Wrapf(err, "GET %q failed", fullUrl)
}
@@ -200,7 +210,7 @@ func (c *Client) GetState() (env Env, err error) {
// UpdateState stores a single key value pair in the state
func (c *Client) UpdateState(key string, value string) error {
- client := &http.Client{Timeout: time.Second * 5}
+ client := &http.Client{Timeout: time.Second * 60}
uri := c.kvUrl(c.state.prefix + key)
if Verbose {
log.Printf("PUT %s\n", uri)
@@ -211,6 +221,9 @@ func (c *Client) UpdateState(key string, value string) error {
if err != nil {
return errors.Wrapf(err, "PUT %q failed", uri)
}
+ if c.consulToken != "" {
+ req.Header.Set("X-Consul-Token", c.consulToken)
+ }
resp, err := client.Do(req)
if err != nil {
return errors.Wrapf(err, "PUT %q failed", uri)

MODIFY
go/consul/consul_test.go
go/consul/consul_test.go
@@ -13,8 +13,9 @@ import (
"time"
)
-var testUrl = ""
+var testUrl = "http://localhost:8500"
var testApp = "beetle"
+var testToken = "randomstring"
func init() {
log.SetFlags(0)
@@ -22,7 +23,6 @@ func init() {
if !Verbose {
log.SetOutput(ioutil.Discard)
}
- testUrl = "http://localhost:8500"
cmd := exec.Command("curl", "-X", "PUT", testUrl+"/v1/kv/datacenters", "-d", "ams1,ams2")
if err := cmd.Run(); err != nil {
fmt.Errorf("could not set up datacenters")
@@ -38,7 +38,7 @@ func init() {
}
func TestConnect(t *testing.T) {
- client := NewClient(testUrl, testApp)
+ client := NewClient(testUrl, testToken, testApp)
client.Initialize()
if !reflect.DeepEqual(client.dataCenters, []string{"ams1", "ams2"}) {
t.Errorf("could not retrieve datacenters: %v", client.dataCenters)
@@ -48,7 +48,7 @@ func TestConnect(t *testing.T) {
}
}
func TestState(t *testing.T) {
- client := NewClient(testUrl, testApp)
+ client := NewClient(testUrl, testToken, testApp)
client.Initialize()
value := strconv.Itoa(rand.Int())
if err := client.UpdateState("test", value); err != nil {
@@ -64,7 +64,7 @@ func TestState(t *testing.T) {
}
func TestWatching(t *testing.T) {
- client := NewClient(testUrl, testApp)
+ client := NewClient(testUrl, testToken, testApp)
client.Initialize()
channel, err := client.WatchConfig()
if err != nil {

MODIFY
go/version.go
go/version.go
@@ -6,7 +6,7 @@ import (
)
// BEETLE_VERSION is displayed in the web UI and can be checked using beetle --version.
-const BEETLE_VERSION = "3.0.0"
+const BEETLE_VERSION = "3.0.3"
// ReportVersionIfRequestedAndExit checks os.Args for the string --version,
// prints the version if found and then exits.

go beetle: increase consul request timeout