From 4bf97d9ca8e70d19538aa125d63faf5fc0907361 Mon Sep 17 00:00:00 2001 From: Julian Reyes Date: Wed, 31 Jan 2018 12:21:00 +0000 Subject: [PATCH 1/2] CRI log parser improvements - Modified [`parseCRILog`](https://github.com/kubernetes-incubator/cri-tools/blob/bb2c5293bfa6dd5207e1ce3f2d5a32b09f1cc549/pkg/validate/container.go#L431) to use `strings.SplitN` instead of multiple calls to `strings.Fields`. - Check the resulting array to make sure it has the correct or the expected size and avoid errors when accessing different fields. --- pkg/validate/container.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/pkg/validate/container.go b/pkg/validate/container.go index 22be19d73a..f01877d6a6 100644 --- a/pkg/validate/container.go +++ b/pkg/validate/container.go @@ -33,6 +33,7 @@ import ( . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" + "github.com/pkg/errors" ) // streamType is the type of the stream. @@ -429,15 +430,19 @@ func parseDockerJSONLog(log []byte, msg *logMessage) { // 2016-10-06T00:17:09.669794202Z stdout P The content of the log entry 1 // 2016-10-06T00:17:10.113242941Z stderr F The content of the log entry 2 func parseCRILog(log string, msg *logMessage) { - timeStamp, err := time.Parse(time.RFC3339Nano, strings.Fields(log)[0]) + logMessage := strings.SplitN(log, " ", 4) + if len(log) < 4 { + err := errors.New("invalid CRI log") + framework.ExpectNoError(err, "failed to parse CRI log: %v", err) + } + timeStamp, err := time.Parse(time.RFC3339Nano, logMessage[0]) framework.ExpectNoError(err, "failed to parse timeStamp: %v", err) - stream := strings.Fields(log)[1] - // Skip the tag field. - logMessage := strings.Fields(log)[3:] + stream := logMessage[1] msg.timestamp = timeStamp msg.stream = streamType(stream) - msg.log = []byte(strings.Join(logMessage, " ") + "\n") + // Skip the tag field. + msg.log = []byte(logMessage[3] + "\n") } // parseLogLine parses log by row. From 65725c8d59cedcb41488676598c304c78f570cc9 Mon Sep 17 00:00:00 2001 From: Julian Reyes Date: Wed, 31 Jan 2018 12:27:20 +0000 Subject: [PATCH 2/2] CRI log parser improvements - Modified [`parseCRILog`](https://github.com/kubernetes-incubator/cri-tools/blob/bb2c5293bfa6dd5207e1ce3f2d5a32b09f1cc549/pkg/validate/container.go#L431) to use `strings.SplitN` instead of multiple calls to `strings.Fields`. - Check the resulting array to make sure it has the correct or the expected size and avoid errors when accessing different fields. --- pkg/validate/container.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/validate/container.go b/pkg/validate/container.go index f01877d6a6..7210088636 100644 --- a/pkg/validate/container.go +++ b/pkg/validate/container.go @@ -19,6 +19,7 @@ package validate import ( "bufio" "encoding/json" + "errors" "fmt" "io/ioutil" "os" @@ -33,7 +34,6 @@ import ( . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" - "github.com/pkg/errors" ) // streamType is the type of the stream.