-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtype.go
152 lines (127 loc) · 3.36 KB
/
type.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
146
147
148
149
150
151
152
package tentez
import (
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
elbv2Types "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2/types"
)
type TargetType string
const (
TargetTypeAwsListener TargetType = "aws_listeners"
TargetTypeAwsListenerRule TargetType = "aws_listener_rules"
)
const (
maxDescribeTargetGroupsItems = 20
maxDescribeRulesItems = 20
maxDescribeListenersItems = 20
)
type Weight struct {
Old int32
New int32
}
func (w Weight) CalcOldRatio() float64 {
return float64(w.Old) / float64(w.New+w.Old)
}
func (w Weight) CalcNewRatio() float64 {
return float64(w.New) / float64(w.New+w.Old)
}
type Switch struct {
Old string
New string
}
func (s Switch) getType(t string) string {
switch t {
case s.Old:
return "old"
case s.New:
return "new"
default:
return "unknown"
}
}
func makeNewActions(actions []elbv2Types.Action, targetSwitch Switch, targetWeight Weight) []elbv2Types.Action {
res := make([]elbv2Types.Action, len(actions))
for i, action := range actions {
if action.Type != elbv2Types.ActionTypeEnumForward {
if action.Type == elbv2Types.ActionTypeEnumAuthenticateOidc {
action.AuthenticateOidcConfig.UseExistingClientSecret = aws.Bool(true)
}
res[i] = action
continue
}
switch {
case targetWeight.Old == 0:
res[i] = elbv2Types.Action{
Type: elbv2Types.ActionTypeEnumForward,
TargetGroupArn: aws.String(targetSwitch.New),
Order: action.Order,
}
case targetWeight.New == 0:
res[i] = elbv2Types.Action{
Type: elbv2Types.ActionTypeEnumForward,
TargetGroupArn: aws.String(targetSwitch.Old),
Order: action.Order,
}
default:
res[i] = elbv2Types.Action{
Type: elbv2Types.ActionTypeEnumForward,
ForwardConfig: &elbv2Types.ForwardActionConfig{
TargetGroups: []elbv2Types.TargetGroupTuple{
{
TargetGroupArn: aws.String(targetSwitch.Old),
Weight: aws.Int32(targetWeight.Old),
},
{
TargetGroupArn: aws.String(targetSwitch.New),
Weight: aws.Int32(targetWeight.New),
},
},
},
Order: action.Order,
}
}
}
return res
}
type Step struct {
Type StepType `yaml:"type"`
Weight Weight `yaml:"weight,omitempty"`
SleepSeconds int `yaml:"sleepSeconds,omitempty"`
}
type StepType string
const (
StepTypePause StepType = "pause"
StepTypeSleep StepType = "sleep"
StepTypeSwitch StepType = "switch"
)
type YamlStruct struct {
Steps []Step `yaml:"steps"`
AwsListeners AwsListeners `yaml:"aws_listeners"`
AwsListenerRules AwsListenerRules `yaml:"aws_listener_rules"`
}
type AwsTargetGroupTuple struct {
TargetGroupArn string `yaml:"arn"`
Weight int32 `yaml:"weight"`
Type string `yaml:"type"`
}
type SkipSwitchError struct {
Message string
}
func (s SkipSwitchError) Error() string {
return fmt.Sprintf("skip switching: %s", s.Message)
}
type FailedFetchTargetGroupsError struct {
tgs []string
}
func NewFailedFetchTargetGroupsError(tgs []string) error {
if len(tgs) == 0 {
return nil
}
return &FailedFetchTargetGroupsError{tgs: tgs}
}
func (f *FailedFetchTargetGroupsError) Error() string {
return fmt.Sprintf("TargetGroupsNotFound: %v", f.tgs)
}
func (f *FailedFetchTargetGroupsError) Is(target error) bool {
_, ok := target.(*FailedFetchTargetGroupsError)
return f != nil && ok
}