Add support for populating host ip address.

pull/6/head
Brendan Burns 2014-06-17 22:28:44 -07:00
parent 57869958bc
commit 420b2fdd57
8 changed files with 77 additions and 38 deletions

View File

@ -44,7 +44,7 @@ func main() {
reg := registry.MakeEtcdRegistry(etcdClient, machineList)
apiserver := apiserver.New(map[string]apiserver.RESTStorage{
"pods": registry.MakePodRegistryStorage(reg, &client.FakeContainerInfo{}, registry.MakeRoundRobinScheduler(machineList)),
"pods": registry.MakePodRegistryStorage(reg, &client.FakeContainerInfo{}, registry.MakeRoundRobinScheduler(machineList), nil),
"replicationControllers": registry.MakeControllerRegistryStorage(reg),
}, "/api/v1beta1")
server := httptest.NewServer(apiserver)

View File

@ -22,10 +22,10 @@ import (
// 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)
// Instances returns an instances interface, or nil if none is supported. Returns an error if one occurs.
Instances() (Instances, error)
// TCPLoadBalancer returns a balancer interface. Also returns true if the interface is supported, false otherwise.
TCPLoadBalancer() (TCPLoadBalancer, bool)
// Instances returns an instances interface. Also returns true if the interface is supported, false otherwise.
Instances() (Instances, bool)
}
type TCPLoadBalancer interface {

View File

@ -35,8 +35,12 @@ func (f *FakeCloud) ClearCalls() {
f.Calls = []string{}
}
func (f *FakeCloud) TCPLoadBalancer() (TCPLoadBalancer, error) {
return f, nil
func (f *FakeCloud) TCPLoadBalancer() (TCPLoadBalancer, bool) {
return f, true
}
func (f *FakeCloud) Instances() (Instances, bool) {
return f, true
}
func (f *FakeCloud) TCPLoadBalancerExists(name, region string) (bool, error) {

View File

@ -79,12 +79,12 @@ func NewGCECloud() (*GCECloud, error) {
}, nil
}
func (gce *GCECloud) TCPLoadBalancer() (TCPLoadBalancer, error) {
return gce, nil
func (gce *GCECloud) TCPLoadBalancer() (TCPLoadBalancer, bool) {
return gce, true
}
func (gce *GCECloud) Instances() (Instances, error) {
return gce, nil
func (gce *GCECloud) Instances() (Instances, bool) {
return gce, true
}
func makeHostLink(projectID, zone, host string) string {

View File

@ -72,7 +72,7 @@ func (m *Master) init(minions []string, cloud cloudprovider.Interface) {
m.minions = minions
m.random = rand.New(rand.NewSource(int64(time.Now().Nanosecond())))
m.storage = map[string]apiserver.RESTStorage{
"pods": registry.MakePodRegistryStorage(m.podRegistry, containerInfo, registry.MakeFirstFitScheduler(m.minions, m.podRegistry, m.random)),
"pods": registry.MakePodRegistryStorage(m.podRegistry, containerInfo, registry.MakeFirstFitScheduler(m.minions, m.podRegistry, m.random), cloud),
"replicationControllers": registry.MakeControllerRegistryStorage(m.controllerRegistry),
"services": registry.MakeServiceRegistryStorage(m.serviceRegistry, cloud, m.minions),
}

View File

@ -19,7 +19,6 @@ import (
"encoding/json"
"fmt"
"log"
"net"
"strings"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
@ -73,20 +72,15 @@ func getInstanceIP(cloud cloudprovider.Interface, host string) string {
if cloud == nil {
return ""
}
instances, err := cloud.Instances()
if instances == nil {
return ""
}
if err != nil {
log.Printf("Error getting instances: %#v", err)
instances, ok := cloud.Instances()
if instances == nil || !ok {
return ""
}
ix := strings.Index(host, ".")
if ix != -1 {
host = host[:ix]
}
var addr net.IP
addr, err = instances.IPAddress(host)
addr, err := instances.IPAddress(host)
if err != nil {
log.Printf("Error getting instance IP: %#v", err)
return ""
@ -99,12 +93,17 @@ func (storage *PodRegistryStorage) Get(id string) (interface{}, error) {
if err != nil {
return pod, err
}
if pod == nil {
return pod, nil
}
if storage.containerInfo != nil {
info, err := storage.containerInfo.GetContainerInfo(pod.CurrentState.Host, id)
if err != nil {
return pod, err
}
pod.CurrentState.Info = info
pod.CurrentState.Status = makePodStatus(info)
}
pod.CurrentState.HostIP = getInstanceIP(storage.cloud, pod.CurrentState.Host)
pod.Kind = "cluster#pod"

View File

@ -22,11 +22,13 @@ import (
"testing"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/cloudprovider"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
)
type MockPodRegistry struct {
err error
pod *api.Pod
pods []api.Pod
}
@ -50,7 +52,7 @@ func (registry *MockPodRegistry) ListPods(query labels.Query) ([]api.Pod, error)
}
func (registry *MockPodRegistry) GetPod(podId string) (*api.Pod, error) {
return &api.Pod{}, registry.err
return registry.pod, registry.err
}
func (registry *MockPodRegistry) CreatePod(machine string, pod api.Pod) error {
@ -145,6 +147,45 @@ func TestExtractJson(t *testing.T) {
}
}
func TestGetPod(t *testing.T) {
mockRegistry := MockPodRegistry{
pod: &api.Pod{
JSONBase: api.JSONBase{ID: "foo"},
},
}
storage := PodRegistryStorage{
registry: &mockRegistry,
}
obj, err := storage.Get("foo")
pod := obj.(*api.Pod)
expectNoError(t, err)
if !reflect.DeepEqual(*mockRegistry.pod, *pod) {
t.Errorf("Unexpected pod. Expected %#v, Got %#v", *mockRegistry.pod, *pod)
}
}
func TestGetPodCloud(t *testing.T) {
fakeCloud := &cloudprovider.FakeCloud{}
mockRegistry := MockPodRegistry{
pod: &api.Pod{
JSONBase: api.JSONBase{ID: "foo"},
},
}
storage := PodRegistryStorage{
registry: &mockRegistry,
cloud: fakeCloud,
}
obj, err := storage.Get("foo")
pod := obj.(*api.Pod)
expectNoError(t, err)
if !reflect.DeepEqual(*mockRegistry.pod, *pod) {
t.Errorf("Unexpected pod. Expected %#v, Got %#v", *mockRegistry.pod, *pod)
}
if len(fakeCloud.Calls) != 1 || fakeCloud.Calls[0] != "ip-address" {
t.Errorf("Unexpected calls: %#v", fakeCloud.Calls)
}
}
func TestMakePodStatus(t *testing.T) {
status := makePodStatus(map[string]interface{}{})
if status != "Pending" {

View File

@ -91,13 +91,11 @@ func (sr *ServiceRegistryStorage) Delete(id string) error {
}
if svc.(*api.Service).CreateExternalLoadBalancer {
var balancer cloudprovider.TCPLoadBalancer
var ok bool
if sr.cloud != nil {
balancer, err = sr.cloud.TCPLoadBalancer()
if err != nil {
return err
balancer, ok = sr.cloud.TCPLoadBalancer()
}
}
if balancer != nil {
if ok && balancer != nil {
err = balancer.DeleteTCPLoadBalancer(id, "us-central1")
if err != nil {
return err
@ -118,14 +116,11 @@ func (sr *ServiceRegistryStorage) Create(obj interface{}) error {
srv := obj.(api.Service)
if srv.CreateExternalLoadBalancer {
var balancer cloudprovider.TCPLoadBalancer
var ok bool
if sr.cloud != nil {
var err error
balancer, err = sr.cloud.TCPLoadBalancer()
if err != nil {
return err
balancer, ok = sr.cloud.TCPLoadBalancer()
}
}
if balancer != nil {
if ok && balancer != nil {
err := balancer.CreateTCPLoadBalancer(srv.ID, "us-central1", srv.Port, sr.hosts)
if err != nil {
return err