-
Notifications
You must be signed in to change notification settings - Fork 0
/
aliyun_slb_vgroup.go
169 lines (123 loc) · 4.04 KB
/
aliyun_slb_vgroup.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
package aliyun
import (
"encoding/json"
"fmt"
"github.com/aliyun/alibaba-cloud-sdk-go/services/ecs"
"github.com/aliyun/alibaba-cloud-sdk-go/services/slb"
"github.com/sirupsen/logrus"
)
func (p *Aliyun) CreateVServerGroup() (err error) {
balancersConfig := p.Config.GetConfig("aliyun.slb.balancer")
if balancersConfig.IsEmpty() {
return
}
lbs, err := p.ListLoadBalancers(true)
if err != nil {
return
}
var reqs []*slb.CreateVServerGroupRequest
for _, balancerName := range balancersConfig.Keys() {
lb, exist := lbs[balancerName]
if !exist {
err = fmt.Errorf("could not find slb balancer: %s", balancerName)
return
}
vpcName := balancersConfig.GetString(balancerName + ".vpc-name")
vSwitchName := balancersConfig.GetString(balancerName + ".vswitch-name")
vServerGroupsConf := balancersConfig.GetConfig(balancerName + ".vserver-group")
if vServerGroupsConf.IsEmpty() {
continue
}
describVgroupReq := slb.CreateDescribeVServerGroupsRequest()
describVgroupReq.LoadBalancerId = lb.LoadBalancerId
describVgroupReq.RegionId = p.Region
var existGroups *slb.DescribeVServerGroupsResponse
existGroups, err = p.SLBClient().DescribeVServerGroups(describVgroupReq)
if err != nil {
return
}
mapExistGroups := map[string]bool{}
if existGroups != nil {
for _, group := range existGroups.VServerGroups.VServerGroup {
mapExistGroups[group.VServerGroupName] = true
}
}
for _, groupName := range vServerGroupsConf.Keys() {
if mapExistGroups[groupName] {
logrus.WithField("SLB-INSTANCE-NAME", balancerName).
WithField("SLB-VGROUP-NAME", groupName).
Infoln("SLB VServerGroup already exist")
continue
}
groupConf := vServerGroupsConf.GetConfig(groupName)
for _, srv := range groupConf.Keys() {
serarchConf := groupConf.GetConfig(srv + ".instance")
var tags []Tag
tagConf := serarchConf.GetConfig("tag")
for _, k := range tagConf.Keys() {
tags = append(tags, Tag{Key: k, Value: tagConf.GetString(k)})
}
var inst *ecs.Instance
inst, err = p.FindECSInstance(
&SearchECSInstanceArgs{
InstanceId: serarchConf.GetString("id"),
InstanceName: serarchConf.GetString("name"),
NetworkType: serarchConf.GetString("network-type"),
ZoneId: serarchConf.GetString("zone-id"),
VPCName: vpcName,
VSwitchName: vSwitchName,
vswitchId: lb.VSwitchId,
vpcId: lb.VpcId,
Tags: tags,
},
)
if err != nil {
return
}
if inst == nil {
err = fmt.Errorf("instance '%s' not found: %s.%s.%s, tags: %#v", serarchConf.GetString("name"), balancerName, groupName, srv, tags)
return
}
var backendServers []slb.BackendServer
portsConf := groupConf.GetConfig(srv + ".ports")
for _, portName := range portsConf.Keys() {
portConf := portsConf.GetConfig(portName)
vSrv := slb.BackendServer{
ServerId: inst.InstanceId,
Port: int(portConf.GetInt32("port")),
Weight: int(portConf.GetInt32("weight")),
}
backendServers = append(backendServers, vSrv)
}
var srvData []byte
srvData, err = json.Marshal(backendServers)
if err != nil {
return
}
req := slb.CreateCreateVServerGroupRequest()
req.LoadBalancerId = lb.LoadBalancerId
req.RegionId = p.Region
req.VServerGroupName = groupName
req.BackendServers = string(srvData)
logrus.WithField("SLB-INSTANCE-NAME", balancerName).
WithField("SLB-VGROUP-NAME", groupName).
WithField("SLB-VGROUP-VSERVER", string(srvData)).
Debugln("SLB VServerGroup Info")
reqs = append(reqs, req)
}
}
}
for i := 0; i < len(reqs); i++ {
var resp *slb.CreateVServerGroupResponse
resp, err = p.SLBClient().CreateVServerGroup(reqs[i])
if err != nil {
return
}
logrus.WithField("CODE", p.Code).
WithField("SLB-BANLANCER-ID", reqs[i].LoadBalancerId).
WithField("SLB-BANLANCER-VGROUP-NAME", reqs[i].VServerGroupName).
WithField("SLB-BANLANCER-VGROUP-ID", resp.VServerGroupId).
Infoln("SLB VGroup created")
}
return
}