Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions pkg/cmd/rev/rev.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,24 @@ func trySaveRevision(ctx context.Context, endpoints []string, outputFile string,
}

tmpPath := fmt.Sprintf("%s.tmp", outputFile)
file, err := os.OpenFile(tmpPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
file, err := os.OpenFile(tmpPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC|os.O_SYNC, 0644)
if err != nil {
klog.Errorf("error opening file: %v", err)
return
}
defer func() {
if err = file.Close(); err != nil {
klog.Errorf("error closing file: %v", err)
if closeErr := file.Close(); closeErr != nil {
klog.Errorf("error closing file: %v", closeErr)
return
}
// Only rename if no error occurred before
if err != nil {
klog.Errorf("could not create temporary revision.json, keeping current version: %v", err)
return
}
if err = os.Rename(tmpPath, outputFile); err != nil {
klog.Errorf("error during rename to destination file: %v", err)
return
}
}()

Expand All @@ -205,11 +215,6 @@ func trySaveRevision(ctx context.Context, endpoints []string, outputFile string,
klog.Errorf("error writing result to file: %v", err)
return
}

if err = os.Rename(tmpPath, outputFile); err != nil {
klog.Errorf("error during rename to destination file: %v", err)
return
}
}

func newETCD3Client(ctx context.Context, endpoints []string, tlsInfo transport.TLSInfo) (*clientv3.Client, error) {
Expand Down