Skip to content

Commit

Permalink
diagd: atomically write the ADS config
Browse files Browse the repository at this point in the history
Write the generated ADS config to a temporary file first, then rename
the file. This ensure that the update is atomic and that ambex won't
load a partially written file.

Fix emissary-ingress#5093

Signed-off-by: Thibault Deutsch <[email protected]>
  • Loading branch information
dethi committed Jul 25, 2023
1 parent faf3052 commit 02cf93f
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions python/ambassador_diag/diagd.py
Original file line number Diff line number Diff line change
Expand Up @@ -1771,8 +1771,19 @@ def _load_ir(
with open(app.bootstrap_path, "w") as output:
output.write(dump_json(bootstrap_config, pretty=True))

with open(app.ads_path, "w") as output:
output.write(dump_json(ads_config, pretty=True))
# Write the ADS config to a temporary file first and then rename to
# ensure atomic update. Otherwise, ambex could be reading the config
# while we are still writing it.
#
# Rename are only atomic if in the same FS. To ensure that this work
# with any setup, we store the temporary file at the same path but with
# an extension that is ignored by ambex.
ads_tmp_path = app.ads_path + ".tmp"
with open(ads_tmp_path, "w") as output:
# Don't use pretty here, to reduce the config size, and improve the
# dump and load speed of the config.
output.write(dump_json(ads_config, pretty=False))
os.rename(ads_tmp_path, app.ads_path)

with open(app.clustermap_path, "w") as output:
output.write(dump_json(clustermap, pretty=True))
Expand Down

0 comments on commit 02cf93f

Please sign in to comment.