Skip to content

Commit 1f3d112

Browse files
authored
Merge pull request #792 from belo4ya/fix/gfd-atomically-file-writing
[gfd] Fixing logic of atomically file writing
2 parents c6b3907 + 442edd4 commit 1f3d112

File tree

11 files changed

+554
-51
lines changed

11 files changed

+554
-51
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
github.com/NVIDIA/go-nvml v0.12.4-0
99
github.com/NVIDIA/nvidia-container-toolkit v1.16.0
1010
github.com/fsnotify/fsnotify v1.7.0
11+
github.com/google/renameio v1.0.1
1112
github.com/google/uuid v1.6.0
1213
github.com/mittwald/go-helm-client v0.12.9
1314
github.com/onsi/ginkgo/v2 v2.19.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0=
162162
github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
163163
github.com/google/pprof v0.0.0-20240424215950-a892ee059fd6 h1:k7nVchz72niMH6YLQNvHSdIE7iqsQxK1P41mySCvssg=
164164
github.com/google/pprof v0.0.0-20240424215950-a892ee059fd6/go.mod h1:kf6iHlnVGwgKolg33glAes7Yg/8iWP8ukqeldJSO7jw=
165+
github.com/google/renameio v1.0.1 h1:Lh/jXZmvZxb0BBeSY5VKEfidcbcbenKjZFzM/q0fSeU=
166+
github.com/google/renameio v1.0.1/go.mod h1:t/HQoYBZSsWSNK35C6CO/TpPLDVWvxOHboWUAweKUpk=
165167
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
166168
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
167169
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=

internal/lm/output.go

Lines changed: 4 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"fmt"
2323
"io"
2424
"os"
25-
"path/filepath"
2625
"strings"
2726

2827
apiequality "k8s.io/apimachinery/pkg/api/equality"
@@ -32,6 +31,8 @@ import (
3231
nfdv1alpha1 "sigs.k8s.io/node-feature-discovery/pkg/apis/nfd/v1alpha1"
3332
nfdclientset "sigs.k8s.io/node-feature-discovery/pkg/generated/clientset/versioned"
3433

34+
"github.com/google/renameio"
35+
3536
spec "github.com/NVIDIA/k8s-device-plugin/api/config/v1"
3637
"github.com/NVIDIA/k8s-device-plugin/internal/flags"
3738
)
@@ -85,8 +86,8 @@ func (path *toFile) Output(labels Labels) error {
8586
if err := output.Output(labels); err != nil {
8687
return fmt.Errorf("error writing labels to buffer: %v", err)
8788
}
88-
err := writeFileAtomically(string(*path), buffer.Bytes(), 0644)
89-
if err != nil {
89+
// write file atomically
90+
if err := renameio.WriteFile(string(*path), buffer.Bytes(), 0644); err != nil {
9091
return fmt.Errorf("error atomically writing file '%s': %w", *path, err)
9192
}
9293
return nil
@@ -102,54 +103,6 @@ func (output *toWriter) Output(labels Labels) error {
102103
return nil
103104
}
104105

105-
func writeFileAtomically(path string, contents []byte, perm os.FileMode) error {
106-
absPath, err := filepath.Abs(path)
107-
if err != nil {
108-
return fmt.Errorf("failed to retrieve absolute path of output file: %v", err)
109-
}
110-
111-
absDir := filepath.Dir(absPath)
112-
tmpDir := filepath.Join(absDir, "gfd-tmp")
113-
114-
err = os.MkdirAll(tmpDir, os.ModePerm)
115-
if err != nil && !os.IsExist(err) {
116-
return fmt.Errorf("failed to create temporary directory: %v", err)
117-
}
118-
defer func() {
119-
if err != nil {
120-
os.RemoveAll(tmpDir)
121-
}
122-
}()
123-
124-
tmpFile, err := os.CreateTemp(tmpDir, "gfd-")
125-
if err != nil {
126-
return fmt.Errorf("fail to create temporary output file: %v", err)
127-
}
128-
defer func() {
129-
if err != nil {
130-
tmpFile.Close()
131-
os.Remove(tmpFile.Name())
132-
}
133-
}()
134-
135-
err = os.WriteFile(tmpFile.Name(), contents, perm)
136-
if err != nil {
137-
return fmt.Errorf("error writing temporary file '%v': %v", tmpFile.Name(), err)
138-
}
139-
140-
err = os.Rename(tmpFile.Name(), path)
141-
if err != nil {
142-
return fmt.Errorf("error moving temporary file to '%v': %v", path, err)
143-
}
144-
145-
err = os.Chmod(path, perm)
146-
if err != nil {
147-
return fmt.Errorf("error setting permissions on '%v': %v", path, err)
148-
}
149-
150-
return nil
151-
}
152-
153106
const nodeFeatureVendorPrefix = "nvidia-features-for"
154107

155108
type nodeFeatureObject struct {

vendor/github.com/google/renameio/.golangci.yml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/google/renameio/CONTRIBUTING.md

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/google/renameio/LICENSE

Lines changed: 202 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/google/renameio/README.md

Lines changed: 61 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)