forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
214 lines (175 loc) · 4.74 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package haproxy
import (
"bytes"
"fmt"
"strings"
"time"
haproxy "github.com/bcicen/go-haproxy"
"github.com/golang/glog"
utilwait "k8s.io/apimachinery/pkg/util/wait"
)
const (
// Prefix for the socket file used for haproxy dynamic API commands.
afUnixSocketPrefix = "unix://"
// Prefix if TCP is used to communicate with haproxy.
tcpSocketPrefix = "tcp://"
// maxRetries is the number of times a command is retried.
maxRetries = 3
)
// Client is a client used to dynamically configure haproxy.
type Client struct {
socketAddress string
timeout int
backends []*Backend
maps map[string]*HAProxyMap
}
// NewClient returns a client used to dynamically change the haproxy config.
func NewClient(socketName string, timeout int) *Client {
sockAddr := socketName
if !strings.HasPrefix(sockAddr, afUnixSocketPrefix) && !strings.HasPrefix(sockAddr, tcpSocketPrefix) {
sockAddr = fmt.Sprintf("%s%s", afUnixSocketPrefix, sockAddr)
}
return &Client{
socketAddress: sockAddr,
timeout: timeout,
backends: make([]*Backend, 0),
maps: make(map[string]*HAProxyMap),
}
}
// RunCommand executes a haproxy dynamic config API command and if present
// converts the response as desired.
func (c *Client) RunCommand(cmd string, converter Converter) ([]byte, error) {
glog.V(4).Infof("Running haproxy command: %q ...", cmd)
buffer, err := c.runCommandWithRetries(cmd, maxRetries)
if err != nil {
glog.Warningf("haproxy dynamic config API command %s failed with error: %v", cmd, err)
return nil, err
}
response := buffer.Bytes()
glog.V(4).Infof("haproxy command response: %q", string(response))
if converter == nil {
return response, nil
}
return converter.Convert(response)
}
// Execute runs a haproxy dynamic config API command.
func (c *Client) Execute(cmd string) ([]byte, error) {
return c.RunCommand(cmd, nil)
}
// Reset resets any changes and clears the backends and maps.
func (c *Client) Reset() {
c.backends = make([]*Backend, 0)
c.maps = make(map[string]*HAProxyMap)
}
// Commit flushes out any pending changes on all the backends and maps.
func (c *Client) Commit() error {
for _, b := range c.backends {
if err := b.Commit(); err != nil {
return err
}
}
for _, m := range c.maps {
if err := m.Commit(); err != nil {
return err
}
}
return nil
}
// Backends returns the list of configured haproxy backends.
func (c *Client) Backends() ([]*Backend, error) {
if len(c.backends) == 0 {
if backends, err := buildHAProxyBackends(c); err != nil {
return nil, err
} else {
c.backends = backends
}
}
return c.backends, nil
}
// FindBackend returns a specific haproxy backend if it is configured.
func (c *Client) FindBackend(id string) (*Backend, error) {
if _, err := c.Backends(); err != nil {
return nil, err
}
for _, b := range c.backends {
if b.Name() == id {
return b, nil
}
}
return nil, fmt.Errorf("no backend found for id: %s", id)
}
// Maps returns the list of configured haproxy maps.
func (c *Client) Maps() ([]*HAProxyMap, error) {
if len(c.maps) == 0 {
hapMaps, err := buildHAProxyMaps(c)
if err != nil {
return nil, err
}
for _, v := range hapMaps {
c.maps[v.Name()] = v
}
return hapMaps, nil
}
mapList := make([]*HAProxyMap, len(c.maps))
i := 0
for _, v := range c.maps {
mapList[i] = v
i++
}
return mapList, nil
}
// FindMap returns a populated haproxy map.
func (c *Client) FindMap(name string) (*HAProxyMap, error) {
if _, err := c.Maps(); err != nil {
return nil, err
}
if m, ok := c.maps[name]; ok {
return m, m.Refresh()
}
return nil, fmt.Errorf("no map found for name: %s", name)
}
// runCommandWithRetries retries a haproxy command upto the retry limit
// if the error for the command is a retryable error.
func (c *Client) runCommandWithRetries(cmd string, limit int) (*bytes.Buffer, error) {
var buffer *bytes.Buffer
var cmdErr error
cmdWaitBackoff := utilwait.Backoff{
Duration: 10 * time.Millisecond,
Factor: 2,
Steps: limit,
}
n := 0
utilwait.ExponentialBackoff(cmdWaitBackoff, func() (bool, error) {
n++
client := &haproxy.HAProxyClient{
Addr: c.socketAddress,
Timeout: c.timeout,
}
buffer, cmdErr = client.RunCommand(cmd)
if cmdErr == nil {
return true, nil
}
if !isRetriable(cmdErr, cmd) {
return false, cmdErr
}
return false, nil
})
if cmdErr != nil {
glog.V(4).Infof("%d attempt(s) to run haproxy command %q failed: %v", n, cmd, cmdErr)
}
return buffer, cmdErr
}
// isRetriable checks if a haproxy command can be retried.
func isRetriable(err error, cmd string) bool {
retryableErrors := []string{
"connection reset by peer",
"connection refused",
}
s := err.Error()
for _, v := range retryableErrors {
if strings.HasSuffix(s, v) {
return true
}
}
return false
}