-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
eks.go
166 lines (142 loc) · 4.11 KB
/
eks.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package eks
import (
"fmt"
"strings"
"time"
"github.com/pkg/errors"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/eks"
"github.com/kubicorn/kubicorn/pkg/logger"
)
func (c *ClusterProvider) CreateControlPlane() error {
input := &eks.CreateClusterInput{
ClusterName: &c.cfg.ClusterName,
RoleArn: &c.cfg.clusterRoleARN,
Subnets: aws.StringSlice(strings.Split(c.cfg.subnetsList, ",")),
SecurityGroups: aws.StringSlice([]string{c.cfg.securityGroup}),
// TODO(p1): find out why there are not tags
}
output, err := c.svc.eks.CreateCluster(input)
if err != nil {
return errors.Wrap(err, "unable to create cluster control plane")
}
logger.Debug("output = %#v", output)
return nil
}
func (c *ClusterProvider) DescribeControlPlane() (*eks.Cluster, error) {
input := &eks.DescribeClusterInput{
ClusterName: &c.cfg.ClusterName,
}
output, err := c.svc.eks.DescribeCluster(input)
if err != nil {
return nil, errors.Wrap(err, "unable to describe cluster control plane")
}
return output.Cluster, nil
}
func (c *ClusterProvider) DeleteControlPlane() error {
cluster, err := c.DescribeControlPlane()
if err != nil {
return errors.Wrap(err, "not able to get control plane for deletion")
}
input := &eks.DeleteClusterInput{
ClusterName: cluster.ClusterName,
}
if _, err := c.svc.eks.DeleteCluster(input); err != nil {
return errors.Wrap(err, "unable to delete cluster control plane")
}
return nil
}
func (c *ClusterProvider) createControlPlane(errs chan error) error {
logger.Info("creating control plane %q", c.cfg.ClusterName)
clusterChan := make(chan eks.Cluster)
taskErrs := make(chan error)
if err := c.CreateControlPlane(); err != nil {
return err
}
go func() {
ticker := time.NewTicker(20 * time.Second)
defer ticker.Stop()
defer close(taskErrs)
defer close(clusterChan)
for {
select {
// TODO(p1): timeout
case <-ticker.C:
cluster, err := c.DescribeControlPlane()
if err != nil {
logger.Warning("continue despite err=%q", err.Error())
continue
}
logger.Debug("cluster = %#v", cluster)
switch *cluster.Status {
case eks.ClusterStatusProvisioning:
continue
case eks.ClusterStatusActive:
taskErrs <- nil
clusterChan <- *cluster
return
default:
taskErrs <- fmt.Errorf("creating control plane: %s", *cluster.Status)
return
}
}
}
}()
go func() {
defer close(errs)
if err := <-taskErrs; err != nil {
errs <- err
return
}
cluster := <-clusterChan
logger.Debug("created control plane – processing outputs")
c.cfg.MasterEndpoint = *cluster.MasterEndpoint
c.cfg.CertificateAuthorityData = []byte(*cluster.CertificateAuthority.Data)
logger.Debug("clusterConfig = %#v", c.cfg)
logger.Success("created control plane %q", c.cfg.ClusterName)
errs <- nil
}()
return nil
}
func (c *ClusterProvider) ListClusters() error {
if c.cfg.ClusterName != "" {
return c.doListCluster(&c.cfg.ClusterName)
}
// TODO(p1): collect results into a data structure (or at least a nicely formatted string)
// TODO(p2): paging
input := &eks.ListClustersInput{}
output, err := c.svc.eks.ListClusters(input)
if err != nil {
return errors.Wrap(err, "listing control planes")
}
for _, clusterName := range output.Clusters {
if err := c.doListCluster(clusterName); err != nil {
return err
}
}
return nil
}
func (c *ClusterProvider) doListCluster(clusterName *string) error {
input := &eks.DescribeClusterInput{
ClusterName: clusterName,
}
output, err := c.svc.eks.DescribeCluster(input)
if err != nil {
return errors.Wrapf(err, "unable to describe control plane %q", *clusterName)
}
if *output.Cluster.Status == "Ready" {
logger.Info("cluster = %#v", *output.Cluster)
stacks, err := c.ListReadyStacks(fmt.Sprintf("^EKS-%s-.*$", *clusterName))
if err != nil {
return errors.Wrapf(err, "listing CloudFormation stack for %q", *clusterName)
}
for _, s := range stacks {
logger.Info("stack = %#v", *s)
}
}
return nil
}
func (c *ClusterProvider) ListAllTaggedResources() error {
// TODO(p1): need this for showing any half-made clusters and pruning them
return nil
}