/
clusters_info.go
389 lines (330 loc) · 9.69 KB
/
clusters_info.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package kluster
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/url"
"os"
"path/filepath"
"strings"
"text/tabwriter"
"text/template"
toml "github.com/pelletier/go-toml"
"github.com/liferaft/kubekit/version"
yaml "gopkg.in/yaml.v2"
)
const tableTemplateKeyword = "table"
var validClusterInfoFieldsToFilterBy = []string{"name", "nodes", "platform", "status", "version", "path", "url", "entrypoint", "kubeconfig"}
// ClusterInfo basic cluster information
type ClusterInfo struct {
Name string `json:"name" yaml:"name" toml:"name"`
Nodes int `json:"nodes" yaml:"nodes" toml:"nodes"`
Platform string `json:"platform" yaml:"platform" toml:"platform"`
Status string `json:"status" yaml:"status" toml:"status"`
Version string `json:"version" yaml:"version" toml:"version"`
Path string `json:"path" yaml:"path" toml:"path"`
URL string `json:"url" yaml:"url" toml:"url"`
Kubeconfig string `json:"kubeconfig" yaml:"kubeconfig" toml:"kubeconfig"`
}
func isValidFilterParam(param string) bool {
for _, p := range validClusterInfoFieldsToFilterBy {
if p == strings.ToLower(param) {
return true
}
}
return false
}
// IsValidFilter returns true if one of the filter parameters for clusters info
// is not valid
func IsValidFilter(params map[string]string) bool {
for p := range params {
if !isValidFilterParam(p) {
return false
}
}
return true
}
// InvalidFilterParams return a list of invalid filter parameters for clusters info
func InvalidFilterParams(params map[string]string) []string {
invalid := []string{}
for p := range params {
if !isValidFilterParam(p) {
invalid = append(invalid, p)
}
}
return invalid
}
// ClustersInfo list of clusters with its information
type ClustersInfo []ClusterInfo
// JSON returns the cluster information in JSON format
func (ci ClustersInfo) JSON(pp bool) (string, error) {
var output []byte
var err error
if pp {
output, err = json.MarshalIndent(ci, "", " ")
} else {
output, err = json.Marshal(ci)
}
return string(output), err
}
// YAML returns the cluster information in YAML format
func (ci ClustersInfo) YAML() (string, error) {
output, err := yaml.Marshal(ci)
return string(output), err
}
// TOML returns the cluster information in TOML format
func (ci ClustersInfo) TOML() (string, error) {
var tomlStruct struct {
Clusters map[string]ClusterInfo `toml:"clusters"`
}
var ciMap map[string]ClusterInfo
ciMap = make(map[string]ClusterInfo, len(ci))
for _, ki := range ci {
ciMap[ki.Name] = ki
}
tomlStruct.Clusters = ciMap
output, err := toml.Marshal(tomlStruct)
return string(output), err
}
// Table returns the cluster information in a plain text table
func (ci ClustersInfo) Table(wide bool) string {
var tableTmpl bytes.Buffer
header := "Name\tNodes\tPlatform\tStatus\tVersion"
if wide {
header = header + "\tEntrypoint\tKubeconfig"
}
tableTmpl.WriteString(header + "\n")
for _, k := range ci {
ver, _ := version.NewSemVer(k.Version)
// ignore error, the error was validated when the cluster was loaded with LoadSummary()
var supported string
if ver.LT(MinSemVersion) {
supported = fmt.Sprintf(" (<%s)", MinSemVersion)
}
if ver.GT(SemVersion) {
supported = fmt.Sprintf(" (>%s)", SemVersion)
}
row := fmt.Sprintf("%s\t%d\t%s\t%s\t%s", k.Name, k.Nodes, k.Platform, k.Status, k.Version+supported)
if wide {
kubeconfigpath := "None"
if _, err := os.Stat(k.Kubeconfig); err == nil {
kubeconfigpath = k.Kubeconfig
}
row = fmt.Sprintf("%s\t%s\t%s", row, k.URL, kubeconfigpath)
}
tableTmpl.WriteString(row + "\n")
}
return tabletize(tableTmpl.String())
}
// Names returns a list of clusters name
func (ci ClustersInfo) Names() []string {
names := []string{}
for _, k := range ci {
names = append(names, k.Name)
}
return names
}
// Stringf returns the clusters info in the requested format to be printed
func (ci ClustersInfo) Stringf(format string, ppArr ...bool) (result string, err error) {
switch format {
case "", "wide", "w":
result = ci.Table((format == "wide") || (format == "w"))
case "quiet", "q", "names":
names := ci.Names()
result = strings.Join(names, "\n")
case "json":
var pp bool
if len(ppArr) != 0 {
pp = ppArr[0]
}
result, err = ci.JSON(pp)
case "yaml":
result, err = ci.YAML()
case "toml":
result, err = ci.TOML()
default:
err = fmt.Errorf("unknown format %q", format)
}
return result, err
}
// func (ci ClustersInfo) String() string {
// result, err := ci.Stringf("")
// if err != nil {
// return err.Error()
// }
// return result
// }
func header(format string) (string, error) {
format = strings.Replace(format, "{{.Nodes}}", "NODES", 1)
tmpl, err := template.New("Clusters Information Header").Parse(format)
if err != nil {
return "", err
}
ci := ClusterInfo{
Name: "NAME",
Platform: "PLATFORM",
Status: "STATUS",
Version: "VERSION",
Path: "PATH",
URL: "ENTRYPOINT",
Kubeconfig: "KUBECONFIG",
}
var rendered bytes.Buffer
if err := tmpl.Execute(&rendered, ci); err != nil {
return "", err
}
return rendered.String() + "\n", nil
}
func tabletize(text string) string {
var output bytes.Buffer
w := tabwriter.NewWriter(&output, 0, 0, 3, ' ', 0)
fmt.Fprint(w, text)
w.Flush()
return output.String()
}
// Template renders the clusters information from a given Go template
func (ci ClustersInfo) Template(format string) (string, error) {
if len(ci) == 0 {
return "", nil
}
// Transformations to template
format = strings.Replace(format, "\\t", "\t", -1)
format = strings.Replace(format, "\\n", "\n", -1)
format = strings.Replace(format, ".Entrypoint", ".URL", -1)
var head string
if strings.Contains(format, tableTemplateKeyword) {
var err error
// More transformations
format = strings.Replace(format, tableTemplateKeyword, "", 1)
format = strings.TrimSpace(format)
if head, err = header(format); err != nil {
return "", err
}
}
// If it's not empty and does not have a final newline, add it
if len(format) != 0 && format[len(format)-1:] != "\n" {
format = format + "\n"
}
// Add the range to loop thru all the cluster information
tmplFormat := "{{range .}}" + format + "{{end}}"
tmpl, err := template.New("Clusters Information").Parse(tmplFormat)
if err != nil {
return "", err
}
var rendered bytes.Buffer
if err := tmpl.Execute(&rendered, ci); err != nil {
return "", err
}
output := rendered.String()
if len(head) != 0 {
output = tabletize(head + output)
}
return output, nil
}
// FilterBy filters the clusters information by the given parameters
func (ci *ClustersInfo) FilterBy(params map[string]string) {
if len(params) == 0 {
return
}
newCi := ClustersInfo{}
for _, i := range *ci {
if i.ContainsAll(params) {
newCi = append(newCi, i)
}
}
*ci = newCi
}
// ContainsAll returns true if the cluster information contains all the given
// parameters. The paramters is a map of key/value pairs, where the keys are the
// fields of the cluster information named as the JSON value
func (i ClusterInfo) ContainsAll(params map[string]string) bool {
if len(params) == 0 {
// This is always true and avoid the Marshaling/unmarshaling of `i`
return true
}
infoJSON, err := json.Marshal(&i)
if err != nil {
return false
}
infoMap := map[string]interface{}{}
if err := json.Unmarshal(infoJSON, &infoMap); err != nil {
return false
}
for kp, vp := range params {
kp := strings.ToLower(kp)
if kp == "entrypoint" {
kp = "url"
if vp == "" {
vp = "None"
}
}
if v, found := infoMap[kp]; !found || vp != fmt.Sprintf("%v", v) {
return false
}
}
return true
}
// GetClustersInfo gets the list of clusters and its basic information. If
// clustersName is empty will return the information for all the existing clusters
func GetClustersInfo(baseDir string, params map[string]string, clustersName ...string) (ClustersInfo, error) {
var ci ClustersInfo
list, err := List(baseDir, clustersName...)
if err != nil {
return nil, err
}
ci = make(ClustersInfo, 0)
for _, k := range list {
platformName := k.Platform()
if platformName == "" || k.Name == "" {
// There is a cluster created incorrectly or corrupt.
// Do not return an error, would be nice to report it but not to stop the collection of the other clusters
continue
}
i := ClusterInfo{
Name: k.Name,
Platform: platformName,
Status: AbsentStatus.String(),
Path: filepath.Dir(k.Path()),
Version: k.Version,
URL: "None",
}
if k.State[platformName] != nil {
i.Status = k.State[platformName].Status
i.Kubeconfig = filepath.Join(filepath.Dir(k.Path()), "certificates", "kubeconfig")
i.Nodes = len(k.State[platformName].Nodes)
if entrypoint := k.GetEntrypoint(); len(entrypoint) != 0 {
i.URL = entrypoint
}
}
ci = append(ci, i)
}
ci.FilterBy(params)
return ci, nil
}
// GetEntrypoint returns the Kubernetes entrypoint or empty string if doesn't
// exists or invalid/malformed
func (k *Kluster) GetEntrypoint() string {
platform := k.Platform()
if len(k.State[platform].Address) == 0 {
return ""
}
scheme := "https://"
if strings.HasPrefix(k.State[platform].Address, scheme) || strings.HasPrefix(k.State[platform].Address, "http://") {
scheme = ""
}
port := fmt.Sprintf(":%d", k.State[platform].Port)
if k.State[platform].Port == 0 || strings.HasSuffix(k.State[platform].Address, port) {
port = ""
}
if entrypoint, err := url.Parse(scheme + k.State[platform].Address + port); err == nil {
return entrypoint.String()
}
return ""
}
// GetKubeconfig returns the content of the KubeConfig file
func (k *Kluster) GetKubeconfig() (string, error) {
kubeconfigFile := filepath.Join(filepath.Dir(k.Path()), "certificates", "kubeconfig")
data, err := ioutil.ReadFile(kubeconfigFile)
return string(data), err
}