This repository was archived by the owner on Sep 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathk8spin.go
257 lines (230 loc) · 6.21 KB
/
k8spin.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"syscall"
"github.com/olekukonko/tablewriter"
"github.com/parnurzeal/gorequest"
"github.com/urfave/cli"
)
type httpError struct {
Error string `json:"error"`
}
type namespace struct {
Namespace string `json:"namespace"`
NamespaceName string `json:"namespace_name"`
IngressWhitelist []string `json:"ingress_whitelist"`
Expiration string `json:"expiration"`
ResourceQuota string `json:"resource_quotas"`
Status string `json:"status"`
}
var api_base string = "https://console.beta.k8spin.cloud/api"
var debug bool
var token string
var set_config bool
var cpu_amount string
var memory_amount string
var storage_amount string
func check(e error) {
if e != nil {
panic(e)
}
}
func main() {
app := cli.NewApp()
app.Name = "k8spin"
app.Version = "1.1.0"
app.Usage = "CLI for managing namespaces"
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "debug",
Usage: "show debug information",
Destination: &debug,
},
cli.StringFlag{
Name: "host",
Usage: "K8Spin host",
EnvVar: "K8SPINHOST",
Value: "https://api.k8spin.cloud",
Destination: &api_base,
},
cli.StringFlag{
Name: "token",
Usage: "K8Spin token",
EnvVar: "K8SPINTOKEN",
Destination: &token,
},
}
app.Commands = []cli.Command{
{
Name: "list",
Aliases: []string{"l"},
Usage: "list all namespaces",
Action: func(c *cli.Context) error {
request := gorequest.New()
var url = api_base+"/namespaces"
resp, body, _ := request.Get(url).
Set("Authorization", "Bearer "+token).
End()
if httpCodeCheck(resp) {
printNamespacesTable(body)
}
return nil
},
},
{
Name: "get_credentials",
Aliases: []string{"gc"},
Usage: "print namespace credentials to STDOUT",
ArgsUsage: "[name]",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "set_config",
Usage: "Saves and sets KUBECONFIG variable to a temporary file",
Destination: &set_config,
},
},
Action: func(c *cli.Context) error {
var namespace = c.Args().First()
if namespace == "" {
fmt.Println("You need to select a namespace")
return nil
}
var url = api_base+"/namespaces/"+namespace
request := gorequest.New()
resp, body, _ := request.Get(url).
Set("Authorization", "Bearer "+token).
End()
if httpCodeCheck(resp) {
fmt.Println(body)
}
return nil
},
},
{
Name: "set_credentials",
Aliases: []string{"sc"},
Usage: "set credentials in a temp KUBECONFIG",
ArgsUsage: "[name]",
Action: func(c *cli.Context) error {
var namespace = c.Args().First()
if namespace == "" {
fmt.Println("You need to select a namespace")
return nil
}
var url = api_base+"/namespaces/"+namespace
request := gorequest.New()
resp, body, _ := request.Get(url).
Set("Authorization", "Bearer "+token).
End()
if httpCodeCheck(resp) {
var filePath = "/tmp/k8spin_" + namespace
err := ioutil.WriteFile(filePath, []byte(body), 0644)
check(err)
os.Setenv("KUBECONFIG", filePath)
fmt.Println("Credentials saved at " + filePath)
fmt.Println("KUBECONFIG variable set")
syscall.Exec(os.Getenv("SHELL"), []string{os.Getenv("SHELL")}, syscall.Environ())
}
return nil
},
},
{
Name: "create",
Aliases: []string{"c"},
Usage: "create a namespace",
Flags: []cli.Flag{
cli.StringFlag{Name: "cpu", Value: "100", Destination: &cpu_amount},
cli.StringFlag{Name: "memory", Value: "128", Destination: &memory_amount},
cli.StringFlag{Name: "storage", Value: "0", Destination: &storage_amount},
},
Action: func(c *cli.Context) error {
var namespace = c.Args().First()
if namespace == "" {
fmt.Println("You have to set a namespace name")
return nil
}
var url = api_base+"/namespaces"
request := gorequest.New()
resp, _ , _ := request.Post(url).
Set("Authorization", "Bearer "+token).
Send(`{"namespace_name":"` + namespace + `", "resources": { "cpu":` + cpu_amount + `, "mem":` + memory_amount + `, "disks_size":` + storage_amount + `} }`).
End()
if httpCodeCheck(resp) {
fmt.Println("Namespace " + namespace + " created")
}
return nil
},
},
{
Name: "delete",
Aliases: []string{"d"},
Usage: "delete a namespace",
Action: func(c *cli.Context) error {
var namespace = c.Args().First()
if namespace == "" {
fmt.Println("You need to select a namespace")
return nil
}
var url = api_base+"/namespaces/"+namespace
request := gorequest.New()
resp, _ , _ := request.Delete(url).
Set("Authorization", "Bearer "+token).
End()
if httpCodeCheck(resp) {
fmt.Println("Namespace " + namespace + " deleted")
}
return nil
},
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
func httpCodeCheck(response gorequest.Response) bool {
if debug {
fmt.Println("HTTP URL:")
fmt.Println(response.Request.URL)
fmt.Println("HTTP Code:")
fmt.Println(response.StatusCode)
fmt.Println("HTTP Request Headers:")
fmt.Println(response.Request.Header)
fmt.Println("HTTP Request:")
buf := new(bytes.Buffer)
buf.ReadFrom(response.Request.Body)
fmt.Println(buf.String())
fmt.Println("HTTP Response Headers:")
fmt.Println(response.Header)
fmt.Println("HTTP Response:")
fmt.Println(response.Body)
}
buf := new(bytes.Buffer)
buf.ReadFrom(response.Body)
jsonResponse := buf.String()
error := httpError{}
json.Unmarshal([]byte(jsonResponse), &error)
if response.StatusCode == 500 {
fmt.Println("Unkown error")
return false
} else if response.StatusCode >= 300 {
fmt.Println(error.Error)
return false
}
return true
}
func printNamespacesTable(body string) {
namespaces := []namespace{}
json.Unmarshal([]byte(body), &namespaces)
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Name", "Namespace", "Expiration", "Status"})
for _, namespace := range namespaces {
table.Append([]string{namespace.NamespaceName, namespace.Namespace, namespace.Expiration, namespace.Status})
}
table.Render() // Send output
}