@@ -189,14 +189,24 @@ func trySaveRevision(ctx context.Context, endpoints []string, outputFile string,
189189	}
190190
191191	tmpPath  :=  fmt .Sprintf ("%s.tmp" , outputFile )
192- 	file , err  :=  os .OpenFile (tmpPath , os .O_CREATE | os .O_WRONLY | os .O_TRUNC , 0644 )
192+ 	file , err  :=  os .OpenFile (tmpPath , os .O_CREATE | os .O_WRONLY | os .O_TRUNC | os . O_SYNC , 0644 )
193193	if  err  !=  nil  {
194194		klog .Errorf ("error opening file: %v" , err )
195195		return 
196196	}
197197	defer  func () {
198- 		if  err  =  file .Close (); err  !=  nil  {
199- 			klog .Errorf ("error closing file: %v" , err )
198+ 		if  closeErr  :=  file .Close (); closeErr  !=  nil  {
199+ 			klog .Errorf ("error closing file: %v" , closeErr )
200+ 			return 
201+ 		}
202+ 		// Only rename if no error occurred before 
203+ 		if  err  !=  nil  {
204+ 			klog .Errorf ("could not create temporary revision.json, keeping current version: %v" , err )
205+ 			return 
206+ 		}
207+ 		if  err  =  os .Rename (tmpPath , outputFile ); err  !=  nil  {
208+ 			klog .Errorf ("error during rename to destination file: %v" , err )
209+ 			return 
200210		}
201211	}()
202212
@@ -205,11 +215,6 @@ func trySaveRevision(ctx context.Context, endpoints []string, outputFile string,
205215		klog .Errorf ("error writing result to file: %v" , err )
206216		return 
207217	}
208- 
209- 	if  err  =  os .Rename (tmpPath , outputFile ); err  !=  nil  {
210- 		klog .Errorf ("error during rename to destination file: %v" , err )
211- 		return 
212- 	}
213218}
214219
215220func  newETCD3Client (ctx  context.Context , endpoints  []string , tlsInfo  transport.TLSInfo ) (* clientv3.Client , error ) {
0 commit comments