Skip to content
This repository has been archived by the owner on Jun 15, 2021. It is now read-only.

Commit

Permalink
Merge pull request kubernetes-retired#332 from mumoshu/ha-etcd
Browse files Browse the repository at this point in the history
Managed HA etcd cluster
  • Loading branch information
mumoshu authored Mar 1, 2017
2 parents 7133773 + 242b332 commit 0ca2439
Show file tree
Hide file tree
Showing 24 changed files with 1,818 additions and 243 deletions.
2 changes: 1 addition & 1 deletion core/controlplane/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (c *Cluster) Assets() (cfnstack.Assets, error) {

return cfnstack.NewAssetsBuilder(c.StackName(), c.StackConfig.S3URI).
Add("userdata-controller", c.UserDataController).
Add("userdata-worker", c.UserDataWorker).
Add("userdata-etcd", c.UserDataEtcd).
Add(STACK_TEMPLATE_FILENAME, stackTemplate).
Build(), nil
}
Expand Down
53 changes: 25 additions & 28 deletions core/controlplane/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/coreos/kube-aws/coreos/amiregistry"
"github.com/coreos/kube-aws/filereader/userdatatemplate"
"github.com/coreos/kube-aws/model"
"github.com/coreos/kube-aws/model/derived"
"github.com/coreos/kube-aws/netutil"
yaml "gopkg.in/yaml.v2"
)
Expand Down Expand Up @@ -630,32 +631,10 @@ func (c Cluster) Config() (*Config, error) {
config.AMI = c.AmiId
}

config.EtcdInstances = make([]model.EtcdInstance, config.EtcdCount)

for etcdIndex := 0; etcdIndex < config.EtcdCount; etcdIndex++ {

//Round-robin etcd instances across all available subnets
subnetIndex := etcdIndex % len(config.Etcd.Subnets)
subnet := config.Etcd.Subnets[subnetIndex]

var instance model.EtcdInstance

if subnet.ManageNATGateway() {
ngw, err := c.FindNATGatewayForPrivateSubnet(subnet)

if err != nil {
return nil, fmt.Errorf("failed getting a NAT gateway for the subnet %s in %v: %v", subnet.LogicalName(), c.NATGateways(), err)
}

instance = model.NewEtcdInstanceDependsOnNewlyCreatedNGW(subnet, *ngw)
} else {
instance = model.NewEtcdInstance(subnet)
}

config.EtcdInstances[etcdIndex] = instance

//http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-instance-addressing.html#concepts-private-addresses

var err error
config.EtcdNodes, err = derived.NewEtcdNodes(c.Etcd.Nodes, c.EtcdCluster())
if err != nil {
return nil, fmt.Errorf("failed to derived etcd nodes configuration: %v", err)
}

// Populate top-level subnets to model
Expand All @@ -670,6 +649,12 @@ func (c Cluster) Config() (*Config, error) {
return &config, nil
}

func (c *Cluster) EtcdCluster() derived.EtcdCluster {
region := model.RegionForName(c.Region)
etcdNetwork := derived.NewNetwork(c.Etcd.Subnets, c.NATGateways())
return derived.NewEtcdCluster(c.Etcd.Cluster, region, etcdNetwork, c.EtcdCount)
}

// releaseVersionIsGreaterThan will return true if the supplied version is greater then
// or equal to the current CoreOS release indicated by the given release
// channel.
Expand Down Expand Up @@ -731,7 +716,7 @@ func (c Cluster) StackConfig(opts StackTemplateOptions) (*StackConfig, error) {
if stackConfig.UserDataController, err = userdatatemplate.GetString(opts.ControllerTmplFile, stackConfig.Config); err != nil {
return nil, fmt.Errorf("failed to render controller cloud config: %v", err)
}
if stackConfig.userDataEtcd, err = userdatatemplate.GetString(opts.EtcdTmplFile, stackConfig.Config); err != nil {
if stackConfig.UserDataEtcd, err = userdatatemplate.GetString(opts.EtcdTmplFile, stackConfig.Config); err != nil {
return nil, fmt.Errorf("failed to render etcd cloud config: %v", err)
}

Expand All @@ -750,7 +735,7 @@ func (c Cluster) StackConfig(opts StackTemplateOptions) (*StackConfig, error) {
type Config struct {
Cluster

EtcdInstances []model.EtcdInstance
EtcdNodes []derived.EtcdNode

// Encoded TLS assets
TLSConfig *CompactTLSAssets
Expand All @@ -764,6 +749,18 @@ func (c Cluster) StackName() string {
return "control-plane"
}

func (c Cluster) StackNameEnvVarName() string {
return "KUBE_AWS_STACK_NAME"
}

func (c Cluster) EtcdNodeEnvFileName() string {
return "/var/run/coreos/etcd-node.env"
}

func (c Cluster) EtcdIndexEnvVarName() string {
return "KUBE_AWS_ETCD_INDEX"
}

func (c Config) VPCLogicalName() string {
return vpcLogicalName
}
Expand Down
23 changes: 10 additions & 13 deletions core/controlplane/config/stack_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"github.com/coreos/kube-aws/coreos/userdatavalidation"
"github.com/coreos/kube-aws/filereader/jsontemplate"
"github.com/coreos/kube-aws/gzipcompressor"
"net/url"
)

Expand All @@ -13,13 +12,12 @@ type StackConfig struct {
StackTemplateOptions
UserDataWorker string
UserDataController string
userDataEtcd string
UserDataEtcd string
ControllerSubnetIndex int
}

type CompressedStackConfig struct {
*StackConfig
UserDataEtcd string
}

func (c *StackConfig) UserDataControllerS3Path() (string, error) {
Expand All @@ -30,28 +28,27 @@ func (c *StackConfig) UserDataControllerS3Path() (string, error) {
return fmt.Sprintf("%s%s/%s/userdata-controller", s3uri.Host, s3uri.Path, c.StackName()), nil
}

func (c *StackConfig) UserDataEtcdS3Path() (string, error) {
s3uri, err := url.Parse(c.S3URI)
if err != nil {
return "", fmt.Errorf("Error in UserDataEtcdS3Path : %v", err)
}
return fmt.Sprintf("%s%s/%s/userdata-etcd", s3uri.Host, s3uri.Path, c.StackName()), nil
}

func (c *StackConfig) ValidateUserData() error {
err := userdatavalidation.Execute([]userdatavalidation.Entry{
{Name: "UserDataWorker", Content: c.UserDataWorker},
{Name: "UserDataController", Content: c.UserDataController},
{Name: "UserDataEtcd", Content: c.userDataEtcd},
{Name: "UserDataEtcd", Content: c.UserDataEtcd},
})

return err
}

func (c *StackConfig) Compress() (*CompressedStackConfig, error) {
var err error
var compressedEtcdUserData string

if compressedEtcdUserData, err = gzipcompressor.CompressString(c.userDataEtcd); err != nil {
return nil, err
}

var stackConfig CompressedStackConfig
stackConfig.StackConfig = &(*c)
stackConfig.UserDataEtcd = compressedEtcdUserData

return &stackConfig, nil
}

Expand Down
Loading

0 comments on commit 0ca2439

Please sign in to comment.