mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-27 13:37:30 +00:00
Merge pull request #765 from brendandburns/service
Add support for regions, and use them for cloud load balancing.
This commit is contained in:
commit
2d3373664f
@ -51,8 +51,14 @@ type Instances interface {
|
|||||||
List(filter string) ([]string, error)
|
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.
|
// Zones is an abstract, pluggable interface for zone enumeration.
|
||||||
type Zones interface {
|
type Zones interface {
|
||||||
// GetZone returns the name of the current failure zone that the program is running in
|
// GetZone returns the Zone containing the current failure zone and locality region that the program is running in
|
||||||
GetZone() (string, error)
|
GetZone() (Zone, error)
|
||||||
}
|
}
|
||||||
|
@ -28,7 +28,7 @@ type FakeCloud struct {
|
|||||||
Calls []string
|
Calls []string
|
||||||
IP net.IP
|
IP net.IP
|
||||||
Machines []string
|
Machines []string
|
||||||
Zone string
|
Zone
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *FakeCloud) addCall(desc string) {
|
func (f *FakeCloud) addCall(desc string) {
|
||||||
@ -104,7 +104,7 @@ func (f *FakeCloud) List(filter string) ([]string, error) {
|
|||||||
return result, f.Err
|
return result, f.Err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *FakeCloud) GetZone() (string, error) {
|
func (f *FakeCloud) GetZone() (Zone, error) {
|
||||||
f.addCall("get-zone")
|
f.addCall("get-zone")
|
||||||
return f.Zone, f.Err
|
return f.Zone, f.Err
|
||||||
}
|
}
|
||||||
|
@ -237,6 +237,24 @@ func (gce *GCECloud) List(filter string) ([]string, error) {
|
|||||||
return instances, nil
|
return instances, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gce *GCECloud) GetZone() (string, error) {
|
func (gce *GCECloud) GetZone() (Zone, error) {
|
||||||
return gce.zone, nil
|
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
|
||||||
}
|
}
|
||||||
|
38
pkg/cloudprovider/gce_test.go
Normal file
38
pkg/cloudprovider/gce_test.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
@ -142,7 +142,7 @@ func (sr *ServiceRegistryStorage) deleteExternalLoadBalancer(service *api.Servic
|
|||||||
return err
|
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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -194,7 +194,7 @@ func (sr *ServiceRegistryStorage) Create(obj interface{}) (<-chan interface{}, e
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user