From 8939bce02ff0a5c36a2bec720e53f3fd31d59302 Mon Sep 17 00:00:00 2001 From: dmitri Date: Mon, 28 Aug 2017 17:50:24 +0200 Subject: [PATCH 1/2] Update to apimachinery release-1.7. --- Godeps/Godeps.json | 6 +- lib/services/saml_test.go | 2 +- lib/web/apiserver_test.go | 2 +- tool/tctl/common/tctl.go | 2 +- .../{client-go => apimachinery}/LICENSE | 2 +- .../pkg/util/yaml/decoder.go | 68 +++++++++++++++---- 6 files changed, 61 insertions(+), 21 deletions(-) rename vendor/k8s.io/{client-go => apimachinery}/LICENSE (99%) rename vendor/k8s.io/{client-go/1.4 => apimachinery}/pkg/util/yaml/decoder.go (85%) diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index b751f03f280b9..c5685a3f28a3f 100644 --- a/Godeps/Godeps.json +++ b/Godeps/Godeps.json @@ -706,9 +706,9 @@ "Rev": "7ad95dd0798a40da1ccdff6dff35fd177b5edf40" }, { - "ImportPath": "k8s.io/client-go/1.4/pkg/util/yaml", - "Comment": "v1.4.0", - "Rev": "3a5b96cfd3d3ff1d53d979b4297c4f3b869aab09" + "ImportPath": "k8s.io/apimachinery/pkg/util/yaml", + "Comment": "release-1.7", + "Rev": "1fd2e63a9a370677308a42f24fd40c86438afddf" } ] } diff --git a/lib/services/saml_test.go b/lib/services/saml_test.go index 4ed366db59dfc..832fe1fcdc8b2 100644 --- a/lib/services/saml_test.go +++ b/lib/services/saml_test.go @@ -24,7 +24,7 @@ import ( "github.com/gravitational/teleport/lib/utils" "gopkg.in/check.v1" - kyaml "k8s.io/client-go/1.4/pkg/util/yaml" + kyaml "k8s.io/apimachinery-go/pkg/util/yaml" ) type SAMLSuite struct{} diff --git a/lib/web/apiserver_test.go b/lib/web/apiserver_test.go index 8240d62ec3526..4fb9e3a1f21b7 100644 --- a/lib/web/apiserver_test.go +++ b/lib/web/apiserver_test.go @@ -69,7 +69,7 @@ import ( "golang.org/x/crypto/ssh" "golang.org/x/net/websocket" . "gopkg.in/check.v1" - kyaml "k8s.io/client-go/1.4/pkg/util/yaml" + kyaml "k8s.io/apimachinery/pkg/util/yaml" ) func TestWeb(t *testing.T) { diff --git a/tool/tctl/common/tctl.go b/tool/tctl/common/tctl.go index c30959561cd6d..77f44f1f6c961 100644 --- a/tool/tctl/common/tctl.go +++ b/tool/tctl/common/tctl.go @@ -47,7 +47,7 @@ import ( "github.com/gravitational/trace" "github.com/jonboulle/clockwork" "golang.org/x/crypto/ssh" - kyaml "k8s.io/client-go/1.4/pkg/util/yaml" + kyaml "k8s.io/apimachinery/pkg/util/yaml" ) type CLIConfig struct { diff --git a/vendor/k8s.io/client-go/LICENSE b/vendor/k8s.io/apimachinery/LICENSE similarity index 99% rename from vendor/k8s.io/client-go/LICENSE rename to vendor/k8s.io/apimachinery/LICENSE index 00b2401109fde..d645695673349 100644 --- a/vendor/k8s.io/client-go/LICENSE +++ b/vendor/k8s.io/apimachinery/LICENSE @@ -187,7 +187,7 @@ same "printed page" as the copyright notice for easier identification within third-party archives. - Copyright 2014 The Kubernetes Authors. + Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/vendor/k8s.io/client-go/1.4/pkg/util/yaml/decoder.go b/vendor/k8s.io/apimachinery/pkg/util/yaml/decoder.go similarity index 85% rename from vendor/k8s.io/client-go/1.4/pkg/util/yaml/decoder.go rename to vendor/k8s.io/apimachinery/pkg/util/yaml/decoder.go index c65c2d6ba347b..6ebfaea707d31 100644 --- a/vendor/k8s.io/client-go/1.4/pkg/util/yaml/decoder.go +++ b/vendor/k8s.io/apimachinery/pkg/util/yaml/decoder.go @@ -69,11 +69,10 @@ func (d *YAMLToJSONDecoder) Decode(into interface{}) error { } if len(bytes) != 0 { - data, err := yaml.YAMLToJSON(bytes) + err := yaml.Unmarshal(bytes, into) if err != nil { - return err + return YAMLSyntaxError{err} } - return json.Unmarshal(data, into) } return err } @@ -137,7 +136,7 @@ func (d *YAMLDecoder) Close() error { } const yamlSeparator = "\n---" -const separator = "---\n" +const separator = "---" // splitYAMLDocument is a bufio.SplitFunc for splitting YAML streams into individual documents. func splitYAMLDocument(data []byte, atEOF bool) (advance int, token []byte, err error) { @@ -181,6 +180,24 @@ type YAMLOrJSONDecoder struct { bufferSize int decoder decoder + rawData []byte +} + +type JSONSyntaxError struct { + Line int + Err error +} + +func (e JSONSyntaxError) Error() string { + return fmt.Sprintf("json: line %d: %s", e.Line, e.Err.Error()) +} + +type YAMLSyntaxError struct { + err error +} + +func (e YAMLSyntaxError) Error() string { + return e.err.Error() } // NewYAMLOrJSONDecoder returns a decoder that will process YAML documents @@ -198,10 +215,11 @@ func NewYAMLOrJSONDecoder(r io.Reader, bufferSize int) *YAMLOrJSONDecoder { // provide object, or returns an error. func (d *YAMLOrJSONDecoder) Decode(into interface{}) error { if d.decoder == nil { - buffer, isJSON := GuessJSONStream(d.r, d.bufferSize) + buffer, origData, isJSON := GuessJSONStream(d.r, d.bufferSize) if isJSON { glog.V(4).Infof("decoding stream as JSON") d.decoder = json.NewDecoder(buffer) + d.rawData = origData } else { glog.V(4).Infof("decoding stream as YAML") d.decoder = NewYAMLToJSONDecoder(buffer) @@ -215,9 +233,19 @@ func (d *YAMLOrJSONDecoder) Decode(into interface{}) error { glog.V(4).Infof("reading stream failed: %v", readErr) } js := string(data) + + // if contents from io.Reader are not complete, + // use the original raw data to prevent panic + if int64(len(js)) <= syntax.Offset { + js = string(d.rawData) + } + start := strings.LastIndex(js[:syntax.Offset], "\n") + 1 line := strings.Count(js[:start], "\n") - return fmt.Errorf("json: line %d: %s", line, syntax.Error()) + return JSONSyntaxError{ + Line: line, + Err: fmt.Errorf(syntax.Error()), + } } } return err @@ -246,16 +274,28 @@ func (r *YAMLReader) Read() ([]byte, error) { return nil, err } - if string(line) == separator || err == io.EOF { + sep := len([]byte(separator)) + if i := bytes.Index(line, []byte(separator)); i == 0 { + // We have a potential document terminator + i += sep + after := line[i:] + if len(strings.TrimRightFunc(string(after), unicode.IsSpace)) == 0 { + if buffer.Len() != 0 { + return buffer.Bytes(), nil + } + if err == io.EOF { + return nil, err + } + } + } + if err == io.EOF { if buffer.Len() != 0 { + // If we're at EOF, we have a final, non-terminated line. Return it. return buffer.Bytes(), nil } - if err == io.EOF { - return nil, err - } - } else { - buffer.Write(line) + return nil, err } + buffer.Write(line) } } @@ -284,10 +324,10 @@ func (r *LineReader) Read() ([]byte, error) { // GuessJSONStream scans the provided reader up to size, looking // for an open brace indicating this is JSON. It will return the // bufio.Reader it creates for the consumer. -func GuessJSONStream(r io.Reader, size int) (io.Reader, bool) { +func GuessJSONStream(r io.Reader, size int) (io.Reader, []byte, bool) { buffer := bufio.NewReaderSize(r, size) b, _ := buffer.Peek(size) - return buffer, hasJSONPrefix(b) + return buffer, b, hasJSONPrefix(b) } var jsonPrefix = []byte("{") From 696be65d765d53f1e71ca866f5753ad99d600da0 Mon Sep 17 00:00:00 2001 From: dmitri Date: Mon, 28 Aug 2017 19:23:28 +0200 Subject: [PATCH 2/2] Fix import typo --- lib/services/saml_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/services/saml_test.go b/lib/services/saml_test.go index 832fe1fcdc8b2..0689a8e6457d6 100644 --- a/lib/services/saml_test.go +++ b/lib/services/saml_test.go @@ -24,7 +24,7 @@ import ( "github.com/gravitational/teleport/lib/utils" "gopkg.in/check.v1" - kyaml "k8s.io/apimachinery-go/pkg/util/yaml" + kyaml "k8s.io/apimachinery/pkg/util/yaml" ) type SAMLSuite struct{}