From ea7a31dfa4fe8eee15728cfc00e3e41223dd7122 Mon Sep 17 00:00:00 2001 From: Victor Castell Date: Wed, 13 May 2020 23:47:49 +0200 Subject: [PATCH 1/4] refactor: Listener creation and fix tests --- dkron/agent.go | 28 ++++++++++++---------------- dkron/agent_test.go | 37 ++++--------------------------------- 2 files changed, 16 insertions(+), 49 deletions(-) diff --git a/dkron/agent.go b/dkron/agent.go index adfeceb68..df1bd3995 100644 --- a/dkron/agent.go +++ b/dkron/agent.go @@ -108,6 +108,8 @@ type Agent struct { peerLock sync.RWMutex activeExecutions sync.Map + + listener net.Listener } // ProcessorFactory is a function type that creates a new instance @@ -171,16 +173,17 @@ func (a *Agent) Start() error { a.config.AdvertiseRPCPort = a.config.RPCPort } + // Create a listener for RPC subsystem + addr := a.bindRPCAddr() + l, err := net.Listen("tcp", addr) + if err != nil { + log.Fatal(err) + } + a.listener = l + if a.config.Server { a.StartServer() } else { - // Create a listener at the desired port. - addr := a.bindRPCAddr() - l, err := net.Listen("tcp", addr) - if err != nil { - log.Fatal(err) - } - opts := []grpc.ServerOption{} if a.TLSConfig != nil { tc := credentials.NewTLS(a.TLSConfig) @@ -495,15 +498,8 @@ func (a *Agent) StartServer() { } a.HTTPTransport.ServeHTTP() - // Create a listener at the desired port. - addr := a.bindRPCAddr() - l, err := net.Listen("tcp", addr) - if err != nil { - log.Fatal(err) - } - // Create a cmux object. - tcpm := cmux.New(l) + tcpm := cmux.New(a.listener) var grpcl, raftl net.Listener // If TLS config present listen to TLS @@ -779,7 +775,7 @@ func (a *Agent) advertiseRPCAddr() string { // Get bind address for RPC func (a *Agent) bindRPCAddr() string { - bindIP, _, _ := net.SplitHostPort(a.config.BindAddr) + bindIP, _, _ := a.config.AddrParts(a.config.BindAddr) return net.JoinHostPort(bindIP, strconv.Itoa(a.config.RPCPort)) } diff --git a/dkron/agent_test.go b/dkron/agent_test.go index cd07fa407..3c4282393 100644 --- a/dkron/agent_test.go +++ b/dkron/agent_test.go @@ -265,6 +265,7 @@ func Test_advertiseRPCAddr(t *testing.T) { c := DefaultConfig() c.BindAddr = a1Addr + ":5000" + c.AdvertiseAddr = "8.8.8.8" c.NodeName = "test1" c.Server = true c.Tags = map[string]string{"role": "test"} @@ -278,37 +279,7 @@ func Test_advertiseRPCAddr(t *testing.T) { time.Sleep(2 * time.Second) advertiseRPCAddr := a.advertiseRPCAddr() - exRPCAddr := a1Addr + ":6868" - - assert.Equal(t, exRPCAddr, advertiseRPCAddr) - - a.Stop() - - ip2, returnFn2 := testutil.TakeIP() - defer returnFn2() - a2Addr := ip2.String() - - c = DefaultConfig() - c.BindAddr = a2Addr + ":5000" - c.AdvertiseAddr = "8.8.8.8" - c.NodeName = "test1" - c.Server = true - c.Tags = map[string]string{"role": "test"} - c.LogLevel = logLevel - c.DevMode = true - c.DataDir = dir - - a = NewAgent(c) - a.Start() - - time.Sleep(2 * time.Second) - - advertiseRPCAddr = a.advertiseRPCAddr() - exRPCAddr = "8.8.8.8:6868" - - assert.Equal(t, exRPCAddr, advertiseRPCAddr) - - c.AdvertiseAddr = "8.8.8.8" + exRPCAddr := "8.8.8.8:6868" assert.Equal(t, exRPCAddr, advertiseRPCAddr) @@ -338,10 +309,10 @@ func Test_bindRPCAddr(t *testing.T) { time.Sleep(2 * time.Second) - getRPCAddr := a.bindRPCAddr() + bindRPCAddr := a.bindRPCAddr() exRPCAddr := a1Addr + ":6868" - assert.Equal(t, exRPCAddr, getRPCAddr) + assert.Equal(t, exRPCAddr, bindRPCAddr) a.Stop() } From 5f0fcbf788b4b86c133502f1751e6513f5ffdb7c Mon Sep 17 00:00:00 2001 From: Victor Castell Date: Fri, 15 May 2020 00:38:09 +0200 Subject: [PATCH 2/4] refactor: Set default bind IP to the first private address --- dkron/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dkron/config.go b/dkron/config.go index e80bfcbfc..2927976e3 100644 --- a/dkron/config.go +++ b/dkron/config.go @@ -180,7 +180,7 @@ func DefaultConfig() *Config { return &Config{ NodeName: hostname, - BindAddr: fmt.Sprintf("0.0.0.0:%d", DefaultBindPort), + BindAddr: fmt.Sprintf("{{ GetPrivateIP }}:%d", DefaultBindPort), HTTPAddr: ":8080", Profile: "lan", LogLevel: "info", From b8f7174760341dea0d2f9fd56e772685790a1853 Mon Sep 17 00:00:00 2001 From: Victor Castell Date: Fri, 15 May 2020 00:38:36 +0200 Subject: [PATCH 3/4] fix: compose --- docker-compose.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/docker-compose.yml b/docker-compose.yml index f13e28687..851dc4149 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -23,6 +23,7 @@ services: build: . ports: - "8946" + - "6868" environment: - GODEBUG=netdns=go command: dkron agent --retry-join=dkron:8946 --log-level=debug From a9ea22a075fd41d512e5046e1c5636e016d4240f Mon Sep 17 00:00:00 2001 From: Victor Castell Date: Fri, 15 May 2020 00:38:44 +0200 Subject: [PATCH 4/4] fix: provision --- scripts/ansible/site.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/scripts/ansible/site.yml b/scripts/ansible/site.yml index 7506e7dff..762a916a7 100644 --- a/scripts/ansible/site.yml +++ b/scripts/ansible/site.yml @@ -36,7 +36,8 @@ tasks: - set_fact: - dkron_servers: "{{ groups['packet'] }}" + dkron_servers: "{{ groups['packet'] | map('extract', hostvars, ['ansible_all_ipv4_addresses', 1]) | list }}" + tags: config - name: Copy package copy: @@ -59,6 +60,7 @@ template: src={{ item }}.yml.j2 dest=/etc/dkron/{{ item }}.yml loop: - dkron + tags: config - name: ensure service is running and enabled systemd: