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{
updateMyCRDV1Beta1Schema{&apiextensionsv1.JSONSchemaProps{
Type: "object",
@ -1246,9 +1246,9 @@ func TestRatchetingFunctionality(t *testing.T) {
},
"otherField": "hello world",
}},
// (This test shows an array can be correlated by index with its old value)
applyPatchOperation{
"add new, valid fields to elements of the array, ratcheting unchanged old fields within the array elements by correlating by index",
// (This test shows an array cannpt be correlated by index with its old value)
expectError{applyPatchOperation{
"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{}{
"values": []interface{}{
map[string]interface{}{
@ -1261,7 +1261,7 @@ func TestRatchetingFunctionality(t *testing.T) {
"key2": "valid value",
},
},
}},
}}},
expectError{
applyPatchOperation{
"reorder the array, preventing index correlation",

View File

@ -114,19 +114,15 @@ func (r *CorrelatedObject) correlateOldValueForChildAtNewIndex(index int) interf
// of value
// (would allow you to add/remove items from sets with ratcheting but not change them)
return nil
case "":
fallthrough
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
return nil
default:
// Correlate by-index by default.
//
// Cannot correlate an out-of-bounds index
if len(oldAsList) <= index {
return nil
}
return oldAsList[index]
// Unrecognized list type. Assume non-correlatable.
return nil
}
}

View File

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