Skip to content

Commit

Permalink
Resolve restore bootstrap (k3s-io#4704)
Browse files Browse the repository at this point in the history
  • Loading branch information
briandowns authored Dec 9, 2021
1 parent a70487d commit a6fe2c0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
5 changes: 4 additions & 1 deletion pkg/bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,12 @@ func WriteToDiskFromStorage(r io.Reader, bootstrap *config.ControlRuntimeBootstr
if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil {
return errors.Wrapf(err, "failed to mkdir %s", filepath.Dir(path))
}
if err := ioutil.WriteFile(path, bsf.Content, 0600); err != nil {
if err := os.WriteFile(path, bsf.Content, 0600); err != nil {
return errors.Wrapf(err, "failed to write to %s", path)
}
if err := os.Chtimes(path, bsf.Timestamp, bsf.Timestamp); err != nil {
return errors.Wrapf(err, "failed to update modified time on %s", path)
}
}

return nil
Expand Down
32 changes: 22 additions & 10 deletions pkg/cluster/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ import (
"path"
"path/filepath"
"reflect"
"strconv"
"strings"
"time"

"github.com/k3s-io/kine/pkg/client"
"github.com/k3s-io/kine/pkg/endpoint"
"github.com/otiai10/copy"
"github.com/rancher/k3s/pkg/bootstrap"
"github.com/rancher/k3s/pkg/clientaccess"
"github.com/rancher/k3s/pkg/daemons/config"
Expand Down Expand Up @@ -225,15 +227,6 @@ func (c *Cluster) shouldBootstrapLoad(ctx context.Context) (bool, bool, error) {
}
}

// Check the stamp file to see if we have successfully bootstrapped using this token.
// NOTE: The fact that we use a hash of the token to generate the stamp
// means that it is unsafe to use the same token for multiple clusters.
// stamp := c.bootstrapStamp()
// if _, err := os.Stat(stamp); err == nil {
// logrus.Info("Cluster bootstrap already complete")
// return false, nil
// }

// No errors and no bootstrap stamp, need to bootstrap.
return true, false, nil
}
Expand Down Expand Up @@ -517,12 +510,31 @@ func (c *Cluster) ReconcileBootstrapData(ctx context.Context, buf io.ReadSeeker,
}
}

if c.config.ClusterReset {
serverTLSDir := filepath.Join(c.config.DataDir, "tls")
tlsBackupDir := filepath.Join(c.config.DataDir, "tls-"+strconv.Itoa(int(time.Now().Unix())))

logrus.Infof("Cluster reset: backing up certificates directory to " + tlsBackupDir)

if _, err := os.Stat(serverTLSDir); err != nil {
return err
}
if err := copy.Copy(serverTLSDir, tlsBackupDir); err != nil {
return err
}
}

for path, res := range results {
switch {
case res.disk:
updateDisk = true
logrus.Warn("datastore newer than " + path)
case res.db:
if c.config.ClusterReset {
logrus.Infof("Cluster reset: replacing file on disk: " + path)
updateDisk = true
continue
}
logrus.Fatal(path + " newer than datastore and could cause cluster outage. Remove the file from disk and restart to be recreated from datastore.")
case res.conflict:
logrus.Warnf("datastore / disk conflict: %s newer than in the datastore", path)
Expand Down Expand Up @@ -606,7 +618,7 @@ func (c *Cluster) compareConfig() error {
if !reflect.DeepEqual(clusterControl.CriticalControlArgs, c.config.CriticalControlArgs) {
logrus.Debugf("This is the server CriticalControlArgs: %#v", clusterControl.CriticalControlArgs)
logrus.Debugf("This is the local CriticalControlArgs: %#v", c.config.CriticalControlArgs)
return errors.New("Unable to join cluster due to critical configuration value mismatch")
return errors.New("unable to join cluster due to critical configuration value mismatch")
}
return nil
}
Expand Down
8 changes: 5 additions & 3 deletions pkg/etcd/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,11 @@ func (e *ETCD) Reset(ctx context.Context, rebootstrap func() error) error {
continue
}

// storageBootstrap() - runtime structure has been written with correct certificate data
if err := rebootstrap(); err != nil {
logrus.Fatal(err)
if rebootstrap != nil {
// storageBootstrap() - runtime structure has been written with correct certificate data
if err := rebootstrap(); err != nil {
logrus.Fatal(err)
}
}

// call functions to rewrite them from daemons/control/server.go (prepare())
Expand Down

0 comments on commit a6fe2c0

Please sign in to comment.