Skip to content

Commit c2f62f3

Browse files
AdamJCrawfordGopher Bot
authored andcommitted
MEDIUM: runtime: add all fields for runtime server.
Add ~20 new read-only fields to the RuntimeServer model parsed from HAProxy's 'show servers state' output, including backend_id, backend_name, uweight, iweight, check_status, check_state, agent_state, fqdn, use_ssl, and more. Also add SetServerSSL method for toggling SSL on servers.
1 parent 88ae947 commit c2f62f3

File tree

4 files changed

+140
-1
lines changed

4 files changed

+140
-1
lines changed

runtime/interface.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ type Servers interface {
8585
SetServerAgentAddr(backend, server string, addr string) error
8686
// SetServerAgentSend set agent-send for server
8787
SetServerAgentSend(backend, server string, send string) error
88+
// SetServerSSL set ssl for server
89+
SetServerSSL(backend, server string, ssl string) error
8890
// GetServerState returns server runtime state
8991
GetServersState(backend string) (models.RuntimeServers, error)
9092
// GetServerState returns server runtime state

runtime/runtime_client.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,19 @@ func (c *client) SetServerAgentSend(backend, server string, send string) error {
412412
return nil
413413
}
414414

415+
// SetServerSSL set ssl for server
416+
func (c *client) SetServerSSL(backend, server string, ssl string) error {
417+
if !c.runtime.IsValid() {
418+
return errors.New("no valid runtime found")
419+
}
420+
err := c.runtime.SetServerSSL(backend, server, ssl)
421+
if err != nil {
422+
return fmt.Errorf("%s %w", c.runtime.socketPath, err)
423+
}
424+
425+
return nil
426+
}
427+
415428
// GetServerState returns server runtime state
416429
func (c *client) GetServersState(backend string) (models.RuntimeServers, error) {
417430
if !c.runtime.IsValid() {

runtime/servers.go

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,12 @@ func (s *SingleRuntime) SetServerAgentSend(backend, server string, send string)
127127
return s.Execute(cmd)
128128
}
129129

130+
// SetServerSSL set ssl for server
131+
func (s *SingleRuntime) SetServerSSL(backend, server string, ssl string) error {
132+
cmd := fmt.Sprintf("set server %s/%s ssl %s", backend, server, ssl)
133+
return s.Execute(cmd)
134+
}
135+
130136
// GetServersState returns servers runtime state
131137
func (s *SingleRuntime) GetServersState(backend string) (models.RuntimeServers, error) {
132138
cmd := "show servers state " + backend
@@ -179,13 +185,23 @@ func parseRuntimeServers(output string) (models.RuntimeServers, error) {
179185
return result, nil
180186
}
181187

188+
func parseInt64P(s string) *int64 {
189+
v, err := strconv.ParseInt(s, 10, 64)
190+
if err != nil {
191+
return nil
192+
}
193+
return &v
194+
}
195+
182196
func parseRuntimeServer(line string) *models.RuntimeServer {
183197
fields := strings.Split(line, " ")
184198

185-
if len(fields) < 19 {
199+
if len(fields) < 25 {
186200
return nil
187201
}
188202

203+
backendID := parseInt64P(fields[0])
204+
189205
p, err := strconv.ParseInt(fields[18], 10, 64)
190206
var port *int64
191207
if err == nil {
@@ -198,6 +214,21 @@ func parseRuntimeServer(line string) *models.RuntimeServer {
198214
weight = &w
199215
}
200216

217+
uweight := parseInt64P(fields[7])
218+
iweight := parseInt64P(fields[8])
219+
lastTimeChange := parseInt64P(fields[9])
220+
checkStatus := parseInt64P(fields[10])
221+
checkResult := parseInt64P(fields[11])
222+
checkHealth := parseInt64P(fields[12])
223+
checkState := parseInt64P(fields[13])
224+
agentState := parseInt64P(fields[14])
225+
backendForcedID := parseInt64P(fields[15])
226+
forecedID := parseInt64P(fields[16])
227+
checkPort := parseInt64P(fields[21])
228+
agentPort := parseInt64P(fields[24])
229+
230+
useSsl := fields[20] == "1"
231+
201232
admState, _ := misc.GetServerAdminState(fields[6])
202233

203234
var opState string
@@ -211,12 +242,31 @@ func parseRuntimeServer(line string) *models.RuntimeServer {
211242
}
212243

213244
return &models.RuntimeServer{
245+
BackendID: backendID,
246+
BackendName: fields[1],
214247
Name: fields[3],
215248
Address: fields[4],
216249
Port: port,
217250
ID: fields[2],
218251
AdminState: admState,
219252
OperationalState: opState,
220253
Weight: weight,
254+
Uweight: uweight,
255+
Iweight: iweight,
256+
LastTimeChange: lastTimeChange,
257+
CheckStatus: checkStatus,
258+
CheckResult: checkResult,
259+
CheckHealth: checkHealth,
260+
CheckState: checkState,
261+
AgentState: agentState,
262+
BackendForcedID: backendForcedID,
263+
ForecedID: forecedID,
264+
Fqdn: fields[17],
265+
Srvrecord: fields[19],
266+
UseSsl: useSsl,
267+
CheckPort: checkPort,
268+
CheckAddr: fields[22],
269+
AgentAddr: fields[23],
270+
AgentPort: agentPort,
221271
}
222272
}

specification/models/runtime/server.yaml

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,80 @@ server:
2828
weight:
2929
type: integer
3030
x-nullable: true
31+
backend_id:
32+
type: integer
33+
readOnly: true
34+
x-nullable: true
35+
backend_name:
36+
type: string
37+
readOnly: true
38+
uweight:
39+
type: integer
40+
readOnly: true
41+
x-nullable: true
42+
iweight:
43+
type: integer
44+
readOnly: true
45+
x-nullable: true
46+
last_time_change:
47+
type: integer
48+
readOnly: true
49+
x-nullable: true
50+
check_status:
51+
type: integer
52+
readOnly: true
53+
x-nullable: true
54+
check_result:
55+
type: integer
56+
readOnly: true
57+
x-nullable: true
58+
check_health:
59+
type: integer
60+
readOnly: true
61+
x-nullable: true
62+
check_state:
63+
type: integer
64+
readOnly: true
65+
x-nullable: true
66+
agent_state:
67+
type: integer
68+
readOnly: true
69+
x-nullable: true
70+
backend_forced_id:
71+
type: integer
72+
readOnly: true
73+
x-nullable: true
74+
foreced_id:
75+
type: integer
76+
readOnly: true
77+
x-nullable: true
78+
fqdn:
79+
type: string
80+
readOnly: true
81+
srvrecord:
82+
type: string
83+
readOnly: true
84+
use_ssl:
85+
type: boolean
86+
readOnly: true
87+
check_port:
88+
type: integer
89+
readOnly: true
90+
x-nullable: true
91+
minimum: 0
92+
maximum: 65535
93+
check_addr:
94+
type: string
95+
readOnly: true
96+
agent_addr:
97+
type: string
98+
readOnly: true
99+
agent_port:
100+
type: integer
101+
readOnly: true
102+
x-nullable: true
103+
minimum: 0
104+
maximum: 65535
31105
example:
32106
server_id: 1
33107
server_name: web_server

0 commit comments

Comments
 (0)