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

Use noninteractive sudo when running podman #7959

Merged
merged 5 commits into from
May 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 deletions cmd/minikube/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,26 +90,31 @@ func init() {
}

// shotgun cleanup to delete orphaned docker container data
func deleteContainersAndVolumes() {
if _, err := exec.LookPath(oci.Docker); err != nil {
glog.Infof("skipping deleteContainersAndVolumes for %s: %v", oci.Docker, err)
func deleteContainersAndVolumes(ociBin string) {
if _, err := exec.LookPath(ociBin); err != nil {
glog.Infof("skipping deleteContainersAndVolumes for %s: %v", ociBin, err)
return
}

glog.Infof("deleting containers and volumes ...")

delLabel := fmt.Sprintf("%s=%s", oci.CreatedByLabelKey, "true")
errs := oci.DeleteContainersByLabel(oci.Docker, delLabel)
errs := oci.DeleteContainersByLabel(ociBin, delLabel)
if len(errs) > 0 { // it will error if there is no container to delete
glog.Infof("error delete containers by label %q (might be okay): %+v", delLabel, errs)
}

errs = oci.DeleteAllVolumesByLabel(oci.Docker, delLabel)
errs = oci.DeleteAllVolumesByLabel(ociBin, delLabel)
if len(errs) > 0 { // it will not error if there is nothing to delete
glog.Warningf("error delete volumes by label %q (might be okay): %+v", delLabel, errs)
}

errs = oci.PruneAllVolumesByLabel(oci.Docker, delLabel)
if ociBin == oci.Podman {
// podman prune does not support --filter
return
Copy link
Member

Choose a reason for hiding this comment

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

in this line it seems to return and wont go to the deleteall process?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The operation is not supported (volume prune) on podman, so we can only list-and-delete

}

errs = oci.PruneAllVolumesByLabel(ociBin, delLabel)
if len(errs) > 0 { // it will not error if there is nothing to delete
glog.Warningf("error pruning volumes by label %q (might be okay): %+v", delLabel, errs)
}
Expand Down Expand Up @@ -137,7 +142,8 @@ func runDelete(cmd *cobra.Command, args []string) {
}

if deleteAll {
deleteContainersAndVolumes()
deleteContainersAndVolumes(oci.Docker)
Copy link
Member

Choose a reason for hiding this comment

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

maybe do them in parallel ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

possibly, the theory is that one of them will be "empty"

deleteContainersAndVolumes(oci.Podman)

errs := DeleteProfiles(profilesToDelete)
if len(errs) > 0 {
Expand Down Expand Up @@ -167,6 +173,7 @@ func runDelete(cmd *cobra.Command, args []string) {
if orphan {
// TODO: generalize for non-KIC drivers: #8040
deletePossibleKicLeftOver(cname, driver.Docker)
deletePossibleKicLeftOver(cname, driver.Podman)
}
}

Expand Down Expand Up @@ -209,8 +216,6 @@ func DeleteProfiles(profiles []*config.Profile) []error {

// TODO: remove and/or move to delete package: #8040
func deletePossibleKicLeftOver(cname string, driverName string) {
glog.Infof("deleting possible KIC leftovers for %s (driver=%s) ...", cname, driverName)

bin := ""
switch driverName {
case driver.Docker:
Expand All @@ -221,6 +226,13 @@ func deletePossibleKicLeftOver(cname string, driverName string) {
return
}

if _, err := exec.LookPath(bin); err != nil {
glog.Infof("skipping deletePossibleKicLeftOver for %s: %v", bin, err)
return
}

glog.Infof("deleting possible KIC leftovers for %s (driver=%s) ...", cname, driverName)

delLabel := fmt.Sprintf("%s=%s", oci.ProfileLabelKey, cname)
cs, err := oci.ListContainersByLabel(bin, delLabel)
if err == nil && len(cs) > 0 {
Expand All @@ -239,6 +251,11 @@ func deletePossibleKicLeftOver(cname string, driverName string) {
glog.Warningf("error deleting volumes (might be okay).\nTo see the list of volumes run: 'docker volume ls'\n:%v", errs)
}

if bin == oci.Podman {
// podman prune does not support --filter
return
}

errs = oci.PruneAllVolumesByLabel(bin, delLabel)
if len(errs) > 0 { // it will not error if there is nothing to delete
glog.Warningf("error pruning volume (might be okay):\n%v", errs)
Expand Down
2 changes: 1 addition & 1 deletion pkg/drivers/kic/oci/cli_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (rr RunResult) Output() string {
// PrefixCmd adds any needed prefix (such as sudo) to the command
func PrefixCmd(cmd *exec.Cmd) *exec.Cmd {
if cmd.Args[0] == Podman && runtime.GOOS == "linux" { // want sudo when not running podman-remote
cmdWithSudo := exec.Command("sudo", cmd.Args...)
cmdWithSudo := exec.Command("sudo", append([]string{"-n"}, cmd.Args...)...)
afbjorklund marked this conversation as resolved.
Show resolved Hide resolved
cmdWithSudo.Env = cmd.Env
cmdWithSudo.Dir = cmd.Dir
cmdWithSudo.Stdin = cmd.Stdin
Expand Down
2 changes: 1 addition & 1 deletion pkg/minikube/registry/drvs/podman/podman.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func status() registry.State {
cmd := exec.CommandContext(ctx, oci.Podman, "version", "--format", "{{.Server.Version}}")
// Run with sudo on linux (local), otherwise podman-remote (as podman)
if runtime.GOOS == "linux" {
cmd = exec.CommandContext(ctx, "sudo", "-n", oci.Podman, "version", "--format", "{{.Version}}")
cmd = exec.CommandContext(ctx, "sudo", "-k", "-n", oci.Podman, "version", "--format", "{{.Version}}")
cmd.Env = append(os.Environ(), "LANG=C", "LC_ALL=C") // sudo is localized
}
o, err := cmd.Output()
Expand Down