-
Notifications
You must be signed in to change notification settings - Fork 30
/
rule_getter.go
141 lines (128 loc) · 3.91 KB
/
rule_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
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
package ecschedule
import (
"context"
"encoding/json"
"strings"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/cloudwatchevents"
cweTypes "github.com/aws/aws-sdk-go-v2/service/cloudwatchevents/types"
ecsTypes "github.com/aws/aws-sdk-go-v2/service/ecs/types"
)
type ruleGetter struct {
svc *cloudwatchevents.Client
clusterArn, ruleArnPrefix, taskDefArnPrefix, roleArnPrefix, roleArn, sqsArnPrefix string
}
func (rg *ruleGetter) getRule(ctx context.Context, r *cweTypes.Rule) (*Rule, error) {
if !strings.HasPrefix(*r.Arn, rg.ruleArnPrefix) {
return nil, nil
}
ta, err := rg.svc.ListTargetsByRule(ctx, &cloudwatchevents.ListTargetsByRuleInput{
Rule: r.Name,
})
if err != nil {
return nil, err
}
var targets []*Target
for _, t := range ta.Targets {
if *t.Arn != rg.clusterArn {
return nil, nil
}
targetID := *t.Id
if targetID == *r.Name {
targetID = ""
}
ecsParams := t.EcsParameters
if ecsParams == nil {
// ignore rule which have some non ecs targets
return nil, nil
}
target := &Target{TargetID: targetID}
if role := *t.RoleArn; role != rg.roleArn {
target.Role = strings.TrimPrefix(role, rg.roleArnPrefix)
}
taskCount := *ecsParams.TaskCount
if taskCount != 1 {
target.TaskCount = taskCount
}
target.TaskDefinition = strings.TrimPrefix(*ecsParams.TaskDefinitionArn, rg.taskDefArnPrefix)
target.Group = aws.ToString(ecsParams.Group)
target.LaunchType = string(ecsParams.LaunchType)
var capacityProviderStrategy []*CapacityProviderStrategyItem
for _, cps := range ecsParams.CapacityProviderStrategy {
capacityProviderStrategy = append(capacityProviderStrategy, &CapacityProviderStrategyItem{
Base: cps.Base,
Weight: cps.Weight,
CapacityProvider: aws.ToString(cps.CapacityProvider),
})
}
target.CapacityProviderStrategy = capacityProviderStrategy
target.PlatformVersion = aws.ToString(ecsParams.PlatformVersion)
target.PropagateTags = aws.String(string(t.EcsParameters.PropagateTags))
if aws.ToString(target.PropagateTags) == "" {
target.PropagateTags = nil
}
if nc := ecsParams.NetworkConfiguration; nc != nil {
target.NetworkConfiguration = &NetworkConfiguration{
AwsVpcConfiguration: &AwsVpcConfiguration{
Subnets: nc.AwsvpcConfiguration.Subnets,
SecurityGroups: nc.AwsvpcConfiguration.SecurityGroups,
AssignPublicIP: string(nc.AwsvpcConfiguration.AssignPublicIp),
},
}
}
taskOv := &ecsTypes.TaskOverride{}
if t.Input != nil {
if err := json.Unmarshal([]byte(*t.Input), taskOv); err != nil {
return nil, err
}
var contOverrides []*ContainerOverride
for _, co := range taskOv.ContainerOverrides {
var cmd []string
for _, c := range co.Command {
cmd = append(cmd, c)
}
env := map[string]string{}
for _, kv := range co.Environment {
env[*kv.Name] = *kv.Value
}
contOverrides = append(contOverrides, &ContainerOverride{
Name: *co.Name,
Command: cmd,
Environment: env,
Cpu: co.Cpu,
Memory: co.Memory,
MemoryReservation: co.MemoryReservation,
})
}
target.ContainerOverrides = contOverrides
}
targets = append(targets, target)
if dlc := t.DeadLetterConfig; dlc != nil {
target.DeadLetterConfig = &DeadLetterConfig{
Sqs: strings.TrimPrefix(*dlc.Arn, rg.sqsArnPrefix),
}
}
}
var desc string
if r.Description != nil {
desc = *r.Description
}
ru := &Rule{
Name: *r.Name,
Description: desc,
ScheduleExpression: *r.ScheduleExpression,
Disabled: string(r.State) == "DISABLED",
}
switch len(targets) {
case 0:
return nil, nil
case 1:
ru.Target = targets[0]
default:
// not supported multiple target yet
return nil, nil
// ru.Target = targets[0]
// ru.Targets = targets[1:]
}
return ru, nil
}