Skip to content
This repository was archived by the owner on Jan 26, 2023. It is now read-only.

Commit 52cc2ec

Browse files
author
Greg Dubicki
committed
Merge PR haproxytech#51 and haproxytech#59 to the upstream
2 parents 6e152ef + 79440ec commit 52cc2ec

20 files changed

+169
-101
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22

33
...using HAproxy configuration template as a base.
44

5-
Please see the [original project README](https://github.com/haproxytech) for information about this project and [this comment](https://github.com/haproxytech/haproxy-consul-connect/issues/60#issuecomment-645318551) for information about this fork.
5+
Please see the [original project README](https://github.com/haproxytech) for information about this project
6+
and [this comment](https://github.com/haproxytech/haproxy-consul-connect/issues/60#issuecomment-645318551)
7+
for information about this fork.

consul/config.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,14 @@ func (n Upstream) Equal(o Upstream) bool {
3232
}
3333

3434
type UpstreamNode struct {
35-
Host string
36-
Port int
37-
Weight int
35+
Name string
36+
Address string
37+
Port int
38+
Weight int
3839
}
3940

4041
func (n UpstreamNode) ID() string {
41-
return fmt.Sprintf("%s:%d", n.Host, n.Port)
42+
return fmt.Sprintf("%s:%d", n.Address, n.Port)
4243
}
4344

4445
func (n UpstreamNode) Equal(o UpstreamNode) bool {

consul/watcher.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"crypto/x509"
55
"fmt"
66
"reflect"
7+
"sort"
78
"sync"
89
"time"
910

@@ -454,9 +455,12 @@ func (w *Watcher) genCfg() Config {
454455
}
455456
for _, s := range up.Nodes {
456457
serviceInstancesTotal++
457-
host := s.Service.Address
458-
if host == "" {
459-
host = s.Node.Address
458+
459+
name := s.Node.Node
460+
461+
address := s.Service.Address
462+
if address == "" {
463+
address = s.Node.Address
460464
}
461465

462466
weight := 1
@@ -474,12 +478,17 @@ func (w *Watcher) genCfg() Config {
474478
serviceInstancesAlive++
475479

476480
upstream.Nodes = append(upstream.Nodes, UpstreamNode{
477-
Host: host,
478-
Port: s.Service.Port,
479-
Weight: weight,
481+
Name: name,
482+
Address: address,
483+
Port: s.Service.Port,
484+
Weight: weight,
480485
})
481486
}
482487

488+
sort.Slice(upstream.Nodes, func(i, j int) bool {
489+
return upstream.Nodes[i].Name < upstream.Nodes[j].Name
490+
})
491+
483492
config.Upstreams = append(config.Upstreams, upstream)
484493
}
485494

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/haproxytech/haproxy-consul-connect
33
go 1.13
44

55
require (
6-
github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a // indirect
6+
github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a
77
github.com/criteo/haproxy-spoe-go v0.0.0-20190925130734-97891c13d324
88
github.com/d4l3k/messagediff v1.2.1 // indirect
99
github.com/facebookgo/freeport v0.0.0-20150612182905-d4adf43b75b9

haproxy/config.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ global
2828
tune.ssl.default-dh-param 1024
2929
nbproc 1
3030
nbthread {{.NbThread}}
31+
log-tag haproxy_sidecar
3132
3233
userlist controller
3334
user {{.DataplaneUser}} insecure-password {{.DataplanePass}}
@@ -57,7 +58,6 @@ type baseParams struct {
5758
SocketPath string
5859
DataplaneUser string
5960
DataplanePass string
60-
LogsPath string
6161
}
6262

6363
type haConfig struct {
@@ -113,7 +113,6 @@ func newHaConfig(baseDir string, sd *lib.Shutdown) (*haConfig, error) {
113113
err = tmpl.Execute(cfgFile, baseParams{
114114
NbThread: runtime.GOMAXPROCS(0),
115115
SocketPath: cfg.StatsSock,
116-
LogsPath: cfg.LogsSock,
117116
DataplaneUser: dataplaneUser,
118117
DataplanePass: dataplanePass,
119118
})

haproxy/dataplane/backend.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,15 @@ func (t *tnx) CreateServer(beName string, srv models.Server) error {
5858
return t.client.makeReq(http.MethodPost, fmt.Sprintf("/v1/services/haproxy/configuration/servers?backend=%s&transaction_id=%s", beName, t.txID), srv, nil)
5959
}
6060

61-
func (t *tnx) ReplaceServer(beName string, srv models.Server) error {
61+
func (t *tnx) ReplaceServer(beName string, oldSrvName string, newSrv models.Server) error {
6262
t.After(func() error {
63-
return t.client.ReplaceServer(beName, srv)
63+
return t.client.ReplaceServer(beName, oldSrvName, newSrv)
6464
})
6565
return nil
6666
}
6767

68-
func (c *Dataplane) ReplaceServer(beName string, srv models.Server) error {
69-
err := c.makeReq(http.MethodPut, fmt.Sprintf("/v1/services/haproxy/configuration/servers/%s?backend=%s&version=%d", srv.Name, beName, c.version), srv, nil)
68+
func (c *Dataplane) ReplaceServer(beName string, oldSrvName string, newSrv models.Server) error {
69+
err := c.makeReq(http.MethodPut, fmt.Sprintf("/v1/services/haproxy/configuration/servers/%s?backend=%s&version=%d", oldSrvName, beName, c.version), newSrv, nil)
7070
if err != nil {
7171
return err
7272
}

haproxy/haproxy.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,13 @@ func (h *HAProxy) start(sd *lib.Shutdown) error {
6060
}
6161
h.haConfig = hc
6262

63-
if h.opts.LogRequests {
63+
if h.opts.HAProxyLogAddress == "" {
6464
err := h.startLogger()
6565
if err != nil {
6666
return err
6767
}
68+
} else {
69+
log.Infof("not starting built-in syslog - using %s as syslog address", h.opts.HAProxyLogAddress)
6870
}
6971

7072
if h.opts.EnableIntentions {
@@ -77,6 +79,7 @@ func (h *HAProxy) start(sd *lib.Shutdown) error {
7779
dpc, err := haproxy_cmd.Start(sd, haproxy_cmd.Config{
7880
HAProxyPath: h.opts.HAProxyBin,
7981
HAProxyConfigPath: hc.HAProxy,
82+
HAProxyLogWithThisApp: h.opts.HAProxyLogAddress == "",
8083
DataplanePath: h.opts.DataplaneBin,
8184
DataplaneTransactionDir: hc.DataplaneTransactionDir,
8285
DataplaneSock: hc.DataplaneSock,
@@ -112,6 +115,8 @@ func (h *HAProxy) startLogger() error {
112115
}
113116
}(channel)
114117

118+
log.Infof("starting built-in syslog server at %s", h.haConfig.LogsSock)
119+
115120
return nil
116121
}
117122

haproxy/haproxy_cmd/cmd.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ import (
1313
log "github.com/sirupsen/logrus"
1414
)
1515

16-
func runCommand(sd *lib.Shutdown, cmdPath string, args ...string) (*exec.Cmd, error) {
16+
func runCommand(sd *lib.Shutdown, cmdPath string, logPrefix string, logWithThisApp bool, args ...string) (*exec.Cmd, error) {
1717
_, file := path.Split(cmdPath)
1818
cmd := exec.Command(cmdPath, args...)
19-
halog.Cmd("haproxy", cmd)
19+
if logWithThisApp {
20+
halog.Cmd(logPrefix, cmd)
21+
}
2022

2123
sd.Add(1)
2224
err := cmd.Start()

haproxy/haproxy_cmd/cmd_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ import (
1010
func Test_runCommand_ok(t *testing.T) {
1111
t.Parallel()
1212
sd := lib.NewShutdown()
13-
cmd, err := runCommand(sd, "ls", ".")
13+
cmd, err := runCommand(sd, "ls", "foobar", true, ".")
1414
require.NoError(t, err)
1515
cmd.Wait()
1616
}
1717

1818
func Test_runCommand_nok_wrong_path(t *testing.T) {
1919
t.Parallel()
2020
sd := lib.NewShutdown()
21-
cmd, err := runCommand(sd, "/path/to/nowhere/that/can/be/found/myExec", "--help")
21+
cmd, err := runCommand(sd, "/path/to/nowhere/that/can/be/found/myExec", "foobar", true, "--help")
2222
require.NotNil(t, err)
2323
require.Contains(t, err.Error(), "no such file or directory")
2424
require.Nil(t, cmd)

haproxy/haproxy_cmd/run.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const (
2525
type Config struct {
2626
HAProxyPath string
2727
HAProxyConfigPath string
28+
HAProxyLogWithThisApp bool
2829
DataplanePath string
2930
DataplaneTransactionDir string
3031
DataplaneSock string
@@ -35,6 +36,8 @@ type Config struct {
3536
func Start(sd *lib.Shutdown, cfg Config) (*dataplane.Dataplane, error) {
3637
haCmd, err := runCommand(sd,
3738
cfg.HAProxyPath,
39+
"haproxy",
40+
cfg.HAProxyLogWithThisApp,
3841
"-f",
3942
cfg.HAProxyConfigPath,
4043
)
@@ -47,6 +50,8 @@ func Start(sd *lib.Shutdown, cfg Config) (*dataplane.Dataplane, error) {
4750

4851
cmd, err := runCommand(sd,
4952
cfg.DataplanePath,
53+
"dataplaneapi",
54+
true,
5055
"--scheme", "unix",
5156
"--socket-path", cfg.DataplaneSock,
5257
"--haproxy-bin", cfg.HAProxyPath,

haproxy/options.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ type Options struct {
88
EnableIntentions bool
99
StatsListenAddr string
1010
StatsRegisterService bool
11-
LogRequests bool
11+
HAProxyLogRequests bool
12+
HAProxyLogAddress string
1213
}

haproxy/state.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,15 @@ func (h *HAProxy) watch(sd *lib.Shutdown) error {
9595

9696
newConsulCfg := nextState.Load().(consul.Config)
9797

98+
logAddress := h.haConfig.LogsSock
99+
if h.opts.HAProxyLogAddress != "" {
100+
logAddress = h.opts.HAProxyLogAddress
101+
}
102+
98103
newState, err := state.Generate(state.Options{
99104
EnableIntentions: h.opts.EnableIntentions,
100-
LogRequests: h.opts.LogRequests,
101-
LogSocket: h.haConfig.LogsSock,
105+
LogRequests: h.opts.HAProxyLogRequests,
106+
LogAddress: logAddress,
102107
SPOEConfigPath: h.haConfig.SPOE,
103108
SPOESocket: h.haConfig.SPOESock,
104109
}, h.haConfig, currentState, newConsulCfg)

haproxy/state/apply.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func applyBackends(ha HAProxy, old, new []Backend) error {
151151
continue
152152
}
153153

154-
err := ha.ReplaceServer(newBack.Backend.Name, s)
154+
err := ha.ReplaceServer(newBack.Backend.Name, oldServers[i].Name, s)
155155
if err != nil {
156156
return err
157157
}

0 commit comments

Comments
 (0)