Skip to content

Commit

Permalink
Merge pull request #7959 from afbjorklund/podman-prompt
Browse files Browse the repository at this point in the history
Use noninteractive sudo when running podman
  • Loading branch information
medyagh authored May 11, 2020
2 parents 07d5472 + 3fa4b91 commit afab7c8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
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
}

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)
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...)...)
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 @@ -90,7 +90,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

0 comments on commit afab7c8

Please sign in to comment.