Skip to content

Commit aa92b94

Browse files
add a given vm to a custom VServerGroup
1 parent 2ed933e commit aa92b94

File tree

8 files changed

+183
-51
lines changed

8 files changed

+183
-51
lines changed

ci/assets/terraform/terraform.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ resource "alicloud_slb" "default" {
136136
address_type = "internet"
137137
}
138138

139+
140+
resource "alicloud_slb_server_group" "default" {
141+
load_balancer_id = alicloud_slb.default.id
142+
name = var.env_name
143+
}
139144
resource "alicloud_slb_listener" "http" {
140145
load_balancer_id = alicloud_slb.default.id
141146
backend_port = 80
@@ -272,6 +277,10 @@ output "slb" {
272277
value = alicloud_slb.default.id
273278
}
274279

280+
output "server_group_slb" {
281+
value = alicloud_slb_server_group.default.id
282+
}
283+
275284
output "blobstore_bucket" {
276285
value = alicloud_oss_bucket.blobstore.id
277286
}

ci/tasks/run-integration.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ exportMetadata2Env CPI_INTERNAL_GW internal_gw
4545
exportMetadata2Env CPI_EXTERNAL_IP external_ip
4646
exportMetadata2Env CPI_STEMCELL_OSS_BUCKET integration_bucket
4747
exportMetadata2Env CPI_SLB_ID slb
48+
exportMetadata2Env CPI_SLB_SERVER_GROUP_ID server_group_slb
4849
exportMetadata2Env RAM_ROLE_NAME ram_role
4950
export CIDR_NOTATION=$(getCidrNotation $CPI_INTERNAL_CIDR)
5051
export CPI_INTERNAL_NETMASK=$(cdr2mask $CIDR_NOTATION)

src/bosh-alicloud-cpi/action/create_vm.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"bosh-alicloud-cpi/registry"
99
"encoding/json"
1010
"fmt"
11+
"strconv"
1112
"strings"
1213

1314
bosherr "github.com/cloudfoundry/bosh-utils/errors"
@@ -42,6 +43,8 @@ type InstanceProps struct {
4243
AvailabilityZone string `json:"availability_zone"`
4344
InstanceName string `json:"instance_name"`
4445
InstanceType string `json:"instance_type"`
46+
SlbServerGroupPort string `json:"slb_server_group_port"`
47+
SlbServerGroup []string `json:"slb_server_group"`
4548
Slbs []string `json:"slbs"`
4649
SlbWeight json.Number `json:"slb_weight"`
4750
Password string `json:"password"`
@@ -368,6 +371,18 @@ func (a CreateVMMethod) updateInstance(instCid string, associatedDiskCIDs []apiv
368371
return bosherr.WrapErrorf(err, "bind %s to slb %s failed ", instCid, slb)
369372
}
370373
}
374+
slbServerGroupPort,err:=strconv.Atoi(instProps.SlbServerGroupPort)
375+
if err!=nil{
376+
slbServerGroupPort=alicloud.DefaultSlbServerGroupProt
377+
}else if slbServerGroupPort==0{
378+
slbServerGroupPort=alicloud.DefaultSlbServerGroupProt
379+
}
380+
for _,slbServerGroup:=range instProps.SlbServerGroup{
381+
err := a.networks.BindSlbServerGroup(instProps.Region, instCid, slbServerGroup, int(slbWeight),slbServerGroupPort)
382+
if err != nil {
383+
return bosherr.WrapErrorf(err, "bind %s to slbServerGroup %s failed,weight:%d,prot:%d ", instCid, slbServerGroup,int(slbWeight),slbServerGroupPort)
384+
}
385+
}
371386
return nil
372387
}
373388

src/bosh-alicloud-cpi/alicloud/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ const (
5252
DefaultSlbWeight = 100
5353
BoshCPI = "Bosh-Alicloud-Cpi"
5454
BoshCPIVersion = "1.0"
55+
DefaultSlbServerGroupProt =33333
5556
)
5657

5758
type OpenApi struct {

src/bosh-alicloud-cpi/alicloud/network_manager.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ type NetworkManager interface {
2121
WaitForEipStatus(region, eip string, toStatus EipStatus) error
2222

2323
BindSLB(region, instanceId, slbId string, weight int) error
24+
BindSlbServerGroup(region, instanceId, slbId string, weight int,port int) error
2425
DescribeSecurityGroupAttribute(region, groupId string) (ecs.DescribeSecurityGroupAttributeResponse, error)
2526
JoinSecurityGroup(region, instanceId, groupId string) error
2627
}
@@ -34,6 +35,11 @@ type BackendServerType struct {
3435
ServerId string
3536
Weight int
3637
}
38+
type SlbServerGroupBackendServerType struct {
39+
ServerId string
40+
Weight int
41+
Port int
42+
}
3743

3844
func NewNetworkManager(config Config, logger boshlog.Logger) NetworkManager {
3945
return NetworkManagerImpl{
@@ -139,6 +145,29 @@ func (a NetworkManagerImpl) WaitForEipStatus(region, eip string, toStatus EipSta
139145
return nil
140146
}
141147

148+
func (a NetworkManagerImpl) BindSlbServerGroup(region, instanceId string, slbServerGroupId string, weight int,port int) error {
149+
client,err:=a.config.NewSlbClient(region)
150+
if err!=nil{
151+
return err
152+
}
153+
if weight == 0 {
154+
weight = DefaultSlbWeight
155+
}
156+
bytes, _ := json.Marshal([]SlbServerGroupBackendServerType{
157+
{ServerId: instanceId, Weight: weight,Port: port},
158+
})
159+
args := slb.CreateAddVServerGroupBackendServersRequest()
160+
args.VServerGroupId = slbServerGroupId
161+
args.BackendServers = string(bytes)
162+
servers, err :=client.AddVServerGroupBackendServers(args)
163+
if err != nil {
164+
a.logger.Error("NetworkManager", "BindSlbServerGroup %s to %s failed %v", instanceId, slbServerGroupId, err)
165+
return bosherr.WrapErrorf(err, "BindSlbServerGroup %s to %s failed", instanceId, slbServerGroupId)
166+
}
167+
168+
a.logger.Info("NetworkManager", "BindSlbServerGroup %s to %s, after bind server=%v", instanceId, slbServerGroupId, servers)
169+
return err
170+
}
142171
//
143172
// TODO: add retry
144173
func (a NetworkManagerImpl) BindSLB(region, instanceId string, slbId string, weight int) error {

src/bosh-alicloud-cpi/integration/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ var (
3434
internalNetmask = envOrDefault("CPI_INTERNAL_NETMASK", "255.240.0.0")
3535
internalGw = envOrDefault("CPI_INTERNAL_GW", "172.16.0.1")
3636
slbId = envOrDefault("CPI_SLB_ID", "")
37+
slbServerGroupId= envOrDefault("CPI_SLB_SERVER_GROUP_ID", "")
3738
externalIp = envOrDefault("CPI_EXTERNAL_IP", "")
3839

3940
// spot

0 commit comments

Comments
 (0)