Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
2 changed files
with
163 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |