-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathk8sfolks.go
139 lines (124 loc) · 4.24 KB
/
k8sfolks.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
/* An Interactive cli to start, stop and delete AKS Cluster
At ADfolks we required this to reduce the AKS billing costs
We use this to stop the Dev clusters when not in use
Or delete and start again with from scratch in a single command
*/
package main
import (
"fmt"
"bufio"
"os"
"os/exec"
"log"
"strings"
)
func getClusterName() string {
readerClusterName := bufio.NewReader(os.Stdin)
fmt.Println("Enter k8s cluster Name:")
clusterName, _ := readerClusterName.ReadString('\n')
clusterName = strings.TrimSuffix(clusterName,"\n")
return clusterName
}
func getClusterRegion() string {
readerClusterName := bufio.NewReader(os.Stdin)
fmt.Println("Enter k8s cluster Region:")
clusterRegion, _ := readerClusterName.ReadString('\n')
clusterRegion = strings.TrimSuffix(clusterRegion,"\n")
return clusterRegion
}
func createResourceGroup(resourceGroupName string, clusterRegion string) {
cmd := exec.Command("az", "group", "create", "-l", clusterRegion, "-n",
resourceGroupName)
out, err := cmd.CombinedOutput()
if err != nil {
log.Fatalf("cmd.Run() failed with %s\n", err)
}
fmt.Printf("Output:\n%s\n", string(out))
}
func createCluster( clusterName string, resourceGroupName string) {
fmt.Println("Starting to set up your k8s Cluster")
fmt.Println("This would take a few minutes...")
fmt.Println("---------------------------------")
//Create AKS Cluster
cmd := exec.Command("az", "aks", "create", "--name", clusterName,
"--resource-group", resourceGroupName, "--node-count",
"6", "--kubernetes-version", "1.11.3")
out, err := cmd.CombinedOutput()
if err != nil {
log.Fatalf("cmd.Run() failed with %s\n", err)
}
fmt.Printf("Output:\n%s\n", string(out))
}
func getKubectlConfig(clusterName string, resourceGroupName string) {
//Get Kubectl credentials copied to ~/.kube/config
cmd := exec.Command("az", "aks", "get-credentials",
"--resource-group", resourceGroupName, "--name",clusterName)
out, err := cmd.CombinedOutput()
if err != nil {
log.Fatalf("cmd.Run() Failed with %s\n", err)
}
fmt.Printf("Get kubectl config Output: \n%s\n", string(out))
}
func intializeHelm() {
//Initialize Helm
fmt.Println("Initialising Package manager Helm")
cmd := exec.Command("helm", "init")
out, err := cmd.CombinedOutput()
if err != nil {
log.Fatalf("cmd.Run() Failed with:\n%s\n", err)
}
fmt.Printf("Helm Init Output:\n%s\n", string(out))
}
func addHelmRepo(repoName string, repoURL string) {
fmt.Println("Adding Helm repo:", repoName)
cmd := exec.Command("helm", "repo", "add", repoName, repoURL)
out, err := cmd.CombinedOutput()
if err != nil {
log.Fatalf("Add Helm Repo failed with: \n%s\n", err)
}
fmt.Println("Helm Add repo output:\n%s\n", string(out))
}
func createNamespace(nameSpace string) {
cmd := exec.Command("kubectl", "create", "namespace", nameSpace)
out, err := cmd.CombinedOutput()
if err != nil {
log.Fatalf("Kubectl create namespace %s failed with: \n%s\n",
nameSpace, err)
}
fmt.Println("kubectl create namespace %s output:\n%s\n", nameSpace,
string(out))
}
func installPackage(packageName string, repoName string) {
//Create Namespace in same name as packageName
createNamespace(packageName)
repowithPackage := repoName + "/" + packageName
cmd := exec.Command("helm", "install", "--name", packageName,
"--namespace", packageName, repowithPackage)
out, err := cmd.CombinedOutput()
if err != nil {
log.Fatalf("cmd.run() Failed with:\n%s\n", err)
}
fmt.Println("Package install %s Output:\n%s\n", packageName, string(out))
}
func main() {
clusterName := getClusterName()
clusterRegion := getClusterRegion()
//assign Azure Resource Group same name as cluster name
resourceGroupName := clusterName
//Create Azure resource group
createResourceGroup(clusterName, clusterRegion)
//Create AKS cluster
createCluster(clusterName, resourceGroupName)
//Get kubectl config copied to ~/.kube/getKubectlConfig
getKubectlConfig(clusterName, resourceGroupName)
//intialize helm
intializeHelm()
//Add helm repo inucubator
repoName := "incubator"
repoURL := "http://storage.googleapis.com/kubernetes-charts-incubator"
addHelmRepo(repoName, repoURL)
//Install ElasticSearch
installPackage("elasticsearch", repoName)
//Install Kafka
installPackage("kafka", repoName)
}