forked from kubernetes/kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add load balancing support to services.
- Loading branch information
1 parent
23e736c
commit 2759b23
Showing
351 changed files
with
318 additions
and
370 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,7 @@ | |
|
||
# Go test binaries | ||
*.test | ||
|
||
# Mercurial files | ||
**/.hg | ||
**/.hg* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"id": "example", | ||
"port": 8000, | ||
"labels": { | ||
"name": "nginx" | ||
}, | ||
"createExternalLoadBalancer": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"id": "example2", | ||
"id": "example", | ||
"port": 8000, | ||
"labels": { | ||
"name": "nginx" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
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 | ||
|
||
// CloudInterface is an abstract, pluggable interface for cloud providers | ||
type Interface interface { | ||
// TCPLoadBalancer returns a balancer interface, or nil if none is supported. Returns an error if one occurs. | ||
TCPLoadBalancer() (TCPLoadBalancer, error) | ||
} | ||
|
||
type TCPLoadBalancer interface { | ||
// 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(name, region string, port int, hosts []string) error | ||
UpdateTCPLoadBalancer(name, region string, hosts []string) error | ||
DeleteTCPLoadBalancer(name, region string) error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
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 supplies interfaces and implementations for cloud service providers | ||
package cloudprovider | ||
|
||
import () |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
/* | ||
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 ( | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"code.google.com/p/goauth2/compute/serviceaccount" | ||
compute "code.google.com/p/google-api-go-client/compute/v1" | ||
) | ||
|
||
type GCECloud struct { | ||
service *compute.Service | ||
projectID string | ||
zone string | ||
} | ||
|
||
func getProjectAndZone() (string, string, error) { | ||
client := http.Client{} | ||
url := "http://metadata/computeMetadata/v1/instance/zone" | ||
req, err := http.NewRequest("GET", url, nil) | ||
if err != nil { | ||
return "", "", err | ||
} | ||
req.Header.Add("X-Google-Metadata-Request", "True") | ||
res, err := client.Do(req) | ||
if err != nil { | ||
return "", "", err | ||
} | ||
defer res.Body.Close() | ||
data, err := ioutil.ReadAll(res.Body) | ||
if err != nil { | ||
return "", "", err | ||
} | ||
parts := strings.Split(string(data), "/") | ||
if len(parts) != 4 { | ||
return "", "", fmt.Errorf("Unexpected response: %s", string(data)) | ||
} | ||
return parts[1], parts[3], nil | ||
} | ||
|
||
func NewGCECloud() (*GCECloud, error) { | ||
projectID, zone, err := getProjectAndZone() | ||
if err != nil { | ||
return nil, err | ||
} | ||
client, err := serviceaccount.NewClient(&serviceaccount.Options{}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
svc, err := compute.New(client) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &GCECloud{ | ||
service: svc, | ||
projectID: projectID, | ||
zone: zone, | ||
}, nil | ||
} | ||
|
||
func (gce *GCECloud) TCPLoadBalancer() (TCPLoadBalancer, error) { | ||
return gce, nil | ||
} | ||
|
||
func makeHostLink(projectID, zone, host string) string { | ||
ix := strings.Index(host, ".") | ||
if ix != -1 { | ||
host = host[:ix] | ||
} | ||
return fmt.Sprintf("https://www.googleapis.com/compute/v1/projects/%s/zones/%s/instances/%s", | ||
projectID, zone, host) | ||
} | ||
|
||
func (gce *GCECloud) makeTargetPool(name, region string, hosts []string) (string, error) { | ||
var instances []string | ||
for _, host := range hosts { | ||
instances = append(instances, makeHostLink(gce.projectID, gce.zone, host)) | ||
} | ||
pool := &compute.TargetPool{ | ||
Name: name, | ||
Instances: instances, | ||
} | ||
_, err := gce.service.TargetPools.Insert(gce.projectID, region, pool).Do() | ||
if err != nil { | ||
return "", err | ||
} | ||
link := fmt.Sprintf("https://www.googleapis.com/compute/v1/projects/%s/regions/%s/targetPools/%s", gce.projectID, region, name) | ||
return link, nil | ||
} | ||
|
||
func (gce *GCECloud) waitForRegionOp(op *compute.Operation, region string) error { | ||
pollOp := op | ||
for pollOp.Status != "DONE" { | ||
var err error | ||
time.Sleep(time.Second * 10) | ||
pollOp, err = gce.service.RegionOperations.Get(gce.projectID, region, op.Name).Do() | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (gce *GCECloud) TCPLoadBalancerExists(name, region string) (bool, error) { | ||
_, err := gce.service.ForwardingRules.Get(gce.projectID, region, name).Do() | ||
return false, err | ||
} | ||
|
||
func (gce *GCECloud) CreateTCPLoadBalancer(name, region string, port int, hosts []string) error { | ||
pool, err := gce.makeTargetPool(name, region, hosts) | ||
if err != nil { | ||
return err | ||
} | ||
req := &compute.ForwardingRule{ | ||
Name: name, | ||
IPProtocol: "TCP", | ||
PortRange: strconv.Itoa(port), | ||
Target: pool, | ||
} | ||
_, err = gce.service.ForwardingRules.Insert(gce.projectID, region, req).Do() | ||
return err | ||
} | ||
|
||
func (gce *GCECloud) UpdateTCPLoadBalancer(name, region string, hosts []string) error { | ||
var refs []*compute.InstanceReference | ||
for _, host := range hosts { | ||
refs = append(refs, &compute.InstanceReference{host}) | ||
} | ||
req := &compute.TargetPoolsAddInstanceRequest{ | ||
Instances: refs, | ||
} | ||
|
||
_, err := gce.service.TargetPools.AddInstance(gce.projectID, region, name, req).Do() | ||
return err | ||
} | ||
|
||
func (gce *GCECloud) DeleteTCPLoadBalancer(name, region string) error { | ||
_, err := gce.service.ForwardingRules.Delete(gce.projectID, region, name).Do() | ||
if err != nil { | ||
return err | ||
} | ||
_, err = gce.service.TargetPools.Delete(gce.projectID, region, name).Do() | ||
return err | ||
} |
Oops, something went wrong.