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

Deogierize consul #195

Closed
wants to merge 5 commits into from
Closed
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
11 changes: 4 additions & 7 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"encoding/json"
"flag"
"io/ioutil"
"os"
"strings"
Expand All @@ -14,7 +15,6 @@ import (
"github.com/allegro/marathon-consul/sentry"
"github.com/allegro/marathon-consul/sync"
"github.com/allegro/marathon-consul/web"
flag "github.com/ogier/pflag"
)

type Config struct {
Expand All @@ -35,9 +35,6 @@ type Config struct {
var config = &Config{}

func New() (*Config, error) {
if !flag.Parsed() {
config.parseFlags()
}
flag.Parse()
err := config.loadConfigFromFile()

Expand All @@ -58,7 +55,7 @@ func New() (*Config, error) {
return config, err
}

func (config *Config) parseFlags() {
func init() {
// Consul
flag.StringVar(&config.Consul.Port, "consul-port", "8500", "Consul port")
flag.BoolVar(&config.Consul.Auth.Enabled, "consul-auth", false, "Use Consul with authentication")
Expand All @@ -71,8 +68,8 @@ func (config *Config) parseFlags() {
flag.StringVar(&config.Consul.Token, "consul-token", "", "The Consul ACL token")
flag.StringVar(&config.Consul.Tag, "consul-tag", "marathon", "Common tag name added to every service registered in Consul, should be unique for every Marathon-cluster connected to Consul")
flag.DurationVar(&config.Consul.Timeout.Duration, "consul-timeout", 3*time.Second, "Time limit for requests made by the Consul HTTP client. A Timeout of zero means no timeout")
flag.Uint32Var(&config.Consul.AgentFailuresTolerance, "consul-max-agent-failures", 3, "Max number of consecutive request failures for agent before removal from cache")
flag.Uint32Var(&config.Consul.RequestRetries, "consul-get-services-retry", 3, "Number of retries on failure when performing requests to Consul. Each retry uses different cached agent")
flag.Uint64Var(&config.Consul.AgentFailuresTolerance, "consul-max-agent-failures", 3, "Max number of consecutive request failures for agent before removal from cache")
flag.Uint64Var(&config.Consul.RequestRetries, "consul-get-services-retry", 3, "Number of retries on failure when performing requests to Consul. Each retry uses different cached agent")
flag.StringVar(&config.Consul.ConsulNameSeparator, "consul-name-separator", ".", "Separator used to create default service name for Consul")
flag.StringVar(&config.Consul.IgnoredHealthChecks, "consul-ignored-healthchecks", "", "A comma separated blacklist of Marathon health check types that will not be migrated to Consul, e.g. command,tcp")

Expand Down
12 changes: 6 additions & 6 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestConfig_NewReturnsErrorWhenFileNotExist(t *testing.T) {
clear()

// given
os.Args = []string{"./marathon-consul", "--config-file=unknown.json"}
os.Args = []string{"./marathon-consul", "-config-file=unknown.json"}

// when
_, err := New()
Expand All @@ -33,7 +33,7 @@ func TestConfig_NewReturnsErrorWhenFileIsNotJson(t *testing.T) {
clear()

// given
os.Args = []string{"./marathon-consul", "--config-file=config.go"}
os.Args = []string{"./marathon-consul", "-config-file=config.go"}

// when
_, err := New()
Expand All @@ -46,7 +46,7 @@ func TestConfig_ShouldReturnErrorForBadLogLevel(t *testing.T) {
clear()

// given
os.Args = []string{"./marathon-consul", "--log-level=bad"}
os.Args = []string{"./marathon-consul", "-log-level=bad"}

// when
_, err := New()
Expand All @@ -59,7 +59,7 @@ func TestConfig_ShouldParseFlags(t *testing.T) {
clear()

// given
os.Args = []string{"./marathon-consul", "--log-level=debug", "--marathon-location=test.host:8080", "--log-format=json"}
os.Args = []string{"./marathon-consul", "-log-level=debug", "-marathon-location=test.host:8080", "-log-format=json"}

// when
actual, err := New()
Expand All @@ -75,7 +75,7 @@ func TestConfig_ShouldUseTextFormatterWhenFormatterIsUnknown(t *testing.T) {
clear()

// given
os.Args = []string{"./marathon-consul", "--log-level=debug", "--log-format=unknown"}
os.Args = []string{"./marathon-consul", "-log-level=debug", "-log-format=unknown"}

// when
_, err := New()
Expand Down Expand Up @@ -142,7 +142,7 @@ func TestConfig_ShouldBeMergedWithFileDefaultsAndFlags(t *testing.T) {
configFile: "../debian/config.json",
}

os.Args = []string{"./marathon-consul", "--log-level=debug", "--config-file=../debian/config.json", "--marathon-location=localhost:8080"}
os.Args = []string{"./marathon-consul", "-log-level=debug", "-config-file=../debian/config.json", "-marathon-location=localhost:8080"}
actual, err := New()

assert.NoError(t, err)
Expand Down
8 changes: 4 additions & 4 deletions consul/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ import (
type Agent struct {
Client *consulapi.Client
IP string
failures uint32
failures uint64
}

func (a *Agent) IncFailures() uint32 {
return atomic.AddUint32(&a.failures, 1)
func (a *Agent) IncFailures() uint64 {
return atomic.AddUint64(&a.failures, 1)
}

func (a *Agent) ClearFailures() {
atomic.StoreUint32(&a.failures, 0)
atomic.StoreUint64(&a.failures, 0)
}

func (a *ConcurrentAgents) createAgent(ipAddress string) (*Agent, error) {
Expand Down
4 changes: 2 additions & 2 deletions consul/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ type Config struct {
Token string
Tag string
Timeout time.Interval
RequestRetries uint32
AgentFailuresTolerance uint32
RequestRetries uint64
AgentFailuresTolerance uint64
ConsulNameSeparator string
IgnoredHealthChecks string
}
Expand Down
2 changes: 1 addition & 1 deletion consul/consul.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (c *Consul) GetServices(name string) ([]*service.Service, error) {
}

func (c *Consul) getServicesUsingProviderWithRetriesOnAgentFailure(provide ServicesProvider) ([]*service.Service, error) {
for retry := uint32(0); retry <= c.config.RequestRetries; retry++ {
for retry := uint64(0); retry <= c.config.RequestRetries; retry++ {
agent, err := c.agents.GetAnyAgent()
if err != nil {
return nil, err
Expand Down
6 changes: 3 additions & 3 deletions consul/consul_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func TestGetServices_RemovingFailingAgentsAndRetrying(t *testing.T) {
server1.AddService("serviceB", "passing", []string{"marathon"})

// add failing clients
for i := uint32(2); i < consul.config.RequestRetries; i++ {
for i := uint64(2); i < consul.config.RequestRetries; i++ {
consul.AddAgent(fmt.Sprintf("127.0.0.%d", i))
}

Expand Down Expand Up @@ -251,7 +251,7 @@ func TestGetServicesUsingProviderWithRetriesOnAgentFailure_ShouldRetryConfigured
consul.config.AgentFailuresTolerance = 2
consul.config.RequestRetries = 4

var called uint32
var called uint64
provider := func(agent *consulapi.Client) ([]*service.Service, error) {
called++
return nil, fmt.Errorf("error")
Expand Down Expand Up @@ -301,7 +301,7 @@ func TestGetAllServices_RemovingFailingAgentsAndRetrying(t *testing.T) {
consul.config.RequestRetries = 100

// add failing clients
for i := uint32(2); i < consul.config.RequestRetries; i++ {
for i := uint64(2); i < consul.config.RequestRetries; i++ {
consul.AddAgent(fmt.Sprintf("127.0.0.%d", i))
}

Expand Down
2 changes: 1 addition & 1 deletion debian/marathon-consul.service
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Requires=network-online.target
After=network-online.target

[Service]
ExecStart=/usr/bin/marathon-consul --config-file=/etc/marathon-consul.d/config.json
ExecStart=/usr/bin/marathon-consul -config-file=/etc/marathon-consul.d/config.json
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
KillSignal=SIGINT
Expand Down
2 changes: 1 addition & 1 deletion debian/marathon-consul.upstart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ respawn

script
echo $$ > /var/run/marathon-consul.pid
exec marathon-consul --config-file=/etc/marathon-consul.d/config.json
exec marathon-consul -config-file=/etc/marathon-consul.d/config.json
end script

post-stop script
Expand Down
2 changes: 0 additions & 2 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import:
subpackages:
- api
- testutil
- package: github.com/ogier/pflag
version: 0.0.1
- package: github.com/rcrowley/go-metrics
version: bdb33529eca3e55eac7328e07c57012a797af602
- package: github.com/stretchr/testify
Expand Down
2 changes: 1 addition & 1 deletion sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,4 @@ func marathonTaskIdsSet(marathonApps []*apps.App) map[apps.TaskID]struct{} {
}
}
return tasksSet
}
}
2 changes: 1 addition & 1 deletion sync/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ func TestSync_WithDeregisteringFallback(t *testing.T) {
consulStub.Register(&task, marathonApp)
}
marathon.TasksStub = map[apps.AppID][]apps.Task{
apps.AppID("/test/app"): []apps.Task{marathonApp.Tasks[0]},
apps.AppID("/test/app"): {marathonApp.Tasks[0]},
}
sync := newSyncWithDefaultConfig(marathon, consulStub)

Expand Down
1 change: 0 additions & 1 deletion vendor/github.com/ogier/pflag/.travis.yml

This file was deleted.

28 changes: 0 additions & 28 deletions vendor/github.com/ogier/pflag/LICENSE

This file was deleted.

Loading