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

Check if Docker installed via Snap Package Manager #11088

Merged
merged 2 commits into from
Apr 14, 2021
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
19 changes: 4 additions & 15 deletions pkg/minikube/command/kic_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"strconv"
"strings"
Expand All @@ -35,6 +34,7 @@ import (
"k8s.io/klog/v2"
"k8s.io/minikube/pkg/drivers/kic/oci"
"k8s.io/minikube/pkg/minikube/assets"
"k8s.io/minikube/pkg/minikube/detect"
)

// kicRunner runs commands inside a container
Expand Down Expand Up @@ -187,8 +187,7 @@ func (k *kicRunner) Copy(f assets.CopyableFile) error {
}
klog.Infof("%s (temp): %s --> %s (%d bytes)", k.ociBin, src, dst, f.GetLength())

isSnap := isSnapBinary()
tmpFolder, err := tempDirectory(isSnap)
tmpFolder, err := tempDirectory(detect.MinikubeInstalledViaSnap(), detect.DockerInstalledViaSnap())
if err != nil {
return errors.Wrap(err, "determining temp directory")
}
Expand All @@ -207,8 +206,8 @@ func (k *kicRunner) Copy(f assets.CopyableFile) error {

// tempDirectory returns the directory to use as the temp directory
// or an empty string if it should use the os default temp directory.
func tempDirectory(isSnap bool) (string, error) {
if !isSnap {
func tempDirectory(isMinikubeSnap bool, isDockerSnap bool) (string, error) {
if !isMinikubeSnap && !isDockerSnap {
return "", nil
}

Expand All @@ -222,16 +221,6 @@ func tempDirectory(isSnap bool) (string, error) {
return home, nil
}

// isSnapBinary returns true if the binary path includes "snap".
func isSnapBinary() bool {
ex, err := os.Executable()
if err != nil {
return false
}
exPath := filepath.Dir(ex)
return strings.Contains(exPath, "snap")
}

func (k *kicRunner) copy(src string, dst string) error {
fullDest := fmt.Sprintf("%s:%s", k.nameOrID, dst)
if k.ociBin == oci.Podman {
Expand Down
15 changes: 9 additions & 6 deletions pkg/minikube/command/kic_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,24 @@ func TestKICRunner(t *testing.T) {
}

tests := []struct {
in bool
want string
isMinikubeSnap bool
isDockerSnap bool
want string
}{
{false, ""},
{true, home},
{false, false, ""},
{true, true, home},
{false, true, home},
{true, false, home},
}

for _, tt := range tests {
got, err := tempDirectory(tt.in)
got, err := tempDirectory(tt.isMinikubeSnap, tt.isDockerSnap)
if err != nil {
t.Fatalf("failed to get temp directory: %v", err)
}

if got != tt.want {
t.Errorf("tempDirectory(%t) = %s; want %s", tt.in, got, tt.want)
t.Errorf("tempDirectory(%t, %t) = %s; want %s", tt.isMinikubeSnap, tt.isDockerSnap, got, tt.want)
}
}
})
Expand Down
24 changes: 24 additions & 0 deletions pkg/minikube/detect/detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package detect
import (
"net/http"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"

Expand Down Expand Up @@ -52,3 +54,25 @@ func IsCloudShell() bool {
func IsAmd64M1Emulation() bool {
return runtime.GOARCH == "amd64" && strings.HasPrefix(cpuid.CPU.BrandName, "VirtualApple")
}

// MinikubeInstalledViaSnap returns true if the minikube binary path includes "snap".
func MinikubeInstalledViaSnap() bool {
ex, err := os.Executable()
if err != nil {
return false
}
exPath := filepath.Dir(ex)

return strings.Contains(exPath, "snap")
}

// DockerInstalledViaSnap returns true if the Docker binary path includes "snap".
func DockerInstalledViaSnap() bool {
c := exec.Command("which", "docker")
o, err := c.Output()
if err != nil {
return false
}

return strings.Contains(string(o), "snap")
}