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

CRI: Stop following container log when container exited. #44406

Merged

Conversation

Random-Liu
Copy link
Member

@Random-Liu Random-Liu commented Apr 12, 2017

Fixes #44340.

This PR changed kubelet to periodically check whether container is running when following container logs, and stop following when container exited.

I've tried this PR in my local cluster:

Wed Apr 12 20:23:54 UTC 2017
Wed Apr 12 20:23:58 UTC 2017
Wed Apr 12 20:24:02 UTC 2017
Wed Apr 12 20:24:06 UTC 2017
Wed Apr 12 20:24:10 UTC 2017
Wed Apr 12 20:24:14 UTC 2017
Wed Apr 12 20:24:18 UTC 2017
Wed Apr 12 20:24:22 UTC 2017
Wed Apr 12 20:24:26 UTC 2017
Wed Apr 12 20:24:30 UTC 2017
Wed Apr 12 20:24:34 UTC 2017
Wed Apr 12 20:24:38 UTC 2017
Wed Apr 12 20:24:42 UTC 2017
Wed Apr 12 20:24:46 UTC 2017
failed to wait logs for log file "/var/log/pods/1d54634c7b31346fc3219f5e0b7507cc/nginx_0.log": container "b9a17a2c53550c3703ab350d85911743af8bf164a41813544fd08fb9585f7501" is not running (state="CONTAINER_EXITED")

The only difference is that ReadLogs will return error when container exits during following. I'm not sure whether we should get rid of it or not.

@yujuhong @feiskyer @JorritSalverda
/cc @kubernetes/sig-node-bugs

Release note:

CRI: `kubectl logs -f` now stops following when container stops, as it did pre-CRI.

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Apr 12, 2017
@k8s-reviewable
Copy link

This change is Reviewable

@k8s-github-robot k8s-github-robot added approved Indicates a PR has been approved by an approver from all required OWNERS files. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. release-note-label-needed labels Apr 12, 2017
if err != nil {
return err
}
// Only keep following container log when it is running. There may be a race condition
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the container is not running, we can't tail it anyway. Do we need to care about that?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we'd only reach this function if the container is running...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK. I tried with docker. docker logs -f will also return immediately if the container is in created state.

If so, I think we could also return here, we just shouldn't throw out the error.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

// stateCheckPeriod here, it should be rare.
// Even when that happen, user could also retry after the container becomes running.
if s.State != runtimeapi.ContainerState_CONTAINER_RUNNING {
return fmt.Errorf("container %q is not running (state=%q)", id, s.State)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't think we need the error message.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then I need to introduce another error to tell waitLogs not to continue parse log.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -196,6 +204,40 @@ func ReadLogs(path string, apiOpts *v1.PodLogOptions, stdout, stderr io.Writer)
}
}

// waitLogs wait for the next log write. If the container is not running
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this sentence complete? If the container is not running

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, must be distracted by something else at that time...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@mrunalp
Copy link
Contributor

mrunalp commented Apr 13, 2017

Can't we continue to tail the logs even if container exited? The log file should be just there, right?

@Random-Liu
Copy link
Member Author

Random-Liu commented Apr 13, 2017

@mrunalp We can still tail the log of exited container. However, we should not keep waiting for new lines after reading all the logs in following mode, because the container has exited already.

@JorritSalverda
Copy link

The behaviour prior to 1.6.0 was to stop following logs as soon as the container exited. With an exit code of 0, at least when a container terminated successfully. I used it to tail containers started by a job until successful completion.

@Random-Liu Random-Liu added release-note-none Denotes a PR that doesn't merit a release note. and removed release-note-label-needed labels Apr 13, 2017
@Random-Liu
Copy link
Member Author

@yujuhong Updated the PR and get rid of the error.
Here is the new output:

$ cluster/kubectl.sh logs -f nginx-3
Thu Apr 13 18:23:42 UTC 2017
Thu Apr 13 18:23:46 UTC 2017
Thu Apr 13 18:23:50 UTC 2017
Thu Apr 13 18:23:54 UTC 2017
Thu Apr 13 18:23:58 UTC 2017
Thu Apr 13 18:24:02 UTC 2017
Thu Apr 13 18:24:06 UTC 2017
Thu Apr 13 18:24:10 UTC 2017
$

@yujuhong yujuhong added release-note Denotes a PR that will be considered when it comes time to generate release notes. and removed release-note-none Denotes a PR that doesn't merit a release note. labels Apr 13, 2017
@yujuhong
Copy link
Contributor

/lgtm

@Random-Liu could you add a release-note so that we can patch 1.6 later.

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Apr 13, 2017
@k8s-github-robot
Copy link

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: Random-Liu, yujuhong

Needs approval from an approver in each of these OWNERS Files:

You can indicate your approval by writing /approve in a comment
You can cancel your approval by writing /approve cancel in a comment

@Random-Liu
Copy link
Member Author

@k8s-bot gce etcd3 e2e test this

@k8s-github-robot
Copy link

Automatic merge from submit-queue

@k8s-github-robot k8s-github-robot merged commit 1cf6ef0 into kubernetes:master Apr 14, 2017
@Random-Liu Random-Liu deleted the stop-following-when-exited branch April 14, 2017 04:36
k8s-github-robot pushed a commit that referenced this pull request Apr 14, 2017
…4406-upstream-release-1.6

Automatic merge from submit-queue

Automated cherry pick of #44406 upstream release 1.6

Cherry pick of #44406 on release-1.6.

#44406: CRI: Stop following container log when container exited.

@enisoc 

**Release note**:
```release-note
`kubectl logs -f` now stops following when container stops.
```
@k8s-cherrypick-bot
Copy link

Commit found in the "release-1.6" branch appears to be this PR. Removing the "cherrypick-candidate" label. If this is an error find help to get your PR picked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants