Merge pull request #765 from brendandburns/service

Add support for regions, and use them for cloud load balancing.
This commit is contained in:
Daniel Smith 2014-08-05 11:59:36 -07:00
commit 2d3373664f
5 changed files with 70 additions and 8 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

@ -0,0 +1,38 @@
/*
Copyright 2014 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cloudprovider
import (
"testing"
)
func TestGetRegion(t *testing.T) {
gce := &GCECloud{
zone: "us-central1-b",
}
zones, ok := gce.Zones()
if !ok {
t.Fatalf("Unexpected missing zones impl")
}
zone, err := zones.GetZone()
if err != nil {
t.Fatalf("unexpected error %v", err)
}
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
}