Skip to content

Commit 66cc6d5

Browse files
committed
Merge pull request #6 from nginxinc/fix-same-service-in-one-ingress
Fixed the problem when a service is referenced from one ingress resou…
2 parents 48c9f33 + 67898d7 commit 66cc6d5

File tree

2 files changed

+53
-20
lines changed

2 files changed

+53
-20
lines changed

nginx-controller/controller/controller.go

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -262,15 +262,18 @@ func (lbc *LoadBalancerController) updateNGINX(name string, ing *extensions.Ingr
262262
}
263263
}
264264

265-
var upstreams []nginx.Upstream
265+
upstreams := make(map[string]nginx.Upstream)
266266

267267
for _, rule := range ing.Spec.Rules {
268268
if rule.IngressRuleValue.HTTP == nil {
269269
continue
270270
}
271271

272272
for _, path := range rule.HTTP.Paths {
273-
name := ing.Name + "-" + path.Backend.ServiceName
273+
name := getNameForUpstream(ing, rule.Host, path.Backend.ServiceName)
274+
if _, exists := upstreams[name]; exists {
275+
continue
276+
}
274277
ups := nginx.NewUpstreamWithDefaultServer(name)
275278

276279
svcKey := ing.Namespace + "/" + path.Backend.ServiceName
@@ -296,8 +299,8 @@ func (lbc *LoadBalancerController) updateNGINX(name string, ing *extensions.Ingr
296299
}
297300
}
298301
}
302+
upstreams[name] = ups
299303

300-
upstreams = append(upstreams, ups)
301304
}
302305
}
303306

@@ -315,21 +318,19 @@ func (lbc *LoadBalancerController) updateNGINX(name string, ing *extensions.Ingr
315318

316319
for _, path := range rule.HTTP.Paths {
317320
loc := nginx.Location{Path: path.Path}
318-
upsName := ing.GetName() + "-" + path.Backend.ServiceName
321+
upsName := getNameForUpstream(ing, rule.Host, path.Backend.ServiceName)
319322

320-
for _, ups := range upstreams {
321-
if upsName == ups.Name {
322-
loc.Upstream = ups
323-
}
323+
if ups, ok := upstreams[upsName]; ok {
324+
loc.Upstream = ups
325+
locations = append(locations, loc)
324326
}
325-
locations = append(locations, loc)
326327
}
327328

328329
server.Locations = locations
329330
servers = append(servers, server)
330331
}
331332

332-
lbc.nginx.AddOrUpdateIngress(name, nginx.IngressNGINXConfig{Upstreams: upstreams, Servers: servers})
333+
lbc.nginx.AddOrUpdateIngress(name, nginx.IngressNGINXConfig{Upstreams: upstreamMapToSlice(upstreams), Servers: servers})
333334
}
334335

335336
func endpointsToUpstreamServers(endps api.Endpoints, servicePort int) []nginx.UpstreamServer {
@@ -348,3 +349,18 @@ func endpointsToUpstreamServers(endps api.Endpoints, servicePort int) []nginx.Up
348349

349350
return upsServers
350351
}
352+
353+
func getNameForUpstream(ing *extensions.Ingress, host string, service string) string {
354+
return fmt.Sprintf("%v-%v-%v-%v", ing.Namespace, ing.Name, host, service)
355+
}
356+
357+
func upstreamMapToSlice(upstreams map[string]nginx.Upstream) []nginx.Upstream {
358+
result := make([]nginx.Upstream, 0, len(upstreams))
359+
360+
for _, ups := range upstreams {
361+
glog.Info(ups)
362+
result = append(result, ups)
363+
}
364+
365+
return result
366+
}

nginx-plus-controller/controller/controller.go

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -155,22 +155,26 @@ func (lbc *LoadBalancerController) updateNGINX(name string, ing *extensions.Ingr
155155
}
156156
}
157157

158-
var upstreams []nginx.Upstream
158+
upstreams := make(map[string]nginx.Upstream)
159159

160160
for _, rule := range ing.Spec.Rules {
161161
if rule.IngressRuleValue.HTTP == nil {
162162
continue
163163
}
164164

165165
for _, path := range rule.HTTP.Paths {
166-
name := ing.Name + "-" + path.Backend.ServiceName
166+
name := getNameForUpstream(ing, rule.Host, path.Backend.ServiceName)
167+
if _, exists := upstreams[name]; exists {
168+
continue
169+
}
167170
upstream := nginx.Upstream{Name: name}
168171
var upsServers []nginx.UpstreamServer
169172
address := fmt.Sprintf("%v.%v.svc.cluster.local", path.Backend.ServiceName, ing.Namespace)
170173
server := nginx.UpstreamServer{Address: address, Port: path.Backend.ServicePort.String()}
171174
upsServers = append(upsServers, server)
172175
upstream.UpstreamServers = upsServers
173-
upstreams = append(upstreams, upstream)
176+
177+
upstreams[name] = upstream
174178
}
175179
}
176180

@@ -188,19 +192,32 @@ func (lbc *LoadBalancerController) updateNGINX(name string, ing *extensions.Ingr
188192

189193
for _, path := range rule.HTTP.Paths {
190194
loc := nginx.Location{Path: path.Path}
191-
upsName := ing.GetName() + "-" + path.Backend.ServiceName
195+
upsName := getNameForUpstream(ing, rule.Host, path.Backend.ServiceName)
192196

193-
for _, ups := range upstreams {
194-
if upsName == ups.Name {
195-
loc.Upstream = ups
196-
}
197+
if ups, ok := upstreams[upsName]; ok {
198+
loc.Upstream = ups
199+
locations = append(locations, loc)
197200
}
198-
locations = append(locations, loc)
199201
}
200202

201203
server.Locations = locations
202204
servers = append(servers, server)
203205
}
204206

205-
lbc.nginx.AddOrUpdateIngress(name, nginx.IngressNGINXConfig{Upstreams: upstreams, Servers: servers})
207+
lbc.nginx.AddOrUpdateIngress(name, nginx.IngressNGINXConfig{Upstreams: upstreamMapToSlice(upstreams), Servers: servers})
208+
}
209+
210+
func getNameForUpstream(ing *extensions.Ingress, host string, service string) string {
211+
return fmt.Sprintf("%v-%v-%v-%v", ing.Namespace, ing.Name, host, service)
212+
}
213+
214+
func upstreamMapToSlice(upstreams map[string]nginx.Upstream) []nginx.Upstream {
215+
result := make([]nginx.Upstream, 0, len(upstreams))
216+
217+
for _, ups := range upstreams {
218+
glog.Info(ups)
219+
result = append(result, ups)
220+
}
221+
222+
return result
206223
}

0 commit comments

Comments
 (0)