Skip to content

Commit 10d8625

Browse files
committed
update TranslateIngressToLB to support new host info structure from store
1 parent 445d1fa commit 10d8625

1 file changed

Lines changed: 51 additions & 35 deletions

File tree

internal/service/loadbalancer/manager.go

Lines changed: 51 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -145,61 +145,77 @@ func (m *Manager) GetIds() []string {
145145
func (m *Manager) TranslateIngressToLB(ingress *networkv1.Ingress, sslCerts map[string]string) (*serverscom.L7LoadBalancerCreateInput, error) {
146146
m.lock.Lock()
147147
defer m.lock.Unlock()
148-
sInfo, err := m.store.GetIngressServiceInfo(ingress)
148+
149+
hostsInfo, err := m.store.GetIngressHostsInfo(ingress)
149150
if err != nil {
150151
return nil, err
151152
}
152153

153-
var upstreamZones []serverscom.L7UpstreamZoneInput
154-
var upstreams []serverscom.L7UpstreamInput
155154
var vhostZones []serverscom.L7VHostZoneInput
156-
var locationZones []serverscom.L7LocationZoneInput
157155

158-
for sKey, service := range sInfo {
159-
sslId := ""
160-
sslEnabled := false
156+
upstreamMap := make(map[string]serverscom.L7UpstreamZoneInput)
157+
158+
for host, hInfo := range hostsInfo {
159+
var locationZones []serverscom.L7LocationZoneInput
161160
vhostPorts := []int32{80}
162-
upstreamId := fmt.Sprintf("upstream-zone-%s", sKey)
163-
for _, ip := range service.NodeIps {
164-
upstreams = append(upstreams, serverscom.L7UpstreamInput{
165-
IP: ip,
166-
Weight: 1,
167-
Port: int32(service.NodePort),
168-
})
161+
sslEnabled := false
162+
sslId := ""
163+
164+
if id, ok := sslCerts[host]; ok {
165+
sslId = id
166+
sslEnabled = true
167+
vhostPorts = []int32{443}
169168
}
170169

171-
for _, host := range service.Hosts {
172-
if id, ok := sslCerts[host]; ok {
173-
sslId = id
174-
sslEnabled = true
175-
vhostPorts = []int32{443}
170+
vhostAnnotations := make(map[string]string)
171+
for _, p := range hInfo.Paths {
172+
upstreamId := fmt.Sprintf("upstream-zone-%s-%d", p.Service.Name, p.NodePort)
173+
174+
locationZones = append(locationZones, serverscom.L7LocationZoneInput{
175+
Location: p.Path,
176+
UpstreamID: upstreamId,
177+
})
178+
179+
if _, ok := upstreamMap[upstreamId]; !ok {
180+
var ups []serverscom.L7UpstreamInput
181+
for _, ip := range p.NodeIps {
182+
ups = append(ups, serverscom.L7UpstreamInput{
183+
IP: ip,
184+
Port: int32(p.NodePort),
185+
Weight: 1,
186+
})
187+
}
188+
upstream := serverscom.L7UpstreamZoneInput{
189+
ID: upstreamId,
190+
Upstreams: ups,
191+
}
192+
upstream = *annotations.FillLBUpstreamZoneWithServiceAnnotations(&upstream, p.Service.Annotations)
193+
upstreamMap[upstreamId] = upstream
176194
}
177-
for _, p := range service.Paths {
178-
locationZones = append(locationZones, serverscom.L7LocationZoneInput{
179-
Location: p,
180-
UpstreamID: upstreamId,
181-
})
195+
196+
// last-win strategy for vhost annotations
197+
for k, v := range p.Service.Annotations {
198+
vhostAnnotations[k] = v
182199
}
183200
}
184201

185-
vZInput := serverscom.L7VHostZoneInput{
186-
ID: fmt.Sprintf("vhost-zone-%s", sKey),
187-
Domains: service.Hosts,
202+
vz := serverscom.L7VHostZoneInput{
203+
ID: fmt.Sprintf("vhost-zone-%s", host),
204+
Domains: []string{host},
188205
SSLCertID: sslId,
189206
SSL: sslEnabled,
190207
Ports: vhostPorts,
191208
LocationZones: locationZones,
192209
}
193-
vZInput = *annotations.FillLBVHostZoneWithServiceAnnotations(&vZInput, service.Annotations)
194-
vhostZones = append(vhostZones, vZInput)
210+
vz = *annotations.FillLBVHostZoneWithServiceAnnotations(&vz, vhostAnnotations)
211+
vhostZones = append(vhostZones, vz)
212+
}
195213

196-
uZInput := serverscom.L7UpstreamZoneInput{
197-
ID: upstreamId,
198-
Upstreams: upstreams,
199-
}
200-
uZInput = *annotations.FillLBUpstreamZoneWithServiceAnnotations(&uZInput, service.Annotations)
201-
upstreamZones = append(upstreamZones, uZInput)
214+
var upstreamZones []serverscom.L7UpstreamZoneInput
215+
for _, u := range upstreamMap {
216+
upstreamZones = append(upstreamZones, u)
202217
}
218+
203219
if len(vhostZones) == 0 || len(upstreamZones) == 0 {
204220
return nil, errors.New("vhost or upstream can't be empty, can't continue")
205221
}

0 commit comments

Comments
 (0)