Skip to content

Commit

Permalink
Change cloud provider CreateTCPLoadBalancer to return endpoint as string
Browse files Browse the repository at this point in the history
Some load balancers (particularly AWS ELB) define the public endpoint
as a hostname (instead of using IP addresses).

This is a partial fix for kubernetes#5224; there will also be some proxy work.
  • Loading branch information
justinsb committed Mar 10, 2015
1 parent a4d871a commit 5d36d3c
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 21 deletions.
3 changes: 3 additions & 0 deletions pkg/api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,9 @@ type ServiceSpec struct {
CreateExternalLoadBalancer bool `json:"createExternalLoadBalancer,omitempty"`
// PublicIPs are used by external load balancers, or can be set by
// users to handle external traffic that arrives at a node.
// For load balancers, the publicIP will usually be the IP address of the load balancer,
// but some load balancers (notably AWS ELB) use a hostname instead of an IP address.
// For hostnames, the user will use a CNAME record (instead of using an A record with the IP)
PublicIPs []string `json:"publicIPs,omitempty"`

// ContainerPort is the name or number of the port on the container to direct traffic to.
Expand Down
4 changes: 2 additions & 2 deletions pkg/cloudprovider/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ type TCPLoadBalancer interface {
// TCPLoadBalancerExists returns whether the specified load balancer exists.
// TODO: Break this up into different interfaces (LB, etc) when we have more than one type of service
TCPLoadBalancerExists(name, region string) (bool, error)
// CreateTCPLoadBalancer creates a new tcp load balancer. Returns the IP address of the balancer
CreateTCPLoadBalancer(name, region string, externalIP net.IP, port int, hosts []string, affinityType api.AffinityType) (net.IP, error)
// CreateTCPLoadBalancer creates a new tcp load balancer. Returns the IP address or hostname of the balancer
CreateTCPLoadBalancer(name, region string, externalIP net.IP, port int, hosts []string, affinityType api.AffinityType) (string, error)
// UpdateTCPLoadBalancer updates hosts under the specified load balancer.
UpdateTCPLoadBalancer(name, region string, hosts []string) error
// DeleteTCPLoadBalancer deletes a specified load balancer.
Expand Down
4 changes: 2 additions & 2 deletions pkg/cloudprovider/fake/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ func (f *FakeCloud) TCPLoadBalancerExists(name, region string) (bool, error) {

// CreateTCPLoadBalancer is a test-spy implementation of TCPLoadBalancer.CreateTCPLoadBalancer.
// It adds an entry "create" into the internal method call record.
func (f *FakeCloud) CreateTCPLoadBalancer(name, region string, externalIP net.IP, port int, hosts []string, affinityType api.AffinityType) (net.IP, error) {
func (f *FakeCloud) CreateTCPLoadBalancer(name, region string, externalIP net.IP, port int, hosts []string, affinityType api.AffinityType) (string, error) {
f.addCall("create")
f.Balancers = append(f.Balancers, FakeBalancer{name, region, externalIP, port, hosts})
return f.ExternalIP, f.Err
return f.ExternalIP.String(), f.Err
}

// UpdateTCPLoadBalancer is a test-spy implementation of TCPLoadBalancer.UpdateTCPLoadBalancer.
Expand Down
12 changes: 6 additions & 6 deletions pkg/cloudprovider/gce/gce.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,10 @@ func translateAffinityType(affinityType api.AffinityType) GCEAffinityType {
}

// CreateTCPLoadBalancer is an implementation of TCPLoadBalancer.CreateTCPLoadBalancer.
func (gce *GCECloud) CreateTCPLoadBalancer(name, region string, externalIP net.IP, port int, hosts []string, affinityType api.AffinityType) (net.IP, error) {
func (gce *GCECloud) CreateTCPLoadBalancer(name, region string, externalIP net.IP, port int, hosts []string, affinityType api.AffinityType) (string, error) {
pool, err := gce.makeTargetPool(name, region, hosts, translateAffinityType(affinityType))
if err != nil {
return nil, err
return "", err
}
req := &compute.ForwardingRule{
Name: name,
Expand All @@ -238,17 +238,17 @@ func (gce *GCECloud) CreateTCPLoadBalancer(name, region string, externalIP net.I
}
op, err := gce.service.ForwardingRules.Insert(gce.projectID, region, req).Do()
if err != nil {
return nil, err
return "", err
}
err = gce.waitForRegionOp(op, region)
if err != nil {
return nil, err
return "", err
}
fwd, err := gce.service.ForwardingRules.Get(gce.projectID, region, name).Do()
if err != nil {
return nil, err
return "", err
}
return net.ParseIP(fwd.IPAddress), nil
return fwd.IPAddress, nil
}

// UpdateTCPLoadBalancer is an implementation of TCPLoadBalancer.UpdateTCPLoadBalancer.
Expand Down
18 changes: 9 additions & 9 deletions pkg/cloudprovider/openstack/openstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,24 +425,24 @@ func (lb *LoadBalancer) TCPLoadBalancerExists(name, region string) (bool, error)
// a list of regions (from config) and query/create loadbalancers in
// each region.

func (lb *LoadBalancer) CreateTCPLoadBalancer(name, region string, externalIP net.IP, port int, hosts []string, affinity api.AffinityType) (net.IP, error) {
func (lb *LoadBalancer) CreateTCPLoadBalancer(name, region string, externalIP net.IP, port int, hosts []string, affinity api.AffinityType) (string, error) {
glog.V(2).Infof("CreateTCPLoadBalancer(%v, %v, %v, %v, %v)", name, region, externalIP, port, hosts)
if affinity != api.AffinityTypeNone {
return nil, fmt.Errorf("unsupported load balancer affinity: %v", affinity)
return "", fmt.Errorf("unsupported load balancer affinity: %v", affinity)
}
pool, err := pools.Create(lb.network, pools.CreateOpts{
Name: name,
Protocol: pools.ProtocolTCP,
SubnetID: lb.opts.SubnetId,
}).Extract()
if err != nil {
return nil, err
return "", err
}

for _, host := range hosts {
addr, err := getAddressByName(lb.compute, host)
if err != nil {
return nil, err
return "", err
}

_, err = members.Create(lb.network, members.CreateOpts{
Expand All @@ -452,7 +452,7 @@ func (lb *LoadBalancer) CreateTCPLoadBalancer(name, region string, externalIP ne
}).Extract()
if err != nil {
pools.Delete(lb.network, pool.ID)
return nil, err
return "", err
}
}

Expand All @@ -466,14 +466,14 @@ func (lb *LoadBalancer) CreateTCPLoadBalancer(name, region string, externalIP ne
}).Extract()
if err != nil {
pools.Delete(lb.network, pool.ID)
return nil, err
return "", err
}

_, err = pools.AssociateMonitor(lb.network, pool.ID, mon.ID).Extract()
if err != nil {
monitors.Delete(lb.network, mon.ID)
pools.Delete(lb.network, pool.ID)
return nil, err
return "", err
}
}

Expand All @@ -490,10 +490,10 @@ func (lb *LoadBalancer) CreateTCPLoadBalancer(name, region string, externalIP ne
monitors.Delete(lb.network, mon.ID)
}
pools.Delete(lb.network, pool.ID)
return nil, err
return "", err
}

return net.ParseIP(vip.Address), nil
return vip.Address, nil
}

func (lb *LoadBalancer) UpdateTCPLoadBalancer(name, region string, hosts []string) error {
Expand Down
4 changes: 2 additions & 2 deletions pkg/registry/service/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,11 @@ func (rs *REST) createExternalLoadBalancer(ctx api.Context, service *api.Service
}
}
} else {
ip, err := balancer.CreateTCPLoadBalancer(name, zone.Region, nil, service.Spec.Port, hostsFromMinionList(hosts), affinityType)
endpoint, err := balancer.CreateTCPLoadBalancer(name, zone.Region, nil, service.Spec.Port, hostsFromMinionList(hosts), affinityType)
if err != nil {
return err
}
service.Spec.PublicIPs = []string{ip.String()}
service.Spec.PublicIPs = []string{endpoint}
}
return nil
}
Expand Down

0 comments on commit 5d36d3c

Please sign in to comment.