Simplify urlmap update logic.

This commit is contained in:
Prashanth Balasubramanian 2016-03-26 14:14:43 -07:00
parent 3464d1c5e3
commit 5b2de6e40b

View file

@ -642,48 +642,29 @@ func (l *L7) UpdateUrlMap(ingressRules utils.GCEURLMap) error {
} }
glog.V(3).Infof("Updating url map %+v", ingressRules) glog.V(3).Infof("Updating url map %+v", ingressRules)
// Every update replaces the entire urlmap.
// TODO: when we have multiple loadbalancers point to a single gce url map
// this needs modification. For now, there is a 1:1 mapping of urlmaps to
// Ingresses, so if the given Ingress doesn't have a host rule we should
// delete the path to that backend.
l.um.HostRules = []*compute.HostRule{}
l.um.PathMatchers = []*compute.PathMatcher{}
for hostname, urlToBackend := range ingressRules { for hostname, urlToBackend := range ingressRules {
// Find the hostrule // Create a host rule
// Find the path matcher // Create a path matcher
// Add all given endpoint:backends to pathRules in path matcher // Add all given endpoint:backends to pathRules in path matcher
var hostRule *compute.HostRule
pmName := getNameForPathMatcher(hostname) pmName := getNameForPathMatcher(hostname)
for _, hr := range l.um.HostRules { l.um.HostRules = append(l.um.HostRules, &compute.HostRule{
// TODO: Hostnames must be exact match?
if hr.Hosts[0] == hostname {
hostRule = hr
break
}
}
if hostRule == nil {
// This is a new host
hostRule = &compute.HostRule{
Hosts: []string{hostname}, Hosts: []string{hostname},
PathMatcher: pmName, PathMatcher: pmName,
} })
// Why not just clobber existing host rules?
// Because we can have multiple loadbalancers point to a single
// gce url map when we have IngressClaims.
l.um.HostRules = append(l.um.HostRules, hostRule)
}
var pathMatcher *compute.PathMatcher
for _, pm := range l.um.PathMatchers {
if pm.Name == hostRule.PathMatcher {
pathMatcher = pm
break
}
}
if pathMatcher == nil {
// This is a dangling or new host
pathMatcher = &compute.PathMatcher{Name: pmName}
l.um.PathMatchers = append(l.um.PathMatchers, pathMatcher)
}
pathMatcher.DefaultService = l.um.DefaultService
// TODO: Every update replaces the entire path map. This will need to pathMatcher := &compute.PathMatcher{
// change when we allow joining. Right now we call a single method Name: pmName,
// to verify current == desired and add new url mappings. DefaultService: l.um.DefaultService,
pathMatcher.PathRules = []*compute.PathRule{} PathRules: []*compute.PathRule{},
}
// Longest prefix wins. For equal rules, first hit wins, i.e the second // Longest prefix wins. For equal rules, first hit wins, i.e the second
// /foo rule when the first is deleted. // /foo rule when the first is deleted.
@ -691,6 +672,7 @@ func (l *L7) UpdateUrlMap(ingressRules utils.GCEURLMap) error {
pathMatcher.PathRules = append( pathMatcher.PathRules = append(
pathMatcher.PathRules, &compute.PathRule{Paths: []string{expr}, Service: be.SelfLink}) pathMatcher.PathRules, &compute.PathRule{Paths: []string{expr}, Service: be.SelfLink})
} }
l.um.PathMatchers = append(l.um.PathMatchers, pathMatcher)
} }
um, err := l.cloud.UpdateUrlMap(l.um) um, err := l.cloud.UpdateUrlMap(l.um)
if err != nil { if err != nil {