Merge pull request #1099 from smarterclayton/return_resource_version_on_list

Add the resource version to api.*List items from etcd
This commit is contained in:
Daniel Smith 2014-08-29 13:41:46 -07:00
commit 9928c40ea6
17 changed files with 120 additions and 89 deletions

View File

@ -77,7 +77,7 @@ func (p *PodCache) UpdateAllContainers() {
glog.Errorf("Error synchronizing container list: %v", err) glog.Errorf("Error synchronizing container list: %v", err)
return return
} }
for _, pod := range pods { for _, pod := range pods.Items {
err := p.updatePodInfo(pod.CurrentState.Host, pod.ID) err := p.updatePodInfo(pod.CurrentState.Host, pod.ID)
if err != nil && err != client.ErrPodInfoNotAvailable { if err != nil && err != client.ErrPodInfoNotAvailable {
glog.Errorf("Error synchronizing container: %v", err) glog.Errorf("Error synchronizing container: %v", err)

View File

@ -96,7 +96,7 @@ func TestPodUpdateAllContainers(t *testing.T) {
} }
pods := []api.Pod{pod} pods := []api.Pod{pod}
mockRegistry := registrytest.NewPodRegistry(pods) mockRegistry := registrytest.NewPodRegistry(&api.PodList{Items: pods})
expected := api.PodInfo{"foo": docker.Container{ID: "foo"}} expected := api.PodInfo{"foo": docker.Container{ID: "foo"}}
fake := FakePodInfoGetter{ fake := FakePodInfoGetter{

View File

@ -23,7 +23,7 @@ import (
// Registry is an interface for things that know how to store ReplicationControllers. // Registry is an interface for things that know how to store ReplicationControllers.
type Registry interface { type Registry interface {
ListControllers() ([]api.ReplicationController, error) ListControllers() (*api.ReplicationControllerList, error)
WatchControllers(resourceVersion uint64) (watch.Interface, error) WatchControllers(resourceVersion uint64) (watch.Interface, error)
GetController(controllerID string) (*api.ReplicationController, error) GetController(controllerID string) (*api.ReplicationController, error)
CreateController(controller api.ReplicationController) error CreateController(controller api.ReplicationController) error

View File

@ -92,16 +92,18 @@ func (rs *RegistryStorage) Get(id string) (interface{}, error) {
// List obtains a list of ReplicationControllers that match selector. // List obtains a list of ReplicationControllers that match selector.
func (rs *RegistryStorage) List(selector labels.Selector) (interface{}, error) { func (rs *RegistryStorage) List(selector labels.Selector) (interface{}, error) {
result := api.ReplicationControllerList{}
controllers, err := rs.registry.ListControllers() controllers, err := rs.registry.ListControllers()
if err == nil { if err != nil {
for _, controller := range controllers { return nil, err
if selector.Matches(labels.Set(controller.Labels)) { }
result.Items = append(result.Items, controller) filtered := []api.ReplicationController{}
} for _, controller := range controllers.Items {
if selector.Matches(labels.Set(controller.Labels)) {
filtered = append(filtered, controller)
} }
} }
return result, err controllers.Items = filtered
return controllers, err
} }
// New creates a new ReplicationController for use with Create and Update. // New creates a new ReplicationController for use with Create and Update.
@ -150,7 +152,7 @@ func (rs *RegistryStorage) waitForController(ctrl api.ReplicationController) (in
if err != nil { if err != nil {
return ctrl, err return ctrl, err
} }
if len(pods) == ctrl.DesiredState.Replicas { if len(pods.Items) == ctrl.DesiredState.Replicas {
break break
} }
time.Sleep(rs.pollPeriod) time.Sleep(rs.pollPeriod)

View File

@ -37,18 +37,17 @@ func TestListControllersError(t *testing.T) {
storage := RegistryStorage{ storage := RegistryStorage{
registry: &mockRegistry, registry: &mockRegistry,
} }
controllersObj, err := storage.List(nil) controllers, err := storage.List(nil)
controllers := controllersObj.(api.ReplicationControllerList)
if err != mockRegistry.Err { if err != mockRegistry.Err {
t.Errorf("Expected %#v, Got %#v", mockRegistry.Err, err) t.Errorf("Expected %#v, Got %#v", mockRegistry.Err, err)
} }
if len(controllers.Items) != 0 { if controllers != nil {
t.Errorf("Unexpected non-zero ctrl list: %#v", controllers) t.Errorf("Unexpected non-nil ctrl list: %#v", controllers)
} }
} }
func TestListEmptyControllerList(t *testing.T) { func TestListEmptyControllerList(t *testing.T) {
mockRegistry := registrytest.ControllerRegistry{} mockRegistry := registrytest.ControllerRegistry{nil, &api.ReplicationControllerList{JSONBase: api.JSONBase{ResourceVersion: 1}}}
storage := RegistryStorage{ storage := RegistryStorage{
registry: &mockRegistry, registry: &mockRegistry,
} }
@ -57,22 +56,27 @@ func TestListEmptyControllerList(t *testing.T) {
t.Errorf("unexpected error: %v", err) t.Errorf("unexpected error: %v", err)
} }
if len(controllers.(api.ReplicationControllerList).Items) != 0 { if len(controllers.(*api.ReplicationControllerList).Items) != 0 {
t.Errorf("Unexpected non-zero ctrl list: %#v", controllers) t.Errorf("Unexpected non-zero ctrl list: %#v", controllers)
} }
if controllers.(*api.ReplicationControllerList).ResourceVersion != 1 {
t.Errorf("Unexpected resource version: %#v", controllers)
}
} }
func TestListControllerList(t *testing.T) { func TestListControllerList(t *testing.T) {
mockRegistry := registrytest.ControllerRegistry{ mockRegistry := registrytest.ControllerRegistry{
Controllers: []api.ReplicationController{ Controllers: &api.ReplicationControllerList{
{ Items: []api.ReplicationController{
JSONBase: api.JSONBase{ {
ID: "foo", JSONBase: api.JSONBase{
ID: "foo",
},
}, },
}, {
{ JSONBase: api.JSONBase{
JSONBase: api.JSONBase{ ID: "bar",
ID: "bar", },
}, },
}, },
}, },
@ -81,7 +85,7 @@ func TestListControllerList(t *testing.T) {
registry: &mockRegistry, registry: &mockRegistry,
} }
controllersObj, err := storage.List(labels.Everything()) controllersObj, err := storage.List(labels.Everything())
controllers := controllersObj.(api.ReplicationControllerList) controllers := controllersObj.(*api.ReplicationControllerList)
if err != nil { if err != nil {
t.Errorf("unexpected error: %v", err) t.Errorf("unexpected error: %v", err)
} }
@ -212,10 +216,12 @@ var validPodTemplate = api.PodTemplate{
func TestCreateController(t *testing.T) { func TestCreateController(t *testing.T) {
mockRegistry := registrytest.ControllerRegistry{} mockRegistry := registrytest.ControllerRegistry{}
mockPodRegistry := registrytest.PodRegistry{ mockPodRegistry := registrytest.PodRegistry{
Pods: []api.Pod{ Pods: &api.PodList{
{ Items: []api.Pod{
JSONBase: api.JSONBase{ID: "foo"}, {
Labels: map[string]string{"a": "b"}, JSONBase: api.JSONBase{ID: "foo"},
Labels: map[string]string{"a": "b"},
},
}, },
}, },
} }

View File

@ -59,22 +59,24 @@ func makePodKey(podID string) string {
} }
// ListPods obtains a list of pods that match selector. // ListPods obtains a list of pods that match selector.
func (r *Registry) ListPods(selector labels.Selector) ([]api.Pod, error) { func (r *Registry) ListPods(selector labels.Selector) (*api.PodList, error) {
allPods := []api.Pod{} allPods := api.PodList{}
filteredPods := []api.Pod{} err := r.ExtractList("/registry/pods", &allPods.Items, &allPods.ResourceVersion)
if err := r.ExtractList("/registry/pods", &allPods); err != nil { if err != nil {
return nil, err return nil, err
} }
for _, pod := range allPods { filtered := []api.Pod{}
for _, pod := range allPods.Items {
if selector.Matches(labels.Set(pod.Labels)) { if selector.Matches(labels.Set(pod.Labels)) {
// TODO: Currently nothing sets CurrentState.Host. We need a feedback loop that sets // TODO: Currently nothing sets CurrentState.Host. We need a feedback loop that sets
// the CurrentState.Host and Status fields. Here we pretend that reality perfectly // the CurrentState.Host and Status fields. Here we pretend that reality perfectly
// matches our desires. // matches our desires.
pod.CurrentState.Host = pod.DesiredState.Host pod.CurrentState.Host = pod.DesiredState.Host
filteredPods = append(filteredPods, pod) filtered = append(filtered, pod)
} }
} }
return filteredPods, nil allPods.Items = filtered
return &allPods, nil
} }
// WatchPods begins watching for new, changed, or deleted pods. // WatchPods begins watching for new, changed, or deleted pods.
@ -225,9 +227,9 @@ func (r *Registry) DeletePod(podID string) error {
} }
// ListControllers obtains a list of ReplicationControllers. // ListControllers obtains a list of ReplicationControllers.
func (r *Registry) ListControllers() ([]api.ReplicationController, error) { func (r *Registry) ListControllers() (*api.ReplicationControllerList, error) {
var controllers []api.ReplicationController controllers := &api.ReplicationControllerList{}
err := r.ExtractList("/registry/controllers", &controllers) err := r.ExtractList("/registry/controllers", &controllers.Items, &controllers.ResourceVersion)
return controllers, err return controllers, err
} }
@ -283,9 +285,9 @@ func makeServiceKey(name string) string {
} }
// ListServices obtains a list of Services. // ListServices obtains a list of Services.
func (r *Registry) ListServices() (api.ServiceList, error) { func (r *Registry) ListServices() (*api.ServiceList, error) {
var list api.ServiceList list := &api.ServiceList{}
err := r.ExtractList("/registry/services/specs", &list.Items) err := r.ExtractList("/registry/services/specs", &list.Items, &list.ResourceVersion)
return list, err return list, err
} }

View File

@ -412,7 +412,7 @@ func TestEtcdEmptyListPods(t *testing.T) {
t.Errorf("unexpected error: %v", err) t.Errorf("unexpected error: %v", err)
} }
if len(pods) != 0 { if len(pods.Items) != 0 {
t.Errorf("Unexpected pod list: %#v", pods) t.Errorf("Unexpected pod list: %#v", pods)
} }
} }
@ -430,7 +430,7 @@ func TestEtcdListPodsNotFound(t *testing.T) {
t.Errorf("unexpected error: %v", err) t.Errorf("unexpected error: %v", err)
} }
if len(pods) != 0 { if len(pods.Items) != 0 {
t.Errorf("Unexpected pod list: %#v", pods) t.Errorf("Unexpected pod list: %#v", pods)
} }
} }
@ -465,11 +465,11 @@ func TestEtcdListPods(t *testing.T) {
t.Errorf("unexpected error: %v", err) t.Errorf("unexpected error: %v", err)
} }
if len(pods) != 2 || pods[0].ID != "foo" || pods[1].ID != "bar" { if len(pods.Items) != 2 || pods.Items[0].ID != "foo" || pods.Items[1].ID != "bar" {
t.Errorf("Unexpected pod list: %#v", pods) t.Errorf("Unexpected pod list: %#v", pods)
} }
if pods[0].CurrentState.Host != "machine" || if pods.Items[0].CurrentState.Host != "machine" ||
pods[1].CurrentState.Host != "machine" { pods.Items[1].CurrentState.Host != "machine" {
t.Errorf("Failed to populate host name.") t.Errorf("Failed to populate host name.")
} }
} }
@ -487,7 +487,7 @@ func TestEtcdListControllersNotFound(t *testing.T) {
t.Errorf("unexpected error: %v", err) t.Errorf("unexpected error: %v", err)
} }
if len(controllers) != 0 { if len(controllers.Items) != 0 {
t.Errorf("Unexpected controller list: %#v", controllers) t.Errorf("Unexpected controller list: %#v", controllers)
} }
} }
@ -534,7 +534,7 @@ func TestEtcdListControllers(t *testing.T) {
t.Errorf("unexpected error: %v", err) t.Errorf("unexpected error: %v", err)
} }
if len(controllers) != 2 || controllers[0].ID != "foo" || controllers[1].ID != "bar" { if len(controllers.Items) != 2 || controllers.Items[0].ID != "foo" || controllers.Items[1].ID != "bar" {
t.Errorf("Unexpected controller list: %#v", controllers) t.Errorf("Unexpected controller list: %#v", controllers)
} }
} }

View File

@ -25,7 +25,7 @@ import (
// Registry is an interface implemented by things that know how to store Pod objects. // Registry is an interface implemented by things that know how to store Pod objects.
type Registry interface { type Registry interface {
// ListPods obtains a list of pods that match selector. // ListPods obtains a list of pods that match selector.
ListPods(selector labels.Selector) ([]api.Pod, error) ListPods(selector labels.Selector) (*api.PodList, error)
// Watch for new/changed/deleted pods // Watch for new/changed/deleted pods
WatchPods(resourceVersion uint64, filter func(*api.Pod) bool) (watch.Interface, error) WatchPods(resourceVersion uint64, filter func(*api.Pod) bool) (watch.Interface, error)
// Get a specific pod // Get a specific pod

View File

@ -105,15 +105,13 @@ func (rs *RegistryStorage) Get(id string) (interface{}, error) {
} }
func (rs *RegistryStorage) List(selector labels.Selector) (interface{}, error) { func (rs *RegistryStorage) List(selector labels.Selector) (interface{}, error) {
var result api.PodList
pods, err := rs.registry.ListPods(selector) pods, err := rs.registry.ListPods(selector)
if err == nil { if err == nil {
result.Items = pods for i := range pods.Items {
for i := range result.Items { rs.fillPodInfo(&pods.Items[i])
rs.fillPodInfo(&result.Items[i])
} }
} }
return result, err return pods, err
} }
// Watch begins watching for new, changed, or deleted pods. // Watch begins watching for new, changed, or deleted pods.

View File

@ -108,13 +108,13 @@ func TestListPodsError(t *testing.T) {
if err != podRegistry.Err { if err != podRegistry.Err {
t.Errorf("Expected %#v, Got %#v", podRegistry.Err, err) t.Errorf("Expected %#v, Got %#v", podRegistry.Err, err)
} }
if len(pods.(api.PodList).Items) != 0 { if pods.(*api.PodList) != nil {
t.Errorf("Unexpected non-zero pod list: %#v", pods) t.Errorf("Unexpected non-nil pod list: %#v", pods)
} }
} }
func TestListEmptyPodList(t *testing.T) { func TestListEmptyPodList(t *testing.T) {
podRegistry := registrytest.NewPodRegistry(nil) podRegistry := registrytest.NewPodRegistry(&api.PodList{JSONBase: api.JSONBase{ResourceVersion: 1}})
storage := RegistryStorage{ storage := RegistryStorage{
registry: podRegistry, registry: podRegistry,
} }
@ -123,22 +123,27 @@ func TestListEmptyPodList(t *testing.T) {
t.Errorf("unexpected error: %v", err) t.Errorf("unexpected error: %v", err)
} }
if len(pods.(api.PodList).Items) != 0 { if len(pods.(*api.PodList).Items) != 0 {
t.Errorf("Unexpected non-zero pod list: %#v", pods) t.Errorf("Unexpected non-zero pod list: %#v", pods)
} }
if pods.(*api.PodList).ResourceVersion != 1 {
t.Errorf("Unexpected resource version: %#v", pods)
}
} }
func TestListPodList(t *testing.T) { func TestListPodList(t *testing.T) {
podRegistry := registrytest.NewPodRegistry(nil) podRegistry := registrytest.NewPodRegistry(nil)
podRegistry.Pods = []api.Pod{ podRegistry.Pods = &api.PodList{
{ Items: []api.Pod{
JSONBase: api.JSONBase{ {
ID: "foo", JSONBase: api.JSONBase{
ID: "foo",
},
}, },
}, {
{ JSONBase: api.JSONBase{
JSONBase: api.JSONBase{ ID: "bar",
ID: "bar", },
}, },
}, },
} }
@ -146,7 +151,7 @@ func TestListPodList(t *testing.T) {
registry: podRegistry, registry: podRegistry,
} }
podsObj, err := storage.List(labels.Everything()) podsObj, err := storage.List(labels.Everything())
pods := podsObj.(api.PodList) pods := podsObj.(*api.PodList)
if err != nil { if err != nil {
t.Errorf("unexpected error: %v", err) t.Errorf("unexpected error: %v", err)
} }

View File

@ -24,10 +24,10 @@ import (
// TODO: Why do we have this AND MemoryRegistry? // TODO: Why do we have this AND MemoryRegistry?
type ControllerRegistry struct { type ControllerRegistry struct {
Err error Err error
Controllers []api.ReplicationController Controllers *api.ReplicationControllerList
} }
func (r *ControllerRegistry) ListControllers() ([]api.ReplicationController, error) { func (r *ControllerRegistry) ListControllers() (*api.ReplicationControllerList, error) {
return r.Controllers, r.Err return r.Controllers, r.Err
} }

View File

@ -27,32 +27,34 @@ import (
type PodRegistry struct { type PodRegistry struct {
Err error Err error
Pod *api.Pod Pod *api.Pod
Pods []api.Pod Pods *api.PodList
sync.Mutex sync.Mutex
mux *watch.Mux mux *watch.Mux
} }
func NewPodRegistry(pods []api.Pod) *PodRegistry { func NewPodRegistry(pods *api.PodList) *PodRegistry {
return &PodRegistry{ return &PodRegistry{
Pods: pods, Pods: pods,
mux: watch.NewMux(0), mux: watch.NewMux(0),
} }
} }
func (r *PodRegistry) ListPods(selector labels.Selector) ([]api.Pod, error) { func (r *PodRegistry) ListPods(selector labels.Selector) (*api.PodList, error) {
r.Lock() r.Lock()
defer r.Unlock() defer r.Unlock()
if r.Err != nil { if r.Err != nil {
return r.Pods, r.Err return nil, r.Err
} }
var filtered []api.Pod var filtered []api.Pod
for _, pod := range r.Pods { for _, pod := range r.Pods.Items {
if selector.Matches(labels.Set(pod.Labels)) { if selector.Matches(labels.Set(pod.Labels)) {
filtered = append(filtered, pod) filtered = append(filtered, pod)
} }
} }
return filtered, nil pods := *r.Pods
pods.Items = filtered
return &pods, nil
} }
func (r *PodRegistry) WatchPods(resourceVersion uint64, filter func(*api.Pod) bool) (watch.Interface, error) { func (r *PodRegistry) WatchPods(resourceVersion uint64, filter func(*api.Pod) bool) (watch.Interface, error) {

View File

@ -37,8 +37,8 @@ type ServiceRegistry struct {
UpdatedID string UpdatedID string
} }
func (r *ServiceRegistry) ListServices() (api.ServiceList, error) { func (r *ServiceRegistry) ListServices() (*api.ServiceList, error) {
return r.List, r.Err return &r.List, r.Err
} }
func (r *ServiceRegistry) CreateService(svc api.Service) error { func (r *ServiceRegistry) CreateService(svc api.Service) error {

View File

@ -25,7 +25,7 @@ import (
// Registry is an interface for things that know how to store services. // Registry is an interface for things that know how to store services.
type Registry interface { type Registry interface {
ListServices() (api.ServiceList, error) ListServices() (*api.ServiceList, error)
CreateService(svc api.Service) error CreateService(svc api.Service) error
GetService(name string) (*api.Service, error) GetService(name string) (*api.Service, error)
DeleteService(name string) error DeleteService(name string) error

View File

@ -317,8 +317,9 @@ func TestServiceRegistryList(t *testing.T) {
JSONBase: api.JSONBase{ID: "foo2"}, JSONBase: api.JSONBase{ID: "foo2"},
Selector: map[string]string{"bar2": "baz2"}, Selector: map[string]string{"bar2": "baz2"},
}) })
registry.List.ResourceVersion = 1
s, _ := storage.List(labels.Everything()) s, _ := storage.List(labels.Everything())
sl := s.(api.ServiceList) sl := s.(*api.ServiceList)
if len(fakeCloud.Calls) != 0 { if len(fakeCloud.Calls) != 0 {
t.Errorf("Unexpected call(s): %#v", fakeCloud.Calls) t.Errorf("Unexpected call(s): %#v", fakeCloud.Calls)
} }
@ -331,4 +332,7 @@ func TestServiceRegistryList(t *testing.T) {
if e, a := "foo2", sl.Items[1].ID; e != a { if e, a := "foo2", sl.Items[1].ID; e != a {
t.Errorf("Expected %v, but got %v", e, a) t.Errorf("Expected %v, but got %v", e, a)
} }
if sl.ResourceVersion != 1 {
t.Errorf("Unexpected resource version: %#v", sl)
}
} }

View File

@ -117,22 +117,29 @@ func etcdErrorIndex(err error) (uint64, bool) {
return 0, false return 0, false
} }
func (h *EtcdHelper) listEtcdNode(key string) ([]*etcd.Node, error) { func (h *EtcdHelper) listEtcdNode(key string) ([]*etcd.Node, uint64, error) {
result, err := h.Client.Get(key, false, true) result, err := h.Client.Get(key, false, true)
if err != nil { if err != nil {
index, ok := etcdErrorIndex(err)
if !ok {
index = 0
}
nodes := make([]*etcd.Node, 0) nodes := make([]*etcd.Node, 0)
if IsEtcdNotFound(err) { if IsEtcdNotFound(err) {
return nodes, nil return nodes, index, nil
} else { } else {
return nodes, err return nodes, index, err
} }
} }
return result.Node.Nodes, nil return result.Node.Nodes, result.EtcdIndex, nil
} }
// Extract a go object per etcd node into a slice. // Extract a go object per etcd node into a slice with the resource version.
func (h *EtcdHelper) ExtractList(key string, slicePtr interface{}) error { func (h *EtcdHelper) ExtractList(key string, slicePtr interface{}, resourceVersion *uint64) error {
nodes, err := h.listEtcdNode(key) nodes, index, err := h.listEtcdNode(key)
if resourceVersion != nil {
*resourceVersion = index
}
if err != nil { if err != nil {
return err return err
} }

View File

@ -66,6 +66,7 @@ func TestExtractList(t *testing.T) {
fakeClient := NewFakeEtcdClient(t) fakeClient := NewFakeEtcdClient(t)
fakeClient.Data["/some/key"] = EtcdResponseWithError{ fakeClient.Data["/some/key"] = EtcdResponseWithError{
R: &etcd.Response{ R: &etcd.Response{
EtcdIndex: 10,
Node: &etcd.Node{ Node: &etcd.Node{
Nodes: []*etcd.Node{ Nodes: []*etcd.Node{
{ {
@ -92,10 +93,14 @@ func TestExtractList(t *testing.T) {
var got []api.Pod var got []api.Pod
helper := EtcdHelper{fakeClient, codec, versioner} helper := EtcdHelper{fakeClient, codec, versioner}
err := helper.ExtractList("/some/key", &got) resourceVersion := uint64(0)
err := helper.ExtractList("/some/key", &got, &resourceVersion)
if err != nil { if err != nil {
t.Errorf("Unexpected error %#v", err) t.Errorf("Unexpected error %#v", err)
} }
if resourceVersion != 10 {
t.Errorf("Unexpected resource version %d", resourceVersion)
}
for i := 0; i < len(expect); i++ { for i := 0; i < len(expect); i++ {
if !reflect.DeepEqual(got[i], expect[i]) { if !reflect.DeepEqual(got[i], expect[i]) {