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

Keep original server names in HAproxy backends #59

Closed
Closed
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
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
16 changes: 10 additions & 6 deletions consul/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,9 +454,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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

name would not be unique: the proxy might try to target several instances of the same service on 1 single node


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

weight := 1
Expand All @@ -474,9 +477,10 @@ 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,
})
}

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
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
40 changes: 20 additions & 20 deletions haproxy/state/apply_backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestNoChangeBackend(t *testing.T) {
},
Servers: []models.Server{
models.Server{
Name: "srv_0",
Name: "some-server",
Address: "1.2.3.4",
Port: int64p(8080),
Maintenance: models.ServerMaintenanceDisabled,
Expand All @@ -82,7 +82,7 @@ func TestNoChangeBackend(t *testing.T) {
},
Servers: []models.Server{
models.Server{
Name: "srv_0",
Name: "some-server",
Address: "1.2.3.4",
Port: int64p(8080),
Maintenance: models.ServerMaintenanceDisabled,
Expand Down Expand Up @@ -138,7 +138,7 @@ func TestAddServerDifferentSize(t *testing.T) {
},
Servers: []models.Server{
models.Server{
Name: "srv_0",
Name: "some-server",
Address: "1.2.3.4",
Port: int64p(8080),
Maintenance: models.ServerMaintenanceDisabled,
Expand All @@ -156,7 +156,7 @@ func TestAddServerDifferentSize(t *testing.T) {
ha.RequireOps(t,
RequireOp(haOpDeleteBackend, "back"),
RequireOp(haOpCreateBackend, "back"),
RequireOp(haOpCreateServer, "srv_0"),
RequireOp(haOpCreateServer, "some-server"),
)
}

Expand All @@ -169,13 +169,13 @@ func TestAddServerSameSize(t *testing.T) {
},
Servers: []models.Server{
models.Server{
Name: "srv_0",
Name: "some-server",
Address: "1.2.3.4",
Port: int64p(8080),
Maintenance: models.ServerMaintenanceDisabled,
},
models.Server{
Name: "srv_1",
Name: "disabled_server_0",
Address: "127.0.0.1",
Port: int64p(1),
Maintenance: models.ServerMaintenanceEnabled,
Expand All @@ -192,13 +192,13 @@ func TestAddServerSameSize(t *testing.T) {
},
Servers: []models.Server{
models.Server{
Name: "srv_0",
Name: "some-server",
Address: "1.2.3.4",
Port: int64p(8080),
Maintenance: models.ServerMaintenanceDisabled,
},
models.Server{
Name: "srv_1",
Name: "different-server",
Address: "1.2.3.5",
Port: int64p(8081),
Maintenance: models.ServerMaintenanceDisabled,
Expand All @@ -214,7 +214,7 @@ func TestAddServerSameSize(t *testing.T) {
require.Nil(t, err)

ha.RequireOps(t,
RequireOp(haOpReplaceServer, "srv_1"),
RequireOp(haOpReplaceServer, "disabled_server_0"),
)
}

Expand All @@ -227,13 +227,13 @@ func TestRemoveServerSameSize(t *testing.T) {
},
Servers: []models.Server{
models.Server{
Name: "srv_0",
Name: "some-server",
Address: "1.2.3.4",
Port: int64p(8080),
Maintenance: models.ServerMaintenanceDisabled,
},
models.Server{
Name: "srv_1",
Name: "different-server",
Address: "1.2.3.5",
Port: int64p(8081),
Maintenance: models.ServerMaintenanceDisabled,
Expand All @@ -250,13 +250,13 @@ func TestRemoveServerSameSize(t *testing.T) {
},
Servers: []models.Server{
models.Server{
Name: "srv_0",
Name: "disabled_server_0",
Address: "127.0.0.1",
Port: int64p(1),
Maintenance: models.ServerMaintenanceEnabled,
},
models.Server{
Name: "srv_1",
Name: "different-server",
Address: "1.2.3.5",
Port: int64p(8081),
Maintenance: models.ServerMaintenanceDisabled,
Expand All @@ -272,7 +272,7 @@ func TestRemoveServerSameSize(t *testing.T) {
require.Nil(t, err)

ha.RequireOps(t,
RequireOp(haOpReplaceServer, "srv_0"),
RequireOp(haOpReplaceServer, "some-server"),
)
}

Expand All @@ -285,7 +285,7 @@ func TestDifferentCerts(t *testing.T) {
},
Servers: []models.Server{
models.Server{
Name: "srv_0",
Name: "some-server",
Address: "1.2.3.4",
Port: int64p(8080),
Maintenance: models.ServerMaintenanceDisabled,
Expand All @@ -304,7 +304,7 @@ func TestDifferentCerts(t *testing.T) {
},
Servers: []models.Server{
models.Server{
Name: "srv_0",
Name: "some-server",
Address: "1.2.3.4",
Port: int64p(8080),
Maintenance: models.ServerMaintenanceDisabled,
Expand All @@ -324,7 +324,7 @@ func TestDifferentCerts(t *testing.T) {
ha.RequireOps(t,
RequireOp(haOpDeleteBackend, "back"),
RequireOp(haOpCreateBackend, "back"),
RequireOp(haOpCreateServer, "srv_0"),
RequireOp(haOpCreateServer, "some-server"),
)
}

Expand All @@ -337,7 +337,7 @@ func TestBackendChange(t *testing.T) {
},
Servers: []models.Server{
models.Server{
Name: "srv_0",
Name: "some-server",
Address: "1.2.3.4",
Port: int64p(8080),
Maintenance: models.ServerMaintenanceDisabled,
Expand All @@ -355,7 +355,7 @@ func TestBackendChange(t *testing.T) {
},
Servers: []models.Server{
models.Server{
Name: "srv_0",
Name: "some-server",
Address: "1.2.3.4",
Port: int64p(8080),
Maintenance: models.ServerMaintenanceDisabled,
Expand All @@ -373,6 +373,6 @@ func TestBackendChange(t *testing.T) {
ha.RequireOps(t,
RequireOp(haOpDeleteBackend, "back"),
RequireOp(haOpCreateBackend, "back"),
RequireOp(haOpCreateServer, "srv_0"),
RequireOp(haOpCreateServer, "some-server"),
)
}
4 changes: 2 additions & 2 deletions haproxy/state/fake_ha_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ func (h *fakeHA) CreateServer(beName string, srv models.Server) error {
return nil
}

func (h *fakeHA) ReplaceServer(beName string, srv models.Server) error {
func (h *fakeHA) ReplaceServer(beName string, oldSrvName string, newSrv models.Server) error {
h.ops = append(h.ops, fakeHAOp{
Type: haOpReplaceServer,
Name: srv.Name,
Name: oldSrvName,
})
return nil
}
Expand Down
70 changes: 38 additions & 32 deletions haproxy/state/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@ func GetTestConsulConfig() consul.Config {
LocalBindPort: 10000,
Nodes: []consul.UpstreamNode{
consul.UpstreamNode{
Host: "1.2.3.4",
Port: 8080,
Weight: 5,
Name: "some-server",
Address: "1.2.3.4",
Port: 8080,
Weight: 5,
},
consul.UpstreamNode{
Host: "1.2.3.5",
Port: 8081,
Weight: 8,
Name: "different-server",
Address: "1.2.3.5",
Port: 8081,
Weight: 8,
},
},
},
Expand Down Expand Up @@ -155,7 +157,7 @@ func GetTestHAConfig(baseCfg string) State {
},
Servers: []models.Server{
models.Server{
Name: "srv_0",
Name: "some-server",
Address: "1.2.3.4",
Port: int64p(8080),
Weight: int64p(5),
Expand All @@ -166,7 +168,7 @@ func GetTestHAConfig(baseCfg string) State {
Maintenance: models.ServerMaintenanceDisabled,
},
models.Server{
Name: "srv_1",
Name: "different-server",
Address: "1.2.3.5",
Port: int64p(8081),
Weight: int64p(8),
Expand Down Expand Up @@ -234,36 +236,40 @@ func TestServerUpdate(t *testing.T) {
consulCfg := GetTestConsulConfig()
consulCfg.Upstreams[0].Nodes = consulCfg.Upstreams[0].Nodes[1:]

oldState := GetTestHAConfig("/")

// remove first server
expectedNewState := GetTestHAConfig("/")
expectedNewState.Backends[1].Servers[0].Maintenance = models.ServerMaintenanceEnabled
expectedNewState.Backends[1].Servers[0].Address = "127.0.0.1"
expectedNewState.Backends[1].Servers[0].Port = int64p(1)
expectedNewState.Backends[1].Servers[0].Weight = int64p(1)
expectedRemovedFirstServer := GetTestHAConfig("/")
expectedRemovedFirstServer.Backends[1].Servers[0].Maintenance = models.ServerMaintenanceEnabled
expectedRemovedFirstServer.Backends[1].Servers[0].Name = "disabled_server_0"
expectedRemovedFirstServer.Backends[1].Servers[0].Address = "127.0.0.1"
expectedRemovedFirstServer.Backends[1].Servers[0].Port = int64p(1)
expectedRemovedFirstServer.Backends[1].Servers[0].Weight = int64p(1)

originalState := GetTestHAConfig("/")
actualRemovedFirstServer, err := Generate(TestOpts, TestCertStore, originalState, consulCfg)

generated, err := Generate(TestOpts, TestCertStore, oldState, consulCfg)
require.Nil(t, err)
require.Equal(t, expectedNewState, generated)
require.Equal(t, expectedRemovedFirstServer, actualRemovedFirstServer)

// re-add first server
generated, err = Generate(TestOpts, TestCertStore, generated, GetTestConsulConfig())
require.Nil(t, err)
require.Equal(t, GetTestHAConfig("/"), generated)
originalConsulCfg := GetTestConsulConfig()
readdedFirstServer, err2 := Generate(TestOpts, TestCertStore, actualRemovedFirstServer, originalConsulCfg)

require.Nil(t, err2)
require.Equal(t, originalState, readdedFirstServer)

// add another one
consulCfg = GetTestConsulConfig()
consulCfg.Upstreams[0].Nodes = append(consulCfg.Upstreams[0].Nodes, consul.UpstreamNode{
Host: "1.2.3.6",
Port: 8082,
Weight: 10,
addedOneServerConsulCfg := GetTestConsulConfig()
addedOneServerConsulCfg.Upstreams[0].Nodes = append(addedOneServerConsulCfg.Upstreams[0].Nodes, consul.UpstreamNode{
Name: "even-different-server",
Address: "1.2.3.6",
Port: 8082,
Weight: 10,
})

expectedNewState = GetTestHAConfig("/")
expectedNewState.Backends[1].Servers = append(expectedNewState.Backends[1].Servers,
expectedAddedOneServer := GetTestHAConfig("/")
expectedAddedOneServer.Backends[1].Servers = append(expectedAddedOneServer.Backends[1].Servers,
models.Server{
Name: "srv_2",
Name: "even-different-server",
Address: "1.2.3.6",
Port: int64p(8082),
Weight: int64p(10),
Expand All @@ -273,8 +279,8 @@ func TestServerUpdate(t *testing.T) {
Verify: models.BindVerifyRequired,
Maintenance: models.ServerMaintenanceDisabled,
},
models.Server{
Name: "srv_3",
models.Server{ // because we always make slot for one more
Name: "disabled_server_3",
Address: "127.0.0.1",
Port: int64p(1),
Weight: int64p(1),
Expand All @@ -286,9 +292,9 @@ func TestServerUpdate(t *testing.T) {
},
)

generated, err = Generate(TestOpts, TestCertStore, generated, consulCfg)
actualAddedOneServer, err := Generate(TestOpts, TestCertStore, expectedAddedOneServer, addedOneServerConsulCfg)
require.Nil(t, err)
require.Equal(t, expectedNewState, generated)
require.Equal(t, expectedAddedOneServer, actualAddedOneServer)
}

type fakeCertStore struct{}
Expand Down
2 changes: 1 addition & 1 deletion haproxy/state/states.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type HAProxy interface {
DeleteBackend(name string) error
CreateBackend(be models.Backend) error
CreateServer(beName string, srv models.Server) error
ReplaceServer(beName string, srv models.Server) error
ReplaceServer(beName string, oldSrvName string, newSrv models.Server) error
DeleteServer(beName string, name string) error
CreateFilter(parentType, parentName string, filter models.Filter) error
CreateTCPRequestRule(parentType, parentName string, rule models.TCPRequestRule) error
Expand Down
Loading