k3s/pkg/cluster/cluster.go

81 lines
1.7 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
}
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")
}
2020-05-05 21:59:15 +00:00
if err := c.start(ctx); err != nil {
return nil, errors.Wrap(err, "start cluster and https")
}
2020-05-05 21:59:15 +00:00
ready, err := c.testClusterDB(ctx)
if err != nil {
return nil, err
}
if c.saveBootstrap {
if err := c.save(ctx); err != nil {
2020-05-05 21:59:15 +00:00
return nil, err
}
}
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)
}
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)
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
}
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
}
func New(config *config.Control) *Cluster {
return &Cluster{
config: config,
runtime: config.Runtime,
}
}