forked from FeLvi-zzz/tentez
-
Notifications
You must be signed in to change notification settings - Fork 0
/
awsListenerRule.go
145 lines (122 loc) · 3.65 KB
/
awsListenerRule.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
package tentez
import (
"context"
"fmt"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
elbv2 "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2"
elbv2Types "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2/types"
)
type AwsListenerRule struct {
Name string `yaml:"name"`
Target string `yaml:"target"`
Switch Switch `yaml:"switch"`
}
type AwsListenerRules []AwsListenerRule
type AwsListenerRuleData struct {
Name string `yaml:"name"`
ListenerRuleArn string `yaml:"target"`
Weights []AwsTargetGroupTuple `yaml:"weights"`
}
func (r AwsListenerRule) execSwitch(targetWeight Weight, isForce bool, cfg Config) error {
// avoid rate limit
time.Sleep(1 * time.Second)
if !isForce {
ruleData, err := cfg.client.elbv2.DescribeRules(context.TODO(), &elbv2.DescribeRulesInput{
RuleArns: []string{r.Target},
})
if err != nil {
return err
}
tgWeight := Weight{}
for _, action := range ruleData.Rules[0].Actions {
for _, tgTuple := range action.ForwardConfig.TargetGroups {
switch *tgTuple.TargetGroupArn {
case r.Switch.Old:
tgWeight.Old = *tgTuple.Weight
case r.Switch.New:
tgWeight.New = *tgTuple.Weight
}
}
}
if tgWeight.CalcOldRatio() < targetWeight.CalcOldRatio() {
return SkipSwitchError{"the old weight target is larger than current one."}
}
if tgWeight.CalcNewRatio() > targetWeight.CalcNewRatio() {
return SkipSwitchError{"the new weight target is smaller than current one."}
}
}
_, err := cfg.client.elbv2.ModifyRule(context.TODO(), &elbv2.ModifyRuleInput{
RuleArn: aws.String(r.Target),
Actions: []elbv2Types.Action{
{
Type: "forward",
ForwardConfig: &elbv2Types.ForwardActionConfig{
TargetGroups: []elbv2Types.TargetGroupTuple{
{
TargetGroupArn: aws.String(r.Switch.Old),
Weight: aws.Int32(targetWeight.Old),
},
{
TargetGroupArn: aws.String(r.Switch.New),
Weight: aws.Int32(targetWeight.New),
},
},
},
},
},
})
return err
}
func (r AwsListenerRule) getName() string {
return r.Name
}
func (rs AwsListenerRules) fetchData(cfg Config) (TargetsData, error) {
if len(rs) == 0 {
return nil, nil
}
ruleArns := []string{}
tgArns := []string{}
ruleMap := map[string]AwsListenerRule{}
for _, rule := range rs {
ruleArns = append(ruleArns, rule.Target)
tgArns = append(tgArns, rule.Switch.New, rule.Switch.Old)
ruleMap[rule.Target] = rule
}
if _, err := cfg.client.elbv2.DescribeTargetGroups(context.TODO(), &elbv2.DescribeTargetGroupsInput{
TargetGroupArns: tgArns,
}); err != nil {
fmt.Fprintln(cfg.io.err, err.Error())
}
rulesData, err := cfg.client.elbv2.DescribeRules(context.TODO(), &elbv2.DescribeRulesInput{
RuleArns: ruleArns,
})
if err != nil {
return nil, err
}
res := []AwsListenerRuleData{}
for _, ruleData := range rulesData.Rules {
for _, action := range ruleData.Actions {
targetGroupTuples := []AwsTargetGroupTuple{}
for _, tgTuple := range action.ForwardConfig.TargetGroups {
targetGroupTuples = append(targetGroupTuples, AwsTargetGroupTuple{
Type: ruleMap[*ruleData.RuleArn].Switch.getType(*tgTuple.TargetGroupArn),
TargetGroupArn: aws.ToString(tgTuple.TargetGroupArn),
Weight: aws.ToInt32(tgTuple.Weight),
})
}
res = append(res, AwsListenerRuleData{
Name: ruleMap[*ruleData.RuleArn].Name,
ListenerRuleArn: *ruleData.RuleArn,
Weights: targetGroupTuples,
})
}
}
return res, nil
}
func (rs AwsListenerRules) targetsSlice() (targets []Target) {
for _, target := range rs {
targets = append(targets, target)
}
return targets
}