Skip to content

Commit

Permalink
Add Linkerd app
Browse files Browse the repository at this point in the history
Closes: #87, thank you to @phumberdroz for the initial attempt.

Tested on a new cluster.

Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
  • Loading branch information
alexellis committed Nov 14, 2019
1 parent e777043 commit 82e7b64
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 1 deletion.
5 changes: 4 additions & 1 deletion pkg/cmd/apps.go
Expand Up @@ -45,10 +45,13 @@ func MakeApps() *cobra.Command {
install.AddCommand(makeInstallNginx())
install.AddCommand(makeInstallChart())
install.AddCommand(makeInstallTiller())
install.AddCommand(makeInstallLinkerd())

return command
}

func getApps() []string {
return []string{"openfaas", "nginx-ingress", "cert-manager", "openfaas-ingress", "inlets-operator", "metrics-server", "chart", "tiller"}
return []string{"openfaas", "nginx-ingress", "cert-manager",
"openfaas-ingress", "inlets-operator", "metrics-server",
"chart", "tiller", "linkerd"}
}
159 changes: 159 additions & 0 deletions pkg/cmd/linkerd_app.go
@@ -0,0 +1,159 @@
package cmd

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

execute "github.com/alexellis/go-execute/pkg/v1"
"github.com/alexellis/k3sup/pkg/config"
"github.com/spf13/cobra"
)

func makeInstallLinkerd() *cobra.Command {
var linkerd = &cobra.Command{
Use: "linkerd",
Short: "Install linkerd",
Long: `Install linkerd`,
Example: ` k3sup app install linkerd`,
SilenceUsage: true,
}

linkerd.RunE = func(command *cobra.Command, args []string) error {
kubeConfigPath := getDefaultKubeconfig()

if command.Flags().Changed("kubeconfig") {
kubeConfigPath, _ = command.Flags().GetString("kubeconfig")
}
fmt.Printf("Using kubeconfig: %s\n", kubeConfigPath)
arch := getArchitecture()
fmt.Printf("Node architecture: %q\n", arch)

userPath, err := config.InitUserDir()
if err != nil {
return err
}

_, clientOS := getClientArch()

fmt.Printf("Client: %q\n", clientOS)

log.Printf("User dir established as: %s\n", userPath)

err = downloadLinkerd(userPath, clientOS)
if err != nil {
return err
}
fmt.Println("Running linkerd check, this may take a few moments.")

_, err = linkerdCli("check", "--pre")
if err != nil {
return err
}

res, err := linkerdCli("install")
if err != nil {
return err
}
file, err := ioutil.TempFile("", "linkerd")
w := bufio.NewWriter(file)
_, err = w.WriteString(res.Stdout)
if err != nil {
return err
}
w.Flush()

err = kubectl("apply", "-R", "-f", file.Name())
if err != nil {
return err
}

defer os.Remove(file.Name())

_, err = linkerdCli("check")
if err != nil {
return err
}

fmt.Println(`=======================================================================
= Linkerd has been installed. =
=======================================================================
# Get the linkerd-cli
curl -sL https://run.linkerd.io/install | sh
# Find out more at:
# https://linkerd.io
# To use the Linkerd CLI set this path:
export PATH=$PATH:` + path.Join(userPath, "bin/") + `
linkerd --help
` + thanksForUsing)
return nil
}

return linkerd
}

func getLinkerdURL(os, version string) string {
osSuffix := strings.ToLower(os)
return fmt.Sprintf("https://github.com/linkerd/linkerd2/releases/download/%s/linkerd2-cli-%s-%s", version, version, osSuffix)
}

func downloadLinkerd(userPath, clientOS string) error {
filePath := path.Join(path.Join(userPath, "bin"), "linkerd")
if _, statErr := os.Stat(filePath); statErr != nil {
linkerdURL := getLinkerdURL(clientOS, "stable-2.6.0")
fmt.Println(linkerdURL)
parsedURL, _ := url.Parse(linkerdURL)

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

defer res.Body.Close()
out, err := os.Create(filePath)
if err != nil {
return err
}
defer out.Close()

// Write the body to file
_, err = io.Copy(out, res.Body)

err = os.Chmod(filePath, 0755)
if err != nil {
return err
}
}
return nil
}

func linkerdCli(parts ...string) (execute.ExecResult, error) {
task := execute.ExecTask{
Command: fmt.Sprintf("%s", localBinary("linkerd")),
Args: parts,
Env: os.Environ(),
}
res, err := task.Execute()

if err != nil {
return res, err
}

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

return res, nil
}

0 comments on commit 82e7b64

Please sign in to comment.