From b7f76aedce9c9d3cc550dca82f63cbd85a91627b Mon Sep 17 00:00:00 2001 From: gsquared94 Date: Wed, 26 Aug 2020 11:38:53 +0530 Subject: [PATCH 1/2] Move `DetectWSL` function into util package --- pkg/skaffold/docker/client.go | 19 ++--------------- pkg/skaffold/util/wsl.go | 39 +++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 17 deletions(-) create mode 100644 pkg/skaffold/util/wsl.go diff --git a/pkg/skaffold/docker/client.go b/pkg/skaffold/docker/client.go index 71294c92c6f..65ce48bde79 100644 --- a/pkg/skaffold/docker/client.go +++ b/pkg/skaffold/docker/client.go @@ -20,7 +20,6 @@ import ( "context" "errors" "fmt" - "io/ioutil" "net/http" "os" "os/exec" @@ -164,22 +163,8 @@ func getUserAgentHeader() map[string]string { } } -func detectWsl() (bool, error) { - if _, err := os.Stat("/proc/version"); err == nil { - b, err := ioutil.ReadFile("/proc/version") - if err != nil { - return false, fmt.Errorf("read /proc/version: %w", err) - } - str := strings.ToLower(string(b)) - if strings.Contains(str, "microsoft") { - return true, nil - } - } - return false, nil -} - func getMiniKubeFilename() (string, error) { - if found, _ := detectWsl(); found { + if found, _ := util.DetectWSL(); found { filename, err := exec.LookPath("minikube.exe") if err != nil { return "", errors.New("unable to find minikube.exe. Please add it to PATH environment variable") @@ -221,7 +206,7 @@ func getMinikubeDockerEnv(minikubeProfile string) (map[string]string, error) { env[kv[0]] = kv[1] } - if found, _ := detectWsl(); found { + if found, _ := util.DetectWSL(); found { cmd := exec.Command("wslpath", env["DOCKER_CERT_PATH"]) out, err := util.RunCmdOut(cmd) if err == nil { diff --git a/pkg/skaffold/util/wsl.go b/pkg/skaffold/util/wsl.go new file mode 100644 index 00000000000..d3f902da1a1 --- /dev/null +++ b/pkg/skaffold/util/wsl.go @@ -0,0 +1,39 @@ +/* +Copyright 2020 The Skaffold Authors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package util + +import ( + "fmt" + "io/ioutil" + "os" + "strings" +) + +func DetectWSL() (bool, error) { + if _, err := os.Stat("/proc/version"); err == nil { + b, err := ioutil.ReadFile("/proc/version") + if err != nil { + return false, fmt.Errorf("read /proc/version: %w", err) + } + + str := strings.ToLower(string(b)) + if strings.Contains(str, "microsoft") { + return true, nil + } + } + return false, nil +} From d81a76217bba02be22219406b3d4b46137c78b32 Mon Sep 17 00:00:00 2001 From: gsquared94 Date: Thu, 27 Aug 2020 16:17:22 +0530 Subject: [PATCH 2/2] add comments --- pkg/skaffold/docker/client.go | 1 + pkg/skaffold/util/wsl.go | 2 ++ 2 files changed, 3 insertions(+) diff --git a/pkg/skaffold/docker/client.go b/pkg/skaffold/docker/client.go index 65ce48bde79..9b38463c1c5 100644 --- a/pkg/skaffold/docker/client.go +++ b/pkg/skaffold/docker/client.go @@ -207,6 +207,7 @@ func getMinikubeDockerEnv(minikubeProfile string) (map[string]string, error) { } if found, _ := util.DetectWSL(); found { + // rewrite Unix path to Windows cmd := exec.Command("wslpath", env["DOCKER_CERT_PATH"]) out, err := util.RunCmdOut(cmd) if err == nil { diff --git a/pkg/skaffold/util/wsl.go b/pkg/skaffold/util/wsl.go index d3f902da1a1..30f07ba1088 100644 --- a/pkg/skaffold/util/wsl.go +++ b/pkg/skaffold/util/wsl.go @@ -23,6 +23,7 @@ import ( "strings" ) +// DetectWSL checks for Windows Subsystem for Linux func DetectWSL() (bool, error) { if _, err := os.Stat("/proc/version"); err == nil { b, err := ioutil.ReadFile("/proc/version") @@ -30,6 +31,7 @@ func DetectWSL() (bool, error) { return false, fmt.Errorf("read /proc/version: %w", err) } + // Microsoft changed the case between WSL1 and WSL2 str := strings.ToLower(string(b)) if strings.Contains(str, "microsoft") { return true, nil