Skip to content

Commit

Permalink
Extract helm download code to public package
Browse files Browse the repository at this point in the history
This allows code re-use with other systems and projects.

Signed-off-by: Alex Ellis (OpenFaaS Ltd) <[email protected]>
  • Loading branch information
alexellis committed Jan 21, 2020
1 parent 5ce8d6a commit 5f315c8
Show file tree
Hide file tree
Showing 18 changed files with 152 additions and 132 deletions.
3 changes: 2 additions & 1 deletion cmd/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ func MakeApps() *cobra.Command {
install.RunE = func(command *cobra.Command, args []string) error {

if len(args) == 0 {
fmt.Printf("You can install: %s\n", strings.TrimRight(strings.Join(getApps(), ", "), ", "))
fmt.Printf("You can install: %s\n%s\n\n", strings.TrimRight("\n - "+strings.Join(getApps(), "\n - "), "\n - "),
`Run k3sup app install NAME --help to see configuration options.`)
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/apps/certmanager_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/alexellis/k3sup/pkg"
"github.com/alexellis/k3sup/pkg/config"
"github.com/alexellis/k3sup/pkg/env"

"github.com/alexellis/k3sup/pkg/helm"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -53,7 +53,7 @@ func MakeInstallCertManager() *cobra.Command {

os.Setenv("HELM_HOME", path.Join(userPath, ".helm"))

_, err = tryDownloadHelm(userPath, clientArch, clientOS, false)
_, err = helm.TryDownloadHelm(userPath, clientArch, clientOS, false)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/apps/chart_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"strings"

"github.com/alexellis/k3sup/pkg"

"github.com/alexellis/k3sup/pkg/config"
"github.com/alexellis/k3sup/pkg/env"
"github.com/alexellis/k3sup/pkg/helm"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -77,7 +77,7 @@ before using the generic helm chart installer command.`,

os.Setenv("HELM_HOME", path.Join(userPath, ".helm"))

_, err = tryDownloadHelm(userPath, clientArch, clientOS, false)
_, err = helm.TryDownloadHelm(userPath, clientArch, clientOS, false)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/apps/cronconnector_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"path"

"github.com/alexellis/k3sup/pkg"

"github.com/alexellis/k3sup/pkg/config"
"github.com/alexellis/k3sup/pkg/env"
"github.com/alexellis/k3sup/pkg/helm"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -57,7 +57,7 @@ func MakeInstallCronConnector() *cobra.Command {

os.Setenv("HELM_HOME", path.Join(userPath, ".helm"))

_, err = tryDownloadHelm(userPath, clientArch, clientOS, false)
_, err = helm.TryDownloadHelm(userPath, clientArch, clientOS, false)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/apps/inletsoperator_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"strings"

"github.com/alexellis/k3sup/pkg"

"github.com/alexellis/k3sup/pkg/config"
"github.com/alexellis/k3sup/pkg/env"
"github.com/alexellis/k3sup/pkg/helm"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -75,7 +75,7 @@ func MakeInstallInletsOperator() *cobra.Command {
if helm3 {
os.Setenv("HELM_VERSION", helm3Version)
}
_, err = tryDownloadHelm(userPath, clientArch, clientOS, helm3)
_, err = helm.TryDownloadHelm(userPath, clientArch, clientOS, helm3)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/apps/istio_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/alexellis/k3sup/pkg"
"github.com/alexellis/k3sup/pkg/config"
"github.com/alexellis/k3sup/pkg/env"

"github.com/alexellis/k3sup/pkg/helm"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -66,7 +66,7 @@ func MakeInstallIstio() *cobra.Command {
os.Setenv("HELM_VERSION", helm3Version)
}

_, err = tryDownloadHelm(userPath, clientArch, clientOS, helm3)
_, err = helm.TryDownloadHelm(userPath, clientArch, clientOS, helm3)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/apps/kafkaconnector_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/alexellis/k3sup/pkg"
"github.com/alexellis/k3sup/pkg/config"
"github.com/alexellis/k3sup/pkg/env"
"github.com/alexellis/k3sup/pkg/helm"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -57,7 +58,7 @@ func MakeInstallKafkaConnector() *cobra.Command {

os.Setenv("HELM_HOME", path.Join(userPath, ".helm"))

_, err = tryDownloadHelm(userPath, clientArch, clientOS, false)
_, err = helm.TryDownloadHelm(userPath, clientArch, clientOS, false)
if err != nil {
return err
}
Expand Down
114 changes: 6 additions & 108 deletions cmd/apps/kubernetes_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@ package apps

import (
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"path"
"strings"

execute "github.com/alexellis/go-execute/pkg/v1"
"github.com/alexellis/k3sup/pkg/env"
)

const helmVersion = "v2.16.0"

func fetchChart(path, chart string, helm3 bool) error {

subdir := ""
Expand All @@ -29,7 +25,7 @@ func fetchChart(path, chart string, helm3 bool) error {
}

task := execute.ExecTask{
Command: fmt.Sprintf("%s fetch %s --untar --untardir %s", localBinary("helm", subdir), chart, path),
Command: fmt.Sprintf("%s fetch %s --untar --untardir %s", env.LocalBinary("helm", subdir), chart, path),
Env: os.Environ(),
StreamStdio: true,
}
Expand Down Expand Up @@ -79,7 +75,7 @@ func helm3Upgrade(basePath, chart, namespace, values string, overrides map[strin
}

task := execute.ExecTask{
Command: localBinary("helm", "helm3"),
Command: env.LocalBinary("helm", "helm3"),
Args: args,
Env: os.Environ(),
Cwd: basePath,
Expand Down Expand Up @@ -131,7 +127,7 @@ func templateChart(basePath, chart, namespace, outputPath, values string, overri

task := execute.ExecTask{
Command: fmt.Sprintf("%s template %s --name %s --namespace %s --output-dir %s %s %s",
localBinary("helm", ""), chart, chart, namespace, outputPath, valuesStr, overridesStr),
env.LocalBinary("helm", ""), chart, chart, namespace, outputPath, valuesStr, overridesStr),
Env: os.Environ(),
Cwd: basePath,
StreamStdio: true,
Expand All @@ -154,24 +150,14 @@ func templateChart(basePath, chart, namespace, outputPath, values string, overri
return nil
}

func localBinary(name, subdir string) string {
home := os.Getenv("HOME")
val := path.Join(home, ".k3sup/bin/")
if len(subdir) > 0 {
val = path.Join(val, subdir)
}

return path.Join(val, name)
}

func addHelmRepo(name, url string, helm3 bool) error {
subdir := ""
if helm3 {
subdir = "helm3"
}

task := execute.ExecTask{
Command: fmt.Sprintf("%s repo add %s %s", localBinary("helm", subdir), name, url),
Command: fmt.Sprintf("%s repo add %s %s", env.LocalBinary("helm", subdir), name, url),
Env: os.Environ(),
StreamStdio: true,
}
Expand All @@ -193,31 +179,8 @@ func updateHelmRepos(helm3 bool) error {
subdir = "helm3"
}
task := execute.ExecTask{
Command: fmt.Sprintf("%s repo update", localBinary("helm", subdir)),
Env: os.Environ(),
StreamStdio: true,
}

res, err := task.Execute()

if err != nil {
return err
}

if res.ExitCode != 0 {
return fmt.Errorf("exit code %d", res.ExitCode)
}
return nil
}

func helmInit() error {
fmt.Printf("Running helm init.\n")
subdir := ""

task := execute.ExecTask{
Command: fmt.Sprintf("%s", localBinary("helm", subdir)),
Command: fmt.Sprintf("%s repo update", env.LocalBinary("helm", subdir)),
Env: os.Environ(),
Args: []string{"init", "--client-only"},
StreamStdio: true,
}

Expand Down Expand Up @@ -275,68 +238,3 @@ func getDefaultKubeconfig() string {

return kubeConfigPath
}

func tryDownloadHelm(userPath, clientArch, clientOS string, helm3 bool) (string, error) {
helmVal := "helm"
if helm3 {
helmVal = "helm3"
}

helmBinaryPath := path.Join(path.Join(userPath, "bin"), helmVal)
if _, statErr := os.Stat(helmBinaryPath); statErr != nil {
subdir := ""
if helm3 {
subdir = "helm3"
}
downloadHelm(userPath, clientArch, clientOS, subdir)

if !helm3 {
err := helmInit()
if err != nil {
return "", err
}
}
}
return helmBinaryPath, nil
}

func getHelmURL(arch, os, version string) string {
archSuffix := "amd64"
osSuffix := strings.ToLower(os)

if strings.HasPrefix(arch, "armv7") {
archSuffix = "arm"
} else if strings.HasPrefix(arch, "aarch64") {
archSuffix = "arm64"
}

return fmt.Sprintf("https://get.helm.sh/helm-%s-%s-%s.tar.gz", version, osSuffix, archSuffix)
}

func downloadHelm(userPath, clientArch, clientOS, subdir string) error {
useHelmVersion := helmVersion
if val, ok := os.LookupEnv("HELM_VERSION"); ok && len(val) > 0 {
useHelmVersion = val
}

helmURL := getHelmURL(clientArch, clientOS, useHelmVersion)
fmt.Println(helmURL)
parsedURL, _ := url.Parse(helmURL)

res, err := http.DefaultClient.Get(parsedURL.String())
if err != nil {
return err
}

dest := path.Join(path.Join(userPath, "bin"), subdir)
os.MkdirAll(dest, 0700)

defer res.Body.Close()
r := ioutil.NopCloser(res.Body)
untarErr := Untar(r, dest)
if untarErr != nil {
return untarErr
}

return nil
}
2 changes: 1 addition & 1 deletion cmd/apps/linkerd_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func downloadLinkerd(userPath, clientOS string) error {

func linkerdCli(parts ...string) (execute.ExecResult, error) {
task := execute.ExecTask{
Command: fmt.Sprintf("%s", localBinary("linkerd", "")),
Command: fmt.Sprintf("%s", env.LocalBinary("linkerd", "")),
Args: parts,
Env: os.Environ(),
StreamStdio: true,
Expand Down
3 changes: 2 additions & 1 deletion cmd/apps/metricsserver_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/alexellis/k3sup/pkg"
"github.com/alexellis/k3sup/pkg/config"
"github.com/alexellis/k3sup/pkg/env"
"github.com/alexellis/k3sup/pkg/helm"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -49,7 +50,7 @@ func MakeInstallMetricsServer() *cobra.Command {

os.Setenv("HELM_HOME", path.Join(userPath, ".helm"))

_, err = tryDownloadHelm(userPath, clientArch, clientOS, false)
_, err = helm.TryDownloadHelm(userPath, clientArch, clientOS, false)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/apps/minio_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/alexellis/k3sup/pkg"
"github.com/alexellis/k3sup/pkg/config"
"github.com/alexellis/k3sup/pkg/env"
"github.com/alexellis/k3sup/pkg/helm"
"github.com/sethvargo/go-password/password"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -61,7 +62,7 @@ func MakeInstallMinio() *cobra.Command {
return fmt.Errorf("please use the helm chart if you'd like to change the namespace to %s", ns)
}

_, err = tryDownloadHelm(userPath, clientArch, clientOS, false)
_, err = helm.TryDownloadHelm(userPath, clientArch, clientOS, false)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/apps/nginx_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/alexellis/k3sup/pkg"
"github.com/alexellis/k3sup/pkg/config"
"github.com/alexellis/k3sup/pkg/env"
"github.com/alexellis/k3sup/pkg/helm"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -53,7 +54,7 @@ func MakeInstallNginx() *cobra.Command {

os.Setenv("HELM_HOME", path.Join(userPath, ".helm"))

_, err = tryDownloadHelm(userPath, clientArch, clientOS, false)
_, err = helm.TryDownloadHelm(userPath, clientArch, clientOS, false)
if err != nil {
return err
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/apps/openfaas_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import (
"strings"

"github.com/alexellis/k3sup/pkg"
"github.com/sethvargo/go-password/password"

"github.com/alexellis/k3sup/pkg/config"
"github.com/alexellis/k3sup/pkg/env"
"github.com/alexellis/k3sup/pkg/helm"
"github.com/sethvargo/go-password/password"

"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -87,7 +88,7 @@ func MakeInstallOpenFaaS() *cobra.Command {
os.Setenv("HELM_VERSION", helm3Version)
}

_, err = tryDownloadHelm(userPath, clientArch, clientOS, helm3)
_, err = helm.TryDownloadHelm(userPath, clientArch, clientOS, helm3)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/apps/postgres_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/alexellis/k3sup/pkg"
"github.com/alexellis/k3sup/pkg/config"
"github.com/alexellis/k3sup/pkg/env"
"github.com/alexellis/k3sup/pkg/helm"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -59,7 +60,7 @@ func MakeInstallPostgresql() *cobra.Command {
return fmt.Errorf("please use the helm chart if you'd like to change the namespace to %s", ns)
}

_, err = tryDownloadHelm(userPath, clientArch, clientOS, false)
_, err = helm.TryDownloadHelm(userPath, clientArch, clientOS, false)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 5f315c8

Please sign in to comment.