Don't use zone for regional load balancers.

This commit is contained in:
Brendan Burns 2014-08-04 09:58:10 -07:00
parent 1d8407cf7d
commit c27ab18481
5 changed files with 37 additions and 13 deletions

View File

@ -51,8 +51,14 @@ type Instances interface {
List(filter string) ([]string, error)
}
// Zone represents the location of a particular machine
type Zone struct {
FailureDomain string
Region string
}
// Zones is an abstract, pluggable interface for zone enumeration.
type Zones interface {
// GetZone returns the name of the current failure zone that the program is running in
GetZone() (string, error)
// GetZone returns the Zone containing the current failure zone and locality region that the program is running in
GetZone() (Zone, error)
}

View File

@ -28,7 +28,7 @@ type FakeCloud struct {
Calls []string
IP net.IP
Machines []string
Zone string
Zone
}
func (f *FakeCloud) addCall(desc string) {
@ -104,7 +104,7 @@ func (f *FakeCloud) List(filter string) ([]string, error) {
return result, f.Err
}
func (f *FakeCloud) GetZone() (string, error) {
func (f *FakeCloud) GetZone() (Zone, error) {
f.addCall("get-zone")
return f.Zone, f.Err
}

View File

@ -237,6 +237,24 @@ func (gce *GCECloud) List(filter string) ([]string, error) {
return instances, nil
}
func (gce *GCECloud) GetZone() (string, error) {
return gce.zone, nil
func (gce *GCECloud) GetZone() (Zone, error) {
region, err := getGceRegion(gce.zone)
if err != nil {
return Zone{}, err
}
return Zone{
FailureDomain: gce.zone,
Region: region,
}, nil
}
// gce zone names are of the form: ${region-name}-${ix}.
// For example "us-central1-b" has a region of "us-central1".
// So we look for the last '-' and trim to just before that.
func getGceRegion(zone string) (string, error) {
ix := strings.LastIndex(zone, "-")
if ix == -1 {
return "", fmt.Errorf("unexpected zone: %s", zone)
}
return zone[:ix], nil
}

View File

@ -26,13 +26,13 @@ func TestGetRegion(t *testing.T) {
}
zones, ok := gce.Zones()
if !ok {
t.Errorf("Unexpected missing zones impl")
t.Fatalf("Unexpected missing zones impl")
}
region, err := zones.GetRegion()
zone, err := zones.GetZone()
if err != nil {
t.Errorf("unexpected error %v (%#v)", err)
t.Fatalf("unexpected error %v", err)
}
if region != "us-central1" {
t.Errorf("Unexpected region: %s", region)
if zone.Region != "us-central1" {
t.Errorf("Unexpected region: %s", zone.Region)
}
}

View File

@ -142,7 +142,7 @@ func (sr *ServiceRegistryStorage) deleteExternalLoadBalancer(service *api.Servic
return err
}
if err := balancer.DeleteTCPLoadBalancer(service.JSONBase.ID, zone); err != nil {
if err := balancer.DeleteTCPLoadBalancer(service.JSONBase.ID, zone.Region); err != nil {
return err
}
@ -194,7 +194,7 @@ func (sr *ServiceRegistryStorage) Create(obj interface{}) (<-chan interface{}, e
if err != nil {
return nil, err
}
err = balancer.CreateTCPLoadBalancer(srv.ID, zone, srv.Port, hosts)
err = balancer.CreateTCPLoadBalancer(srv.ID, zone.Region, srv.Port, hosts)
if err != nil {
return nil, err
}