yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/google/loadbalancer_backend.go (about) 1 // Copyright 2019 Yunion 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package google 16 17 import ( 18 "context" 19 "fmt" 20 "strconv" 21 "strings" 22 "time" 23 24 "yunion.io/x/pkg/errors" 25 26 api "yunion.io/x/cloudmux/pkg/apis/compute" 27 "yunion.io/x/cloudmux/pkg/cloudprovider" 28 ) 29 30 type SLoadbalancerBackend struct { 31 lbbg *SLoadBalancerBackendGroup 32 33 backendService SBackendServices // 34 instanceGroup SInstanceGroup // 实例组 35 Backend SInstanceGroupInstance // backend 36 37 Port int `json:"port"` 38 } 39 40 func (self *SLoadbalancerBackend) GetId() string { 41 return fmt.Sprintf("%s::%s::%s::%d", self.lbbg.GetGlobalId(), self.instanceGroup.GetGlobalId(), self.GetBackendId(), self.Port) 42 } 43 44 func (self *SLoadbalancerBackend) GetName() string { 45 segs := strings.Split(self.Backend.Instance, "/") 46 name := "" 47 if len(segs) > 0 { 48 name = segs[len(segs)-1] 49 } 50 return fmt.Sprintf("%s::%s::%d", self.instanceGroup.GetName(), name, self.Port) 51 } 52 53 func (self *SLoadbalancerBackend) GetGlobalId() string { 54 return self.GetId() 55 } 56 57 func (self *SLoadbalancerBackend) GetStatus() string { 58 return api.LB_STATUS_ENABLED 59 } 60 61 func (self *SLoadbalancerBackend) Refresh() error { 62 return nil 63 } 64 65 func (self *SLoadbalancerBackend) IsEmulated() bool { 66 return true 67 } 68 69 func (self *SLoadbalancerBackend) GetSysTags() map[string]string { 70 return nil 71 } 72 73 func (self *SLoadbalancerBackend) GetTags() (map[string]string, error) { 74 return nil, nil 75 } 76 77 func (self *SLoadbalancerBackend) SetTags(tags map[string]string, replace bool) error { 78 return cloudprovider.ErrNotSupported 79 } 80 81 func (self *SLoadbalancerBackend) GetProjectId() string { 82 return self.lbbg.GetProjectId() 83 } 84 85 func (self *SLoadbalancerBackend) GetWeight() int { 86 return 0 87 } 88 89 func (self *SLoadbalancerBackend) GetPort() int { 90 return self.Port 91 } 92 93 func (self *SLoadbalancerBackend) GetCreatedAt() time.Time { 94 return time.Time{} 95 } 96 97 func (self *SLoadbalancerBackend) GetBackendType() string { 98 return api.LB_BACKEND_GUEST 99 } 100 101 func (self *SLoadbalancerBackend) GetBackendRole() string { 102 return api.LB_BACKEND_ROLE_DEFAULT 103 } 104 105 func (self *SLoadbalancerBackend) GetBackendId() string { 106 r := SResourceBase{ 107 Name: "", 108 SelfLink: self.Backend.Instance, 109 } 110 return r.GetGlobalId() 111 } 112 113 func (self *SLoadbalancerBackend) GetIpAddress() string { 114 return "" 115 } 116 117 func (self *SLoadbalancerBackend) SyncConf(ctx context.Context, port, weight int) error { 118 return cloudprovider.ErrNotSupported 119 } 120 121 func (self *SLoadBalancerBackendGroup) GetLoadbalancerBackends() ([]SLoadbalancerBackend, error) { 122 if self.backends != nil { 123 return self.backends, nil 124 } 125 126 _igs, err := self.lb.GetInstanceGroupsMap() 127 if err != nil { 128 return nil, errors.Wrap(err, "GetInstanceGroupsMap") 129 } 130 131 igs := make([]SInstanceGroup, 0) 132 for i := range self.backendService.Backends { 133 backend := self.backendService.Backends[i] 134 if v, ok := _igs[backend.Group]; ok { 135 igs = append(igs, v) 136 } 137 } 138 139 ret := make([]SLoadbalancerBackend, 0) 140 for i := range igs { 141 ig := igs[i] 142 // http lb 143 if self.lb.isHttpLb { 144 for j := range ig.NamedPorts { 145 np := ig.NamedPorts[j] 146 if np.Name != self.backendService.PortName { 147 continue 148 } 149 150 bs, err := ig.GetInstances() 151 if err != nil { 152 return nil, errors.Wrap(err, "GetInstances") 153 } 154 155 for n := range bs { 156 backend := SLoadbalancerBackend{ 157 lbbg: self, 158 backendService: self.backendService, 159 instanceGroup: ig, 160 Backend: bs[n], 161 Port: int(np.Port), 162 } 163 164 ret = append(ret, backend) 165 } 166 } 167 } else { 168 // tcp & udp lb 169 bs, err := ig.GetInstances() 170 if err != nil { 171 return nil, errors.Wrap(err, "GetInstances") 172 } 173 174 frs, err := self.lb.GetForwardingRules() 175 if err != nil { 176 return nil, errors.Wrap(err, "GetForwardingRules") 177 } 178 179 for m := range frs { 180 fr := frs[m] 181 for p := range fr.Ports { 182 port, _ := strconv.Atoi(fr.Ports[p]) 183 if port <= 0 { 184 continue 185 } 186 187 for n := range bs { 188 backend := SLoadbalancerBackend{ 189 lbbg: self, 190 backendService: self.backendService, 191 instanceGroup: ig, 192 Backend: bs[n], 193 Port: port, 194 } 195 196 ret = append(ret, backend) 197 } 198 } 199 } 200 } 201 } 202 203 self.backends = ret 204 return ret, nil 205 }