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

Closed
Closed
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
31 changes: 29 additions & 2 deletions pkg/server/config/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sync"
Expand Down Expand Up @@ -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

View workflow job for this annotation

GitHub Actions / test (1.20.x, ubuntu-latest)

parameter 'perm' seems to be unused, consider removing or renaming it as _

Check warning on line 69 in pkg/server/config/manager.go

View workflow job for this annotation

GitHub Actions / test (1.21.x, ubuntu-latest)

parameter 'perm' seems to be unused, consider removing or renaming it as _
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?

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

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)
Expand All @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down
Loading