Skip to content

Commit

Permalink
fix commands running crictl
Browse files Browse the repository at this point in the history
Running "kubeadm reset --cri-socket unix:///var/run/crio/crio.sock"
fails with this error:
[reset] Cleaning up running containers using crictl with socket unix:///var/run/crio/crio.sock
[reset] Failed to list running pods using crictl. Trying using docker instead.

The actual error returned by underlying API os/exec is:
fork/exec /usr/bin/crictl -r /var/run/crio/crio.sock info: no such file or directory

This is caused by passing full command line instead of executable
path as a first parameter to the Command API.

Fixed by passing correct parameters to the Command API.
Improved error output.

Fixed crictl commands stop->stopp, rm->rmp (taken from kubernetes#63862)
  • Loading branch information
bart0sh committed May 15, 2018
1 parent 42b63c8 commit 52ed4cc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
31 changes: 16 additions & 15 deletions cmd/kubeadm/app/cmd/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@ import (
utilsexec "k8s.io/utils/exec"
)

var (
crictlSandboxesParamsFormat = "%s -r %s sandboxes --quiet | xargs -r"
crictlStopParamsFormat = "%s -r %s stops %s"
crictlRemoveParamsFormat = "%s -r %s rms %s"
)

// NewCmdReset returns the "kubeadm reset" command
func NewCmdReset(out io.Writer) *cobra.Command {
var skipPreFlight bool
Expand Down Expand Up @@ -183,24 +177,31 @@ func resetWithDocker(execer utilsexec.Interface, dockerCheck preflight.Checker)
func resetWithCrictl(execer utilsexec.Interface, dockerCheck preflight.Checker, criSocketPath, crictlPath string) {
if criSocketPath != "" {
fmt.Printf("[reset] Cleaning up running containers using crictl with socket %s\n", criSocketPath)
listcmd := fmt.Sprintf(crictlSandboxesParamsFormat, crictlPath, criSocketPath)
output, err := execer.Command(listcmd).CombinedOutput()
fmt.Println("[reset] Listing running pods using crictl")
params := []string{"-r", criSocketPath, "pods", "--quiet"}
fmt.Printf("[reset] Executing command %s %s\n", crictlPath, strings.Join(params, " "))
output, err := execer.Command(crictlPath, params...).CombinedOutput()
if err != nil {
fmt.Println("[reset] Failed to list running pods using crictl. Trying using docker instead.")
fmt.Printf("[reset] failed to list running pods using crictl: %v. Trying to use docker instead\n", err)
resetWithDocker(execer, dockerCheck)
return
}
sandboxes := strings.Split(string(output), " ")
for _, s := range sandboxes {
stopcmd := fmt.Sprintf(crictlStopParamsFormat, crictlPath, criSocketPath, s)
if err := execer.Command(stopcmd).Run(); err != nil {
fmt.Println("[reset] Failed to stop the running containers using crictl. Trying using docker instead.")
if strings.TrimSpace(s) == "" {
continue
}
params = []string{"-r", criSocketPath, "stopp", s}
fmt.Printf("[reset] Executing command %s %s\n", crictlPath, strings.Join(params, " "))
if err := execer.Command(crictlPath, params...).Run(); err != nil {
fmt.Printf("[reset] failed to stop the running containers using crictl: %v. Trying to use docker instead\n", err)
resetWithDocker(execer, dockerCheck)
return
}
removecmd := fmt.Sprintf(crictlRemoveParamsFormat, crictlPath, criSocketPath, s)
if err := execer.Command(removecmd).Run(); err != nil {
fmt.Println("[reset] Failed to remove the running containers using crictl. Trying using docker instead.")
params = []string{"-r", criSocketPath, "rmp", s}
fmt.Printf("[reset] executing command %s %s\n", crictlPath, strings.Join(params, " "))
if err := execer.Command(crictlPath, params...).Run(); err != nil {
fmt.Printf("[reset] failed to remove the running containers using crictl: %v. Trying to use docker instead\n", err)
resetWithDocker(execer, dockerCheck)
return
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/kubeadm/app/preflight/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,11 @@ func (CRICheck) Name() string {
func (criCheck CRICheck) Check() (warnings, errors []error) {
crictlPath, err := criCheck.exec.LookPath("crictl")
if err != nil {
errors = append(errors, fmt.Errorf("unable to find command crictl: %s", err))
errors = append(errors, fmt.Errorf("unable to find command crictl: %v", err))
return warnings, errors
}
if err := criCheck.exec.Command(fmt.Sprintf("%s -r %s info", crictlPath, criCheck.socket)).Run(); err != nil {
errors = append(errors, fmt.Errorf("unable to check if the container runtime at %q is running: %s", criCheck.socket, err))
if err := criCheck.exec.Command(crictlPath, "-r", criCheck.socket, "info").Run(); err != nil {
errors = append(errors, fmt.Errorf("unable to check if the container runtime at %q is running: %v", criCheck.socket, err))
return warnings, errors
}
return warnings, errors
Expand Down

0 comments on commit 52ed4cc

Please sign in to comment.