From 0c03f6e784861b01324dfbd11d8a1345a16587b5 Mon Sep 17 00:00:00 2001 From: Tim Hockin Date: Wed, 26 Nov 2014 10:54:28 -0800 Subject: [PATCH] Enable public IPs even in not createExternal... This allows the proxier to portal Public IPs even if the createExternalLoadBalancer flag is not set. This also fixes what appears to be a bug in the createExternalLoadBalancer path wherein multiple PublicIPs would get truncated. --- pkg/proxy/proxier.go | 21 +++++++++++++++------ pkg/registry/service/rest.go | 16 ++++++++-------- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/pkg/proxy/proxier.go b/pkg/proxy/proxier.go index 0472c73dc51..221cf2d1814 100644 --- a/pkg/proxy/proxier.go +++ b/pkg/proxy/proxier.go @@ -40,8 +40,7 @@ type serviceInfo struct { timeout time.Duration mu sync.Mutex // protects active active bool - // TODO: make this an net.IP address - publicIP []string + publicIP []string // TODO: make this a []net.IP } func (si *serviceInfo) isActive() bool { @@ -445,7 +444,7 @@ func (proxier *Proxier) OnUpdate(services []api.Service) { if exists && info.isActive() && info.portalPort == service.Spec.Port && info.portalIP.Equal(serviceIP) { continue } - if exists && (info.portalPort != service.Spec.Port || !info.portalIP.Equal(serviceIP) || service.Spec.CreateExternalLoadBalancer != (len(info.publicIP) > 0)) { + if exists && (info.portalPort != service.Spec.Port || !info.portalIP.Equal(serviceIP) || !ipsEqual(service.Spec.PublicIPs, info.publicIP)) { glog.V(4).Infof("Something changed for service %q: stopping it", service.Name) err := proxier.closePortal(service.Name, info) if err != nil { @@ -464,9 +463,7 @@ func (proxier *Proxier) OnUpdate(services []api.Service) { } info.portalIP = serviceIP info.portalPort = service.Spec.Port - if service.Spec.CreateExternalLoadBalancer { - info.publicIP = service.Spec.PublicIPs - } + info.publicIP = service.Spec.PublicIPs err = proxier.openPortal(service.Name, info) if err != nil { glog.Errorf("Failed to open portal for %q: %v", service.Name, err) @@ -489,6 +486,18 @@ func (proxier *Proxier) OnUpdate(services []api.Service) { } } +func ipsEqual(lhs, rhs []string) bool { + if len(lhs) != len(rhs) { + return false + } + for i := range lhs { + if lhs[i] != rhs[i] { + return false + } + } + return true +} + func (proxier *Proxier) openPortal(service string, info *serviceInfo) error { args := iptablesPortalArgs(info.portalIP, info.portalPort, info.protocol, proxier.listenAddress, info.proxyPort, service) existed, err := proxier.iptables.EnsureRule(iptables.TableNAT, iptablesProxyChain, args...) diff --git a/pkg/registry/service/rest.go b/pkg/registry/service/rest.go index 56a662a88d2..ba15a61083f 100644 --- a/pkg/registry/service/rest.go +++ b/pkg/registry/service/rest.go @@ -130,21 +130,21 @@ func (rs *REST) Create(ctx api.Context, obj runtime.Object) (<-chan apiserver.RE if err != nil { return nil, err } - var ip net.IP if len(service.Spec.PublicIPs) > 0 { for _, publicIP := range service.Spec.PublicIPs { - ip, err = balancer.CreateTCPLoadBalancer(service.Name, zone.Region, net.ParseIP(publicIP), service.Spec.Port, hostsFromMinionList(hosts)) + _, err = balancer.CreateTCPLoadBalancer(service.Name, zone.Region, net.ParseIP(publicIP), service.Spec.Port, hostsFromMinionList(hosts)) if err != nil { - break + // TODO: have to roll-back any successful calls. + return nil, err } } } else { - ip, err = balancer.CreateTCPLoadBalancer(service.Name, zone.Region, nil, service.Spec.Port, hostsFromMinionList(hosts)) + ip, err := balancer.CreateTCPLoadBalancer(service.Name, zone.Region, nil, service.Spec.Port, hostsFromMinionList(hosts)) + if err != nil { + return nil, err + } + service.Spec.PublicIPs = []string{ip.String()} } - if err != nil { - return nil, err - } - service.Spec.PublicIPs = []string{ip.String()} } err := rs.registry.CreateService(ctx, service) if err != nil {