forked from FeLvi-zzz/tentez
-
Notifications
You must be signed in to change notification settings - Fork 0
/
awsListener.go
144 lines (122 loc) · 3.74 KB
/
awsListener.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
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 AwsListener struct {
Name string `yaml:"name"`
Target string `yaml:"target"`
Switch Switch `yaml:"switch"`
}
type AwsListeners []AwsListener
type AwsListenerData struct {
Name string `yaml:"name"`
ListnerArn string `yaml:"target"`
Weights []AwsTargetGroupTuple `yaml:"weights"`
}
func (l AwsListener) execSwitch(targetWeight Weight, isForce bool, cfg Config) error {
// avoid rate limit
time.Sleep(1 * time.Second)
if !isForce {
listenerData, err := cfg.client.elbv2.DescribeListeners(context.TODO(), &elbv2.DescribeListenersInput{
ListenerArns: []string{l.Target},
})
if err != nil {
return err
}
tgWeight := Weight{}
for _, action := range listenerData.Listeners[0].DefaultActions {
for _, tgTuple := range action.ForwardConfig.TargetGroups {
switch *tgTuple.TargetGroupArn {
case l.Switch.Old:
tgWeight.Old = *tgTuple.Weight
case l.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.ModifyListener(context.TODO(), &elbv2.ModifyListenerInput{
ListenerArn: aws.String(l.Target),
DefaultActions: []elbv2Types.Action{
{
Type: "forward",
ForwardConfig: &elbv2Types.ForwardActionConfig{
TargetGroups: []elbv2Types.TargetGroupTuple{
{
TargetGroupArn: aws.String(l.Switch.Old),
Weight: aws.Int32(targetWeight.Old),
},
{
TargetGroupArn: aws.String(l.Switch.New),
Weight: aws.Int32(targetWeight.New),
},
},
},
},
},
})
return err
}
func (l AwsListener) getName() string {
return l.Name
}
func (ls AwsListeners) fetchData(cfg Config) (TargetsData, error) {
if len(ls) == 0 {
return nil, nil
}
listenerArns := []string{}
tgArns := []string{}
listenerMap := map[string]AwsListener{}
for _, listener := range ls {
listenerArns = append(listenerArns, listener.Target)
tgArns = append(tgArns, listener.Switch.New, listener.Switch.Old)
listenerMap[listener.Target] = listener
}
if _, err := cfg.client.elbv2.DescribeTargetGroups(context.TODO(), &elbv2.DescribeTargetGroupsInput{
TargetGroupArns: tgArns,
}); err != nil {
fmt.Fprintln(cfg.io.err, err.Error())
}
listenersData, err := cfg.client.elbv2.DescribeListeners(context.TODO(), &elbv2.DescribeListenersInput{
ListenerArns: listenerArns,
})
if err != nil {
return nil, err
}
res := []AwsListenerData{}
for _, listenerData := range listenersData.Listeners {
for _, action := range listenerData.DefaultActions {
targetGroupTuples := []AwsTargetGroupTuple{}
for _, tgTuple := range action.ForwardConfig.TargetGroups {
targetGroupTuples = append(targetGroupTuples, AwsTargetGroupTuple{
Type: listenerMap[*listenerData.ListenerArn].Switch.getType(*tgTuple.TargetGroupArn),
TargetGroupArn: aws.ToString(tgTuple.TargetGroupArn),
Weight: aws.ToInt32(tgTuple.Weight),
})
}
res = append(res, AwsListenerData{
Name: listenerMap[*listenerData.ListenerArn].Name,
ListnerArn: *listenerData.ListenerArn,
Weights: targetGroupTuples,
})
}
}
return res, nil
}
func (ls AwsListeners) targetsSlice() (targets []Target) {
for _, target := range ls {
targets = append(targets, target)
}
return targets
}