Skip to content

Commit 0ac9443

Browse files
Merge pull request #20557 from ramr/fix-router-dyn-cookie-bug
Fix router bug where cookie is not set correctly when dynamic changes are enabled
2 parents edad963 + b1045db commit 0ac9443

File tree

6 files changed

+51
-4
lines changed

6 files changed

+51
-4
lines changed

images/router/haproxy/conf/haproxy-config.template

+2
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,7 @@ backend {{genBackendNamePrefix $cfg.TLSTermination}}:{{$cfgIdx}}
495495
{{- else }}
496496
{{- with $name := $dynamicConfigManager.ServerTemplateName $cfgIdx }}
497497
{{- with $size := $dynamicConfigManager.ServerTemplateSize $cfgIdx }}
498+
dynamic-cookie-key {{$cfg.RoutingKeyName}}
498499
server-template {{$name}}- 1-{{$size}} 172.4.0.4:8765 check disabled
499500
{{- end }}
500501
{{- end }}
@@ -566,6 +567,7 @@ backend {{genBackendNamePrefix $cfg.TLSTermination}}:{{$cfgIdx}}
566567
{{- with $dynamicConfigManager }}
567568
{{- with $name := $dynamicConfigManager.ServerTemplateName $cfgIdx }}
568569
{{- with $size := $dynamicConfigManager.ServerTemplateSize $cfgIdx }}
570+
dynamic-cookie-key {{$cfg.RoutingKeyName}}
569571
server-template {{$name}}- 1-{{$size}} 172.4.0.4:8765 check disabled
570572
{{- end }}
571573
{{- end }}

pkg/router/template/configmanager/haproxy/backend.go

+33
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package haproxy
22

33
import (
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.
171204
func (b *Backend) Disable() error {
172205
if _, err := b.Servers(); err != nil {

pkg/router/template/configmanager/haproxy/blueprint_plugin_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func (cm *fakeConfigManager) FindBlueprint(id string) (*routeapi.Route, bool) {
4343
func (cm *fakeConfigManager) Register(id string, route *routeapi.Route) {
4444
}
4545

46-
func (cm *fakeConfigManager) AddRoute(id string, route *routeapi.Route) error {
46+
func (cm *fakeConfigManager) AddRoute(id, routingKey string, route *routeapi.Route) error {
4747
return nil
4848
}
4949

pkg/router/template/configmanager/haproxy/manager.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ func (cm *haproxyConfigManager) Register(id string, route *routeapi.Route) {
286286
}
287287

288288
// AddRoute adds a new route or updates an existing route.
289-
func (cm *haproxyConfigManager) AddRoute(id string, route *routeapi.Route) error {
289+
func (cm *haproxyConfigManager) AddRoute(id, routingKey string, route *routeapi.Route) error {
290290
if cm.isReloading() {
291291
return fmt.Errorf("Router reload in progress, cannot dynamically add route %s", id)
292292
}
@@ -334,6 +334,18 @@ func (cm *haproxyConfigManager) AddRoute(id string, route *routeapi.Route) error
334334
return fmt.Errorf("adding map associations for id %s: %v", id, err)
335335
}
336336

337+
backendName := entry.BackendName()
338+
glog.V(4).Infof("Finding backend %s ...", backendName)
339+
backend, err := cm.client.FindBackend(backendName)
340+
if err != nil {
341+
return err
342+
}
343+
344+
glog.V(4).Infof("Setting routing key for backend %s ...", backendName)
345+
if err := backend.SetRoutingKey(routingKey); err != nil {
346+
return err
347+
}
348+
337349
glog.V(4).Infof("Route %s added using blueprint pool slot %s", id, slotName)
338350
return nil
339351
}

pkg/router/template/router.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ func (r *templateRouter) dynamicallyAddRoute(backendKey string, route *routeapi.
598598
return false
599599
}
600600

601-
err := r.dynamicConfigManager.AddRoute(backendKey, route)
601+
err := r.dynamicConfigManager.AddRoute(backendKey, backend.RoutingKeyName, route)
602602
if err != nil {
603603
glog.V(4).Infof("Router will reload as the ConfigManager could not dynamically add route for backend %s: %v", backendKey, err)
604604
return false

pkg/router/template/types.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ type ConfigManager interface {
190190
Register(id string, route *routeapi.Route)
191191

192192
// AddRoute adds a new route or updates an existing route.
193-
AddRoute(id string, route *routeapi.Route) error
193+
AddRoute(id, routingKey string, route *routeapi.Route) error
194194

195195
// RemoveRoute removes a route.
196196
RemoveRoute(id string, route *routeapi.Route) error

0 commit comments

Comments
 (0)