Skip to content

Commit

Permalink
More sensible naming of types
Browse files Browse the repository at this point in the history
  • Loading branch information
errordeveloper committed Jun 5, 2018
1 parent 3f70ebb commit d463324
Show file tree
Hide file tree
Showing 8 changed files with 205 additions and 188 deletions.
6 changes: 3 additions & 3 deletions cmd/eksctl/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var (
)

func createClusterCmd() *cobra.Command {
cfg := &eks.Config{}
cfg := &eks.ClusterConfig{}

cmd := &cobra.Command{
Use: "cluster",
Expand Down Expand Up @@ -73,7 +73,7 @@ func createClusterCmd() *cobra.Command {
return cmd
}

func doCreateCluster(cfg *eks.Config) error {
func doCreateCluster(cfg *eks.ClusterConfig) error {
ctl := eks.New(cfg)

if err := ctl.CheckAuth(); err != nil {
Expand All @@ -99,7 +99,7 @@ func doCreateCluster(cfg *eks.Config) error {
{ // core action
taskErr := make(chan error)
// create each of the core cloudformation stacks
ctl.CreateAllStacks(taskErr)
ctl.CreateCluster(taskErr)
// read any errors (it only gets non-nil errors)
for err := range taskErr {
return err
Expand Down
4 changes: 2 additions & 2 deletions cmd/eksctl/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func deleteCmd() *cobra.Command {
}

func deleteClusterCmd() *cobra.Command {
cfg := &eks.Config{}
cfg := &eks.ClusterConfig{}

cmd := &cobra.Command{
Use: "cluster",
Expand All @@ -45,7 +45,7 @@ func deleteClusterCmd() *cobra.Command {
return cmd
}

func doDeleteCluster(cfg *eks.Config) error {
func doDeleteCluster(cfg *eks.ClusterConfig) error {
ctl := eks.New(cfg)

if err := ctl.CheckAuth(); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions cmd/eksctl/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func getCmd() *cobra.Command {
}

func getClusterCmd() *cobra.Command {
cfg := &eks.Config{}
cfg := &eks.ClusterConfig{}

cmd := &cobra.Command{
Use: "cluster",
Expand All @@ -45,7 +45,7 @@ func getClusterCmd() *cobra.Command {
return cmd
}

func doGetCluster(cfg *eks.Config) error {
func doGetCluster(cfg *eks.ClusterConfig) error {
ctl := eks.New(cfg)

if err := ctl.CheckAuth(); err != nil {
Expand Down
153 changes: 153 additions & 0 deletions pkg/eks/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package eks

import (
"os"
"sync"

"github.com/pkg/errors"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/eks"
"github.com/aws/aws-sdk-go/service/sts"

"github.com/kubicorn/kubicorn/pkg/logger"
)

const ClusterNameTag = "eksctl.cluster.k8s.io/v1alpha1/cluster-name"

type ClusterProvider struct {
cfg *ClusterConfig
svc *providerServices
}

type providerServices struct {
cfn *cloudformation.CloudFormation
eks *eks.EKS
ec2 *ec2.EC2
sts *sts.STS
arn string
}

// simple config, to be replaced with Cluster API
type ClusterConfig struct {
Region string
ClusterName string
NodeAMI string
NodeType string
Nodes int
MinNodes int
MaxNodes int

SSHPublicKeyPath string
SSHPublicKey []byte

keyName string
clusterRoleARN string
securityGroup string
subnetsList string
clusterVPC string

nodeInstanceRoleARN string

MasterEndpoint string
CertificateAuthorityData []byte
}

func New(clusterConfig *ClusterConfig) *ClusterProvider {
// we might want to use bits from kops, although right now it seems like too many thing we
// don't want yet
// https://github.com/kubernetes/kops/blob/master/upup/pkg/fi/cloudup/awsup/aws_cloud.go#L179
config := aws.NewConfig()
config = config.WithRegion(clusterConfig.Region)
config = config.WithCredentialsChainVerboseErrors(true)

s := session.Must(session.NewSession(config))

cfn := &ClusterProvider{
cfg: clusterConfig,
svc: &providerServices{
cfn: cloudformation.New(s),
eks: eks.New(s),
ec2: ec2.New(s),
sts: sts.New(s),
},
}

// override sessions if any custom endpoints specified
if endpoint, ok := os.LookupEnv("AWS_CLOUDFORMATION_ENDPOINT"); ok {
s := session.Must(session.NewSession(config.WithEndpoint(endpoint)))
cfn.svc.cfn = cloudformation.New(s)
}
if endpoint, ok := os.LookupEnv("AWS_EKS_ENDPOINT"); ok {
s := session.Must(session.NewSession(config.WithEndpoint(endpoint)))
cfn.svc.eks = eks.New(s)
}
if endpoint, ok := os.LookupEnv("AWS_EC2_ENDPOINT"); ok {
s := session.Must(session.NewSession(config.WithEndpoint(endpoint)))
cfn.svc.ec2 = ec2.New(s)
}
if endpoint, ok := os.LookupEnv("AWS_STS_ENDPOINT"); ok {
s := session.Must(session.NewSession(config.WithEndpoint(endpoint)))
cfn.svc.sts = sts.New(s)
}

return cfn
}

func (c *ClusterProvider) CheckAuth() error {
{
input := &sts.GetCallerIdentityInput{}
output, err := c.svc.sts.GetCallerIdentity(input)
if err != nil {
return errors.Wrap(err, "checking AWS STS access – cannot get role ARN for current session")
}
c.svc.arn = *output.Arn
logger.Debug("role ARN for the current session is %q", c.svc.arn)
}
{
input := &cloudformation.ListStacksInput{}
if _, err := c.svc.cfn.ListStacks(input); err != nil {
return errors.Wrap(err, "checking AWS CloudFormation access – cannot list stacks")
}
}
return nil
}

func (c *ClusterProvider) runCreateTask(tasks map[string]func(chan error) error, taskErrs chan error) {
wg := &sync.WaitGroup{}
wg.Add(len(tasks))
for taskName := range tasks {
task := tasks[taskName]
go func(tn string) {
defer wg.Done()
logger.Debug("task %q started", tn)
errs := make(chan error)
if err := task(errs); err != nil {
taskErrs <- err
return
}
if err := <-errs; err != nil {
taskErrs <- err
return
}
logger.Debug("task %q returned without errors", tn)
}(taskName)
}
logger.Debug("waiting for %d tasks to complete", len(tasks))
wg.Wait()
}

func (c *ClusterProvider) CreateCluster(taskErrs chan error) {
c.runCreateTask(map[string]func(chan error) error{
"createStackServiceRole": func(errs chan error) error { return c.createStackServiceRole(errs) },
"createStackVPC": func(errs chan error) error { return c.createStackVPC(errs) },
}, taskErrs)
c.runCreateTask(map[string]func(chan error) error{
"createControlPlane": func(errs chan error) error { return c.createControlPlane(errs) },
"createStackDefaultNodeGroup": func(errs chan error) error { return c.createStackDefaultNodeGroup(errs) },
}, taskErrs)
close(taskErrs)
}
20 changes: 10 additions & 10 deletions pkg/eks/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"k8s.io/kops/upup/pkg/fi/utils"
)

func (c *CloudFormation) LoadSSHPublicKey() error {
func (c *ClusterProvider) LoadSSHPublicKey() error {
c.cfg.SSHPublicKeyPath = utils.ExpandPath(c.cfg.SSHPublicKeyPath)
sshPublicKey, err := ioutil.ReadFile(c.cfg.SSHPublicKeyPath)
if err != nil {
Expand All @@ -32,7 +32,7 @@ func (c *CloudFormation) LoadSSHPublicKey() error {
input := &ec2.DescribeKeyPairsInput{
KeyNames: aws.StringSlice([]string{c.cfg.SSHPublicKeyPath}),
}
output, err := c.ec2.DescribeKeyPairs(input)
output, err := c.svc.ec2.DescribeKeyPairs(input)
if err != nil {
return errors.Wrap(err, "cannot find EC2 key pair")
}
Expand All @@ -54,35 +54,35 @@ func (c *CloudFormation) LoadSSHPublicKey() error {
PublicKeyMaterial: c.cfg.SSHPublicKey,
}
logger.Info("importing SSH public key %q as %q", c.cfg.SSHPublicKeyPath, c.cfg.keyName)
if _, err := c.ec2.ImportKeyPair(input); err != nil {
if _, err := c.svc.ec2.ImportKeyPair(input); err != nil {
return errors.Wrap(err, "importing SSH public key")
}
}
return nil
}

func (c *CloudFormation) MaybeDeletePublicSSHKey() {
func (c *ClusterProvider) MaybeDeletePublicSSHKey() {
input := &ec2.DeleteKeyPairInput{
KeyName: aws.String("EKS-" + c.cfg.ClusterName),
}
c.ec2.DeleteKeyPair(input)
c.svc.ec2.DeleteKeyPair(input)
}

func (c *CloudFormation) getUsername() string {
usernameParts := strings.Split(c.arn, "/")
func (c *ClusterProvider) getUsername() string {
usernameParts := strings.Split(c.svc.arn, "/")
username := usernameParts[len(usernameParts)-1]
return username
}

type ClientConfig struct {
Client *clientcmdapi.Config
Cluster *Config
Cluster *ClusterConfig
roleARN string
}

// based on "k8s.io/kubernetes/cmd/kubeadm/app/util/kubeconfig"
// these are small, so we can copy these, and no need to deal with k/k as dependency
func (c *CloudFormation) NewClientConfig() (*ClientConfig, error) {
func (c *ClusterProvider) NewClientConfig() (*ClientConfig, error) {
clusterName := fmt.Sprintf("%s.%s.eksctl.io", c.cfg.ClusterName, c.cfg.Region)
contextName := fmt.Sprintf("%s@%s", c.getUsername(), clusterName)

Expand All @@ -106,7 +106,7 @@ func (c *CloudFormation) NewClientConfig() (*ClientConfig, error) {
},
CurrentContext: contextName,
},
roleARN: c.arn,
roleARN: c.svc.arn,
}

return clientConfig, nil
Expand Down
Loading

0 comments on commit d463324

Please sign in to comment.