Skip to content

Commit 3e3f9db

Browse files
authored
Make the behavior of TypedStore.Update match the described behavior in the godoc (#604)
Make the behavior of `TypedStore.Update` match the described behavior in the godoc with respect to ResourceVersion. If the ResourceVersion is empty, return an error, and otherwise always pass the ResourceVersion to the underlying UpdateOptions. Duplicate of #595 applied to the LTS branch.
1 parent 85aaab9 commit 3e3f9db

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

resource/typedstore.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"time"
99
)
1010

11+
var ErrMissingResourceVersion = fmt.Errorf("object is missing a ResourceVersion")
12+
1113
// TypedStore is a single-Schema store where returned Objects from the underlying client are assumed
1214
// to be of ObjectType. It is a thin convenience layer over using a raw ClientGenerator.ClientFor()-created
1315
// Client for a Schema and doing type conversions in-code.
@@ -91,7 +93,13 @@ func (t *TypedStore[T]) Update(ctx context.Context, identifier Identifier, obj T
9193
md := obj.GetCommonMetadata()
9294
md.UpdateTimestamp = time.Now().UTC()
9395
obj.SetCommonMetadata(md)
94-
ret, err := t.client.Update(ctx, identifier, obj, UpdateOptions{})
96+
if obj.GetResourceVersion() == "" {
97+
var n T
98+
return n, ErrMissingResourceVersion
99+
}
100+
ret, err := t.client.Update(ctx, identifier, obj, UpdateOptions{
101+
ResourceVersion: obj.GetResourceVersion(),
102+
})
95103
if err != nil {
96104
var n T
97105
return n, err

resource/typedstore_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,25 @@ func TestTypedStore_Update(t *testing.T) {
161161
assert.Equal(t, cerr, err)
162162
})
163163

164+
t.Run("no ResourceVersion", func(t *testing.T) {
165+
client.UpdateFunc = func(ctx context.Context, identifier Identifier, obj Object, options UpdateOptions) (Object, error) {
166+
assert.Fail(t, "client update should not be called")
167+
return nil, nil
168+
}
169+
obj := updateObj.Copy().(*TypedSpecStatusObject[string, string])
170+
obj.SetResourceVersion("")
171+
ret, err := store.Update(ctx, id, obj)
172+
assert.Nil(t, ret)
173+
assert.Equal(t, ErrMissingResourceVersion, err)
174+
})
175+
164176
t.Run("success, no metadata options", func(t *testing.T) {
165177
client.UpdateFunc = func(c context.Context, identifier Identifier, obj Object, options UpdateOptions) (Object, error) {
166178
assert.Equal(t, ctx, c)
167179
assert.Equal(t, updateObj.Name, identifier.Name)
168180
assert.Equal(t, updateObj.Namespace, identifier.Namespace)
169181
assert.Equal(t, updateObj, obj)
170-
assert.Equal(t, "", options.ResourceVersion)
182+
assert.Equal(t, updateObj.ResourceVersion, options.ResourceVersion)
171183
assert.Equal(t, "", options.Subresource)
172184
return retObj, nil
173185
}

0 commit comments

Comments
 (0)