diff --git a/pkg/haproxy/maps/main.go b/pkg/haproxy/maps/main.go index 8496ee1d..d19de9b5 100644 --- a/pkg/haproxy/maps/main.go +++ b/pkg/haproxy/maps/main.go @@ -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" @@ -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) @@ -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))