/
make_cluster.go
139 lines (120 loc) · 3.47 KB
/
make_cluster.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
package platform_aws_ecs
import (
"context"
"errors"
"strings"
"time"
"github.com/awlsring/texit/internal/pkg/interfaces"
"github.com/awlsring/texit/internal/pkg/logger"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ecs"
"github.com/aws/aws-sdk-go-v2/service/ecs/types"
)
const (
fargateCapacityProvider = "FARGATE"
clusterName = "texit"
clusterActive = "ACTIVE"
clusterPollBackoff = 10 * time.Second
clusterPollMaxInterval = 10
)
func makeCluster(ctx context.Context, client interfaces.EcsClient) error {
log := logger.FromContext(ctx)
log.Debug().Msg("Checking if ECS cluster exists")
exists, err := checkIfClusterExists(ctx, client)
if err != nil {
log.Error().Err(err).Msg("Failed to check if cluster exists")
return err
}
if exists {
log.Debug().Msg("Cluster exists")
return nil
}
log.Debug().Msg("Cluster does not exist, creating")
err = createNewCluster(ctx, client)
if err != nil {
log.Error().Err(err).Msg("Failed to create cluster")
return err
}
log.Debug().Msg("Polling cluster till it is created")
err = pollClusterTillCreated(ctx, client)
if err != nil {
log.Error().Err(err).Msg("error while polling cluster")
return err
}
log.Debug().Msg("Cluster created")
return nil
}
func createNewCluster(ctx context.Context, client interfaces.EcsClient) error {
log := logger.FromContext(ctx)
log.Debug().Msg("Creating new ECS cluster")
_, err := client.CreateCluster(ctx, &ecs.CreateClusterInput{
CapacityProviders: []string{fargateCapacityProvider},
ClusterName: aws.String(clusterName),
DefaultCapacityProviderStrategy: []types.CapacityProviderStrategyItem{
{
CapacityProvider: aws.String(fargateCapacityProvider),
},
},
Tags: []types.Tag{
{
Key: aws.String("created-by"),
Value: aws.String("texit"),
},
{
Key: aws.String("created-at"),
Value: aws.String(time.Now().Format(time.RFC3339Nano)),
},
},
})
if err != nil {
return err
}
return nil
}
func pollClusterTillCreated(ctx context.Context, client interfaces.EcsClient) error {
log := logger.FromContext(ctx)
log.Debug().Msg("Polling ECS cluster till it is created")
i := 0
for {
log.Debug().Msg("Checking if cluster exists")
resp, err := client.DescribeClusters(ctx, &ecs.DescribeClustersInput{
Clusters: []string{clusterName},
})
if err != nil {
log.Error().Err(err).Msg("Failed to describe ECS clusters")
return err
}
if len(resp.Clusters) == 0 {
log.Error().Msg("Expected cluster not found")
return err
}
if *resp.Clusters[0].Status == clusterActive {
log.Debug().Msg("Cluster is active")
return nil
}
log.Debug().Msg("Cluster does not exist, sleeping for 5 seconds")
time.Sleep(clusterPollBackoff)
i++
if i > clusterPollMaxInterval {
log.Error().Msg("Cluster did not become active in time")
return errors.New("cluster did not become active in time")
}
}
}
func checkIfClusterExists(ctx context.Context, client interfaces.EcsClient) (bool, error) {
log := logger.FromContext(ctx)
log.Debug().Msg("Describing ECS clusters")
resp, err := client.ListClusters(ctx, &ecs.ListClustersInput{})
if err != nil {
log.Error().Err(err).Msg("Failed to describe ECS clusters")
return false, err
}
log.Debug().Msgf("Clusters: %v", resp)
for _, cluster := range resp.ClusterArns {
if strings.Contains(cluster, clusterName) {
return true, nil
}
}
log.Debug().Msgf("Expected cluster with name %s not found", clusterName)
return false, nil
}