mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-21 19:01:49 +00:00
Make writing version.txt more resilient
Writing file first truncate it and writes later on. During disk space pressure it may cause file to become empty. To mitigate above, we create file with new version first and then move it in place of old one (to make sure that disk space is available)
This commit is contained in:
parent
aff056d8a1
commit
d5e1ee4de8
@ -121,6 +121,10 @@ type VersionFile struct {
|
|||||||
path string
|
path string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (v *VersionFile) nextPath() string {
|
||||||
|
return fmt.Sprintf("%s-next", v.path)
|
||||||
|
}
|
||||||
|
|
||||||
// Exists returns true if a version.txt file exists on the file system.
|
// Exists returns true if a version.txt file exists on the file system.
|
||||||
func (v *VersionFile) Exists() (bool, error) {
|
func (v *VersionFile) Exists() (bool, error) {
|
||||||
return exists(v.path)
|
return exists(v.path)
|
||||||
@ -142,8 +146,14 @@ func (v *VersionFile) Read() (*EtcdVersionPair, error) {
|
|||||||
|
|
||||||
// Write creates or overwrites the contents of the version.txt file with the given EtcdVersionPair.
|
// Write creates or overwrites the contents of the version.txt file with the given EtcdVersionPair.
|
||||||
func (v *VersionFile) Write(vp *EtcdVersionPair) error {
|
func (v *VersionFile) Write(vp *EtcdVersionPair) error {
|
||||||
data := []byte(fmt.Sprintf("%s/%s", vp.version, vp.storageVersion))
|
// We do write + rename instead of just write to protect from version.txt
|
||||||
return ioutil.WriteFile(v.path, data, 0666)
|
// corruption under full disk condition.
|
||||||
|
// See https://github.com/kubernetes/kubernetes/issues/98989.
|
||||||
|
err := ioutil.WriteFile(v.nextPath(), []byte(vp.String()), 0666)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to write new version file %s: %v", v.nextPath(), err)
|
||||||
|
}
|
||||||
|
return os.Rename(v.nextPath(), v.path)
|
||||||
}
|
}
|
||||||
|
|
||||||
func exists(path string) (bool, error) {
|
func exists(path string) (bool, error) {
|
||||||
|
Loading…
Reference in New Issue
Block a user