Skip to content

Commit c336dcf

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 ddb111d commit c336dcf

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/haproxy/instance"
2829
"github.com/haproxytech/kubernetes-ingress/pkg/utils"
@@ -117,7 +118,6 @@ func (m mapFiles) RefreshMaps(client api.HAProxyClient) {
117118
if mapFile.hash == hash {
118119
continue
119120
}
120-
mapFile.hash = hash
121121
var f *os.File
122122
var err error
123123
filename := GetPath(name)
@@ -129,14 +129,18 @@ func (m mapFiles) RefreshMaps(client api.HAProxyClient) {
129129
logger.Error(err)
130130
continue
131131
}
132-
defer f.Close()
132+
f.Close()
133+
var buff strings.Builder
134+
buff.Grow(api.BufferSize * len(content))
133135
for _, d := range content {
134-
if _, err = f.WriteString(d); err != nil {
135-
logger.Error(err)
136-
return
137-
}
136+
buff.WriteString(d)
137+
}
138+
err = renameio.WriteFile(string(filename), []byte(buff.String()), 0o666)
139+
if err != nil {
140+
logger.Error(err)
141+
continue
138142
}
139-
logger.Error(f.Sync())
143+
mapFile.hash = hash
140144
if err = client.SetMapContent(string(name), content); err != nil {
141145
if errors.Is(err, api.ErrMapNotFound) {
142146
instance.Reload("Map file %s created", string(name))

0 commit comments

Comments
 (0)