Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release 1.16 #82557

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,9 @@ func (f *FieldManager) Update(liveObj, newObj runtime.Object, manager string) (r
// object and update the managed fields.
func (f *FieldManager) Apply(liveObj runtime.Object, patch []byte, fieldManager string, force bool) (runtime.Object, error) {
// If the object doesn't have metadata, apply isn't allowed.
accessor, err := meta.Accessor(liveObj)
if err != nil {
if _, err := meta.Accessor(liveObj); err != nil {
return nil, fmt.Errorf("couldn't get accessor: %v", err)
}
missingManagedFields := (len(accessor.GetManagedFields()) == 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dropping this isn't correct (it doesn't compile/pass unit tests). see if the hack script for creating the cherry pick comes up with a diff that matches the original PR better

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@liggitt The hack script modified much more than required for some reason #82581

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strange, it seemed to work for me... opened #82582


managed, err := internal.DecodeObjectManagedFields(liveObj)
if err != nil {
return nil, fmt.Errorf("failed to decode managed fields: %v", err)
Expand All @@ -192,6 +189,11 @@ func (f *FieldManager) Apply(liveObj runtime.Object, patch []byte, fieldManager
if err := yaml.Unmarshal(patch, &patchObj.Object); err != nil {
return nil, fmt.Errorf("error decoding YAML: %v", err)
}

if patchObj.GetManagedFields() != nil {
return nil, fmt.Errorf("managed fields must be nil but was %v", patchObj.GetManagedFields())
}

if patchObj.GetAPIVersion() != f.groupVersion.String() {
return nil,
errors.NewBadRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"net/http"
"testing"

"time"

corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
Expand Down Expand Up @@ -104,37 +106,36 @@ func TestApplyStripsFields(t *testing.T) {
obj := &corev1.Pod{}
obj.ObjectMeta.ManagedFields = []metav1.ManagedFieldsEntry{{}}

newObj, err := f.Apply(obj, []byte(`{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": {
"name": "b",
"namespace": "b",
"creationTimestamp": "2016-05-19T09:59:00Z",
"selfLink": "b",
"uid": "b",
"clusterName": "b",
"generation": 0,
"managedFields": [{
"manager": "apply",
"operation": "Apply",
"apiVersion": "apps/v1",
"fields": {
"f:metadata": {
"f:labels": {
"f:test-label": {}
}
}
}
}],
"resourceVersion": "b"
}
}`), "fieldmanager_test", false)
newObj := &corev1.Pod{
TypeMeta: metav1.TypeMeta{
APIVersion: "apps/v1",
Kind: "Deployment",
},
ObjectMeta: metav1.ObjectMeta{
Name: "b",
Namespace: "b",
CreationTimestamp: metav1.NewTime(time.Now()),
SelfLink: "b",
UID: "b",
ClusterName: "b",
Generation: 0,
ManagedFields: []metav1.ManagedFieldsEntry{
{
Manager: "update",
Operation: metav1.ManagedFieldsOperationApply,
APIVersion: "apps/v1",
},
},
ResourceVersion: "b",
},
}

updatedObj, err := f.Update(obj, newObj, "fieldmanager_test")
if err != nil {
t.Fatalf("failed to apply object: %v", err)
}

accessor, err := meta.Accessor(newObj)
accessor, err := meta.Accessor(updatedObj)
if err != nil {
t.Fatalf("couldn't get accessor: %v", err)
}
Expand Down Expand Up @@ -468,3 +469,41 @@ func BenchmarkRepeatedUpdate(b *testing.B) {
}
}
}

func TestApplyFailsWithManagedFields(t *testing.T) {
f := NewTestFieldManager()

_, err := f.Apply(&corev1.Pod{}, []byte(`{
"apiVersion": "apps/v1",
"kind": "Pod",
"metadata": {
"managedFields": [
{
"manager": "test",
}
]
}
}`), "fieldmanager_test", false)

if err == nil {
t.Fatalf("successfully applied with set managed fields")
}
}

func TestApplySuccessWithNoManagedFields(t *testing.T) {
f := NewTestFieldManager()

_, err := f.Apply(&corev1.Pod{}, []byte(`{
"apiVersion": "apps/v1",
"kind": "Pod",
"metadata": {
"labels": {
"a": "b"
},
}
}`), "fieldmanager_test", false)

if err != nil {
t.Fatalf("failed to apply object: %v", err)
}
}