Skip to content

Commit 09384a3

Browse files
Merge pull request #16 from dcorbett-haproxy/20200407-add-tcpmode
MEDIUM: Add support for "mode tcp"
2 parents 210ea5c + 5fd6cd3 commit 09384a3

4 files changed

Lines changed: 38 additions & 7 deletions

File tree

consul/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type Upstream struct {
1818
Service string
1919
LocalBindAddress string
2020
LocalBindPort int
21+
Protocol string
2122

2223
TLS
2324

@@ -47,6 +48,7 @@ func (n UpstreamNode) Equal(o UpstreamNode) bool {
4748
type Downstream struct {
4849
LocalBindAddress string
4950
LocalBindPort int
51+
Protocol string
5052
TargetAddress string
5153
TargetPort int
5254

consul/watcher.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type upstream struct {
2121
LocalBindPort int
2222
Service string
2323
Datacenter string
24+
Protocol string
2425
Nodes []*api.ServiceEntry
2526

2627
done bool
@@ -29,6 +30,7 @@ type upstream struct {
2930
type downstream struct {
3031
LocalBindAddress string
3132
LocalBindPort int
33+
Protocol string
3234
TargetAddress string
3335
TargetPort int
3436
}
@@ -111,6 +113,13 @@ func (w *Watcher) handleProxyChange(first bool, srv *api.AgentService) {
111113
w.downstream.LocalBindAddress = defaultDownstreamBindAddr
112114
w.downstream.LocalBindPort = srv.Port
113115
w.downstream.TargetAddress = defaultUpstreamBindAddr
116+
117+
if srv.Proxy != nil && srv.Proxy.Config != nil {
118+
if c, ok := srv.Proxy.Config["protocol"].(string); ok {
119+
w.downstream.Protocol = c
120+
}
121+
}
122+
114123
if srv.Connect != nil && srv.Connect.SidecarService != nil && srv.Connect.SidecarService.Proxy != nil && srv.Connect.SidecarService.Proxy.Config != nil {
115124
if b, ok := srv.Connect.SidecarService.Proxy.Config["bind_address"].(string); ok {
116125
w.downstream.LocalBindAddress = b
@@ -155,6 +164,12 @@ func (w *Watcher) startUpstream(up api.Upstream) {
155164
Datacenter: up.Datacenter,
156165
}
157166

167+
if up.Config["protocol"] != nil {
168+
if p, ok := up.Config["protocol"].(string); ok {
169+
u.Protocol = p
170+
}
171+
}
172+
158173
w.lock.Lock()
159174
w.upstreams[up.DestinationName] = u
160175
w.lock.Unlock()
@@ -332,7 +347,7 @@ func (w *Watcher) genCfg() Config {
332347
LocalBindPort: w.downstream.LocalBindPort,
333348
TargetAddress: w.downstream.TargetAddress,
334349
TargetPort: w.downstream.TargetPort,
335-
350+
Protocol: w.downstream.Protocol,
336351
TLS: TLS{
337352
CAs: w.certCAs,
338353
Cert: w.leaf.Cert,
@@ -346,14 +361,13 @@ func (w *Watcher) genCfg() Config {
346361
Service: up.Service,
347362
LocalBindAddress: up.LocalBindAddress,
348363
LocalBindPort: up.LocalBindPort,
349-
364+
Protocol: up.Protocol,
350365
TLS: TLS{
351366
CAs: w.certCAs,
352367
Cert: w.leaf.Cert,
353368
Key: w.leaf.Key,
354369
},
355370
}
356-
357371
for _, s := range up.Nodes {
358372
serviceInstancesTotal++
359373
host := s.Service.Address

haproxy/state/downstream.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,26 @@ import (
1010
func generateDownstream(opts Options, certStore CertificateStore, cfg consul.Downstream, state State) (State, error) {
1111
feName := "front_downstream"
1212
beName := "back_downstream"
13+
feMode := models.FrontendModeHTTP
14+
beMode := models.BackendModeHTTP
1315

1416
caPath, crtPath, err := certStore.CertsPath(cfg.TLS)
1517
if err != nil {
1618
return state, err
1719
}
1820

21+
if cfg.Protocol != "" && cfg.Protocol == "tcp" {
22+
feMode = models.FrontendModeTCP
23+
beMode = models.BackendModeTCP
24+
}
25+
1926
// Main config
2027
fe := Frontend{
2128
Frontend: models.Frontend{
2229
Name: feName,
2330
DefaultBackend: beName,
2431
ClientTimeout: &clientTimeout,
25-
Mode: models.FrontendModeHTTP,
32+
Mode: feMode,
2633
Httplog: opts.LogRequests,
2734
},
2835
Bind: models.Bind{
@@ -73,7 +80,7 @@ func generateDownstream(opts Options, certStore CertificateStore, cfg consul.Dow
7380
Name: beName,
7481
ServerTimeout: &serverTimeout,
7582
ConnectTimeout: &connectTimeout,
76-
Mode: models.BackendModeHTTP,
83+
Mode: beMode,
7784
},
7885
Servers: []models.Server{
7986
models.Server{

haproxy/state/upstream.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,22 @@ import (
1010
func generateUpstream(opts Options, certStore CertificateStore, cfg consul.Upstream, oldState, newState State) (State, error) {
1111
feName := fmt.Sprintf("front_%s", cfg.Service)
1212
beName := fmt.Sprintf("back_%s", cfg.Service)
13+
feMode := models.FrontendModeHTTP
14+
beMode := models.BackendModeHTTP
1315

1416
fePort64 := int64(cfg.LocalBindPort)
17+
18+
if cfg.Protocol != "" && cfg.Protocol == "tcp" {
19+
feMode = models.FrontendModeTCP
20+
beMode = models.BackendModeTCP
21+
}
22+
1523
fe := Frontend{
1624
Frontend: models.Frontend{
1725
Name: feName,
1826
DefaultBackend: beName,
1927
ClientTimeout: &clientTimeout,
20-
Mode: models.FrontendModeHTTP,
28+
Mode: feMode,
2129
Httplog: opts.LogRequests,
2230
},
2331
Bind: models.Bind{
@@ -45,7 +53,7 @@ func generateUpstream(opts Options, certStore CertificateStore, cfg consul.Upstr
4553
Balance: &models.Balance{
4654
Algorithm: models.BalanceAlgorithmLeastconn,
4755
},
48-
Mode: models.BackendModeHTTP,
56+
Mode: beMode,
4957
},
5058
}
5159
if opts.LogRequests && opts.LogSocket != "" {

0 commit comments

Comments
 (0)