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

refactor: Listener creation and fix tests #763

Merged
merged 4 commits into from
May 14, 2020
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
28 changes: 12 additions & 16 deletions dkron/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))
}

Expand Down
37 changes: 4 additions & 33 deletions dkron/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand All @@ -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)

Expand Down Expand Up @@ -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()
}

Expand Down
2 changes: 1 addition & 1 deletion dkron/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ services:
build: .
ports:
- "8946"
- "6868"
environment:
- GODEBUG=netdns=go
command: dkron agent --retry-join=dkron:8946 --log-level=debug
4 changes: 3 additions & 1 deletion scripts/ansible/site.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down