2019-10-27 05:53:25 +00:00
|
|
|
package cluster
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-11-11 22:18:26 +00:00
|
|
|
"strings"
|
2019-10-27 05:53:25 +00:00
|
|
|
|
2019-12-16 18:44:13 +00:00
|
|
|
"github.com/pkg/errors"
|
2019-10-27 05:53:25 +00:00
|
|
|
"github.com/rancher/k3s/pkg/clientaccess"
|
2020-05-05 21:59:15 +00:00
|
|
|
"github.com/rancher/k3s/pkg/cluster/managed"
|
2019-10-27 05:53:25 +00:00
|
|
|
"github.com/rancher/k3s/pkg/daemons/config"
|
2019-11-11 22:18:26 +00:00
|
|
|
"github.com/rancher/kine/pkg/client"
|
|
|
|
"github.com/rancher/kine/pkg/endpoint"
|
2019-10-27 05:53:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Cluster struct {
|
|
|
|
clientAccessInfo *clientaccess.Info
|
|
|
|
config *config.Control
|
|
|
|
runtime *config.ControlRuntime
|
2020-05-05 21:59:15 +00:00
|
|
|
managedDB managed.Driver
|
|
|
|
shouldBootstrap bool
|
2019-11-11 22:18:26 +00:00
|
|
|
storageStarted bool
|
|
|
|
etcdConfig endpoint.ETCDConfig
|
|
|
|
joining bool
|
|
|
|
saveBootstrap bool
|
|
|
|
storageClient client.Client
|
2019-10-27 05:53:25 +00:00
|
|
|
}
|
|
|
|
|
2020-05-05 21:59:15 +00:00
|
|
|
func (c *Cluster) Start(ctx context.Context) (<-chan struct{}, error) {
|
|
|
|
if err := c.initClusterAndHTTPS(ctx); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "start cluster and https")
|
2019-10-27 05:53:25 +00:00
|
|
|
}
|
|
|
|
|
2020-05-05 21:59:15 +00:00
|
|
|
if err := c.start(ctx); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "start cluster and https")
|
2019-10-27 05:53:25 +00:00
|
|
|
}
|
|
|
|
|
2020-05-05 21:59:15 +00:00
|
|
|
ready, err := c.testClusterDB(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-10-27 05:53:25 +00:00
|
|
|
}
|
|
|
|
|
2019-11-11 22:18:26 +00:00
|
|
|
if c.saveBootstrap {
|
|
|
|
if err := c.save(ctx); err != nil {
|
2020-05-05 21:59:15 +00:00
|
|
|
return nil, err
|
2019-10-27 05:53:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-05 21:59:15 +00:00
|
|
|
if c.shouldBootstrap {
|
|
|
|
if err := c.bootstrapped(); err != nil {
|
|
|
|
return nil, err
|
2019-11-11 22:18:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-05 21:59:15 +00:00
|
|
|
return ready, c.startStorage(ctx)
|
2019-11-11 22:18:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cluster) startStorage(ctx context.Context) error {
|
|
|
|
if c.storageStarted {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
c.storageStarted = true
|
|
|
|
|
2019-11-16 00:12:27 +00:00
|
|
|
etcdConfig, err := endpoint.Listen(ctx, c.config.Datastore)
|
2019-11-11 22:18:26 +00:00
|
|
|
if err != nil {
|
2019-12-16 18:44:13 +00:00
|
|
|
return errors.Wrap(err, "creating storage endpoint")
|
2019-10-31 02:05:40 +00:00
|
|
|
}
|
|
|
|
|
2019-11-11 22:18:26 +00:00
|
|
|
c.etcdConfig = etcdConfig
|
2019-11-16 00:12:27 +00:00
|
|
|
c.config.Datastore.Config = etcdConfig.TLSConfig
|
|
|
|
c.config.Datastore.Endpoint = strings.Join(etcdConfig.Endpoints, ",")
|
2019-11-11 22:18:26 +00:00
|
|
|
c.config.NoLeaderElect = !etcdConfig.LeaderElect
|
|
|
|
return nil
|
2019-10-27 05:53:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func New(config *config.Control) *Cluster {
|
|
|
|
return &Cluster{
|
|
|
|
config: config,
|
|
|
|
runtime: config.Runtime,
|
|
|
|
}
|
|
|
|
}
|