cloudinit.GenerateLocalData: defer removal of temp files
We should defer the removal of a temporary file right after we create
it. This ensures that the file would be removed if the function exists
early.
@@ -530,16 +530,20 @@ func GenerateLocalData(vmiName string, namespace string, data *CloudInitData) er
if err != nil {
return err
}
+ defer os.Remove(userFile)
+
err = ioutil.WriteFile(metaFile, metaData, 0644)
if err != nil {
return err
}
+ defer os.Remove(metaFile)
if len(networkData) > 0 {
err = ioutil.WriteFile(networkFile, networkData, 0644)
if err != nil {
return err
}
+ defer os.Remove(networkFile)
}
switch data.DataSource {
@@ -551,9 +555,6 @@ func GenerateLocalData(vmiName string, namespace string, data *CloudInitData) er
if err != nil {
return err
}
- diskutils.RemoveFile(metaFile)
- diskutils.RemoveFile(userFile)
- diskutils.RemoveFile(networkFile)
if err := diskutils.DefaultOwnershipManager.SetFileOwnership(isoStaging); err != nil {
return err
