Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
b967f927
Commit
b967f927
authored
Aug 01, 2023
by
Derek Nola
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Replace os.Write with AtomicWrite function
Signed-off-by:
Derek Nola
<
derek.nola@suse.com
>
parent
ced330c6
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
29 additions
and
2 deletions
+29
-2
deps.go
pkg/daemons/control/deps/deps.go
+1
-1
config.go
pkg/secretsencrypt/config.go
+2
-1
file.go
pkg/util/file.go
+26
-0
No files found.
pkg/daemons/control/deps/deps.go
View file @
b967f927
...
@@ -777,7 +777,7 @@ func genEncryptionConfigAndState(controlConfig *config.Control) error {
...
@@ -777,7 +777,7 @@ func genEncryptionConfigAndState(controlConfig *config.Control) error {
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
if
err
:=
os
.
WriteFil
e
(
runtime
.
EncryptionConfig
,
b
,
0600
);
err
!=
nil
{
if
err
:=
util
.
AtomicWrit
e
(
runtime
.
EncryptionConfig
,
b
,
0600
);
err
!=
nil
{
return
err
return
err
}
}
encryptionConfigHash
:=
sha256
.
Sum256
(
b
)
encryptionConfigHash
:=
sha256
.
Sum256
(
b
)
...
...
pkg/secretsencrypt/config.go
View file @
b967f927
...
@@ -8,6 +8,7 @@ import (
...
@@ -8,6 +8,7 @@ import (
"os"
"os"
"github.com/k3s-io/k3s/pkg/daemons/config"
"github.com/k3s-io/k3s/pkg/daemons/config"
"github.com/k3s-io/k3s/pkg/util"
"github.com/k3s-io/k3s/pkg/version"
"github.com/k3s-io/k3s/pkg/version"
corev1
"k8s.io/api/core/v1"
corev1
"k8s.io/api/core/v1"
...
@@ -106,7 +107,7 @@ func WriteEncryptionConfig(runtime *config.ControlRuntime, keys []apiserverconfi
...
@@ -106,7 +107,7 @@ func WriteEncryptionConfig(runtime *config.ControlRuntime, keys []apiserverconfi
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
return
os
.
WriteFil
e
(
runtime
.
EncryptionConfig
,
jsonfile
,
0600
)
return
util
.
AtomicWrit
e
(
runtime
.
EncryptionConfig
,
jsonfile
,
0600
)
}
}
func
GenEncryptionConfigHash
(
runtime
*
config
.
ControlRuntime
)
(
string
,
error
)
{
func
GenEncryptionConfigHash
(
runtime
*
config
.
ControlRuntime
)
(
string
,
error
)
{
...
...
pkg/util/file.go
View file @
b967f927
...
@@ -2,6 +2,7 @@ package util
...
@@ -2,6 +2,7 @@ package util
import
(
import
(
"os"
"os"
"path/filepath"
"strings"
"strings"
"time"
"time"
...
@@ -37,3 +38,28 @@ func ReadFile(path string) (string, error) {
...
@@ -37,3 +38,28 @@ func ReadFile(path string) (string, error) {
return
""
,
errors
.
New
(
"Timeout while trying to read the file"
)
return
""
,
errors
.
New
(
"Timeout while trying to read the file"
)
}
}
// AtomicWrite firsts writes data to a temp file, then renames to the destination file.
// This ensures that the destination file is never partially written.
func
AtomicWrite
(
fileName
string
,
data
[]
byte
,
perm
os
.
FileMode
)
error
{
f
,
err
:=
os
.
CreateTemp
(
filepath
.
Dir
(
fileName
),
filepath
.
Base
(
fileName
)
+
".tmp"
)
if
err
!=
nil
{
return
err
}
tmpName
:=
f
.
Name
()
if
_
,
err
:=
f
.
Write
(
data
);
err
!=
nil
{
f
.
Close
()
return
err
}
if
err
:=
f
.
Chmod
(
perm
);
err
!=
nil
{
return
err
}
if
err
:=
f
.
Sync
();
err
!=
nil
{
return
err
}
if
err
:=
f
.
Close
();
err
!=
nil
{
return
err
}
return
os
.
Rename
(
tmpName
,
fileName
)
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment