-
Notifications
You must be signed in to change notification settings - Fork 0
/
aliyun_slb_rule.go
198 lines (145 loc) · 4.69 KB
/
aliyun_slb_rule.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package aliyun
import (
"encoding/json"
"fmt"
"strconv"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
"github.com/aliyun/alibaba-cloud-sdk-go/services/slb"
"github.com/sirupsen/logrus"
)
type Rule struct {
RuleName string
Domain string
Url string `json:",omitempty"`
VServerGroupId string
}
func (p *Aliyun) CreateSLBHTTPListenerRule() (err error) {
balancersConfig := p.Config.GetConfig("aliyun.slb.balancer")
if balancersConfig.IsEmpty() {
return
}
balancers, err := p.ListLoadBalancers(true)
if err != nil {
return
}
if len(balancers) == 0 {
return
}
slbNames := balancersConfig.Keys()
if len(slbNames) == 0 {
return
}
var reqs []*slb.CreateRulesRequest
for _, slbName := range slbNames {
slbInstance, exist := balancers[slbName]
if !exist {
err = fmt.Errorf("slb instance not exist: %s", slbName)
return
}
var alreadyListendPorts = make(map[string]bool)
for _, port := range slbInstance.ListenerPorts.ListenerPort {
alreadyListendPorts[port] = true
}
for _, listenerConfName := range []string{"listener.http", "listener.https"} {
lbConfig := balancersConfig.GetConfig(slbName)
listenersConfig := lbConfig.GetConfig(listenerConfName)
if listenersConfig.IsEmpty() {
continue
}
describeVgroupReq := slb.CreateDescribeVServerGroupsRequest()
describeVgroupReq.LoadBalancerId = slbInstance.LoadBalancerId
describeVgroupReq.RegionId = slbInstance.RegionId
var vSrvGroupsResp *slb.DescribeVServerGroupsResponse
vSrvGroupsResp, err = p.SLBClient().DescribeVServerGroups(describeVgroupReq)
if err != nil {
return
}
if vSrvGroupsResp == nil || len(vSrvGroupsResp.VServerGroups.VServerGroup) == 0 {
err = fmt.Errorf("no vserver group exist, lb: %s", slbName)
return
}
var mapSrvGroups = make(map[string]string) //name:id
for _, vg := range vSrvGroupsResp.VServerGroups.VServerGroup {
mapSrvGroups[vg.VServerGroupName] = vg.VServerGroupId
}
for _, listenerName := range listenersConfig.Keys() {
listenerConf := listenersConfig.GetConfig(listenerName)
if listenerConf.IsEmpty() {
continue
}
port := int(listenerConf.GetInt32("listen-port"))
if !alreadyListendPorts[strconv.Itoa(port)] {
err = fmt.Errorf("port %d not listened in balance %s", port, slbName)
return
}
describeRulReq := slb.CreateDescribeRulesRequest()
describeRulReq.RegionId = p.Region
describeRulReq.LoadBalancerId = slbInstance.LoadBalancerId
describeRulReq.ListenerPort = requests.NewInteger(port)
var ruleDescribRep *slb.DescribeRulesResponse
ruleDescribRep, err = p.SLBClient().DescribeRules(describeRulReq)
mapExistsRules := map[string]slb.Rule{}
for i, rule := range ruleDescribRep.Rules.Rule {
mapExistsRules[rule.RuleName] = ruleDescribRep.Rules.Rule[i]
}
rulesConf := listenerConf.GetConfig("rules")
var rules []Rule
for _, ruleName := range rulesConf.Keys() {
if _, ruleExist := mapExistsRules[ruleName]; ruleExist {
logrus.WithField("CODE", p.Code).
WithField("SLB-NAME", slbName).
WithField("SLB-ID", slbInstance.LoadBalancerId).
WithField("SLB-LISTENER", listenerName).
WithField("LSB-LISTENER-RULE", ruleName).Infoln("Listener rule already created")
continue
}
ruleConf := rulesConf.GetConfig(ruleName)
vGroupName := ruleConf.GetString("vserver-group-name")
vGroupId, exist := mapSrvGroups[vGroupName]
if !exist {
err = fmt.Errorf("vgroup of %s in lb %s not created.", vGroupName, slbName)
return
}
domain := ruleConf.GetString("domain")
url := ruleConf.GetString("url")
r := Rule{
RuleName: ruleName,
Domain: domain,
Url: url,
VServerGroupId: vGroupId,
}
rules = append(rules, r)
}
if len(rules) > 0 {
var ruleData []byte
ruleData, err = json.Marshal(rules)
if err != nil {
err = fmt.Errorf("marshal rule list error, slb instance: %s, listener: %s", slbName, listenerName)
return
}
req := slb.CreateCreateRulesRequest()
req.RegionId = p.Region
req.LoadBalancerId = slbInstance.LoadBalancerId
req.ListenerPort = requests.NewInteger(port)
req.RuleList = string(ruleData)
reqs = append(reqs, req)
}
}
}
}
for _, req := range reqs {
_, err = p.SLBClient().CreateRules(req)
if err != nil {
if IsAliErrCode(err, "DomainExist") {
err = nil
continue
}
return
}
logrus.WithField("CODE", p.Code).
WithField("SLB-BANLANCER-ID", req.LoadBalancerId).
WithField("SLB-BANLANCER-LISTENER-PORT", req.ListenerPort).
Infoln("SLB listener rules created")
}
return
}