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

kubeadm: reset: use crictl to reset containers #54721

Merged
merged 1 commit into from
Nov 4, 2017

Conversation

runcom
Copy link
Contributor

@runcom runcom commented Oct 27, 2017

@luxas PTAL

Signed-off-by: Antonio Murdaca [email protected]

What this PR does / why we need it:

This patch makes kubeadm to try and reset containers using crictl first instead of docker. The reason is that kubeadm reset is ineffective with new container runtimes using the CRI (like CRI-O).
This patch uses crictl first and falls back to docker in case crictl isn't available.

Which issue this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close that issue when PR gets merged): fixes #

Fix kubernetes/kubeadm#508

Special notes for your reviewer:

Release note:

kubeadm: reset: use crictl to reset containers

@k8s-ci-robot k8s-ci-robot added 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. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Oct 27, 2017
Copy link
Member

@luxas luxas left a comment

Choose a reason for hiding this comment

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

Generally LGTM, some nits/comments/questions

fmt.Println("[reset] Removing kubernetes-managed containers.")
if err := exec.Command("sh", "-c", "docker ps -a --filter name=k8s_ -q | xargs -r docker rm --force --volumes").Run(); err != nil {
fmt.Println("[reset] Failed to stop the running containers.")
resetWithDocker := func() {
Copy link
Member

Choose a reason for hiding this comment

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

write this as a normal function top-level

resetWithDocker()
}
} else {
fmt.Println("[reest] CRI socket path not provided for crictl. Trying docker instead.")
Copy link
Member

Choose a reason for hiding this comment

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

typo: reset

crictlPath, err := exec.LookPath("crictl")
if err == nil {
if r.criSocketPath != "" {
if err := exec.Command("sh", "-c", crictlPath+" -r "+r.criSocketPath+" sandboxes --quiet | xargs -r "+crictlPath+" -r "+r.criSocketPath+" rms"); err != nil {
Copy link
Member

Choose a reason for hiding this comment

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

Logging here that "resetting foo env with tool bar and socket baz" would be helpful

}
} else {
fmt.Println("[reset] Docker doesn't seem to be running. Skipping the removal of running Kubernetes containers.")
fmt.Println("[reset] crictl isn't available on your system. Trying using docker instead.")
Copy link
Member

Choose a reason for hiding this comment

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

not sure we need to log this, seems like an error to users, while everything's fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I'm fine dropping this one.

@@ -56,16 +57,22 @@ func NewCmdReset(out io.Writer) *cobra.Command {
"The path to the directory where the certificates are stored. If specified, clean this directory.",
)

cmd.PersistentFlags().StringVar(
&criSocketPath, "cri-socket", "",
"The path to the CRI socket to use with crictl when cleaning up containers.",
Copy link
Member

Choose a reason for hiding this comment

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

should we default to the dockershim socket?

@runcom
Copy link
Contributor Author

runcom commented Oct 28, 2017

@luxas addressed all your comments

Copy link
Member

@luxas luxas left a comment

Choose a reason for hiding this comment

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

Found two more things 😄

if err == nil {
if r.criSocketPath != "" {
fmt.Println("[reset] Cleaning up running containers using crictl with socket %s", r.criSocketPath)
if err := exec.Command("sh", "-c", crictlPath+" -r "+r.criSocketPath+" sandboxes --quiet | xargs -r "+crictlPath+" -r "+r.criSocketPath+" rms"); err != nil {
Copy link
Member

Choose a reason for hiding this comment

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

for readability, can you please create a separate string for this and use fmt.Sprintf?

func resetWithDocker() {
dockerCheck := preflight.ServiceCheck{Service: "docker", CheckIfActive: true}
if _, errors := dockerCheck.Check(); len(errors) == 0 {
if err := exec.Command("sh", "-c", "docker ps -a --filter name=k8s_ -q | xargs -r docker rm --force --volumes").Run(); err != nil {
Copy link
Member

Choose a reason for hiding this comment

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

I know this wasn't here before sigh, but now I really think we should have unit tests for this, as the complexity grows.
Can you please use k8s.io/utils/exec that you inject to the functions here, and then fake it out and test the different cases out in unit tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah, I'll add a unit test for this function but it needs to mock out dockerCheck as well

@runcom runcom force-pushed the reset-remove-dockerism branch 2 times, most recently from e1e4cb1 to b60bbbc Compare October 29, 2017 16:27
@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Oct 29, 2017
@runcom
Copy link
Contributor Author

runcom commented Oct 29, 2017

@luxas reworked and added unit tests. PTAL

@runcom
Copy link
Contributor Author

runcom commented Oct 29, 2017

/retest

@runcom
Copy link
Contributor Author

runcom commented Oct 29, 2017

Looks like tests are failing cause they're using old code (???)

@luxas
Copy link
Member

luxas commented Nov 1, 2017

/retest

Copy link
Member

@luxas luxas left a comment

Choose a reason for hiding this comment

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

/approve

Bazel is failing with, I don't know why:

W1101 19:03:02.819] 2017/11/01 19:03:02 missing strict dependencies:
W1101 19:03:02.819]     cmd/kubeadm/app/cmd/reset.go: import of k8s.io/utils/exec, which is not a direct dependency

cc @sttts @ixdy

@@ -181,3 +185,131 @@ func TestConfigDirCleaner(t *testing.T) {
}
}
}

// TODO(justinsb): Add test for nodePort conflict detection, once we have nodePort wired in
Copy link
Member

Choose a reason for hiding this comment

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

?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Mmm i don't know where that comes from, I'll fix remove it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

@k8s-github-robot k8s-github-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Nov 1, 2017
@luxas
Copy link
Member

luxas commented Nov 1, 2017

@sttts mentioned that bazel-contrib/rules_go#831 might be related/relevant for bazel failure

@ixdy
Copy link
Member

ixdy commented Nov 1, 2017

it looks like you need to run hack/update-bazel.sh?

@runcom
Copy link
Contributor Author

runcom commented Nov 2, 2017

it looks like you need to run hack/update-bazel.sh?

done

@runcom
Copy link
Contributor Author

runcom commented Nov 2, 2017

/test pull-kubernetes-verify

@runcom
Copy link
Contributor Author

runcom commented Nov 2, 2017

/test pull-kubernetes-verify

@runcom
Copy link
Contributor Author

runcom commented Nov 3, 2017

@luxas everything passed now

Copy link
Member

@luxas luxas left a comment

Choose a reason for hiding this comment

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

/lgtm

Thank you for this contribution!

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

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: luxas, runcom

Associated issue: 508

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

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

@k8s-github-robot
Copy link

/test all [submit-queue is verifying that this PR is safe to merge]

@k8s-github-robot
Copy link

Automatic merge from submit-queue. If you want to cherry-pick this change to another branch, please follow the instructions here.

@k8s-github-robot k8s-github-robot merged commit 5691a8d into kubernetes:master Nov 4, 2017
@runcom runcom deleted the reset-remove-dockerism branch November 4, 2017 21:57
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/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

kubeadm reset shouldn't assume docker, use crictl
7 participants