Merge pull request #55631 from mrunalp/parse_last_partial

Automatic merge from submit-queue (batch tested with PRs 55697, 55631, 51905, 55647, 55826). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

Parse the last partial line in CRI logs

@Random-Liu @yujuhong @runcom @feiskyer PTAL

Fixes part of https://github.com/kubernetes/kubernetes/issues/44976

This should work while we find a longer term format for CRI logs.

Signed-off-by: Mrunal Patel <mrunalp@gmail.com>

**What this PR does / why we need it**:
This PR attempts to parse the last line of  log even if it doesn't have a newline. Today for CRI based log formats such lines are ignored and empty output is returned.

**Which issue(s) this PR fixes** 
Fixes part of #44976

**Special notes for your reviewer**:
Use a CRI runtime like CRI-O and the output will be empty for these commands without this PR.
```
# kubectl create -f https://raw.githubusercontent.com/openshift-qe/v3-testfiles/master/configmap/configmap.yaml
# kubectl create -f https://raw.githubusercontent.com/openshift-qe/v3-testfiles/master/configmap/pod-configmap-volume1.yaml 
# kubectl logs dapi-test-pod-1 
very# 
```

**Release Note**:
```release-note
NONE
```
pull/6/head
Kubernetes Submit Queue 2017-11-16 03:57:26 -08:00 committed by GitHub
commit 154b30a011
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 21 additions and 0 deletions

View File

@ -289,6 +289,27 @@ func ReadLogs(path, containerID string, opts *LogOptions, runtimeService interna
// Return directly when reading to the end if not follow.
if len(l) > 0 {
glog.Warningf("Incomplete line in log file %q: %q", path, l)
if parse == nil {
// Intialize the log parsing function.
parse, err = getParseFunc(l)
if err != nil {
return fmt.Errorf("failed to get parse function: %v", err)
}
}
// Log a warning and exit if we can't parse the partial line.
if err := parse(l, msg); err != nil {
glog.Warningf("Failed with err %v when parsing partial line for log file %q: %q", err, path, l)
return nil
}
// Write the log line into the stream.
if err := writer.write(msg); err != nil {
if err == errMaximumWrite {
glog.V(2).Infof("Finish parsing log file %q, hit bytes limit %d(bytes)", path, opts.bytes)
return nil
}
glog.Errorf("Failed with err %v when writing partial log for log file %q: %+v", err, path, msg)
return err
}
}
glog.V(2).Infof("Finish parsing log file %q", path)
return nil