Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

dougbtv
Copy link
Member

@dougbtv dougbtv commented Mar 28, 2024

This change introduces a method atomicFileWrite(...) which writes to a temporary file and then renames it to avoid partial reads, e.g.

Mar 29 04:13:05 labkubedualhost-node-1 crio[33593]: time="2024-03-29 04:13:05.977398607+09:00" level=info msg="CNI monitoring event CREATE        \"/etc/cni/net.d/00-multus.conf\""
Mar 29 04:13:05 labkubedualhost-node-1 crio[33593]: time="2024-03-29 04:13:05.977767189+09:00" level=error msg="Error loading CNI config file /etc/cni/net.d/00-multus.conf: error parsing configuration: unexpected end of JSON input"

This change introduces a method `atomicFileWrite(...)` which writes to a temporary file and then renames it to avoid partial reads, e.g.

```
Mar 29 04:13:05 labkubedualhost-node-1 crio[33593]: time="2024-03-29 04:13:05.977398607+09:00" level=info msg="CNI monitoring event CREATE        \"/etc/cni/net.d/00-multus.conf\""
Mar 29 04:13:05 labkubedualhost-node-1 crio[33593]: time="2024-03-29 04:13:05.977767189+09:00" level=error msg="Error loading CNI config file /etc/cni/net.d/00-multus.conf: error parsing configuration: unexpected end of JSON input"
```
@coveralls
Copy link

coveralls commented Mar 28, 2024

Coverage Status

coverage: 62.8% (+0.02%) from 62.778%
when pulling 5d0ff07 on dougbtv:thick-atomic-config-write
into 0fd3fa7 on k8snetworkplumbingwg:master.

@@ -64,6 +65,32 @@ func NewManager(config MultusConf) (*Manager, error) {
return newManager(config, defaultPluginName)
}

// atomicFileWrite writes our configs atomically to avoid partial reads
func atomicFileWrite(filename string, data []byte, perm os.FileMode) error {
Copy link
Member

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?

func atomicFileWrite(filename string, data []byte, perm os.FileMode) error {
// Create a temporary file in the same directory as the destination file
dir := filepath.Dir(filename)
tmpFile, err := ioutil.TempFile(dir, ".multus.tmp")
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants