Skip to content

Commit

Permalink
BUG: avoid writing incomplete maps files and store hash only in case …
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
hdurand0710 authored and oktalz committed May 3, 2024
1 parent ddb111d commit c336dcf
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions pkg/haproxy/maps/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"sort"
"strings"

"github.com/google/renameio"
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy/api"
"github.com/haproxytech/kubernetes-ingress/pkg/haproxy/instance"
"github.com/haproxytech/kubernetes-ingress/pkg/utils"
Expand Down Expand Up @@ -117,7 +118,6 @@ func (m mapFiles) RefreshMaps(client api.HAProxyClient) {
if mapFile.hash == hash {
continue
}
mapFile.hash = hash
var f *os.File
var err error
filename := GetPath(name)
Expand All @@ -129,14 +129,18 @@ func (m mapFiles) RefreshMaps(client api.HAProxyClient) {
logger.Error(err)
continue
}
defer f.Close()
f.Close()
var buff strings.Builder
buff.Grow(api.BufferSize * len(content))
for _, d := range content {
if _, err = f.WriteString(d); err != nil {
logger.Error(err)
return
}
buff.WriteString(d)
}
err = renameio.WriteFile(string(filename), []byte(buff.String()), 0o666)
if err != nil {
logger.Error(err)
continue
}
logger.Error(f.Sync())
mapFile.hash = hash
if err = client.SetMapContent(string(name), content); err != nil {
if errors.Is(err, api.ErrMapNotFound) {
instance.Reload("Map file %s created", string(name))
Expand Down

0 comments on commit c336dcf

Please sign in to comment.