forked from steamhaus/kubeswitch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
184 lines (145 loc) · 4.66 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package main
import (
"bufio"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"runtime"
"strings"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/akamensky/argparse"
)
//Releases is enerated with https://mholt.github.io/json-to-go/
type Releases []struct {
TagName string `json:"name"`
}
const (
stableURL = "https://storage.googleapis.com/kubernetes-release/release/stable.txt"
releaseURL = "https://api.github.com/repos/kubernetes/kubernetes/releases"
downloadURL = "https://storage.googleapis.com/kubernetes-release/release/"
installLocation = "/usr/local/bin/kubectl"
binPathLinux = "/bin/linux/amd64/kubectl"
binPathMac = "/bin/darwin/amd64/kubectl"
//GOOS is used to detect the OS used by the host
GOOS = runtime.GOOS
)
var binPath string
var versionToInstall string
func checkOS() {
if GOOS == "linux" {
binPath = binPathLinux
} else if GOOS == "darwin" {
binPath = binPathMac
} else {
os.Exit(1)
}
}
func main() {
checkOS()
parser := argparse.NewParser("kubeswitch", "easily swap kubectl versions")
versionFlag := parser.String("v", "version", &argparse.Options{Required: false, Help: "specifiy a version to download"})
awsFlag := parser.Flag("a", "aws", &argparse.Options{Required: false, Help: "Check your AWS EKS/Kops version"})
// Maybe in the future there can be an acceptance check, but if we want this as part of an automated sequence it make senses to assume user input is always a correct version.
err := parser.Parse(os.Args)
if err != nil {
fmt.Println(parser.Usage(err))
return
}
if *awsFlag {
checkAWSAuth()
}
if *versionFlag != "" {
fmt.Println("You've selected kubectl version: ", *versionFlag, "to install")
fmt.Println("Downloading kubectl version: ", *versionFlag, "to ", installLocation)
downloadFile(installLocation, *versionFlag)
fmt.Println(*versionFlag)
} else if *versionFlag == "" {
getStable()
}
}
func getStable() {
resp, err := http.Get(stableURL)
if err != nil {
fmt.Println("Cannot read latest stable version from remote repository", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("Error reading body: %v", err)
}
result := string(body)
reader := bufio.NewReader(os.Stdin)
fmt.Println("Latest stable release is:" + " " + result + "" + "Do you want to install this version? [yes/no]")
text, _ := reader.ReadString('\n')
if strings.TrimRight(text, "\n") == "yes" || strings.TrimRight(text, "\n") == "y" {
fmt.Println("Downloading Kubernetes version: " + " " + result + "to" + " " + installLocation)
// There is a bug somewhere appending a new line to the result, causing a nil pointer reference
downloadFile(installLocation, strings.TrimRight(result, "\n"))
fmt.Println("version" + " " + result + "has been installed")
os.Exit(0)
} else {
getAllReleases()
fmt.Println("Which version would you like to install?")
versionInput, _ := reader.ReadString('\n')
versionWanted := strings.TrimRight(versionInput, "\n")
downloadFile(installLocation, versionWanted)
fmt.Println("Downloading Kubernetes version....", versionWanted, "....to", installLocation)
}
}
func getAllReleases() {
resp, err := http.Get(releaseURL)
if err != nil {
fmt.Println("Cannot see all latest releaes", err, http.StatusInternalServerError)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Errorf("Read body: %v", err)
}
var data Releases
json.Unmarshal(body, &data)
//TODO: Work out how we can format this list better wit a new line after each result
fmt.Printf("Other releases available are: %v\n", data)
defer resp.Body.Close()
}
func downloadFile(installDirectory string, versionWanted string) {
resp, err := http.Get(downloadURL + versionWanted + binPath)
out, err := os.Create("kubectl")
if err != nil {
fmt.Println(err)
}
n, err := io.Copy(out, resp.Body)
err = os.Chmod("kubectl", 755)
if err != nil {
fmt.Println(err, n)
}
x := os.Rename("kubectl", installLocation)
if x != nil {
fmt.Println(x)
}
defer out.Close()
defer resp.Body.Close()
}
func checkAWSAuth() {
creds := credentials.NewEnvCredentials()
_, err := creds.Get()
if err != nil {
fmt.Println("AWS Credentials not found or set. Skipping...")
} else {
out, _ := exec.Command("kubectl", "version", "--short").Output()
verParse := string(out)
ver := verParse[40:47]
reader := bufio.NewReader(os.Stdin)
fmt.Println("Your EKS cluster version is: ", ver, "- do you want to match your client version? [yes/no]")
text, _ := reader.ReadString('\n')
if strings.TrimRight(text, "\n") == "yes" {
downloadFile(installLocation, ver)
fmt.Println("Client and Server matched.")
os.Exit(0)
}
}
}