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

Commit

Permalink
Merge PR haproxytech#51 and haproxytech#59 to the upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
Greg Dubicki committed Jun 17, 2020
2 parents 6e152ef + 79440ec commit 52cc2ec
Show file tree
Hide file tree
Showing 20 changed files with 169 additions and 101 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

...using HAproxy configuration template as a base.

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.
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.
9 changes: 5 additions & 4 deletions consul/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ func (n Upstream) Equal(o Upstream) bool {
}

type UpstreamNode struct {
Host string
Port int
Weight int
Name string
Address string
Port int
Weight int
}

func (n UpstreamNode) ID() string {
return fmt.Sprintf("%s:%d", n.Host, n.Port)
return fmt.Sprintf("%s:%d", n.Address, n.Port)
}

func (n UpstreamNode) Equal(o UpstreamNode) bool {
Expand Down
21 changes: 15 additions & 6 deletions consul/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/x509"
"fmt"
"reflect"
"sort"
"sync"
"time"

Expand Down Expand Up @@ -454,9 +455,12 @@ func (w *Watcher) genCfg() Config {
}
for _, s := range up.Nodes {
serviceInstancesTotal++
host := s.Service.Address
if host == "" {
host = s.Node.Address

name := s.Node.Node

address := s.Service.Address
if address == "" {
address = s.Node.Address
}

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

upstream.Nodes = append(upstream.Nodes, UpstreamNode{
Host: host,
Port: s.Service.Port,
Weight: weight,
Name: name,
Address: address,
Port: s.Service.Port,
Weight: weight,
})
}

sort.Slice(upstream.Nodes, func(i, j int) bool {
return upstream.Nodes[i].Name < upstream.Nodes[j].Name
})

config.Upstreams = append(config.Upstreams, upstream)
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/haproxytech/haproxy-consul-connect
go 1.13

require (
github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a // indirect
github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a
github.com/criteo/haproxy-spoe-go v0.0.0-20190925130734-97891c13d324
github.com/d4l3k/messagediff v1.2.1 // indirect
github.com/facebookgo/freeport v0.0.0-20150612182905-d4adf43b75b9
Expand Down
3 changes: 1 addition & 2 deletions haproxy/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ global
tune.ssl.default-dh-param 1024
nbproc 1
nbthread {{.NbThread}}
log-tag haproxy_sidecar
userlist controller
user {{.DataplaneUser}} insecure-password {{.DataplanePass}}
Expand Down Expand Up @@ -57,7 +58,6 @@ type baseParams struct {
SocketPath string
DataplaneUser string
DataplanePass string
LogsPath string
}

type haConfig struct {
Expand Down Expand Up @@ -113,7 +113,6 @@ func newHaConfig(baseDir string, sd *lib.Shutdown) (*haConfig, error) {
err = tmpl.Execute(cfgFile, baseParams{
NbThread: runtime.GOMAXPROCS(0),
SocketPath: cfg.StatsSock,
LogsPath: cfg.LogsSock,
DataplaneUser: dataplaneUser,
DataplanePass: dataplanePass,
})
Expand Down
8 changes: 4 additions & 4 deletions haproxy/dataplane/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ func (t *tnx) CreateServer(beName string, srv models.Server) error {
return t.client.makeReq(http.MethodPost, fmt.Sprintf("/v1/services/haproxy/configuration/servers?backend=%s&transaction_id=%s", beName, t.txID), srv, nil)
}

func (t *tnx) ReplaceServer(beName string, srv models.Server) error {
func (t *tnx) ReplaceServer(beName string, oldSrvName string, newSrv models.Server) error {
t.After(func() error {
return t.client.ReplaceServer(beName, srv)
return t.client.ReplaceServer(beName, oldSrvName, newSrv)
})
return nil
}

func (c *Dataplane) ReplaceServer(beName string, srv models.Server) error {
err := c.makeReq(http.MethodPut, fmt.Sprintf("/v1/services/haproxy/configuration/servers/%s?backend=%s&version=%d", srv.Name, beName, c.version), srv, nil)
func (c *Dataplane) ReplaceServer(beName string, oldSrvName string, newSrv models.Server) error {
err := c.makeReq(http.MethodPut, fmt.Sprintf("/v1/services/haproxy/configuration/servers/%s?backend=%s&version=%d", oldSrvName, beName, c.version), newSrv, nil)
if err != nil {
return err
}
Expand Down
7 changes: 6 additions & 1 deletion haproxy/haproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,13 @@ func (h *HAProxy) start(sd *lib.Shutdown) error {
}
h.haConfig = hc

if h.opts.LogRequests {
if h.opts.HAProxyLogAddress == "" {
err := h.startLogger()
if err != nil {
return err
}
} else {
log.Infof("not starting built-in syslog - using %s as syslog address", h.opts.HAProxyLogAddress)
}

if h.opts.EnableIntentions {
Expand All @@ -77,6 +79,7 @@ func (h *HAProxy) start(sd *lib.Shutdown) error {
dpc, err := haproxy_cmd.Start(sd, haproxy_cmd.Config{
HAProxyPath: h.opts.HAProxyBin,
HAProxyConfigPath: hc.HAProxy,
HAProxyLogWithThisApp: h.opts.HAProxyLogAddress == "",
DataplanePath: h.opts.DataplaneBin,
DataplaneTransactionDir: hc.DataplaneTransactionDir,
DataplaneSock: hc.DataplaneSock,
Expand Down Expand Up @@ -112,6 +115,8 @@ func (h *HAProxy) startLogger() error {
}
}(channel)

log.Infof("starting built-in syslog server at %s", h.haConfig.LogsSock)

return nil
}

Expand Down
6 changes: 4 additions & 2 deletions haproxy/haproxy_cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ import (
log "github.com/sirupsen/logrus"
)

func runCommand(sd *lib.Shutdown, cmdPath string, args ...string) (*exec.Cmd, error) {
func runCommand(sd *lib.Shutdown, cmdPath string, logPrefix string, logWithThisApp bool, args ...string) (*exec.Cmd, error) {
_, file := path.Split(cmdPath)
cmd := exec.Command(cmdPath, args...)
halog.Cmd("haproxy", cmd)
if logWithThisApp {
halog.Cmd(logPrefix, cmd)
}

sd.Add(1)
err := cmd.Start()
Expand Down
4 changes: 2 additions & 2 deletions haproxy/haproxy_cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import (
func Test_runCommand_ok(t *testing.T) {
t.Parallel()
sd := lib.NewShutdown()
cmd, err := runCommand(sd, "ls", ".")
cmd, err := runCommand(sd, "ls", "foobar", true, ".")
require.NoError(t, err)
cmd.Wait()
}

func Test_runCommand_nok_wrong_path(t *testing.T) {
t.Parallel()
sd := lib.NewShutdown()
cmd, err := runCommand(sd, "/path/to/nowhere/that/can/be/found/myExec", "--help")
cmd, err := runCommand(sd, "/path/to/nowhere/that/can/be/found/myExec", "foobar", true, "--help")
require.NotNil(t, err)
require.Contains(t, err.Error(), "no such file or directory")
require.Nil(t, cmd)
Expand Down
5 changes: 5 additions & 0 deletions haproxy/haproxy_cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const (
type Config struct {
HAProxyPath string
HAProxyConfigPath string
HAProxyLogWithThisApp bool
DataplanePath string
DataplaneTransactionDir string
DataplaneSock string
Expand All @@ -35,6 +36,8 @@ type Config struct {
func Start(sd *lib.Shutdown, cfg Config) (*dataplane.Dataplane, error) {
haCmd, err := runCommand(sd,
cfg.HAProxyPath,
"haproxy",
cfg.HAProxyLogWithThisApp,
"-f",
cfg.HAProxyConfigPath,
)
Expand All @@ -47,6 +50,8 @@ func Start(sd *lib.Shutdown, cfg Config) (*dataplane.Dataplane, error) {

cmd, err := runCommand(sd,
cfg.DataplanePath,
"dataplaneapi",
true,
"--scheme", "unix",
"--socket-path", cfg.DataplaneSock,
"--haproxy-bin", cfg.HAProxyPath,
Expand Down
3 changes: 2 additions & 1 deletion haproxy/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ type Options struct {
EnableIntentions bool
StatsListenAddr string
StatsRegisterService bool
LogRequests bool
HAProxyLogRequests bool
HAProxyLogAddress string
}
9 changes: 7 additions & 2 deletions haproxy/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,15 @@ func (h *HAProxy) watch(sd *lib.Shutdown) error {

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

logAddress := h.haConfig.LogsSock
if h.opts.HAProxyLogAddress != "" {
logAddress = h.opts.HAProxyLogAddress
}

newState, err := state.Generate(state.Options{
EnableIntentions: h.opts.EnableIntentions,
LogRequests: h.opts.LogRequests,
LogSocket: h.haConfig.LogsSock,
LogRequests: h.opts.HAProxyLogRequests,
LogAddress: logAddress,
SPOEConfigPath: h.haConfig.SPOE,
SPOESocket: h.haConfig.SPOESock,
}, h.haConfig, currentState, newConsulCfg)
Expand Down
2 changes: 1 addition & 1 deletion haproxy/state/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func applyBackends(ha HAProxy, old, new []Backend) error {
continue
}

err := ha.ReplaceServer(newBack.Backend.Name, s)
err := ha.ReplaceServer(newBack.Backend.Name, oldServers[i].Name, s)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 52cc2ec

Please sign in to comment.