ratcheting: disable correlation by index

discussion: https://github.com/kubernetes/kubernetes/pull/121118#discussion_r1358865893
This commit is contained in:
Alexander Zielenski 2023-10-13 14:36:46 -07:00
parent d991ed56c2
commit fb1fc8b4a7
3 changed files with 14 additions and 22 deletions

View File

@ -1175,7 +1175,7 @@ func TestRatchetingFunctionality(t *testing.T) {
}, },
}, },
{ {
Name: "ArrayItems correlate by index", Name: "ArrayItems do not correlate by index",
Operations: []ratchetingTestOperation{ Operations: []ratchetingTestOperation{
updateMyCRDV1Beta1Schema{&apiextensionsv1.JSONSchemaProps{ updateMyCRDV1Beta1Schema{&apiextensionsv1.JSONSchemaProps{
Type: "object", Type: "object",
@ -1246,9 +1246,9 @@ func TestRatchetingFunctionality(t *testing.T) {
}, },
"otherField": "hello world", "otherField": "hello world",
}}, }},
// (This test shows an array can be correlated by index with its old value) // (This test shows an array cannpt be correlated by index with its old value)
applyPatchOperation{ expectError{applyPatchOperation{
"add new, valid fields to elements of the array, ratcheting unchanged old fields within the array elements by correlating by index", "add new, valid fields to elements of the array, failing to ratchet unchanged old fields within the array elements by correlating by index due to atomic list",
myCRDV1Beta1, myCRDInstanceName, map[string]interface{}{ myCRDV1Beta1, myCRDInstanceName, map[string]interface{}{
"values": []interface{}{ "values": []interface{}{
map[string]interface{}{ map[string]interface{}{
@ -1261,7 +1261,7 @@ func TestRatchetingFunctionality(t *testing.T) {
"key2": "valid value", "key2": "valid value",
}, },
}, },
}}, }}},
expectError{ expectError{
applyPatchOperation{ applyPatchOperation{
"reorder the array, preventing index correlation", "reorder the array, preventing index correlation",

View File

@ -114,20 +114,16 @@ func (r *CorrelatedObject) correlateOldValueForChildAtNewIndex(index int) interf
// of value // of value
// (would allow you to add/remove items from sets with ratcheting but not change them) // (would allow you to add/remove items from sets with ratcheting but not change them)
return nil return nil
case "":
fallthrough
case "atomic": case "atomic":
// Atomic lists are not correlatable by item // Atomic lists are the default are not correlatable by item
// Ratcheting is not available on a per-index basis // Ratcheting is not available on a per-index basis
return nil return nil
default: default:
// Correlate by-index by default. // Unrecognized list type. Assume non-correlatable.
//
// Cannot correlate an out-of-bounds index
if len(oldAsList) <= index {
return nil return nil
} }
return oldAsList[index]
}
} }
// CachedDeepEqual is equivalent to reflect.DeepEqual, but caches the // CachedDeepEqual is equivalent to reflect.DeepEqual, but caches the

View File

@ -142,7 +142,7 @@ func TestCorrelation(t *testing.T) {
OldValue: "b", OldValue: "b",
}, },
{ {
Name: "Basic Index", Name: "Atomic Array not correlatable",
RootObject: mustUnstructured(`[a, b]`), RootObject: mustUnstructured(`[a, b]`),
RootOldObject: mustUnstructured(`[a, b]`), RootOldObject: mustUnstructured(`[a, b]`),
Schema: mustSchema(` Schema: mustSchema(`
@ -150,8 +150,6 @@ func TestCorrelation(t *testing.T) {
type: string type: string
`), `),
KeyPath: []interface{}{1}, KeyPath: []interface{}{1},
NewValue: "b",
OldValue: "b",
}, },
{ {
Name: "Added Key Not In Old Object", Name: "Added Key Not In Old Object",
@ -187,7 +185,7 @@ func TestCorrelation(t *testing.T) {
KeyPath: []interface{}{2}, KeyPath: []interface{}{2},
}, },
{ {
Name: "Changed Index In Old Object", Name: "Changed Index In Old Object not correlatable",
RootObject: []interface{}{ RootObject: []interface{}{
"a", "a",
"b", "b",
@ -201,8 +199,6 @@ func TestCorrelation(t *testing.T) {
type: string type: string
`), `),
KeyPath: []interface{}{1}, KeyPath: []interface{}{1},
NewValue: "b",
OldValue: "oldB",
}, },
{ {
Name: "Changed Index In Nested Old Object", Name: "Changed Index In Nested Old Object",