-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.go
173 lines (135 loc) · 4.65 KB
/
metrics.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
/* metrics pkg is to get the resource metrics usage of pods and nodes */
package metrics
import (
"context"
"encoding/json"
"strconv"
//"log"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/kubernetes"
metricsStruct "github.com/ayush5588/ClusterAutoscaler/metricsStruct"
)
type PodUsage struct {
PodName string
PodCpuUsage int
PodMemUsage int
}
type NodeUsage struct {
NodeName string
NodeCpuUsage int
NodeMemUsage int
}
/* Gets the Pod metrics from the specified API */
func getPodMetrics(clientset *kubernetes.Clientset, pods *metricsStruct.PodMetricsStruct, apiPath string) error {
data, err:= clientset.RESTClient().Get().AbsPath(apiPath).Do(context.TODO()).Raw()
if err != nil {
return err
}
err = json.Unmarshal(data, &pods)
return err
}
// Gets the Node metrics from the specified API
func getNodeMetrics(clientset *kubernetes.Clientset, nodes *metricsStruct.NodeMetricsStruct, apiPath string) error {
data, err := clientset.RESTClient().Get().AbsPath(apiPath).Do(context.TODO()).Raw()
if err != nil {
return err
}
err = json.Unmarshal(data, &nodes)
return err
}
// kubeConfig file path
//var kubeconfig string = "/home/ayush5588/go/src/github.com/ClusterAutoscaler/realKubeConfig.conf"
/*
1. Creates a config object from the kubeconfig file provided
2. Creates a clientset (groups of client) from the config object
3. Calls the appropriate function to get the metrics from the given API
4. Creates a slice of struct elements having resource usage and returns it back
*/
func GetNodeMetrics(kubeConfig string) ([]NodeUsage, error) {
var NodeMetricsArr []NodeUsage
config, err := clientcmd.BuildConfigFromFlags("",kubeConfig)
if err != nil {
return NodeMetricsArr, err
//log.Fatal(err)
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return NodeMetricsArr, err
//log.Fatal(err)
}
var nodes metricsStruct.NodeMetricsStruct
err = getNodeMetrics(clientset, &nodes, "apis/metrics.k8s.io/v1beta1/nodes")
for _, m := range nodes.Items {
var NodeMetrics NodeUsage
NodeMetrics.NodeName = m.Metadata.Name
fmtNodeCPU := m.Usage.CPU[:len(m.Usage.CPU)-1]
if len(fmtNodeCPU) > 0 {
ci, err := strconv.Atoi(fmtNodeCPU)
if err != nil {
return NodeMetricsArr, err
}
NodeMetrics.NodeCpuUsage = ci
}
fmtNodeMem := m.Usage.Memory[:len(m.Usage.Memory)-2]
if len(fmtNodeMem) > 0 {
cm, err := strconv.Atoi(fmtNodeMem)
if err != nil {
return NodeMetricsArr, err
}
NodeMetrics.NodeMemUsage = cm
}
NodeMetricsArr = append(NodeMetricsArr,NodeMetrics)
}
return NodeMetricsArr, nil
}
func GetPodMetrics(kubeConfig string) ([]PodUsage, error) {
var PodMetricsArr []PodUsage
config, err := clientcmd.BuildConfigFromFlags("",kubeConfig)
if err != nil {
//panic(err.Error())
return PodMetricsArr, nil
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
//panic(err.Error())
return PodMetricsArr, nil
}
var pods metricsStruct.PodMetricsStruct
err = getPodMetrics(clientset, &pods, "apis/metrics.k8s.io/v1beta1/pods")
if err != nil {
return PodMetricsArr, err
}
//var PodMetricsArr []PodUsage
for _, m := range pods.Items {
var PodMetrics PodUsage
PodMetrics.PodName = m.Metadata.Name
var cUsage int = 0
var mUsage int = 0
/* Goes through all the containers in the pod to gather the metrics usage */
for _,c := range m.Containers {
/* removes the last character (or last 2 in case of memory) which denotes the unit of
measurement (such as n in 34334n i.e. CPU usage) of the usage in order to sum it up
*/
fmtPodCPU := c.Usage.CPU[:len(c.Usage.CPU)-1]
if len(fmtPodCPU) > 0 {
ci, err := strconv.Atoi(fmtPodCPU)
if err != nil {
return PodMetricsArr, err
}
cUsage = cUsage + ci
}
fmtPodMem := c.Usage.Memory[:len(c.Usage.Memory)-2]
if len(fmtPodMem) > 0 {
mi, err := strconv.Atoi(fmtPodMem)
if err != nil {
return PodMetricsArr, err
}
mUsage = mUsage + mi
}
}
PodMetrics.PodCpuUsage = cUsage
PodMetrics.PodMemUsage = mUsage
PodMetricsArr = append(PodMetricsArr,PodMetrics)
}
return PodMetricsArr,nil
}