Skip to content

Commit 9a2af57

Browse files
committed
BUG/MEDIUM: Fix inconsistent processing of haproxy files
- Fix uninitialized map access - handling files (errorfiles and patternfiles) by reference to make changes persistent. Regression introduced in 708775f
1 parent 8c3310e commit 9a2af57

File tree

4 files changed

+26
-19
lines changed

4 files changed

+26
-19
lines changed

controller/configuration/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ func (c *ControllerCfg) envInit() (err error) {
173173
c.Env.PatternDir = filepath.Join(c.Env.CfgDir, "patterns")
174174
}
175175
if c.Env.ErrFileDir == "" {
176-
c.Env.ErrFileDir = filepath.Join(c.Env.CfgDir, "errors")
176+
c.Env.ErrFileDir = filepath.Join(c.Env.CfgDir, "errorfiles")
177177
}
178178

179179
for _, d := range []string{

controller/handler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ func (c *HAProxyController) initHandlers() {
4141
Port: c.OSArgs.HTTPSBindPort,
4242
},
4343
handler.ProxyProtocol{},
44-
handler.ErrorFile{},
44+
&handler.ErrorFile{},
4545
handler.TCPServices{
4646
CertDir: c.Cfg.Env.FrontendCertDir,
4747
IPv4: !c.OSArgs.DisableIPV4,
4848
AddrIPv4: c.OSArgs.IPV4BindAddr,
4949
IPv6: !c.OSArgs.DisableIPV6,
5050
AddrIPv6: c.OSArgs.IPV6BindAddr,
5151
},
52-
handler.PatternFiles{},
52+
&handler.PatternFiles{},
5353
handler.Refresh{},
5454
}
5555
if c.OSArgs.PprofEnabled {

controller/handler/errorfile.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type ErrorFile struct {
3131
updateAPI bool
3232
}
3333

34-
func (h ErrorFile) Update(k store.K8s, cfg *config.ControllerCfg, api api.HAProxyClient) (reload bool, err error) {
34+
func (h *ErrorFile) Update(k store.K8s, cfg *config.ControllerCfg, api api.HAProxyClient) (reload bool, err error) {
3535
h.files.dir = cfg.Env.ErrFileDir
3636
if k.ConfigMaps.Errorfiles == nil {
3737
return false, nil

controller/handler/pattern-files.go

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,8 @@ type PatternFiles struct {
3030
files files
3131
}
3232

33-
type files struct {
34-
dir string
35-
data map[string]file
36-
}
37-
type file struct {
38-
hash string
39-
inUse bool
40-
updated bool
41-
}
42-
43-
func (h PatternFiles) Update(k store.K8s, cfg *config.ControllerCfg, api api.HAProxyClient) (reload bool, err error) {
33+
func (h *PatternFiles) Update(k store.K8s, cfg *config.ControllerCfg, api api.HAProxyClient) (reload bool, err error) {
34+
h.files.dir = cfg.Env.PatternDir
4435
if k.ConfigMaps.PatternFiles == nil {
4536
return false, nil
4637
}
@@ -77,23 +68,39 @@ func (h PatternFiles) Update(k store.K8s, cfg *config.ControllerCfg, api api.HAP
7768
return reload, nil
7869
}
7970

80-
func (f files) deleteFile(code string) error {
71+
type files struct {
72+
dir string
73+
data map[string]*file
74+
}
75+
76+
type file struct {
77+
hash string
78+
inUse bool
79+
updated bool
80+
}
81+
82+
func (f *files) deleteFile(code string) error {
8183
delete(f.data, code)
8284
err := os.Remove(filepath.Join(f.dir, code))
8385
return err
8486
}
85-
func (f files) newFile(code, value string) error {
87+
88+
func (f *files) newFile(code, value string) error {
8689
if err := renameio.WriteFile(filepath.Join(f.dir, code), []byte(value), os.ModePerm); err != nil {
8790
return err
8891
}
89-
f.data[code] = file{
92+
if f.data == nil {
93+
f.data = map[string]*file{}
94+
}
95+
f.data[code] = &file{
9096
hash: utils.Hash([]byte(value)),
9197
inUse: true,
9298
updated: true,
9399
}
94100
return nil
95101
}
96-
func (f files) updateFile(name, value string) error {
102+
103+
func (f *files) updateFile(name, value string) error {
97104
newHash := utils.Hash([]byte(value))
98105
file := f.data[name]
99106
if file.hash != newHash {

0 commit comments

Comments
 (0)