Skip to content

Commit 0a10614

Browse files
ivanmatmatioktalz
authored andcommitted
BUG/MINOR: configsnippets from different ingresses pointing to same service
1 parent e5eee04 commit 0a10614

File tree

5 files changed

+66
-19
lines changed

5 files changed

+66
-19
lines changed

pkg/annotations/annotations.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import (
1717
"github.com/haproxytech/kubernetes-ingress/pkg/utils"
1818
)
1919

20+
var logger = utils.GetLogger()
21+
2022
type Annotation interface {
2123
GetName() string
2224
Process(k store.K8s, annotations ...map[string]string) error

pkg/annotations/cfgSnippet.go

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package annotations
22

33
import (
4+
"sort"
45
"strings"
56

67
"github.com/go-test/deep"
78

89
"github.com/haproxytech/kubernetes-ingress/pkg/annotations/common"
910
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy/api"
1011
"github.com/haproxytech/kubernetes-ingress/pkg/store"
12+
"github.com/haproxytech/kubernetes-ingress/pkg/utils"
1113
)
1214

1315
type CfgSnippet struct {
@@ -17,8 +19,9 @@ type CfgSnippet struct {
1719
}
1820

1921
type cfgData struct {
20-
value []string
21-
updated []string
22+
value []string
23+
previousValue []string
24+
updated []string
2225
}
2326

2427
// cfgSnippet is a particular type of config that is not
@@ -76,15 +79,12 @@ func (a *CfgSnippet) Process(k store.K8s, annotations ...map[string]string) erro
7679
cfgSnippet.frontends[a.frontend].updated = updated
7780
}
7881
case a.backend != "":
79-
_, ok := cfgSnippet.backends[a.backend]
82+
cfg, ok := cfgSnippet.backends[a.backend]
8083
if !ok {
81-
cfgSnippet.backends[a.backend] = &cfgData{}
82-
}
83-
updated := deep.Equal(cfgSnippet.backends[a.backend].value, data)
84-
if len(updated) != 0 {
85-
cfgSnippet.backends[a.backend].value = data
86-
cfgSnippet.backends[a.backend].updated = updated
84+
cfg = &cfgData{}
8785
}
86+
cfg.value = append(cfg.value, data...)
87+
cfgSnippet.backends[a.backend] = cfg
8888
default:
8989
updated := deep.Equal(cfgSnippet.global.value, data)
9090
if len(updated) != 0 {
@@ -133,16 +133,25 @@ func UpdateBackendCfgSnippet(api api.HAProxyClient, backend string) (updated []s
133133
if !ok {
134134
return
135135
}
136-
if len(data.updated) == 0 {
136+
defer func() {
137+
data.value = nil
138+
}()
139+
valueCopy := make([]string, len(data.value))
140+
copy(valueCopy, data.value)
141+
prevValueCopy := make([]string, len(data.previousValue))
142+
copy(prevValueCopy, data.previousValue)
143+
sort.StringSlice(valueCopy).Sort()
144+
sort.StringSlice(prevValueCopy).Sort()
145+
updated = deep.Equal(valueCopy, prevValueCopy)
146+
if len(updated) == 0 {
137147
return
138148
}
139149
err = api.BackendCfgSnippetSet(backend, data.value)
140150
if err != nil {
141151
return
142152
}
143-
updated = data.updated
144-
data.updated = nil
145-
cfgSnippet.backends[backend] = data
153+
data.previousValue = data.value
154+
data.value = nil
146155
return
147156
}
148157

@@ -152,3 +161,17 @@ func RemoveBackendCfgSnippet(backend string) {
152161
}
153162
delete(cfgSnippet.backends, backend)
154163
}
164+
165+
func HandleBackendCfgSnippet(api api.HAProxyClient) (reload bool, err error) {
166+
var errs utils.Errors
167+
for backend := range cfgSnippet.backends {
168+
updated, errBackend := UpdateBackendCfgSnippet(api, backend)
169+
if len(updated) != 0 {
170+
logger.Debugf("backend configsnippet of '%s' has been updated, reload required", backend)
171+
}
172+
reload = reload || len(updated) != 0
173+
errs.Add(errBackend)
174+
}
175+
err = errs.Result()
176+
return
177+
}

pkg/controller/handler.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ func (c *HAProxyController) initHandlers() {
5050
AddrIPv6: c.osArgs.IPV6BindAddr,
5151
},
5252
&handler.PatternFiles{},
53+
&handler.BackendCfgSnippet{},
5354
}
5455
if c.osArgs.PprofEnabled {
5556
c.updateHandlers = append(c.updateHandlers, handler.Pprof{})

pkg/handler/backendCfgSnippet.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2019 HAProxy Technologies LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package handler
16+
17+
import (
18+
"github.com/haproxytech/kubernetes-ingress/pkg/annotations"
19+
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy"
20+
"github.com/haproxytech/kubernetes-ingress/pkg/store"
21+
)
22+
23+
type BackendCfgSnippet struct{}
24+
25+
func (handler BackendCfgSnippet) Update(k store.K8s, h haproxy.HAProxy, a annotations.Annotations) (reload bool, err error) {
26+
return annotations.HandleBackendCfgSnippet(h.HAProxyClient)
27+
}

pkg/service/service.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,6 @@ func (s *Service) HandleBackend(store store.K8s, client api.HAProxyClient, a ann
143143
}
144144
// config-snippet
145145
logger.Error(annotations.NewBackendCfgSnippet("backend-config-snippet", newBackend.Name).Process(store, s.annotations...))
146-
change, errSnipp := annotations.UpdateBackendCfgSnippet(client, newBackend.Name)
147-
logger.Error(errSnipp)
148-
if len(change) != 0 {
149-
reload = true
150-
logger.Debugf("Service '%s/%s': backend '%s' config-snippet updated: %s\nReload required", s.resource.Namespace, s.resource.Name, newBackend.Name, change)
151-
}
152146
return
153147
}
154148

0 commit comments

Comments
 (0)