Skip to content

Commit

Permalink
Make config reloader file writes atomic (#962)
Browse files Browse the repository at this point in the history
* Make config reloader file writes atomic

This addresses an issue found in the Prometheus Operator, which reuses
this reloader sidecar, but which then also has a second sidecar which
may trigger rule-based reloads while the config sidecar is in the middle
of writing out its config (in a non-atomic way):

prometheus-operator/prometheus-operator#2501

I didn't add a test for this because it's hard to catch the original
problem to begin with, but it has happened.

Signed-off-by: Julius Volz <[email protected]>

* Explicitly ignore os.Remove() error

Signed-off-by: Julius Volz <[email protected]>
  • Loading branch information
juliusv authored and bwplotka committed Mar 22, 2019
1 parent b777825 commit e756fe1
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion pkg/reloader/reloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,16 @@ func (r *Reloader) apply(ctx context.Context) error {
return errors.Wrap(err, "expand environment variables")
}

if err := ioutil.WriteFile(r.cfgOutputFile, b, 0666); err != nil {
tmpFile := r.cfgOutputFile + ".tmp"
defer func() {
_ = os.Remove(tmpFile)
}()
if err := ioutil.WriteFile(tmpFile, b, 0666); err != nil {
return errors.Wrap(err, "write file")
}
if err := os.Rename(tmpFile, r.cfgOutputFile); err != nil {
return errors.Wrap(err, "rename file")
}
}
}

Expand Down

0 comments on commit e756fe1

Please sign in to comment.