Skip to content

Commit

Permalink
Merge pull request kubernetes#106658 from wpedrak/even-more-resilient…
Browse files Browse the repository at this point in the history
…-version.txt

Even more resilient version.txt
  • Loading branch information
k8s-ci-robot authored Dec 9, 2021
2 parents f98f27b + 183793c commit e3c83c6
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion cluster/images/etcd/migrate/data_dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,36 @@ func (v *VersionFile) Read() (*EtcdVersionPair, error) {
return vp, nil
}

// equals returns true iff VersionFile exists and contains given EtcdVersionPair.
func (v *VersionFile) equals(vp *EtcdVersionPair) (bool, error) {
exists, err := v.Exists()
if err != nil {
return false, err
}
if !exists {
return false, nil
}
cvp, err := v.Read()
if err != nil {
return false, err
}
return vp.Equals(cvp), nil
}

// Write creates or overwrites the contents of the version.txt file with the given EtcdVersionPair.
func (v *VersionFile) Write(vp *EtcdVersionPair) error {
// We do write only if file content differs from given EtcdVersionPair.
isUpToDate, err := v.equals(vp)
if err != nil {
return fmt.Errorf("failed to to check if version file %s should be changed: %v", v.path, err)
}
if isUpToDate {
return nil
}
// We do write + rename instead of just write to protect from version.txt
// corruption under full disk condition.
// See https://github.com/kubernetes/kubernetes/issues/98989.
err := ioutil.WriteFile(v.nextPath(), []byte(vp.String()), 0666)
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)
}
Expand Down

0 comments on commit e3c83c6

Please sign in to comment.