2020-05-05 21:59:15 +00:00
|
|
|
package cluster
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2020-09-24 05:22:15 +00:00
|
|
|
"errors"
|
2020-05-05 21:59:15 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/rancher/k3s/pkg/bootstrap"
|
|
|
|
"github.com/rancher/k3s/pkg/clientaccess"
|
|
|
|
"github.com/rancher/k3s/pkg/version"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2020-09-24 06:29:25 +00:00
|
|
|
// Bootstrap attempts to load a managed database driver, if one has been initialized or should be created/joined.
|
|
|
|
// It then checks to see if the cluster needs to load boostrap data, and if so, loads data into the
|
|
|
|
// ControlRuntimeBoostrap struct, either via HTTP or from the datastore.
|
2020-05-05 21:59:15 +00:00
|
|
|
func (c *Cluster) Bootstrap(ctx context.Context) error {
|
|
|
|
if err := c.assignManagedDriver(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-24 06:43:53 +00:00
|
|
|
shouldBootstrap, err := c.shouldBootstrapLoad(ctx)
|
2020-05-05 21:59:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-24 06:43:53 +00:00
|
|
|
c.shouldBootstrap = shouldBootstrap
|
|
|
|
|
|
|
|
if shouldBootstrap {
|
2020-05-05 21:59:15 +00:00
|
|
|
if err := c.bootstrap(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-24 05:22:15 +00:00
|
|
|
// shouldBootstrapLoad returns true if we need to load ControlRuntimeBootstrap data again.
|
|
|
|
// This is controlled by a stamp file on disk that records successful bootstrap using a hash of the join token.
|
|
|
|
func (c *Cluster) shouldBootstrapLoad(ctx context.Context) (bool, error) {
|
|
|
|
// Non-nil managedDB indicates that the database is either initialized, initializing, or joining
|
2020-05-05 21:59:15 +00:00
|
|
|
if c.managedDB != nil {
|
|
|
|
c.runtime.HTTPBootstrap = true
|
|
|
|
|
2020-09-24 05:22:15 +00:00
|
|
|
isInitialized, err := c.managedDB.IsInitialized(ctx, c.config)
|
2020-05-05 21:59:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2020-09-24 05:22:15 +00:00
|
|
|
if isInitialized {
|
|
|
|
// If the database is initialized we skip bootstrapping; if the user wants to rejoin a
|
|
|
|
// cluster they need to delete the database.
|
|
|
|
logrus.Infof("Managed %s cluster bootstrap already complete and initialized", c.managedDB.EndpointName())
|
2020-09-30 10:37:45 +00:00
|
|
|
// This is a workaround for an issue that can be caused by terminating the cluster bootstrap before
|
|
|
|
// etcd is promoted from learner. Odds are we won't need this info, and we don't want to fail startup
|
|
|
|
// due to failure to retrieve it as this will break cold cluster restart, so we ignore any errors.
|
|
|
|
if c.config.JoinURL != "" && c.config.Token != "" {
|
|
|
|
c.clientAccessInfo, _ = clientaccess.ParseAndValidateTokenForUser(c.config.JoinURL, c.config.Token, "server")
|
|
|
|
}
|
2020-09-24 05:22:15 +00:00
|
|
|
return false, nil
|
|
|
|
} else if c.config.JoinURL == "" {
|
|
|
|
// Not initialized, not joining - must be initializing (cluster-init)
|
|
|
|
logrus.Infof("Managed %s cluster initializing", c.managedDB.EndpointName())
|
|
|
|
return false, nil
|
|
|
|
} else {
|
|
|
|
// Not initialized, but have a Join URL - fail if there's no token; if there is then validate it.
|
|
|
|
if c.config.Token == "" {
|
|
|
|
return false, errors.New(version.ProgramUpper + "_TOKEN is required to join a cluster")
|
|
|
|
}
|
|
|
|
|
2020-09-24 06:47:17 +00:00
|
|
|
// Fail if the token isn't syntactically valid, or if the CA hash on the remote server doesn't match
|
|
|
|
// the hash in the token. The password isn't actually checked until later when actually bootstrapping.
|
|
|
|
info, err := clientaccess.ParseAndValidateTokenForUser(c.config.JoinURL, c.config.Token, "server")
|
2020-09-24 05:22:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
logrus.Infof("Managed %s cluster not yet initialized", c.managedDB.EndpointName())
|
|
|
|
c.clientAccessInfo = info
|
2020-05-05 21:59:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-24 05:22:15 +00:00
|
|
|
// 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.
|
2020-05-05 21:59:15 +00:00
|
|
|
stamp := c.bootstrapStamp()
|
|
|
|
if _, err := os.Stat(stamp); err == nil {
|
|
|
|
logrus.Info("Cluster bootstrap already complete")
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2020-09-24 05:22:15 +00:00
|
|
|
// No errors and no bootstrap stamp, need to bootstrap.
|
2020-05-05 21:59:15 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2020-09-24 06:29:25 +00:00
|
|
|
// bootstrapped touches a file to indicate that bootstrap has been completed.
|
2020-05-05 21:59:15 +00:00
|
|
|
func (c *Cluster) bootstrapped() error {
|
2020-09-24 06:43:53 +00:00
|
|
|
stamp := c.bootstrapStamp()
|
|
|
|
if err := os.MkdirAll(filepath.Dir(stamp), 0700); err != nil {
|
2020-05-05 21:59:15 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-09-24 06:43:53 +00:00
|
|
|
// return if file already exists
|
|
|
|
if _, err := os.Stat(stamp); err == nil {
|
2020-05-05 21:59:15 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-24 06:43:53 +00:00
|
|
|
// otherwise try to create it
|
|
|
|
f, err := os.Create(stamp)
|
2020-05-05 21:59:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return f.Close()
|
|
|
|
}
|
|
|
|
|
2020-09-24 06:29:25 +00:00
|
|
|
// httpBootstrap retrieves bootstrap data (certs and keys, etc) from the remote server via HTTP
|
|
|
|
// and loads it into the ControlRuntimeBootstrap struct. Unlike the storage bootstrap path,
|
|
|
|
// this data does not need to be decrypted since it is generated on-demand by an existing server.
|
2020-05-05 21:59:15 +00:00
|
|
|
func (c *Cluster) httpBootstrap() error {
|
|
|
|
content, err := clientaccess.Get("/v1-"+version.Program+"/server-bootstrap", c.clientAccessInfo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return bootstrap.Read(bytes.NewBuffer(content), &c.runtime.ControlRuntimeBootstrap)
|
|
|
|
}
|
|
|
|
|
2020-09-24 06:29:25 +00:00
|
|
|
// bootstrap performs cluster bootstrapping, either via HTTP (for managed databases) or direct load from datastore.
|
2020-05-05 21:59:15 +00:00
|
|
|
func (c *Cluster) bootstrap(ctx context.Context) error {
|
|
|
|
c.joining = true
|
|
|
|
|
2020-09-24 06:29:25 +00:00
|
|
|
// bootstrap managed database via HTTP
|
2020-05-05 21:59:15 +00:00
|
|
|
if c.runtime.HTTPBootstrap {
|
|
|
|
return c.httpBootstrap()
|
|
|
|
}
|
|
|
|
|
2020-09-24 05:22:15 +00:00
|
|
|
// Bootstrap directly from datastore
|
|
|
|
return c.storageBootstrap(ctx)
|
2020-05-05 21:59:15 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 06:29:25 +00:00
|
|
|
// bootstrapStamp returns the path to a file in datadir/db that is used to record
|
|
|
|
// that a cluster has been joined. The filename is based on a portion of the sha256 hash of the token.
|
|
|
|
// We hash the token value exactly as it is provided by the user, NOT the normalized version.
|
2020-05-05 21:59:15 +00:00
|
|
|
func (c *Cluster) bootstrapStamp() string {
|
|
|
|
return filepath.Join(c.config.DataDir, "db/joined-"+keyHash(c.config.Token))
|
|
|
|
}
|