Add the resource version to api.*List items from etcd

Allows clients to watch more easily (can invoke Get, then
Watch).
This commit is contained in:
Clayton Coleman
2014-08-28 20:48:07 -04:00
parent c380dd0426
commit bafc422ac0
17 changed files with 120 additions and 89 deletions

View File

@@ -23,7 +23,7 @@ import (
// Registry is an interface for things that know how to store ReplicationControllers.
type Registry interface {
ListControllers() ([]api.ReplicationController, error)
ListControllers() (*api.ReplicationControllerList, error)
WatchControllers(resourceVersion uint64) (watch.Interface, error)
GetController(controllerID string) (*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.
func (rs *RegistryStorage) List(selector labels.Selector) (interface{}, error) {
result := api.ReplicationControllerList{}
controllers, err := rs.registry.ListControllers()
if err == nil {
for _, controller := range controllers {
if selector.Matches(labels.Set(controller.Labels)) {
result.Items = append(result.Items, controller)
}
if err != nil {
return nil, err
}
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.
@@ -150,7 +152,7 @@ func (rs *RegistryStorage) waitForController(ctrl api.ReplicationController) (in
if err != nil {
return ctrl, err
}
if len(pods) == ctrl.DesiredState.Replicas {
if len(pods.Items) == ctrl.DesiredState.Replicas {
break
}
time.Sleep(rs.pollPeriod)

View File

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