/
ecs_service_state_getter.go
59 lines (52 loc) · 1.81 KB
/
ecs_service_state_getter.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
package throttler
import (
"context"
"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/ecsiface"
"github.com/pkg/errors"
)
// ECSServiceState represents the state of an AWS ECS
// Service in the context of the throttler app.
type ECSServiceState struct {
// The number of deployments the service has
// going on. The normal number of deployments
// is 1: the currently running version of the service.
// If there are > 1, that probably means a new version
// of the service is being deployed.
NumDeployments int
}
// ElasticsearchStateGetter queries AWS to return status information
// about an ECS service in an ECS cluster that is useful when deciding
// whether to allow scaling up or down of the Elasticsearch cluster.
type ECSServiceStateGetter struct {
client ecsiface.ClientAPI
}
// NewECSServiceStateGetter returns a new ECSServiceStateGetter.
func NewECSServiceStateGetter(cfg aws.Config) *ECSServiceStateGetter {
return &ECSServiceStateGetter{
client: ecs.New(cfg),
}
}
// MockECSServiceStateGetter takes an ECSClient arg and returns a new ECSServiceStateGetter.
func MockECSServiceStateGetter(mockClient ecsiface.ClientAPI) *ECSServiceStateGetter {
return &ECSServiceStateGetter{
client: mockClient,
}
}
// Get returns the state of an ECS service.
func (g *ECSServiceStateGetter) Get(cluster, service string) (*ECSServiceState, error) {
req := g.client.DescribeServicesRequest(&ecs.DescribeServicesInput{
Cluster: aws.String(cluster),
Services: []string{service},
})
resp, err := req.Send(context.Background())
if err != nil {
return nil, errors.Wrap(err, "error DescribeServices "+service)
}
serv := resp.DescribeServicesOutput.Services[0]
state := &ECSServiceState{
NumDeployments: len(serv.Deployments),
}
return state, nil
}