Skip to content

Commit 33ee8e3

Browse files
hdurand0710oktalz
authored andcommitted
BUG: avoid writing incomplete maps files and store hash only in case of success
This fixes different potential issues: - mapFile.hash was stored even though the map file was not written succesfully. Then we would not attempt to write it on the next Refresh. - WriteString writes into the file, even though Sync() is not called. Hence, we can write some chunks but not all.
1 parent 6a67203 commit 33ee8e3

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

pkg/haproxy/maps/main.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"sort"
2424
"strings"
2525

26+
"github.com/google/renameio"
2627
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy/api"
2728
"github.com/haproxytech/kubernetes-ingress/pkg/utils"
2829
)
@@ -120,7 +121,6 @@ func (m mapFiles) RefreshMaps(client api.HAProxyClient) (reload bool) {
120121
if mapFile.hash == hash {
121122
continue
122123
}
123-
mapFile.hash = hash
124124
var f *os.File
125125
var err error
126126
filename := GetPath(name)
@@ -132,14 +132,18 @@ func (m mapFiles) RefreshMaps(client api.HAProxyClient) (reload bool) {
132132
logger.Error(err)
133133
continue
134134
}
135-
defer f.Close()
135+
f.Close()
136+
var buff strings.Builder
137+
buff.Grow(bufSize * len(content))
136138
for _, d := range content {
137-
if _, err = f.WriteString(d); err != nil {
138-
logger.Error(err)
139-
return
140-
}
139+
buff.WriteString(d)
140+
}
141+
err = renameio.WriteFile(string(filename), []byte(buff.String()), 0o666)
142+
if err != nil {
143+
logger.Error(err)
144+
continue
141145
}
142-
logger.Error(f.Sync())
146+
mapFile.hash = hash
143147
if err = client.SetMapContent(string(name), content); err != nil {
144148
if errors.Is(err, api.ErrMapNotFound) {
145149
logger.Debugf("Map file %s created, reload required", name)

0 commit comments

Comments
 (0)