Drop managed fields entries with unknown fields

This is aligned to the behaviour of server-side apply on main resources.

Kubernetes-commit: c10dd884c494734d12aceb41daaccd1d8da9356b
This commit is contained in:
Andrea Nodari 2021-04-09 17:17:23 +02:00 committed by Kubernetes Publisher
parent 9c3786c066
commit a178a9c5d1
2 changed files with 61 additions and 2 deletions

View File

@ -68,7 +68,12 @@ func (h *ScaleHandler) ToSubresource() ([]metav1.ManagedFieldsEntry, error) {
f := fieldpath.ManagedFields{}
t := map[string]*metav1.Time{}
for manager, versionedSet := range managed.Fields() {
path := h.mappings[string(versionedSet.APIVersion())]
path, ok := h.mappings[string(versionedSet.APIVersion())]
// Drop the field if the APIVersion of the managed field is unknown
if !ok {
continue
}
if versionedSet.Set().Has(path) {
newVersionedSet := fieldpath.NewVersionedSet(
fieldpath.NewSet(replicasPathInScale),
@ -104,7 +109,11 @@ func (h *ScaleHandler) ToParent(scaleEntries []metav1.ManagedFieldsEntry) ([]met
for manager, versionedSet := range parentFields {
// Get the main resource "replicas" path
path := h.mappings[string(versionedSet.APIVersion())]
path, ok := h.mappings[string(versionedSet.APIVersion())]
// Drop the field if the APIVersion of the managed field is unknown
if !ok {
continue
}
// If the parent entry does not have the replicas path, just keep it as it is
if !versionedSet.Set().Has(path) {

View File

@ -117,6 +117,19 @@ func TestTransformManagedFieldsToSubresource(t *testing.T) {
},
},
},
{
desc: "drops fields if the api version is unknown",
input: []metav1.ManagedFieldsEntry{
{
Manager: "manager-1",
Operation: metav1.ManagedFieldsOperationApply,
APIVersion: "apps/v10",
FieldsType: "FieldsV1",
FieldsV1: &metav1.FieldsV1{Raw: []byte(`{"f:spec":{"f:replicas":{}}}`)},
},
},
expected: nil,
},
}
for _, test := range tests {
@ -564,6 +577,43 @@ func TestTransformingManagedFieldsToParent(t *testing.T) {
},
},
},
{
desc: "drops other fields if the api version is unknown",
parent: []metav1.ManagedFieldsEntry{
{
Manager: "test",
Operation: metav1.ManagedFieldsOperationApply,
APIVersion: "apps/v1",
FieldsType: "FieldsV1",
FieldsV1: &metav1.FieldsV1{Raw: []byte(`{"f:spec":{"f:replicas":{}}}`)},
},
{
Manager: "another-manager",
Operation: metav1.ManagedFieldsOperationApply,
APIVersion: "apps/v10",
FieldsType: "FieldsV1",
FieldsV1: &metav1.FieldsV1{Raw: []byte(`{"f:spec":{"f:selector":{}}}`)},
},
},
subresource: []metav1.ManagedFieldsEntry{
{
Manager: "scale",
Operation: metav1.ManagedFieldsOperationUpdate,
APIVersion: "autoscaling/v1",
FieldsType: "FieldsV1",
FieldsV1: &metav1.FieldsV1{Raw: []byte(`{"f:spec":{"f:replicas":{}}}`)},
},
},
expected: []metav1.ManagedFieldsEntry{
{
Manager: "scale",
Operation: metav1.ManagedFieldsOperationUpdate,
APIVersion: "apps/v1",
FieldsType: "FieldsV1",
FieldsV1: &metav1.FieldsV1{Raw: []byte(`{"f:spec":{"f:replicas":{}}}`)},
},
},
},
}
for _, test := range tests {