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
|
|
|
|
|
|
|
"github.com/rancher/k3s/pkg/clientaccess"
|
|
|
|
"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
|
|
|
|
db interface{}
|
2019-11-11 22:18:26 +00:00
|
|
|
runJoin bool
|
|
|
|
storageStarted bool
|
|
|
|
etcdConfig endpoint.ETCDConfig
|
|
|
|
joining bool
|
|
|
|
saveBootstrap bool
|
|
|
|
storageClient client.Client
|
2019-10-27 05:53:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cluster) Start(ctx context.Context) error {
|
2019-11-11 22:18:26 +00:00
|
|
|
if err := c.startClusterAndHTTPS(ctx); err != nil {
|
2019-10-27 05:53:25 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-11-11 22:18:26 +00:00
|
|
|
if c.runJoin {
|
|
|
|
if err := c.postJoin(ctx); err != nil {
|
2019-10-27 05:53:25 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-11 22:18:26 +00:00
|
|
|
if err := c.testClusterDB(ctx); err != nil {
|
2019-10-27 05:53:25 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-11-11 22:18:26 +00:00
|
|
|
if c.saveBootstrap {
|
|
|
|
if err := c.save(ctx); err != nil {
|
2019-10-27 05:53:25 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-11 22:18:26 +00:00
|
|
|
if c.runJoin {
|
|
|
|
if err := c.joined(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.startStorage(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cluster) startStorage(ctx context.Context) error {
|
|
|
|
if c.storageStarted {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
c.storageStarted = true
|
|
|
|
|
|
|
|
etcdConfig, err := endpoint.Listen(ctx, c.config.Storage)
|
|
|
|
if err != nil {
|
2019-10-31 02:05:40 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-11-11 22:18:26 +00:00
|
|
|
c.etcdConfig = etcdConfig
|
|
|
|
c.config.Storage.Config = etcdConfig.TLSConfig
|
|
|
|
c.config.Storage.Endpoint = strings.Join(etcdConfig.Endpoints, ",")
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|