/
main.go
114 lines (95 loc) · 2.9 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
package main
import (
"context"
"flag"
"fmt"
"sort"
"strings"
"time"
calc "github.com/matteogazzadi/kuberes/pkg/calculator"
domain "github.com/matteogazzadi/kuberes/pkg/domain"
helper "github.com/matteogazzadi/kuberes/pkg/helpers"
"k8s.io/client-go/kubernetes"
ctrl "sigs.k8s.io/controller-runtime"
)
// Arguments Parameter
var groupByNamespace bool
var outputAsTable bool
var outputAsCsv bool
var outputAsXlsx bool
var outputFilePath string
var excludeNamespace []string
var matchNamespace string
// Init Function - Arguments parsing
func init() {
var outputFormat string
var excludeNs string
flag.BoolVar(&groupByNamespace, "group-by-ns", true, "Should group statistics by namespace ?")
flag.StringVar(&outputFormat, "output", "table", "Output type. Valid values are: table,csv,xlsx")
flag.StringVar(&outputFilePath, "file-path", "", "Full Path to the .CSV/.XLSX File to produce")
flag.StringVar(&excludeNs, "exclude-ns", "", "Namespaces Names to be excluded separated by ,")
flag.StringVar(&matchNamespace, "match-ns-regex", "", "Namespaces Names to be matched on the given RegEx")
flag.Parse()
// Check if output parameter is valid
switch strings.ToLower(outputFormat) {
case "table":
outputAsTable = true
outputAsCsv = false
outputAsXlsx = false
case "csv":
outputAsTable = false
outputAsCsv = true
outputAsXlsx = false
case "xlsx":
outputAsTable = false
outputAsCsv = false
outputAsXlsx = true
default:
panic("Unrecognized 'output' value '" + outputFormat + "'")
}
// Check if output file Path is valid (only if output is CSV or XLSX)
if (outputAsCsv || outputAsXlsx) && outputFilePath == "" {
panic("Output file path is not set")
}
if excludeNs != "" {
excludeNamespace = strings.Split(excludeNs, ",")
}
}
// Main function - Application Entry Point
func main() {
startTime := time.Now().UTC()
// 1. Initialize Kubernets Clients
ctx := context.Background()
config := ctrl.GetConfigOrDie()
clientset := kubernetes.NewForConfigOrDie(config)
// Get All Pods in cluster
pods, err := helper.GetAllPods(&excludeNamespace, matchNamespace, clientset, ctx)
if err != nil {
panic(err)
}
var resources []domain.K8sStats
// Calculate Resources
calc.CalculateResources(groupByNamespace, &pods, &resources)
// Sort Resources by Namespace
sort.Slice(resources, func(i, j int) bool {
return resources[i].Namespace < resources[j].Namespace
})
// =============== //
// Generate Output //
// =============== //
if outputAsTable {
// Table Output
helper.WriteOutputAsTable(&resources, groupByNamespace)
} else {
if outputAsCsv && outputFilePath != "" {
helper.WriteOutputAsCsv(&resources, groupByNamespace, outputFilePath)
} else {
if outputAsXlsx && outputFilePath != "" {
helper.WriteOutputAsXlsx(&resources, groupByNamespace, outputFilePath)
}
}
}
// Report Elapsed Time
fmt.Println()
fmt.Println("Elapsed: ", time.Since(startTime).Milliseconds(), "ms")
}