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

Manually cherrypick #65034 to 1.10 #65157

Merged
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
12 changes: 10 additions & 2 deletions Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

418 changes: 418 additions & 0 deletions Godeps/LICENSES

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion cmd/kubeadm/app/apis/kubeadm/v1alpha1/BUILD
Expand Up @@ -58,7 +58,6 @@ go_library(
"//pkg/kubelet/apis/kubeletconfig/v1beta1:go_default_library",
"//pkg/proxy/apis/kubeproxyconfig/scheme:go_default_library",
"//pkg/proxy/apis/kubeproxyconfig/v1alpha1:go_default_library",
"//vendor/github.com/json-iterator/go:go_default_library",
"//vendor/github.com/ugorji/go/codec:go_default_library",
"//vendor/gopkg.in/yaml.v2:go_default_library",
"//vendor/k8s.io/api/core/v1:go_default_library",
Expand Down
3 changes: 0 additions & 3 deletions cmd/kubeadm/app/apis/kubeadm/v1alpha1/upgrade.go
Expand Up @@ -24,7 +24,6 @@ import (
"strconv"
"strings"

"github.com/json-iterator/go"
"github.com/ugorji/go/codec"
yaml "gopkg.in/yaml.v2"

Expand All @@ -33,8 +32,6 @@ import (
"k8s.io/kubernetes/pkg/api/legacyscheme"
)

var json = jsoniter.ConfigCompatibleWithStandardLibrary

type configMutationFunc func(map[string]interface{}) error

// These migrations are a stop-gap until we get a properly-versioned configuration file for MasterConfiguration.
Expand Down
1 change: 1 addition & 0 deletions pkg/api/testing/BUILD
Expand Up @@ -96,6 +96,7 @@ go_test(
"//vendor/k8s.io/apimachinery/pkg/conversion:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/runtime/serializer/json:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/runtime/serializer/protobuf:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/runtime/serializer/streaming:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/types:go_default_library",
Expand Down
11 changes: 7 additions & 4 deletions pkg/api/testing/serialization_test.go
Expand Up @@ -37,6 +37,7 @@ import (
"k8s.io/apimachinery/pkg/conversion"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
k8s_json "k8s.io/apimachinery/pkg/runtime/serializer/json"
"k8s.io/apimachinery/pkg/runtime/serializer/streaming"
"k8s.io/apimachinery/pkg/util/diff"
"k8s.io/apimachinery/pkg/util/sets"
Expand Down Expand Up @@ -557,9 +558,10 @@ func BenchmarkDecodeIntoJSONCodecGenConfigFast(b *testing.B) {
b.StopTimer()
}

// BenchmarkDecodeIntoJSONCodecGenConfigCompatibleWithStandardLibrary
// provides a baseline for JSON decode performance
// with jsoniter.ConfigCompatibleWithStandardLibrary
// BenchmarkDecodeIntoJSONCodecGenConfigCompatibleWithStandardLibrary provides a
// baseline for JSON decode performance with
// jsoniter.ConfigCompatibleWithStandardLibrary, but with case sensitivity set
// to true
func BenchmarkDecodeIntoJSONCodecGenConfigCompatibleWithStandardLibrary(b *testing.B) {
kcodec := testapi.Default.Codec()
items := benchmarkItems(b)
Expand All @@ -574,9 +576,10 @@ func BenchmarkDecodeIntoJSONCodecGenConfigCompatibleWithStandardLibrary(b *testi
}

b.ResetTimer()
iter := k8s_json.CaseSensitiveJsonIterator()
for i := 0; i < b.N; i++ {
obj := v1.Pod{}
if err := jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(encoded[i%width], &obj); err != nil {
if err := iter.Unmarshal(encoded[i%width], &obj); err != nil {
b.Fatal(err)
}
}
Expand Down
10 changes: 9 additions & 1 deletion staging/src/k8s.io/apiextensions-apiserver/Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion staging/src/k8s.io/apimachinery/Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/BUILD
Expand Up @@ -21,9 +21,9 @@ go_test(
embed = [":go_default_library"],
deps = [
"//vendor/github.com/ghodss/yaml:go_default_library",
"//vendor/github.com/json-iterator/go:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/labels:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/runtime/serializer/json:go_default_library",
],
)

Expand Down
Expand Up @@ -21,7 +21,7 @@ import (
"reflect"
"testing"

jsoniter "github.com/json-iterator/go"
k8s_json "k8s.io/apimachinery/pkg/runtime/serializer/json"
)

type GroupVersionHolder struct {
Expand All @@ -47,7 +47,8 @@ func TestGroupVersionUnmarshalJSON(t *testing.T) {
t.Errorf("JSON codec failed to unmarshal input '%s': expected %+v, got %+v", c.input, c.expect, result.GV)
}
// test the json-iterator codec
if err := jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(c.input, &result); err != nil {
iter := k8s_json.CaseSensitiveJsonIterator()
if err := iter.Unmarshal(c.input, &result); err != nil {
t.Errorf("json-iterator codec failed to unmarshal input '%v': %v", c.input, err)
}
if !reflect.DeepEqual(result.GV, c.expect) {
Expand Down
Expand Up @@ -21,7 +21,7 @@ import (
"reflect"
"testing"

jsoniter "github.com/json-iterator/go"
k8s_json "k8s.io/apimachinery/pkg/runtime/serializer/json"
)

func TestVerbsUgorjiMarshalJSON(t *testing.T) {
Expand All @@ -45,7 +45,7 @@ func TestVerbsUgorjiMarshalJSON(t *testing.T) {
}
}

func TestVerbsUgorjiUnmarshalJSON(t *testing.T) {
func TestVerbsJsonIterUnmarshalJSON(t *testing.T) {
cases := []struct {
input string
result APIResource
Expand All @@ -56,9 +56,10 @@ func TestVerbsUgorjiUnmarshalJSON(t *testing.T) {
{`{"verbs":["delete"]}`, APIResource{Verbs: Verbs([]string{"delete"})}},
}

iter := k8s_json.CaseSensitiveJsonIterator()
for i, c := range cases {
var result APIResource
if err := jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal([]byte(c.input), &result); err != nil {
if err := iter.Unmarshal([]byte(c.input), &result); err != nil {
t.Errorf("[%d] Failed to unmarshal input '%v': %v", i, c.input, err)
}
if !reflect.DeepEqual(result, c.result) {
Expand Down
Expand Up @@ -98,6 +98,20 @@ func init() {
jsoniter.RegisterTypeDecoderFunc("interface {}", decodeNumberAsInt64IfPossible)
}

// CaseSensitiveJsonIterator returns a jsoniterator API that's configured to be
// case-sensitive when unmarshalling, and otherwise compatible with
// the encoding/json standard library.
func CaseSensitiveJsonIterator() jsoniter.API {
return jsoniter.Config{
EscapeHTML: true,
SortMapKeys: true,
ValidateJsonRawMessage: true,
CaseSensitive: true,
}.Froze()
}

var caseSensitiveJsonIterator = CaseSensitiveJsonIterator()

// gvkWithDefaults returns group kind and version defaulting from provided default
func gvkWithDefaults(actual, defaultGVK schema.GroupVersionKind) schema.GroupVersionKind {
if len(actual.Kind) == 0 {
Expand Down Expand Up @@ -162,7 +176,7 @@ func (s *Serializer) Decode(originalData []byte, gvk *schema.GroupVersionKind, i
types, _, err := s.typer.ObjectKinds(into)
switch {
case runtime.IsNotRegisteredError(err), isUnstructured:
if err := jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(data, into); err != nil {
if err := caseSensitiveJsonIterator.Unmarshal(data, into); err != nil {
return nil, actual, err
}
return into, actual, nil
Expand All @@ -186,7 +200,7 @@ func (s *Serializer) Decode(originalData []byte, gvk *schema.GroupVersionKind, i
return nil, actual, err
}

if err := jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(data, obj); err != nil {
if err := caseSensitiveJsonIterator.Unmarshal(data, obj); err != nil {
return nil, actual, err
}
return obj, actual, nil
Expand All @@ -195,7 +209,7 @@ func (s *Serializer) Decode(originalData []byte, gvk *schema.GroupVersionKind, i
// Encode serializes the provided object to the given writer.
func (s *Serializer) Encode(obj runtime.Object, w io.Writer) error {
if s.yaml {
json, err := jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(obj)
json, err := caseSensitiveJsonIterator.Marshal(obj)
if err != nil {
return err
}
Expand All @@ -208,7 +222,7 @@ func (s *Serializer) Encode(obj runtime.Object, w io.Writer) error {
}

if s.pretty {
data, err := jsoniter.ConfigCompatibleWithStandardLibrary.MarshalIndent(obj, "", " ")
data, err := caseSensitiveJsonIterator.MarshalIndent(obj, "", " ")
if err != nil {
return err
}
Expand Down
Expand Up @@ -30,10 +30,31 @@ import (

type testDecodable struct {
Other string
Value int `json:"value"`
Value int `json:"value"`
Spec DecodableSpec `json:"spec"`
gvk schema.GroupVersionKind
}

// DecodableSpec has 15 fields. json-iterator treats struct with more than 10
// fields differently from struct that has less than 10 fields.
type DecodableSpec struct {
A int `json:"A"`
B int `json:"B"`
C int `json:"C"`
D int `json:"D"`
E int `json:"E"`
F int `json:"F"`
G int `json:"G"`
H int `json:"h"`
I int `json:"i"`
J int `json:"j"`
K int `json:"k"`
L int `json:"l"`
M int `json:"m"`
N int `json:"n"`
O int `json:"o"`
}

func (d *testDecodable) GetObjectKind() schema.ObjectKind { return d }
func (d *testDecodable) SetGroupVersionKind(gvk schema.GroupVersionKind) { d.gvk = gvk }
func (d *testDecodable) GroupVersionKind() schema.GroupVersionKind { return d.gvk }
Expand Down Expand Up @@ -221,6 +242,28 @@ func TestDecode(t *testing.T) {
},
},
},
// Unmarshalling is case-sensitive
{
// "VaLue" should have been "value"
data: []byte(`{"kind":"Test","apiVersion":"other/blah","VaLue":1,"Other":"test"}`),
into: &testDecodable{},
typer: &mockTyper{err: runtime.NewNotRegisteredErrForKind(schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"})},
expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
expectedObject: &testDecodable{
Other: "test",
},
},
// Unmarshalling is case-sensitive for big struct.
{
// "b" should have been "B", "I" should have been "i"
data: []byte(`{"kind":"Test","apiVersion":"other/blah","spec": {"A": 1, "b": 2, "h": 3, "I": 4}}`),
into: &testDecodable{},
typer: &mockTyper{err: runtime.NewNotRegisteredErrForKind(schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"})},
expectedGVK: &schema.GroupVersionKind{Kind: "Test", Group: "other", Version: "blah"},
expectedObject: &testDecodable{
Spec: DecodableSpec{A: 1, H: 3},
},
},
}

for i, test := range testCases {
Expand Down
10 changes: 9 additions & 1 deletion staging/src/k8s.io/apiserver/Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion staging/src/k8s.io/client-go/Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion staging/src/k8s.io/kube-aggregator/Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion staging/src/k8s.io/metrics/Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.