mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-27 13:37:30 +00:00
decode crd objectmeta properly
This commit is contained in:
parent
3abba25160
commit
486f42d2a4
@ -17,7 +17,6 @@ limitations under the License.
|
|||||||
package apiserver
|
package apiserver
|
||||||
|
|
||||||
import (
|
import (
|
||||||
encodingjson "encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path"
|
"path"
|
||||||
@ -821,6 +820,8 @@ func (v *unstructuredSchemaCoercer) apply(u *unstructured.Unstructured) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var encodingjson = json.CaseSensitiveJsonIterator()
|
||||||
|
|
||||||
func getObjectMeta(u *unstructured.Unstructured, dropMalformedFields bool) (*metav1.ObjectMeta, bool, error) {
|
func getObjectMeta(u *unstructured.Unstructured, dropMalformedFields bool) (*metav1.ObjectMeta, bool, error) {
|
||||||
metadata, found := u.UnstructuredContent()["metadata"]
|
metadata, found := u.UnstructuredContent()["metadata"]
|
||||||
if !found {
|
if !found {
|
||||||
@ -862,6 +863,7 @@ func getObjectMeta(u *unstructured.Unstructured, dropMalformedFields bool) (*met
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return accumulatedObjectMeta, true, nil
|
return accumulatedObjectMeta, true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,7 +17,6 @@ limitations under the License.
|
|||||||
package apiserver
|
package apiserver
|
||||||
|
|
||||||
import (
|
import (
|
||||||
encodingjson "encoding/json"
|
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
@ -136,7 +135,7 @@ func TestRoundtripObjectMeta(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TestMalformedObjectMetaFields sets a number of different random values and types for all
|
// 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.
|
// gives the same result. Otherwise, drop malformed fields.
|
||||||
func TestMalformedObjectMetaFields(t *testing.T) {
|
func TestMalformedObjectMetaFields(t *testing.T) {
|
||||||
fuzzer := fuzzer.FuzzerFor(metafuzzer.Funcs, rand.NewSource(rand.Int63()), serializer.NewCodecFactory(runtime.NewScheme()))
|
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)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -62,8 +62,8 @@ func TestPostInvalidObjectMeta(t *testing.T) {
|
|||||||
t.Fatalf("expected APIStatus error, but got: %#v", err)
|
t.Fatalf("expected APIStatus error, but got: %#v", err)
|
||||||
} else if !errors.IsBadRequest(err) {
|
} else if !errors.IsBadRequest(err) {
|
||||||
t.Fatalf("expected BadRequst error, but got: %v", errors.ReasonForError(err))
|
t.Fatalf("expected BadRequst error, but got: %v", errors.ReasonForError(err))
|
||||||
} else if !strings.Contains(status.Status().Message, "json: cannot unmarshal") {
|
} else if !strings.Contains(status.Status().Message, "cannot be handled") {
|
||||||
t.Fatalf("expected 'json: cannot unmarshal' error message, got: %v", status.Status().Message)
|
t.Fatalf("expected 'cannot be handled' error message, got: %v", status.Status().Message)
|
||||||
}
|
}
|
||||||
|
|
||||||
unstructured.SetNestedField(obj.UnstructuredContent(), map[string]interface{}{"bar": "abc"}, "metadata", "labels")
|
unstructured.SetNestedField(obj.UnstructuredContent(), map[string]interface{}{"bar": "abc"}, "metadata", "labels")
|
||||||
|
Loading…
Reference in New Issue
Block a user