Skip to content

Commit 7a589fa

Browse files
author
Thibault Gilles
committed
Perform state diff to configure haproxy
1 parent 4679ef5 commit 7a589fa

19 files changed

+1526
-495
lines changed

haproxy/certs.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package haproxy
2+
3+
import (
4+
"crypto/sha256"
5+
"encoding/hex"
6+
"os"
7+
"path"
8+
9+
"github.com/criteo/haproxy-consul-connect/consul"
10+
log "github.com/sirupsen/logrus"
11+
)
12+
13+
func (h *haConfig) FilePath(content []byte) (string, error) {
14+
sum := sha256.Sum256(content)
15+
16+
path := path.Join(h.Base, hex.EncodeToString(sum[:]))
17+
18+
_, err := os.Stat(path)
19+
if err != nil && !os.IsNotExist(err) {
20+
return "", err
21+
}
22+
23+
if err == nil {
24+
return path, nil
25+
}
26+
27+
f, err := os.OpenFile(path, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0600)
28+
if err != nil {
29+
return "", err
30+
}
31+
defer f.Close()
32+
33+
_, err = f.Write(content)
34+
if err != nil {
35+
return "", err
36+
}
37+
38+
log.Debugf("wrote new config file %s", path)
39+
40+
return path, nil
41+
}
42+
43+
func (h *haConfig) CertsPath(t consul.TLS) (string, string, error) {
44+
crt := []byte{}
45+
crt = append(crt, t.Cert...)
46+
crt = append(crt, t.Key...)
47+
48+
crtPath, err := h.FilePath(crt)
49+
if err != nil {
50+
return "", "", err
51+
}
52+
53+
ca := []byte{}
54+
for _, c := range t.CAs {
55+
ca = append(ca, c...)
56+
}
57+
58+
caPath, err := h.FilePath(ca)
59+
if err != nil {
60+
return "", "", err
61+
}
62+
63+
return caPath, crtPath, nil
64+
}

haproxy/config.go

Lines changed: 5 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
package haproxy
22

33
import (
4-
"crypto/sha256"
5-
"encoding/hex"
64
"io/ioutil"
75
"os"
86
"path"
97
"runtime"
108

119
"text/template"
1210

13-
"github.com/criteo/haproxy-consul-connect/consul"
1411
"github.com/criteo/haproxy-consul-connect/lib"
1512
log "github.com/sirupsen/logrus"
1613
)
1714

15+
const (
16+
dataplaneUser = "haproxy"
17+
dataplanePass = "pass"
18+
)
19+
1820
var baseCfgTmpl = `
1921
global
2022
master-worker
@@ -126,56 +128,3 @@ func newHaConfig(baseDir string, sd *lib.Shutdown) (*haConfig, error) {
126128

127129
return cfg, nil
128130
}
129-
130-
func (h *haConfig) FilePath(content []byte) (string, error) {
131-
sum := sha256.Sum256(content)
132-
133-
path := path.Join(h.Base, hex.EncodeToString(sum[:]))
134-
135-
_, err := os.Stat(path)
136-
if err != nil && !os.IsNotExist(err) {
137-
return "", err
138-
}
139-
140-
if err == nil {
141-
return path, nil
142-
}
143-
144-
f, err := os.OpenFile(path, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0600)
145-
if err != nil {
146-
return "", err
147-
}
148-
defer f.Close()
149-
150-
_, err = f.Write(content)
151-
if err != nil {
152-
return "", err
153-
}
154-
155-
log.Debugf("wrote new config file %s", path)
156-
157-
return path, nil
158-
}
159-
160-
func (h *haConfig) CertsPath(t consul.TLS) (string, string, error) {
161-
crt := []byte{}
162-
crt = append(crt, t.Cert...)
163-
crt = append(crt, t.Key...)
164-
165-
crtPath, err := h.FilePath(crt)
166-
if err != nil {
167-
return "", "", err
168-
}
169-
170-
ca := []byte{}
171-
for _, c := range t.CAs {
172-
ca = append(ca, c...)
173-
}
174-
175-
caPath, err := h.FilePath(ca)
176-
if err != nil {
177-
return "", "", err
178-
}
179-
180-
return crtPath, caPath, nil
181-
}

haproxy/dataplane.go renamed to haproxy/dataplane/dataplane.go

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package haproxy
1+
package dataplane
22

33
import (
44
"bytes"
@@ -14,27 +14,32 @@ import (
1414
log "github.com/sirupsen/logrus"
1515
)
1616

17-
const (
18-
dataplaneUser = "haproxy"
19-
dataplanePass = "pass"
20-
)
21-
22-
type dataplaneClient struct {
17+
type Dataplane struct {
2318
addr string
2419
userName, password string
2520
client *http.Client
2621
lock sync.Mutex
2722
version int
2823
}
2924

25+
func New(addr, userName, password string, client *http.Client) *Dataplane {
26+
return &Dataplane{
27+
addr: addr,
28+
userName: userName,
29+
password: password,
30+
client: client,
31+
version: 1,
32+
}
33+
}
34+
3035
type tnx struct {
3136
txID string
32-
client *dataplaneClient
37+
client *Dataplane
3338

3439
after []func() error
3540
}
3641

37-
func (c *dataplaneClient) Tnx() *tnx {
42+
func (c *Dataplane) Tnx() *tnx {
3843
return &tnx{
3944
client: c,
4045
}
@@ -55,7 +60,7 @@ func (t *tnx) ensureTnx() error {
5560
return nil
5661
}
5762

58-
func (c *dataplaneClient) Info() (*models.ProcessInfo, error) {
63+
func (c *Dataplane) Info() (*models.ProcessInfo, error) {
5964
res := &models.ProcessInfo{}
6065
err := c.makeReq(http.MethodGet, "/services/haproxy/info", nil, res)
6166
if err != nil {
@@ -64,11 +69,11 @@ func (c *dataplaneClient) Info() (*models.ProcessInfo, error) {
6469
return res, nil
6570
}
6671

67-
func (c *dataplaneClient) Ping() error {
72+
func (c *Dataplane) Ping() error {
6873
return c.makeReq(http.MethodGet, "/v1/specification", nil, nil)
6974
}
7075

71-
func (c *dataplaneClient) Stats() (models.NativeStats, error) {
76+
func (c *Dataplane) Stats() (models.NativeStats, error) {
7277
res := models.NativeStats{}
7378
return res, c.makeReq(http.MethodGet, "/v1/services/haproxy/stats/native", nil, &res)
7479
}
@@ -140,13 +145,13 @@ func (t *tnx) CreateServer(beName string, srv models.Server) error {
140145
}
141146

142147
func (t *tnx) ReplaceServer(beName string, srv models.Server) error {
143-
if err := t.ensureTnx(); err != nil {
144-
return err
145-
}
146-
return t.client.makeReq(http.MethodPut, fmt.Sprintf("/v1/services/haproxy/configuration/servers/%s?backend=%s&transaction_id=%s", srv.Name, beName, t.txID), srv, nil)
148+
t.After(func() error {
149+
return t.client.ReplaceServer(beName, srv)
150+
})
151+
return nil
147152
}
148153

149-
func (c *dataplaneClient) ReplaceServer(beName string, srv models.Server) error {
154+
func (c *Dataplane) ReplaceServer(beName string, srv models.Server) error {
150155
err := c.makeReq(http.MethodPut, fmt.Sprintf("/v1/services/haproxy/configuration/servers/%s?backend=%s&version=%d", srv.Name, beName, c.version), srv, nil)
151156
if err != nil {
152157
return err
@@ -184,7 +189,7 @@ func (t *tnx) CreateLogTargets(parentType, parentName string, rule models.LogTar
184189
return t.client.makeReq(http.MethodPost, fmt.Sprintf("/v1/services/haproxy/configuration/log_targets?parent_type=%s&parent_name=%s&transaction_id=%s", parentType, parentName, t.txID), rule, nil)
185190
}
186191

187-
func (c *dataplaneClient) makeReq(method, url string, reqData, resData interface{}) error {
192+
func (c *Dataplane) makeReq(method, url string, reqData, resData interface{}) error {
188193
c.lock.Lock()
189194
defer c.lock.Unlock()
190195

haproxy/downstream.go

Lines changed: 0 additions & 132 deletions
This file was deleted.

0 commit comments

Comments
 (0)