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

Commit

Permalink
Fix missing Name and ControllerHost in status and up commands
Browse files Browse the repository at this point in the history
  • Loading branch information
mumoshu committed Feb 27, 2017
1 parent de22f46 commit cf677a9
Show file tree
Hide file tree
Showing 9 changed files with 216 additions and 93 deletions.
7 changes: 3 additions & 4 deletions cmd/status.go
Expand Up @@ -3,8 +3,7 @@ package cmd
import (
"fmt"

"github.com/coreos/kube-aws/core/controlplane/cluster"
"github.com/coreos/kube-aws/core/controlplane/config"
"github.com/coreos/kube-aws/core/root"
"github.com/spf13/cobra"
)

Expand All @@ -23,12 +22,12 @@ func init() {
}

func runCmdStatus(cmd *cobra.Command, args []string) error {
conf, err := config.ClusterFromFile(configPath)
describer, err := root.ClusterDescriberFromFile(configPath)
if err != nil {
return fmt.Errorf("Failed to read cluster config: %v", err)
}

info, err := cluster.NewClusterRef(conf, false).Info()
info, err := describer.Info()
if err != nil {
return fmt.Errorf("Failed fetching cluster info: %v", err)
}
Expand Down
61 changes: 2 additions & 59 deletions core/controlplane/cluster/cluster.go
Expand Up @@ -6,14 +6,12 @@ import (
"fmt"
"regexp"
"strings"
"text/tabwriter"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"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/elb"
"github.com/aws/aws-sdk-go/service/route53"

"github.com/aws/aws-sdk-go/service/s3"
Expand All @@ -26,23 +24,6 @@ var VERSION = "UNKNOWN"

const STACK_TEMPLATE_FILENAME = "stack.json"

type Info struct {
Name string
ControllerHost string
}

func (c *Info) String() string {
buf := new(bytes.Buffer)
w := new(tabwriter.Writer)
w.Init(buf, 0, 8, 0, '\t', 0)

fmt.Fprintf(w, "Cluster Name:\t%s\n", c.Name)
fmt.Fprintf(w, "Controller DNS Name:\t%s\n", c.ControllerHost)

w.Flush()
return buf.String()
}

func NewClusterRef(cfg *config.Cluster, awsDebug bool) *ClusterRef {
awsConfig := aws.NewConfig().
WithRegion(cfg.Region).
Expand Down Expand Up @@ -360,46 +341,8 @@ func (c *Cluster) Update() (string, error) {
}

func (c *ClusterRef) Info() (*Info, error) {
var elbName string
{
cfSvc := cloudformation.New(c.session)
resp, err := cfSvc.DescribeStackResource(
&cloudformation.DescribeStackResourceInput{
LogicalResourceId: aws.String("ElbAPIServer"),
StackName: aws.String(c.StackName()),
},
)
if err != nil {
errmsg := "unable to get public IP of controller instance:\n" + err.Error()
return nil, fmt.Errorf(errmsg)
}
elbName = *resp.StackResourceDetail.PhysicalResourceId
}

elbSvc := elb.New(c.session)

var info Info
{
resp, err := elbSvc.DescribeLoadBalancers(&elb.DescribeLoadBalancersInput{
LoadBalancerNames: []*string{
aws.String(elbName),
},
PageSize: aws.Int64(2),
})
if err != nil {
return nil, fmt.Errorf("error describing load balancer %s: %v", elbName, err)
}
if len(resp.LoadBalancerDescriptions) == 0 {
return nil, fmt.Errorf("could not find a load balancer with name %s", elbName)
}
if len(resp.LoadBalancerDescriptions) > 1 {
return nil, fmt.Errorf("found multiple load balancers with name %s: %v", elbName, resp)
}

info.Name = c.ClusterName
info.ControllerHost = *resp.LoadBalancerDescriptions[0].DNSName
}
return &info, nil
describer := NewClusterDescriber(c.ClusterName, c.StackName(), c.session)
return describer.Info()
}

func (c *ClusterRef) Destroy() error {
Expand Down
70 changes: 70 additions & 0 deletions core/controlplane/cluster/describer.go
@@ -0,0 +1,70 @@
package cluster

import (
"fmt"
"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/elb"
)

type ClusterDescriber interface {
Info() (*Info, error)
}

type clusterDescriberImpl struct {
session *session.Session
clusterName string
stackName string
}

func NewClusterDescriber(clusterName string, stackName string, session *session.Session) ClusterDescriber {
return clusterDescriberImpl{
clusterName: clusterName,
stackName: stackName,
session: session,
}
}

func (c clusterDescriberImpl) Info() (*Info, error) {
var elbName string
{
cfSvc := cloudformation.New(c.session)
resp, err := cfSvc.DescribeStackResource(
&cloudformation.DescribeStackResourceInput{
LogicalResourceId: aws.String("ElbAPIServer"),
StackName: aws.String(c.stackName),
},
)
if err != nil {
errmsg := "unable to get public IP of controller instance:\n" + err.Error()
return nil, fmt.Errorf(errmsg)
}
elbName = *resp.StackResourceDetail.PhysicalResourceId
}

elbSvc := elb.New(c.session)

var info Info
{
resp, err := elbSvc.DescribeLoadBalancers(&elb.DescribeLoadBalancersInput{
LoadBalancerNames: []*string{
aws.String(elbName),
},
PageSize: aws.Int64(2),
})
if err != nil {
return nil, fmt.Errorf("error describing load balancer %s: %v", elbName, err)
}
if len(resp.LoadBalancerDescriptions) == 0 {
return nil, fmt.Errorf("could not find a load balancer with name %s", elbName)
}
if len(resp.LoadBalancerDescriptions) > 1 {
return nil, fmt.Errorf("found multiple load balancers with name %s: %v", elbName, resp)
}

info.Name = c.clusterName
info.ControllerHost = *resp.LoadBalancerDescriptions[0].DNSName
}
return &info, nil
}
24 changes: 24 additions & 0 deletions core/controlplane/cluster/info.go
@@ -0,0 +1,24 @@
package cluster

import (
"bytes"
"fmt"
"text/tabwriter"
)

type Info struct {
Name string
ControllerHost string
}

func (c *Info) String() string {
buf := new(bytes.Buffer)
w := new(tabwriter.Writer)
w.Init(buf, 0, 8, 0, '\t', 0)

fmt.Fprintf(w, "Cluster Name:\t%s\n", c.Name)
fmt.Fprintf(w, "Controller DNS Name:\t%s\n", c.ControllerHost)

w.Flush()
return buf.String()
}
24 changes: 8 additions & 16 deletions core/root/cluster.go
Expand Up @@ -25,15 +25,6 @@ const (
REMOTE_STACK_TEMPLATE_FILENAME = "stack.json"
)

type Info struct {
Name string
ControllerHost string
}

func (i *Info) String() string {
return fmt.Sprintf("Name=%s, ControllerHost=%s", i.Name, i.ControllerHost)
}

func (c clusterImpl) Export() error {
assets, err := c.assets()

Expand Down Expand Up @@ -102,10 +93,6 @@ func (c clusterImpl) EstimateCost() ([]string, error) {

}

func (c clusterImpl) Info() (*Info, error) {
return &Info{}, nil
}

type Cluster interface {
Create() error
Export() error
Expand All @@ -117,15 +104,15 @@ type Cluster interface {
ValidateUserData() error
}

func ClusterFromFile(configPath string, opts Options, awsDebug bool) (Cluster, error) {
func ClusterFromFile(configPath string, opts options, awsDebug bool) (Cluster, error) {
cfg, err := config.ConfigFromFile(configPath)
if err != nil {
return nil, err
}
return ClusterFromConfig(cfg, opts, awsDebug)
}

func ClusterFromConfig(cfg *config.Config, opts Options, awsDebug bool) (Cluster, error) {
func ClusterFromConfig(cfg *config.Config, opts options, awsDebug bool) (Cluster, error) {
cpOpts := controlplane_cfg.StackTemplateOptions{
TLSAssetsDir: opts.TLSAssetsDir,
ControllerTmplFile: opts.ControllerTmplFile,
Expand Down Expand Up @@ -178,7 +165,7 @@ func ClusterFromConfig(cfg *config.Config, opts Options, awsDebug bool) (Cluster
type clusterImpl struct {
controlPlane *controlplane.Cluster
nodePools []*nodepool.Cluster
opts Options
opts options
session *session.Session
}

Expand All @@ -193,6 +180,11 @@ func (c clusterImpl) Create() error {
return c.stackProvisioner().CreateStackAtURLAndWait(cfSvc, stackTemplateURL)
}

func (c clusterImpl) Info() (*Info, error) {
describer := NewClusterDescriber(c.controlPlane.ClusterName, c.stackName(), c.session)
return describer.Info()
}

func (c clusterImpl) prepareTemplateWithAssets() (string, error) {
assets, err := c.assets()

Expand Down
96 changes: 96 additions & 0 deletions core/root/describer.go
@@ -0,0 +1,96 @@
package root

import (
"fmt"
"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/coreos/kube-aws/core/controlplane/cluster"
"github.com/coreos/kube-aws/core/root/config"
)

type Info struct {
ControlPlane *cluster.Info
}

func (i *Info) String() string {
return i.ControlPlane.String()
}

type ClusterDescriber interface {
Info() (*Info, error)
}

type clusterDescriberImpl struct {
session *session.Session
clusterName string
stackName string
}

func ClusterDescriberFromFile(configPath string) (ClusterDescriber, error) {
config, err := config.ConfigFromFile(configPath)
if err != nil {
return nil, err
}
awsConfig := aws.NewConfig().
WithRegion(config.Region).
WithCredentialsChainVerboseErrors(true)

session, err := session.NewSession(awsConfig)
if err != nil {
return nil, fmt.Errorf("failed to establish aws session: %v", err)
}

return NewClusterDescriber(config.ClusterName, config.ClusterName, session), nil
}

func NewClusterDescriber(clusterName string, stackName string, session *session.Session) ClusterDescriber {
return clusterDescriberImpl{
clusterName: clusterName,
stackName: stackName,
session: session,
}
}

func (c clusterDescriberImpl) Info() (*Info, error) {
cfSvc := cloudformation.New(c.session)

var cpStackName string
{
resp, err := cfSvc.DescribeStackResource(
&cloudformation.DescribeStackResourceInput{
LogicalResourceId: aws.String("Controlplane"),
StackName: aws.String(c.stackName),
},
)
if err != nil {
errmsg := "unable to get nested stack for control-plane:\n" + err.Error()
return nil, fmt.Errorf(errmsg)
}
cpStackName = *resp.StackResourceDetail.PhysicalResourceId
}

var info Info
{
resp, err := cfSvc.DescribeStacks(&cloudformation.DescribeStacksInput{
StackName: aws.String(cpStackName),
})
if err != nil {
return nil, fmt.Errorf("error describing stack %s: %v", cpStackName, err)
}
if len(resp.Stacks) == 0 {
return nil, fmt.Errorf("could not find a stack with name %s", cpStackName)
}
if len(resp.Stacks) > 1 {
return nil, fmt.Errorf("found multiple load balancers with name %s: %v", cpStackName, resp)
}

cpDescriber := cluster.NewClusterDescriber(c.clusterName, cpStackName, c.session)

cpInfo, err := cpDescriber.Info()

info.ControlPlane = cpInfo
}

return &info, nil
}
6 changes: 3 additions & 3 deletions core/root/options.go
Expand Up @@ -2,7 +2,7 @@ package root

import "github.com/coreos/kube-aws/core/root/defaults"

type Options struct {
type options struct {
TLSAssetsDir string
ControllerTmplFile string
WorkerTmplFile string
Expand All @@ -15,8 +15,8 @@ type Options struct {
PrettyPrint bool
}

func NewOptions(s3URI string, prettyPrint bool, skipWait bool) Options {
return Options{
func NewOptions(s3URI string, prettyPrint bool, skipWait bool) options {
return options{
TLSAssetsDir: defaults.TLSAssetsDir,
ControllerTmplFile: defaults.ControllerTmplFile,
WorkerTmplFile: defaults.WorkerTmplFile,
Expand Down

0 comments on commit cf677a9

Please sign in to comment.