Skip to content

Commit

Permalink
Merge pull request #9 from navilg/7-cleanmyarr-flag-to-use-proxy-duri…
Browse files Browse the repository at this point in the history
…ng-install

feat #7: Flag to use proxy during install
  • Loading branch information
navilg authored Mar 31, 2023
2 parents a00c5bb + c0971f4 commit ccd30a8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
4 changes: 3 additions & 1 deletion cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

var overwriteInstall bool
var timeout int
var proxy string

// installCmd represents the install command
var installCmd = &cobra.Command{
Expand Down Expand Up @@ -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)

},
}
Expand All @@ -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.:
Expand Down
4 changes: 3 additions & 1 deletion cmd/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

},
}
Expand Down Expand Up @@ -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.:
Expand Down
23 changes: 22 additions & 1 deletion internal/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"strings"
"time"
Expand All @@ -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()

Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion internal/use/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit ccd30a8

Please sign in to comment.