forked from yunionio/cloudmux
/
loadbalancer_backend.go
158 lines (130 loc) · 3.93 KB
/
loadbalancer_backend.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
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package qcloud
import (
"context"
"fmt"
"github.com/nyl1001/pkg/jsonutils"
api "github.com/nyl1001/cloudmux/pkg/apis/compute"
"github.com/nyl1001/cloudmux/pkg/cloudprovider"
"github.com/nyl1001/cloudmux/pkg/multicloud"
)
type SLBBackend struct {
multicloud.SResourceBase
QcloudTags
group *SLBBackendGroup
PublicIPAddresses []string `json:"PublicIpAddresses"`
Weight int `json:"Weight"`
InstanceId string `json:"InstanceId"`
InstanceName string `json:"InstanceName"`
PrivateIPAddresses []string `json:"PrivateIpAddresses"`
RegisteredTime string `json:"RegisteredTime"`
Type string `json:"Type"`
Port int `json:"Port"`
}
// ==========================================================
type SListenerBackend struct {
Rules []Rule `json:"Rules"`
Targets []SLBBackend `json:"Targets"`
Protocol string `json:"Protocol"`
ListenerId string `json:"ListenerId"`
Port int64 `json:"Port"`
}
type Rule struct {
URL string `json:"Url"`
Domain string `json:"Domain"`
LocationId string `json:"LocationId"`
Targets []SLBBackend `json:"Targets"`
}
// ==========================================================
// backend InstanceId + protocol +Port + ip + rip全局唯一
func (self *SLBBackend) GetId() string {
return fmt.Sprintf("%s/%s-%d", self.group.GetId(), self.InstanceId, self.Port)
}
func (self *SLBBackend) GetName() string {
return self.GetId()
}
func (self *SLBBackend) GetGlobalId() string {
return self.GetId()
}
func (self *SLBBackend) GetStatus() string {
return api.LB_STATUS_ENABLED
}
func (self *SLBBackend) Refresh() error {
backends, err := self.group.GetBackends()
if err != nil {
return err
}
for _, backend := range backends {
if backend.GetId() == self.GetId() {
return jsonutils.Update(self, backend)
}
}
return cloudprovider.ErrNotFound
}
func (self *SLBBackend) GetWeight() int {
return self.Weight
}
func (self *SLBBackend) GetPort() int {
return self.Port
}
func (self *SLBBackend) GetBackendType() string {
return api.LB_BACKEND_GUEST
}
func (self *SLBBackend) GetBackendRole() string {
return api.LB_BACKEND_ROLE_DEFAULT
}
func (self *SLBBackend) GetBackendId() string {
return self.InstanceId
}
func (self *SLBBackend) GetIpAddress() string {
return ""
}
// 应用型: https://cloud.tencent.com/document/product/214/30684
func (self *SRegion) GetBackends(lbId, listenerId string) ([]SLBBackend, error) {
params := map[string]string{"LoadBalancerId": lbId}
if len(listenerId) > 0 {
params["ListenerIds.0"] = listenerId
}
resp, err := self.clbRequest("DescribeTargets", params)
if err != nil {
return nil, err
}
lbackends := []SListenerBackend{}
err = resp.Unmarshal(&lbackends, "Listeners")
if err != nil {
return nil, err
}
backends := []SLBBackend{}
for _, entry := range lbackends {
if len(entry.Targets) > 0 {
backends = append(backends, entry.Targets...)
}
for _, r := range entry.Rules {
if len(r.Targets) > 0 {
backends = append(backends, r.Targets...)
}
}
}
return backends, nil
}
func (self *SLBBackend) SyncConf(ctx context.Context, port, weight int) error {
//err := self.group.UpdateBackendServer(self.InstanceId, self.Weight, self.Port, weight, port)
//if err != nil {
// return err
//}
self.Port = port
self.Weight = weight
return nil
}