-
Notifications
You must be signed in to change notification settings - Fork 0
/
services.go
81 lines (69 loc) · 2.82 KB
/
services.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
package main
import (
"time"
"github.com/360EntSecGroup-Skylar/excelize"
"github.com/tidwall/gjson"
)
func getSvcPorts(result gjson.Result) string {
var ports string
result.ForEach(func(key, value gjson.Result) bool {
vars := gjson.GetMany(value.String(), `protocol`, `port`)
ports = ports + vars[0].String() + "/" + vars[1].String() + " "
return true
})
return ports
}
func createServiceSheet() {
var csvHeader = []string{"Cluster", "Namespace", "Name", "Type", "ClusterIP", "ExternalIP", "Ports", "Affinity", "CreationDate", "Version", "UID"}
var csvData []string
var startTime time.Time
var duration time.Duration
var xR int = 1
var sheetName string = "Services"
var apiurl string
// Initialize Excel sheet
index := xf.NewSheet(sheetName)
xf.SetActiveSheet(index)
xf.SetSheetRow(sheetName, "A1", &csvHeader)
info.Printf("%s: Section started\n", sheetName)
startTime = time.Now()
for i := 0; i < len(cfg.Clusters); i++ {
if !cfg.Clusters[i].Enable {
continue
}
boolCheck, msg := checkClusterAPI(cfg.Clusters[i].BaseURL, cfg.Clusters[i].Token)
if !boolCheck {
erro.Printf("%s: %s: %s\n", sheetName, cfg.Clusters[i].Name, msg)
continue
}
info.Printf("%s: Working on %s\n", sheetName, cfg.Clusters[i].Name)
apiurl = cfg.Clusters[i].BaseURL + "/api/v1/services?limit=1000"
body, _ := getRest(apiurl, cfg.Clusters[i].Token)
// Loop in list items and export data into Excel file
items := gjson.GetBytes(body, "items")
items.ForEach(func(key, value gjson.Result) bool {
vars := gjson.GetMany(value.String(),
`metadata.namespace`, `metadata.name`, `spec.type`, `spec.clusterIP`, `spec.externalIPs.0`,
`spec.ports`, `spec.sessionAffinity`, `metadata.creationTimestamp`, `metadata.resourceVersion`, `metadata.uid`)
csvData = nil
csvData = append(csvData, cfg.Clusters[i].Name) // Cluster Name
csvData = append(csvData, vars[0].String()) // Namespace Name
csvData = append(csvData, vars[1].String()) // Service Name
csvData = append(csvData, vars[2].String()) // Type
csvData = append(csvData, vars[3].String()) // ClusterIP
csvData = append(csvData, vars[4].String()) // ExternalIP
csvData = append(csvData, getSvcPorts(vars[5])) // Ports
csvData = append(csvData, vars[6].String()) // Session Affinity
csvData = append(csvData, formatDate(vars[7].String())) // Creation Timestamp
csvData = append(csvData, vars[8].String()) // Resource Version
csvData = append(csvData, vars[9].String()) // UID
xR++
cell, _ := excelize.CoordinatesToCellName(1, xR)
xf.SetSheetRow(sheetName, cell, &csvData)
return true
})
}
formatTable(sheetName, len(csvHeader))
duration = time.Since(startTime)
info.Printf("%s: Section ended in %s\n", sheetName, duration)
}