Merge pull request #65138 from liggitt/crd-objectmeta

Automatic merge from submit-queue (batch tested with PRs 64838, 65138). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

use case-sensitive json-iter to decode custom resource ObjectMeta

found one additional spot where case-insensitivity could bite us. uses the case-sensitive decode added in https://github.com/kubernetes/kubernetes/pull/65034


/assign @sttts
/cc @caesarxuchao 

```release-note
NONE
```
This commit is contained in:
Kubernetes Submit Queue 2018-06-15 09:16:16 -07:00 committed by GitHub
commit 4ba0dbde84
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 5 deletions

View File

@ -17,7 +17,6 @@ limitations under the License.
package apiserver
import (
encodingjson "encoding/json"
"fmt"
"net/http"
"path"
@ -821,6 +820,8 @@ func (v *unstructuredSchemaCoercer) apply(u *unstructured.Unstructured) error {
return nil
}
var encodingjson = json.CaseSensitiveJsonIterator()
func getObjectMeta(u *unstructured.Unstructured, dropMalformedFields bool) (*metav1.ObjectMeta, bool, error) {
metadata, found := u.UnstructuredContent()["metadata"]
if !found {
@ -862,6 +863,7 @@ func getObjectMeta(u *unstructured.Unstructured, dropMalformedFields bool) (*met
}
}
}
return accumulatedObjectMeta, true, nil
}

View File

@ -17,7 +17,6 @@ limitations under the License.
package apiserver
import (
encodingjson "encoding/json"
"math/rand"
"reflect"
"testing"
@ -136,7 +135,7 @@ func TestRoundtripObjectMeta(t *testing.T) {
}
// TestMalformedObjectMetaFields sets a number of different random values and types for all
// metadata fields. If encoding/json.Unmarshal accepts them, compare that getObjectMeta
// metadata fields. If json.Unmarshal accepts them, compare that getObjectMeta
// gives the same result. Otherwise, drop malformed fields.
func TestMalformedObjectMetaFields(t *testing.T) {
fuzzer := fuzzer.FuzzerFor(metafuzzer.Funcs, rand.NewSource(rand.Int63()), serializer.NewCodecFactory(runtime.NewScheme()))
@ -277,3 +276,33 @@ func TestGetObjectMetaNils(t *testing.T) {
t.Errorf("expected labels to be %v, got %v", expected, got)
}
}
func TestGetObjectMeta(t *testing.T) {
for i := 0; i < 100; i++ {
u := &unstructured.Unstructured{Object: map[string]interface{}{
"metadata": map[string]interface{}{
"name": "good",
"Name": "bad1",
"nAme": "bad2",
"naMe": "bad3",
"namE": "bad4",
"namespace": "good",
"Namespace": "bad1",
"nAmespace": "bad2",
"naMespace": "bad3",
"namEspace": "bad4",
"creationTimestamp": "a",
},
}}
meta, _, err := getObjectMeta(u, true)
if err != nil {
t.Fatal(err)
}
if meta.Name != "good" || meta.Namespace != "good" {
t.Fatalf("got %#v", meta)
}
}
}

View File

@ -62,8 +62,8 @@ func TestPostInvalidObjectMeta(t *testing.T) {
t.Fatalf("expected APIStatus error, but got: %#v", err)
} else if !errors.IsBadRequest(err) {
t.Fatalf("expected BadRequst error, but got: %v", errors.ReasonForError(err))
} else if !strings.Contains(status.Status().Message, "json: cannot unmarshal") {
t.Fatalf("expected 'json: cannot unmarshal' error message, got: %v", status.Status().Message)
} else if !strings.Contains(status.Status().Message, "cannot be handled") {
t.Fatalf("expected 'cannot be handled' error message, got: %v", status.Status().Message)
}
unstructured.SetNestedField(obj.UnstructuredContent(), map[string]interface{}{"bar": "abc"}, "metadata", "labels")