2019-10-27 05:53:25 +00:00
|
|
|
package cluster
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-11-11 22:18:26 +00:00
|
|
|
"context"
|
2019-10-27 05:53:25 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/rancher/k3s/pkg/bootstrap"
|
|
|
|
"github.com/rancher/k3s/pkg/clientaccess"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2019-11-11 22:18:26 +00:00
|
|
|
func (c *Cluster) Join(ctx context.Context) error {
|
|
|
|
runJoin, err := c.shouldJoin()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.runJoin = runJoin
|
|
|
|
|
|
|
|
if runJoin {
|
|
|
|
if err := c.join(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-27 05:53:25 +00:00
|
|
|
func (c *Cluster) shouldJoin() (bool, error) {
|
2019-11-11 22:18:26 +00:00
|
|
|
dqlite := c.dqliteEnabled()
|
|
|
|
if dqlite {
|
|
|
|
c.runtime.HTTPBootstrap = true
|
|
|
|
if c.config.JoinURL == "" {
|
|
|
|
return false, nil
|
|
|
|
}
|
2019-10-27 05:53:25 +00:00
|
|
|
}
|
|
|
|
|
2019-11-11 22:18:26 +00:00
|
|
|
stamp := c.joinStamp()
|
2019-10-27 05:53:25 +00:00
|
|
|
if _, err := os.Stat(stamp); err == nil {
|
2019-11-11 22:18:26 +00:00
|
|
|
logrus.Info("Cluster bootstrap already complete")
|
2019-10-27 05:53:25 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2019-11-11 22:18:26 +00:00
|
|
|
if dqlite && c.config.Token == "" {
|
2019-10-27 05:53:25 +00:00
|
|
|
return false, fmt.Errorf("K3S_TOKEN is required to join a cluster")
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cluster) joined() error {
|
|
|
|
if err := os.MkdirAll(filepath.Dir(c.joinStamp()), 0700); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(c.joinStamp()); err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Create(c.joinStamp())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return f.Close()
|
|
|
|
}
|
|
|
|
|
2019-11-11 22:18:26 +00:00
|
|
|
func (c *Cluster) httpJoin() error {
|
2019-10-27 05:53:25 +00:00
|
|
|
token, err := clientaccess.NormalizeAndValidateTokenForUser(c.config.JoinURL, c.config.Token, "server")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
info, err := clientaccess.ParseAndValidateToken(c.config.JoinURL, token)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.clientAccessInfo = info
|
|
|
|
|
|
|
|
content, err := clientaccess.Get("/v1-k3s/server-bootstrap", info)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return bootstrap.Read(bytes.NewBuffer(content), &c.runtime.ControlRuntimeBootstrap)
|
|
|
|
}
|
|
|
|
|
2019-11-11 22:18:26 +00:00
|
|
|
func (c *Cluster) join(ctx context.Context) error {
|
|
|
|
c.joining = true
|
|
|
|
|
|
|
|
if c.runtime.HTTPBootstrap {
|
|
|
|
return c.httpJoin()
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.storageJoin(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-27 05:53:25 +00:00
|
|
|
func (c *Cluster) joinStamp() string {
|
2019-11-11 22:18:26 +00:00
|
|
|
return filepath.Join(c.config.DataDir, "db/joined-"+keyHash(c.config.Token))
|
2019-10-27 05:53:25 +00:00
|
|
|
}
|