Make ResourceVersion a string internally instead of uint64

Allows us to define different watch versioning regimes in the future
as well as to encode information with the resource version.

This changes /watch/resources?resourceVersion=3 to start the watch at
4 instead of 3, which means clients can read a resource version and
then send it back to the server. Clients should no longer do math on
resource versions.
This commit is contained in:
Clayton Coleman
2014-10-07 16:51:28 -04:00
parent 31e02b882b
commit 82bcdd3b3b
54 changed files with 518 additions and 240 deletions

View File

@@ -36,8 +36,8 @@ type Codec interface {
// ResourceVersioner provides methods for setting and retrieving
// the resource version from an API object.
type ResourceVersioner interface {
SetResourceVersion(obj Object, version uint64) error
ResourceVersion(obj Object) (uint64, error)
SetResourceVersion(obj Object, version string) error
ResourceVersion(obj Object) (string, error)
}
// SelfLinker provides methods for setting and retrieving the SelfLink field of an API object.

View File

@@ -30,15 +30,15 @@ func NewTypeMetaResourceVersioner() ResourceVersioner {
// jsonBaseModifier implements ResourceVersioner and SelfLinker.
type jsonBaseModifier struct{}
func (v jsonBaseModifier) ResourceVersion(obj Object) (uint64, error) {
func (v jsonBaseModifier) ResourceVersion(obj Object) (string, error) {
json, err := FindTypeMeta(obj)
if err != nil {
return 0, err
return "", err
}
return json.ResourceVersion(), nil
}
func (v jsonBaseModifier) SetResourceVersion(obj Object, version uint64) error {
func (v jsonBaseModifier) SetResourceVersion(obj Object, version string) error {
json, err := FindTypeMeta(obj)
if err != nil {
return err
@@ -86,8 +86,8 @@ type TypeMetaInterface interface {
SetAPIVersion(version string)
Kind() string
SetKind(kind string)
ResourceVersion() uint64
SetResourceVersion(version uint64)
ResourceVersion() string
SetResourceVersion(version string)
SelfLink() string
SetSelfLink(selfLink string)
}
@@ -96,7 +96,7 @@ type genericTypeMeta struct {
id *string
apiVersion *string
kind *string
resourceVersion *uint64
resourceVersion *string
selfLink *string
}
@@ -124,11 +124,11 @@ func (g genericTypeMeta) SetKind(kind string) {
*g.kind = kind
}
func (g genericTypeMeta) ResourceVersion() uint64 {
func (g genericTypeMeta) ResourceVersion() string {
return *g.resourceVersion
}
func (g genericTypeMeta) SetResourceVersion(version uint64) {
func (g genericTypeMeta) SetResourceVersion(version string) {
*g.resourceVersion = version
}

View File

@@ -29,14 +29,14 @@ func TestGenericTypeMeta(t *testing.T) {
ID string `json:"id,omitempty" yaml:"id,omitempty"`
CreationTimestamp util.Time `json:"creationTimestamp,omitempty" yaml:"creationTimestamp,omitempty"`
SelfLink string `json:"selfLink,omitempty" yaml:"selfLink,omitempty"`
ResourceVersion uint64 `json:"resourceVersion,omitempty" yaml:"resourceVersion,omitempty"`
ResourceVersion string `json:"resourceVersion,omitempty" yaml:"resourceVersion,omitempty"`
APIVersion string `json:"apiVersion,omitempty" yaml:"apiVersion,omitempty"`
}
j := TypeMeta{
ID: "foo",
APIVersion: "a",
Kind: "b",
ResourceVersion: 1,
ResourceVersion: "1",
SelfLink: "some/place/only/we/know",
}
g, err := newGenericTypeMeta(reflect.ValueOf(&j).Elem())
@@ -54,7 +54,7 @@ func TestGenericTypeMeta(t *testing.T) {
if e, a := "b", jbi.Kind(); e != a {
t.Errorf("expected %v, got %v", e, a)
}
if e, a := uint64(1), jbi.ResourceVersion(); e != a {
if e, a := "1", jbi.ResourceVersion(); e != a {
t.Errorf("expected %v, got %v", e, a)
}
if e, a := "some/place/only/we/know", jbi.SelfLink(); e != a {
@@ -64,7 +64,7 @@ func TestGenericTypeMeta(t *testing.T) {
jbi.SetID("bar")
jbi.SetAPIVersion("c")
jbi.SetKind("d")
jbi.SetResourceVersion(2)
jbi.SetResourceVersion("2")
jbi.SetSelfLink("google.com")
// Prove that jbi changes the original object.
@@ -77,7 +77,7 @@ func TestGenericTypeMeta(t *testing.T) {
if e, a := "d", j.Kind; e != a {
t.Errorf("expected %v, got %v", e, a)
}
if e, a := uint64(2), j.ResourceVersion; e != a {
if e, a := "2", j.ResourceVersion; e != a {
t.Errorf("expected %v, got %v", e, a)
}
if e, a := "google.com", j.SelfLink; e != a {
@@ -99,12 +99,12 @@ func (*MyIncorrectlyMarkedAsAPIObject) IsAnAPIObject() {}
func TestResourceVersionerOfAPI(t *testing.T) {
type T struct {
Object
Expected uint64
Expected string
}
testCases := map[string]T{
"empty api object": {&MyAPIObject{}, 0},
"api object with version": {&MyAPIObject{TypeMeta: TypeMeta{ResourceVersion: 1}}, 1},
"pointer to api object with version": {&MyAPIObject{TypeMeta: TypeMeta{ResourceVersion: 1}}, 1},
"empty api object": {&MyAPIObject{}, ""},
"api object with version": {&MyAPIObject{TypeMeta: TypeMeta{ResourceVersion: "1"}}, "1"},
"pointer to api object with version": {&MyAPIObject{TypeMeta: TypeMeta{ResourceVersion: "1"}}, "1"},
}
versioning := NewTypeMetaResourceVersioner()
for key, testCase := range testCases {
@@ -119,9 +119,9 @@ func TestResourceVersionerOfAPI(t *testing.T) {
failingCases := map[string]struct {
Object
Expected uint64
Expected string
}{
"not a valid object to try": {&MyIncorrectlyMarkedAsAPIObject{}, 1},
"not a valid object to try": {&MyIncorrectlyMarkedAsAPIObject{}, "1"},
}
for key, testCase := range failingCases {
_, err := versioning.ResourceVersion(testCase.Object)
@@ -132,20 +132,20 @@ func TestResourceVersionerOfAPI(t *testing.T) {
setCases := map[string]struct {
Object
Expected uint64
Expected string
}{
"pointer to api object with version": {&MyAPIObject{TypeMeta: TypeMeta{ResourceVersion: 1}}, 1},
"pointer to api object with version": {&MyAPIObject{TypeMeta: TypeMeta{ResourceVersion: "1"}}, "1"},
}
for key, testCase := range setCases {
if err := versioning.SetResourceVersion(testCase.Object, 5); err != nil {
if err := versioning.SetResourceVersion(testCase.Object, "5"); err != nil {
t.Errorf("%s: unexpected error %#v", key, err)
}
actual, err := versioning.ResourceVersion(testCase.Object)
if err != nil {
t.Errorf("%s: unexpected error %#v", key, err)
}
if actual != 5 {
t.Errorf("%s: expected %d, got %d", key, 5, actual)
if actual != "5" {
t.Errorf("%s: expected %d, got %d", key, "5", actual)
}
}
}

View File

@@ -39,7 +39,7 @@ type TypeMeta struct {
ID string `json:"id,omitempty" yaml:"id,omitempty"`
CreationTimestamp util.Time `json:"creationTimestamp,omitempty" yaml:"creationTimestamp,omitempty"`
SelfLink string `json:"selfLink,omitempty" yaml:"selfLink,omitempty"`
ResourceVersion uint64 `json:"resourceVersion,omitempty" yaml:"resourceVersion,omitempty"`
ResourceVersion string `json:"resourceVersion,omitempty" yaml:"resourceVersion,omitempty"`
APIVersion string `json:"apiVersion,omitempty" yaml:"apiVersion,omitempty"`
}