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_stats_provider: do not consider exited containers when calculating cpu usage #83504

Merged
merged 2 commits into from
Dec 17, 2019

Conversation

ashleycutalo
Copy link
Contributor

What type of PR is this?
/kind bug

What this PR does / why we need it:
When using the cri_stats_provider with cri-o, the current implementation of remove_terminated_containers() causes the kubelet to report inaccurate stats for pods that have terminated and restarted containers, for example during a crashloop, such as:

NAME                      CPU(cores)       MEMORY(bytes)              
crashy-784977b697-pjnsr   1800454008594m   3Mi             

This in turn causes undesired behavior in the HPA, as it reports seeing an unrealistically high cpu usage and scales up to max:

NAME              REFERENCE                    TARGETS          MINPODS   MAXPODS   REPLICAS   AGE
crashy-main       Deployment/crashy-main       263322784%/50%   5         20        20         175d

This is because the calculation for usageNanoCores subtracts the cached UsageCoreNanoSeconds (a nonzero number) from the newly observed UsageCoreNanoSeconds (which is set to zero for an exited container), resulting in a nonsensical number. Note that the check for zero or negative interval will not trip in this case, as the timestamp will continue to be nonzero and increasing.

Removing all exited containers from the cpu usage calculation fixes this issue. All exited containers will report 0 for UsageNanoCoreSeconds and therefore will never be significant when calculating cpu usage for the pod, so they should be removed from consideration.

Which issue(s) this PR fixes:

Fixes #

Special notes for your reviewer:

Does this PR introduce a user-facing change?:

NONE

Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:


@k8s-ci-robot
Copy link
Contributor

Welcome @ashleykasim!

It looks like this is your first PR to kubernetes/kubernetes 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes/kubernetes has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added release-note-none Denotes a PR that doesn't merit a release note. kind/bug Categorizes issue or PR as related to a bug. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Oct 4, 2019
@k8s-ci-robot
Copy link
Contributor

Thanks for your pull request. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please follow instructions at https://git.k8s.io/community/CLA.md#the-contributor-license-agreement to sign the CLA.

It may take a couple minutes for the CLA signature to be fully registered; after that, please reply here with a new comment and we'll verify. Thanks.


Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here.

@k8s-ci-robot k8s-ci-robot added cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. labels Oct 4, 2019
@k8s-ci-robot
Copy link
Contributor

Hi @ashleykasim. Thanks for your PR.

I'm waiting for a kubernetes member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@k8s-ci-robot k8s-ci-robot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Oct 4, 2019
@k8s-ci-robot k8s-ci-robot added area/kubelet sig/node Categorizes an issue or PR as relevant to SIG Node. and removed needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Oct 4, 2019
@vllry
Copy link
Contributor

vllry commented Oct 4, 2019

/cc

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Oct 4, 2019
result = append(result, refs[0])
continue
}
found := false
for i := 0; i < len(refs); i++ {
if refs[i].State == runtimeapi.ContainerState_CONTAINER_RUNNING {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick: “terminated” and “not running” aren’t the same thing: https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#container-states

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What is the suggestion here? Rename the function? From a logical perspective, only "running" containers are consuming resources, so only running containers should be used when calculating pod resource usage. "Waiting" containers should also be excluded, as they are also not consuming resources.

@vllry
Copy link
Contributor

vllry commented Oct 4, 2019

/priority important-soon

@k8s-ci-robot k8s-ci-robot added priority/important-soon Must be staffed and worked on either currently, or very soon, ideally in time for the next release. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-priority Indicates a PR lacks a `priority/foo` label and requires one. labels Oct 4, 2019
Copy link
Contributor

@mattjmcnaughton mattjmcnaughton left a comment

Choose a reason for hiding this comment

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

Welcome to the k8s project :) thank you for the diff!

/ok-to-test

I agree with @vllry's nit, but other than that, this makes a lot of sense to me.

@k8s-ci-robot k8s-ci-robot removed the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Oct 5, 2019
@vllry
Copy link
Contributor

vllry commented Oct 5, 2019

/assign @dashpole

// It only removes a terminated container when there is a running instance
// of the container.
// removeTerminatedContainers removes all terminated containers since they should
// not be used for usage calculations.
func removeTerminatedContainers(containers []*runtimeapi.Container) []*runtimeapi.Container {
Copy link
Contributor

Choose a reason for hiding this comment

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

I did some more digging, and I think a lot of this function can be removed.

I can only find 2 references to removeTerminatedContainers, and neither use the fact that the output is sorted (by container ID and by create time). Because of that, I think we can remove the whole map and sort process.

@mattjmcnaughton am I barking up the wrong tree?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was originally added in the cadvisor_stats_provider as a fix for #47853. Its primary functionality was for deduping due to a race condition between cadvisor and cgroup removal. If deduping is no longer necessary (fwiw I have not observed this race condition happening between crio and cgroups), this function can indeed be greatly simplified. I did some searching through the commit history and was unable to determine why the deduping functionality was ported forward from cadvisor_stats_provider when cri_stats_provider was added. I have preserved the deduping logic to minimize the impact of this diff, but personally I feel this can probably be removed as well.

@ashleycutalo ashleycutalo force-pushed the remove-all-terminated-containers branch 2 times, most recently from 8a9b9a3 to de23437 Compare October 17, 2019 17:23
@ashleycutalo ashleycutalo force-pushed the remove-all-terminated-containers branch from de23437 to 4b339e6 Compare October 28, 2019 20:31
@ashleycutalo
Copy link
Contributor Author

/retest

2 similar comments
@ashleycutalo
Copy link
Contributor Author

/retest

@ashleycutalo
Copy link
Contributor Author

/retest

@dims
Copy link
Member

dims commented Dec 13, 2019

/assign @yujuhong
/milestone v1.18

@k8s-ci-robot k8s-ci-robot added this to the v1.18 milestone Dec 13, 2019
@dashpole
Copy link
Contributor

/lgtm
/approve
This seems reasonable to me

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Dec 16, 2019
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: ashleykasim, dashpole

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Dec 16, 2019
@k8s-ci-robot k8s-ci-robot merged commit a931227 into kubernetes:master Dec 17, 2019
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. area/kubelet cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/bug Categorizes issue or PR as related to a bug. lgtm "Looks good to me", indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. priority/important-soon Must be staffed and worked on either currently, or very soon, ideally in time for the next release. release-note-none Denotes a PR that doesn't merit a release note. sig/node Categorizes an issue or PR as relevant to SIG Node. 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.

None yet

7 participants