@@ -145,59 +145,77 @@ func (m *Manager) GetIds() []string {
145145func (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 }
176- }
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+
177174 locationZones = append (locationZones , serverscom.L7LocationZoneInput {
178- Location : "/" ,
175+ Location : p . Path ,
179176 UpstreamID : upstreamId ,
180177 })
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
194+ }
195+
196+ // last-win strategy for vhost annotations
197+ for k , v := range p .Service .Annotations {
198+ vhostAnnotations [k ] = v
199+ }
181200 }
182201
183- vZInput := serverscom.L7VHostZoneInput {
184- ID : fmt .Sprintf ("vhost-zone-%s" , sKey ),
185- Domains : service . Hosts ,
202+ vz := serverscom.L7VHostZoneInput {
203+ ID : fmt .Sprintf ("vhost-zone-%s" , host ),
204+ Domains : [] string { host } ,
186205 SSLCertID : sslId ,
187206 SSL : sslEnabled ,
188207 Ports : vhostPorts ,
189208 LocationZones : locationZones ,
190209 }
191- vZInput = * annotations .FillLBVHostZoneWithServiceAnnotations (& vZInput , service .Annotations )
192- vhostZones = append (vhostZones , vZInput )
210+ vz = * annotations .FillLBVHostZoneWithServiceAnnotations (& vz , vhostAnnotations )
211+ vhostZones = append (vhostZones , vz )
212+ }
193213
194- uZInput := serverscom.L7UpstreamZoneInput {
195- ID : upstreamId ,
196- Upstreams : upstreams ,
197- }
198- uZInput = * annotations .FillLBUpstreamZoneWithServiceAnnotations (& uZInput , service .Annotations )
199- upstreamZones = append (upstreamZones , uZInput )
214+ var upstreamZones []serverscom.L7UpstreamZoneInput
215+ for _ , u := range upstreamMap {
216+ upstreamZones = append (upstreamZones , u )
200217 }
218+
201219 if len (vhostZones ) == 0 || len (upstreamZones ) == 0 {
202220 return nil , errors .New ("vhost or upstream can't be empty, can't continue" )
203221 }
0 commit comments