@@ -2,6 +2,7 @@ package haproxy
22
33import (
44 "bytes"
5+ "errors"
56 "fmt"
67 "strconv"
78 "strings"
@@ -167,6 +168,38 @@ func (b *Backend) Refresh() error {
167168 return nil
168169}
169170
171+ // SetRoutingKey sets the cookie routing key for the haproxy backend.
172+ func (b * Backend ) SetRoutingKey (k string ) error {
173+ glog .V (4 ).Infof ("Setting routing key for %s" , b .name )
174+
175+ cmd := fmt .Sprintf ("set dynamic-cookie-key backend %s %s" , b .name , k )
176+ if err := b .executeCommand (cmd ); err != nil {
177+ return fmt .Errorf ("setting routing key for backend %s: %v" , b .name , err )
178+ }
179+
180+ cmd = fmt .Sprintf ("enable dynamic-cookie backend %s" , b .name )
181+ if err := b .executeCommand (cmd ); err != nil {
182+ return fmt .Errorf ("enabling routing key for backend %s: %v" , b .name , err )
183+ }
184+
185+ return nil
186+ }
187+
188+ // executeCommand runs a command using the haproxy dynamic config api client.
189+ func (b * Backend ) executeCommand (cmd string ) error {
190+ responseBytes , err := b .client .Execute (cmd )
191+ if err != nil {
192+ return err
193+ }
194+
195+ response := strings .TrimSpace (string (responseBytes ))
196+ if len (response ) > 0 {
197+ return errors .New (response )
198+ }
199+
200+ return nil
201+ }
202+
170203// Disable stops serving traffic for all servers for a haproxy backend.
171204func (b * Backend ) Disable () error {
172205 if _ , err := b .Servers (); err != nil {
0 commit comments