k3s/pkg/cluster/cluster.go

98 lines
3.0 KiB
Go
Raw Normal View History

package cluster
import (
"context"
"strings"
2019-12-16 18:44:13 +00:00
"github.com/pkg/errors"
"github.com/rancher/k3s/pkg/clientaccess"
2020-05-05 21:59:15 +00:00
"github.com/rancher/k3s/pkg/cluster/managed"
"github.com/rancher/k3s/pkg/daemons/config"
"github.com/rancher/kine/pkg/client"
"github.com/rancher/kine/pkg/endpoint"
)
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
storageStarted bool
etcdConfig endpoint.ETCDConfig
joining bool
saveBootstrap bool
storageClient client.Client
}
// Start creates the dynamic tls listener, http request handler,
// handles starting and writing/reading bootstrap data, and returns a channel
// that will be closed when datastore is ready.
2020-05-05 21:59:15 +00:00
func (c *Cluster) Start(ctx context.Context) (<-chan struct{}, error) {
// Set up the dynamiclistener and http request handlers
2020-05-05 21:59:15 +00:00
if err := c.initClusterAndHTTPS(ctx); err != nil {
return nil, errors.Wrap(err, "init cluster datastore and https")
}
// start managed database (if necessary)
2020-05-05 21:59:15 +00:00
if err := c.start(ctx); err != nil {
return nil, errors.Wrap(err, "start managed database")
}
// get the wait channel for testing managed database readiness
2020-05-05 21:59:15 +00:00
ready, err := c.testClusterDB(ctx)
if err != nil {
return nil, err
}
// if necessary, store bootstrap data to datastore
if c.saveBootstrap {
if err := c.save(ctx); err != nil {
2020-05-05 21:59:15 +00:00
return nil, err
}
}
// if necessary, record successful bootstrap
2020-05-05 21:59:15 +00:00
if c.shouldBootstrap {
if err := c.bootstrapped(); err != nil {
return nil, err
}
}
2020-05-05 21:59:15 +00:00
return ready, c.startStorage(ctx)
}
// startStorage starts the kine listener and configures the endpoints, if necessary.
// This calls into the kine endpoint code, which sets up the database client
// and unix domain socket listener if using an external database. In the case of an etcd
// backend it just returns the user-provided etcd endpoints and tls config.
func (c *Cluster) startStorage(ctx context.Context) error {
if c.storageStarted {
return nil
}
c.storageStarted = true
// start listening on the kine socket as an etcd endpoint, or return the external etcd endpoints
2019-11-16 00:12:27 +00:00
etcdConfig, err := endpoint.Listen(ctx, c.config.Datastore)
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
}
// Persist the returned etcd configuration. We decide if we're doing leader election for embedded controllers
// based on what the kine wrapper tells us about the datastore. Single-node datastores like sqlite don't require
// leader election, while basically all others (etcd, external database, etc) do since they allow multiple servers.
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, ",")
c.config.NoLeaderElect = !etcdConfig.LeaderElect
return nil
}
// New creates an initial cluster using the provided configuration
func New(config *config.Control) *Cluster {
return &Cluster{
config: config,
runtime: config.Runtime,
}
}