From c0971f4fd3b450d7332181544e86fd2c8def8ee6 Mon Sep 17 00:00:00 2001 From: Navratan Lal Gupta Date: Fri, 31 Mar 2023 19:48:21 +0530 Subject: [PATCH] feat #7: Flag to use proxy during install --- cmd/install.go | 4 +++- cmd/kubectl.go | 4 +++- internal/install/install.go | 23 ++++++++++++++++++++++- internal/use/use.go | 2 +- 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/cmd/install.go b/cmd/install.go index 5a60b3d..8af83bc 100644 --- a/cmd/install.go +++ b/cmd/install.go @@ -13,6 +13,7 @@ import ( var overwriteInstall bool var timeout int +var proxy string // installCmd represents the install command var installCmd = &cobra.Command{ @@ -53,7 +54,7 @@ Supported version formats: fmt.Println("Exactly one argumanet is required. Provide kubectl version to install e.g. v1.20.3") os.Exit(1) } - _ = install.InstallKubectl(args[0], overwriteInstall, timeout) + _ = install.InstallKubectl(args[0], overwriteInstall, timeout, proxy) }, } @@ -71,6 +72,7 @@ func init() { // installKubectlCmd.PersistentFlags().BoolP("overwrite", "f", false, "Overwrite or re-install existing version") installKubectlCmd.PersistentFlags().BoolVarP(&overwriteInstall, "overwrite", "f", false, "Overwrite or re-install existing version") installKubectlCmd.PersistentFlags().IntVarP(&timeout, "timeout", "t", 120, "Timeout in seconds [DEFAULT: 120 seconds]") + installKubectlCmd.PersistentFlags().StringVarP(&proxy, "proxy", "p", "", "HTTP/HTTPS proxy to use for downloading clients from its source") // Cobra supports local flags which will only run when this command // is called directly, e.g.: diff --git a/cmd/kubectl.go b/cmd/kubectl.go index 9f6bbf0..33851b2 100644 --- a/cmd/kubectl.go +++ b/cmd/kubectl.go @@ -54,7 +54,7 @@ Supported version formats: fmt.Println("Exactly one argumanet is required. Provide kubectl version to install e.g. v1.20.3") os.Exit(1) } - _ = install.InstallKubectl(args[0], overwriteInstall, timeout) + _ = install.InstallKubectl(args[0], overwriteInstall, timeout, proxy) }, } @@ -128,6 +128,8 @@ func init() { // and all subcommands, e.g.: // kubectlCmd.PersistentFlags().String("foo", "", "A help for foo") kubectlInstallCmd.PersistentFlags().BoolVarP(&overwriteInstall, "overwrite", "f", false, "Overwrite or re-install existing version") + kubectlInstallCmd.PersistentFlags().IntVarP(&timeout, "timeout", "t", 120, "Timeout in seconds [DEFAULT: 120 seconds]") + kubectlInstallCmd.PersistentFlags().StringVarP(&proxy, "proxy", "p", "", "HTTP/HTTPS proxy to use for downloading clients from its source") // Cobra supports local flags which will only run when this command // is called directly, e.g.: diff --git a/internal/install/install.go b/internal/install/install.go index e1caa40..2ceb0b1 100644 --- a/internal/install/install.go +++ b/internal/install/install.go @@ -6,6 +6,7 @@ import ( "io/ioutil" "log" "net/http" + "net/url" "os" "strings" "time" @@ -14,7 +15,7 @@ import ( "github.com/navilg/k8senv/internal/config" ) -func InstallKubectl(version string, overwrite bool, timeout int) error { +func InstallKubectl(version string, overwrite bool, timeout int, proxy string) error { latestVersionUrl := "https://storage.googleapis.com/kubernetes-release/release/stable.txt" dotK8sEnvPath := config.GetDotK8senvPath() @@ -35,6 +36,16 @@ func InstallKubectl(version string, overwrite bool, timeout int) error { }, } + if proxy != "" { + proxy, err := url.Parse(proxy) + if err != nil { + fmt.Println("Failed to fetch latest kubectl version") + fmt.Println(err) + return err + } + client.Transport = &http.Transport{Proxy: http.ProxyURL(proxy)} + } + resp, err := client.Get(latestVersionUrl) if err != nil { log.Fatal(err) @@ -93,6 +104,16 @@ func InstallKubectl(version string, overwrite bool, timeout int) error { }, } + if proxy != "" { + proxy, err := url.Parse(proxy) + if err != nil { + fmt.Println("Failed to fetch latest kubectl version") + fmt.Println(err) + return err + } + client.Transport = &http.Transport{Proxy: http.ProxyURL(proxy)} + } + // Perform HTTP GET request resp, err := client.Get(downloadUrl) if err != nil { diff --git a/internal/use/use.go b/internal/use/use.go index 2e2d248..4b1e25b 100644 --- a/internal/use/use.go +++ b/internal/use/use.go @@ -37,7 +37,7 @@ func UseKubectl(version string) error { if _, err := os.Stat(binaryFileName); os.IsNotExist(err) { fmt.Println("kubectl version", version, "is not installed.") fmt.Println("Installing") - install.InstallKubectl(version, false, 120) + install.InstallKubectl(version, false, 120, "") } if _, err := os.Lstat(kubectlBinaryPath); err == nil {