第十九课
PR #150
Add in service registry tests and a Fake Cloud impl.
type FakeCloud struct {
Exists bool
Err error
Calls []string
IP net.IP
}
func (f *FakeCloud) addCall(desc string) {
f.Calls = append(f.Calls, desc)
}
func (f *FakeCloud) ClearCalls() {
f.Calls = []string{}
}
func (f *FakeCloud) TCPLoadBalancer() (TCPLoadBalancer, error) {
return f, nil
}
func (f *FakeCloud) TCPLoadBalancerExists(name, region string) (bool, error) {
return f.Exists, f.Err
}
//--------------------
func TestServiceRegistryExternalService(t *testing.T) {
memory := MakeMemoryRegistry()
fakeCloud := &cloudprovider.FakeCloud{}
machines := []string{"foo", "bar", "baz"}
storage := MakeServiceRegistryStorage(memory, fakeCloud, machines)
svc := api.Service{
JSONBase: api.JSONBase{ID: "foo"},
CreateExternalLoadBalancer: true,
}
storage.Create(svc)
if len(fakeCloud.Calls) != 1 || fakeCloud.Calls[0] != "create" {
t.Errorf("Unexpected call(s): %#v", fakeCloud.Calls)
}
srv, err := memory.GetService(svc.ID)
expectNoError(t, err)
if srv == nil {
t.Errorf("Failed to find service: %s", svc.ID)
}
}
PR #161
Add population of the host IP address when on GCE.
func getInstanceIP(cloud cloudprovider.Interface, host string) string {
if cloud == nil {
return ""
}
instances, ok := cloud.Instances()
if instances == nil || !ok {
return ""
}
ix := strings.Index(host, ".")
if ix != -1 {
host = host[:ix]
}
addr, err := instances.IPAddress(host)
if err != nil {
log.Printf("Error getting instance IP: %#v", err)
return ""
}
return addr.String()
}
func (storage *PodRegistryStorage) Get(id string) (interface{}, error) {
pod, err := storage.registry.GetPod(id)
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"
return pod, err
}
//--------------------------
// CloudInterface is an abstract, pluggable interface for cloud providers
type Interface interface {
// 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)
}
PR #163
Add IP to hostname in cloudcfg printing