decode crd objectmeta properly

This commit is contained in:
Jordan Liggitt 2018-06-14 17:00:33 -04:00
parent 3abba25160
commit 486f42d2a4
No known key found for this signature in database
GPG Key ID: 39928704103C7229
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")