Skip to content

Commit 9527df1

Browse files
committed
MINOR: use dynamic cookies
When cookie usage is enabled for session persistence the controller will configure dynamic cookie in the corresponding backend via the dynamic-cookie-key directive. This enables session persistence even when using multiple haproxy instances.
1 parent 4392301 commit 9527df1

File tree

4 files changed

+18
-14
lines changed

4 files changed

+18
-14
lines changed

controller/annotations/service/cookie.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ func (a *Cookie) Process(k store.K8s, annotations ...map[string]string) error {
3535
Type: "insert",
3636
Nocache: true,
3737
Indirect: true,
38+
Dynamic: true,
3839
}
3940
return nil
4041
}

controller/service/service.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,22 @@ func (s *SvcContext) HandleBackend(client api.HAProxyClient, store store.K8s) (r
148148
func (s *SvcContext) getBackendModel(store store.K8s) (*models.Backend, error) {
149149
var backend *models.Backend
150150
var err error
151+
var cookieKey = "ohph7OoGhong"
151152
crInuse := true
152153
backend, err = annotations.ModelBackend("cr-backend", s.service.Namespace, store, s.service.Annotations, s.ingress.Annotations, store.ConfigMaps.Main.Annotations)
153154
logger.Warning(err)
154155
if backend == nil {
155156
backend = &models.Backend{DefaultServer: &models.DefaultServer{}}
156157
crInuse = false
157158
}
159+
if !crInuse {
160+
for _, a := range annotations.Backend(backend, store, s.certs) {
161+
err = a.Process(store, s.service.Annotations, s.ingress.Annotations, store.ConfigMaps.Main.Annotations)
162+
if err != nil {
163+
logger.Errorf("service '%s/%s': annotation '%s': %s", s.service.Namespace, s.service.Name, a.GetName(), err)
164+
}
165+
}
166+
}
158167
if s.modeTCP {
159168
backend.Mode = "tcp"
160169
} else {
@@ -166,14 +175,8 @@ func (s *SvcContext) getBackendModel(store store.K8s) (*models.Backend, error) {
166175
if s.service.DNS != "" {
167176
backend.DefaultServer = &models.DefaultServer{InitAddr: "last,libc,none"}
168177
}
169-
if crInuse {
170-
return backend, nil
171-
}
172-
for _, a := range annotations.Backend(backend, store, s.certs) {
173-
err = a.Process(store, s.service.Annotations, s.ingress.Annotations, store.ConfigMaps.Main.Annotations)
174-
if err != nil {
175-
logger.Errorf("service '%s/%s': annotation '%s': %s", s.service.Namespace, s.service.Name, a.GetName(), err)
176-
}
178+
if backend.Cookie != nil && backend.Cookie.Dynamic && backend.DynamicCookieKey == "" {
179+
backend.DynamicCookieKey = cookieKey
177180
}
178181
return backend, nil
179182
}

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ go 1.16
55
require (
66
github.com/go-test/deep v1.0.7
77
github.com/google/renameio v1.0.1
8-
github.com/haproxytech/client-native/v2 v2.5.1-0.20211004122714-a56b971bedc9
9-
github.com/haproxytech/config-parser/v4 v4.0.0-rc1.0.20211004110516-d82fb76fc458
8+
github.com/haproxytech/client-native/v2 v2.5.1-0.20211007115328-c277059aa132
9+
github.com/haproxytech/config-parser/v4 v4.0.0-rc2.0.20211007114222-853a40685fb7
1010
github.com/jessevdk/go-flags v1.4.0
1111
github.com/stretchr/testify v1.6.1
1212
k8s.io/api v0.21.3

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,10 @@ github.com/googleapis/gnostic v0.4.1 h1:DLJCy1n/vrD4HPjOvYcT8aYQXpPIzoRZONaYwyyc
219219
github.com/googleapis/gnostic v0.4.1/go.mod h1:LRhVm6pbyptWbWbuZ38d1eyptfvIytN3ir6b65WBswg=
220220
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
221221
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA=
222-
github.com/haproxytech/client-native/v2 v2.5.1-0.20211004122714-a56b971bedc9 h1:U9LbBWz1BW3Pkfq4KggfJlZZa/0XvrUt045Ez5CBKe4=
223-
github.com/haproxytech/client-native/v2 v2.5.1-0.20211004122714-a56b971bedc9/go.mod h1:gX1omLb7Snhn7czPbbBt7Ucd8rbM39UzM5zN1P/slQY=
224-
github.com/haproxytech/config-parser/v4 v4.0.0-rc1.0.20211004110516-d82fb76fc458 h1:Hq+lWbjH25PeHZikzcpJWJjyaSv0KLixUZehe/xzYe8=
225-
github.com/haproxytech/config-parser/v4 v4.0.0-rc1.0.20211004110516-d82fb76fc458/go.mod h1:4gaeOV9SKzre1pApNZF5/hOp8mjCpr7wdUZ8BRg3U/k=
222+
github.com/haproxytech/client-native/v2 v2.5.1-0.20211007115328-c277059aa132 h1:8lvpPziu4Udk7f0t8rh3DHzDS1CYTIReR5YNsULfzqw=
223+
github.com/haproxytech/client-native/v2 v2.5.1-0.20211007115328-c277059aa132/go.mod h1:1EpygYWZ9ujhR/O0zYSgww+D9TILAJ0qySwxDTLodQA=
224+
github.com/haproxytech/config-parser/v4 v4.0.0-rc2.0.20211007114222-853a40685fb7 h1:u2ACwJkXIVa8o340cKoTaA3rBHxQi/gPpR5mzTDscLE=
225+
github.com/haproxytech/config-parser/v4 v4.0.0-rc2.0.20211007114222-853a40685fb7/go.mod h1:4gaeOV9SKzre1pApNZF5/hOp8mjCpr7wdUZ8BRg3U/k=
226226
github.com/haproxytech/go-logger v1.0.0 h1:+erE6HEOjOLPogv9rcp1Jm6FojZ6FNPVMxrp9Pm75HI=
227227
github.com/haproxytech/go-logger v1.0.0/go.mod h1:OekUd8HCb7ubxMplzHUPBTHNxZmddOWfOjWclZsqIeM=
228228
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=

0 commit comments

Comments
 (0)