Skip to content

Commit

Permalink
Apply resource versioning to list extractions from etcd
Browse files Browse the repository at this point in the history
Set the resource version on lists of objects extracted from etcd to prevent
them from always being interpreted as new during updates.
  • Loading branch information
ironcladlou committed Aug 11, 2014
1 parent 9050c81 commit b986c52
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
4 changes: 4 additions & 0 deletions pkg/tools/etcd_tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ func (h *EtcdHelper) ExtractList(key string, slicePtr interface{}) error {
for _, node := range nodes {
obj := reflect.New(v.Type().Elem())
err = h.Codec.DecodeInto([]byte(node.Value), obj.Interface())
if h.ResourceVersioner != nil {
_ = h.ResourceVersioner.SetResourceVersion(obj.Interface(), node.ModifiedIndex)
// being unable to set the version does not prevent the object from being extracted
}
if err != nil {
return err
}
Expand Down
17 changes: 12 additions & 5 deletions pkg/tools/etcd_tools_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,30 +70,37 @@ func TestExtractList(t *testing.T) {
Nodes: []*etcd.Node{
{
Value: `{"id":"foo"}`,
ModifiedIndex: 1,
},
{
Value: `{"id":"bar"}`,
ModifiedIndex: 2,
},
{
Value: `{"id":"baz"}`,
ModifiedIndex: 3,
},
},
},
},
}
expect := []api.Pod{
{JSONBase: api.JSONBase{ID: "foo"}},
{JSONBase: api.JSONBase{ID: "bar"}},
{JSONBase: api.JSONBase{ID: "baz"}},
{JSONBase: api.JSONBase{ID: "foo", ResourceVersion: 1}},
{JSONBase: api.JSONBase{ID: "bar", ResourceVersion: 2}},
{JSONBase: api.JSONBase{ID: "baz", ResourceVersion: 3}},
}

var got []api.Pod
helper := EtcdHelper{fakeClient, codec, versioner}
err := helper.ExtractList("/some/key", &got)
if err != nil {
t.Errorf("Unexpected error %#v", err)
}
if !reflect.DeepEqual(got, expect) {
t.Errorf("Wanted %#v, got %#v", expect, got)

for i := 0; i < len(expect); i++ {
if !reflect.DeepEqual(got[i], expect[i]) {
t.Errorf("\nWanted:\n%#v\nGot:\n%#v\n", expect[i], got[i])
}
}
}

Expand Down

0 comments on commit b986c52

Please sign in to comment.