Skip to content

Commit ee07221

Browse files
author
Matthew Fisher
committed
Merge pull request #3156 from glogiotatidis/3115
feat(publisher): Check if app port is open before publishing it. Fixes 3115
2 parents 1f60fcf + 3285e21 commit ee07221

2 files changed

Lines changed: 32 additions & 2 deletions

File tree

publisher/server/publisher.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66
"log"
7+
"net"
78
"os"
89
"regexp"
910
"strconv"
@@ -91,8 +92,9 @@ func (s *Server) publishContainer(container *docker.APIContainers, ttl time.Dura
9192
keyPath := fmt.Sprintf("/deis/services/%s/%s", appName, containerName)
9293
for _, p := range container.Ports {
9394
port := strconv.Itoa(int(p.PublicPort))
94-
if s.IsPublishableApp(containerName) {
95-
s.setEtcd(keyPath, host+":"+port, uint64(ttl.Seconds()))
95+
hostAndPort := host + ":" + port
96+
if s.IsPublishableApp(containerName) && s.IsPortOpen(hostAndPort) {
97+
s.setEtcd(keyPath, hostAndPort, uint64(ttl.Seconds()))
9698
}
9799
// TODO: support multiple exposed ports
98100
break
@@ -113,12 +115,23 @@ func (s *Server) IsPublishableApp(name string) bool {
113115
log.Println(err)
114116
return false
115117
}
118+
116119
if version >= latestRunningVersion(s.EtcdClient, appName) {
117120
return true
118121
}
119122
return false
120123
}
121124

125+
func (s *Server) IsPortOpen(hostAndPort string) bool {
126+
portOpen := false
127+
conn, err := net.Dial("tcp", hostAndPort)
128+
if err == nil {
129+
portOpen = true
130+
defer conn.Close()
131+
}
132+
return portOpen
133+
}
134+
122135
// latestRunningVersion retrieves the highest version of the application published
123136
// to etcd. If no app has been published, returns 0.
124137
func latestRunningVersion(client *etcd.Client, appName string) int {

publisher/server/publisher_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package server
22

33
import (
4+
"net"
45
"testing"
56
)
67

@@ -28,3 +29,19 @@ func TestIsPublishableApp(t *testing.T) {
2829
t.Errorf("%s should be publishable", futureVersion)
2930
}
3031
}
32+
33+
func TestIsPortOpen(t *testing.T) {
34+
ln, err := net.Listen("tcp4", "127.0.0.1:0")
35+
if err != nil {
36+
t.Fatalf("Listen failed: %v", err)
37+
}
38+
defer ln.Close()
39+
40+
s := &Server{nil, nil}
41+
if !s.IsPortOpen(ln.Addr().String()) {
42+
t.Errorf("Port should be open")
43+
}
44+
if s.IsPortOpen("127.0.0.1:-1") {
45+
t.Errorf("Port should be closed")
46+
}
47+
}

0 commit comments

Comments
 (0)