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

Apply will fail with managed fields + tests #81453

Merged
merged 3 commits into from Sep 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -164,6 +164,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
Expand Up @@ -22,9 +22,12 @@ import (
"net/http"
"testing"

"time"

corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand Down Expand Up @@ -76,37 +79,36 @@ func TestApplyStripsFields(t *testing.T) {

obj := &corev1.Pod{}

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": [{
Copy link
Member

Choose a reason for hiding this comment

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

That's good :-) We still need that test though, so maybe we should add an update instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thats a good idea. I didnt know it was called in update. I updated the test

"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 @@ -439,3 +441,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": {
apelisse marked this conversation as resolved.
Show resolved Hide resolved
"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)
}
}