-
Notifications
You must be signed in to change notification settings - Fork 594
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Configuration writes should be atomic #1252
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"sync" | ||
|
@@ -64,6 +65,32 @@ | |
return newManager(config, defaultPluginName) | ||
} | ||
|
||
// atomicFileWrite writes our configs atomically to avoid partial reads | ||
func atomicFileWrite(filename string, data []byte, perm os.FileMode) error { | ||
Check warning on line 69 in pkg/server/config/manager.go GitHub Actions / test (1.20.x, ubuntu-latest)
|
||
// Create a temporary file in the same directory as the destination file | ||
dir := filepath.Dir(filename) | ||
tmpFile, err := ioutil.TempFile(dir, ".multus.tmp") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ioutil.TempFile() is deprecated as https://pkg.go.dev/io/ioutil#TempFile so please change that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
if err != nil { | ||
return err | ||
} | ||
tmpFileName := tmpFile.Name() | ||
|
||
// Always remove that temp file... | ||
defer os.Remove(tmpFileName) | ||
|
||
// Write data to the temporary file | ||
if _, err := tmpFile.Write(data); err != nil { | ||
tmpFile.Close() | ||
return err | ||
} | ||
if err := tmpFile.Close(); err != nil { | ||
return err | ||
} | ||
|
||
// Atomically rename the temporary file to the final filename | ||
return os.Rename(tmpFileName, filename) | ||
} | ||
|
||
// overrideCNIVersion overrides cniVersion in cniConfigFile, it should be used only in kind case | ||
func overrideCNIVersion(cniConfigFile string, multusCNIVersion string) error { | ||
path, err := filepath.Abs(cniConfigFile) | ||
|
@@ -87,7 +114,7 @@ | |
return fmt.Errorf("couldn't update cluster network config: %v", err) | ||
} | ||
|
||
err = os.WriteFile(path, configBytes, 0644) | ||
err = atomicFileWrite(path, configBytes, 0644) | ||
if err != nil { | ||
return fmt.Errorf("couldn't update cluster network config: %v", err) | ||
} | ||
|
@@ -268,7 +295,7 @@ | |
} else { | ||
logging.Debugf("Writing Multus CNI configuration @ %s", m.multusConfigFilePath) | ||
} | ||
return m.multusConfigFilePath, os.WriteFile(m.multusConfigFilePath, []byte(config), UserRWPermission) | ||
return m.multusConfigFilePath, atomicFileWrite(m.multusConfigFilePath, []byte(config), UserRWPermission) | ||
} | ||
|
||
func (m *Manager) shouldRegenerateConfig(event fsnotify.Event) bool { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As warning mentiond, perm is not used in the function hence, none changed the files permission to 0644. Could you please change the file permission?