-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
288 lines (245 loc) · 7.83 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
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
package main
import (
"flag"
"fmt"
"math"
"os"
"time"
"dat067/costestimation/kubernetes"
"dat067/costestimation/kubernetes/azure"
"dat067/costestimation/models"
//"dat067/costestimation/models"
"dat067/costestimation/prometheus"
"net/http"
officialkube "k8s.io/client-go/kubernetes"
//http
"github.com/gin-gonic/gin"
//model shouldn't be needed after test printing functionality removed.
"github.com/prometheus/common/model"
)
var clientSet *officialkube.Clientset
var pricedNodes []kubernetes.PricedNode
type ResponseItem struct {
Price float64 `json:"price"`
DeploymentName string `json:"deployment"`
}
func main() {
address := flag.String("url", "http://localhost:9090", "Put the address here, dummy!")
flag.Parse()
router := gin.Default()
//router.GET("/price", getDeploymentPrices)
router.GET("/price/:deployment", getDeploymentPrices)
//endTime := time.Now()
//startTime := endTime.Add(-time.Hour)
var err error
fmt.Println("Before kubernetes clientSet")
clientSet, err = kubernetes.CreateClientSet()
fmt.Println("After kubernetes clientSet")
if err != nil {
fmt.Errorf("An error occured when creating the Kubernetes client: '%v'", err)
os.Exit(-1)
}
fmt.Println("Before getting price from Azure")
pricedNodes, err = azure.GetPricedAzureNodes(clientSet)
fmt.Println("After getting price from Azure")
if err != nil {
fmt.Errorf("An error occured while retrieving Azure node prices: '%v'", err)
os.Exit(-1)
}
fmt.Println("Before Prometheus API")
prometheus.CreateAPI(*address)
fmt.Println("After Prometheus API")
router.Run()
fmt.Println("After staring Gin router")
}
func getDeploymentPrices(c *gin.Context) {
wantedDeployment := c.Param("deployment")
// in postman URL: http://localhost:8080/price/coredns-autoscaler?startTime=2021-12-24T00:00:00.371Z&endTime=2021-12-25T00:00:00.371Z
endTimeStr := c.Query("endTime")
startTimeStr := c.Query("startTime")
resolutionStr := c.DefaultQuery("resolution", "None")
layout := "2006-01-02T15:04:05.000Z"
endTime, err := time.Parse(layout, endTimeStr)
if err != nil {
fmt.Print("ERROR")
fmt.Print(err.Error())
os.Exit(-1)
}
startTime, err := time.Parse(layout, startTimeStr)
if err != nil {
fmt.Print("ERROR")
fmt.Print(err.Error())
os.Exit(-1)
}
var pricedMap map[string]float64
//Abomination below
if resolutionStr != "None" {
resolution, err := time.ParseDuration(resolutionStr)
if err != nil {
fmt.Print("ERROR")
fmt.Print(err.Error())
os.Exit(-1)
}
pricedMap, err = getDeploymentPrice(startTime, endTime, resolution)
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
}
} else {
pricedMap, err = getDeploymentPriceOverPeriod(startTime, endTime)
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
}
}
priceArray := make([]ResponseItem, len(pricedMap))
index := 0
for deployment, price := range pricedMap {
priceInfoStruct := ResponseItem{
Price: price,
DeploymentName: deployment,
}
priceArray[index] = priceInfoStruct
index++
}
for i, s := range priceArray {
if s.DeploymentName == wantedDeployment {
c.JSON(http.StatusOK, priceArray[i])
return
}
//couldn't find deployment-name add message
}
c.JSON(http.StatusNotFound, "deployment not found")
}
func getDeploymentPriceOverPeriod(startTime time.Time, endTime time.Time) (map[string]float64, error) {
return getDeploymentPrice(startTime, endTime, endTime.Sub(startTime))
}
func getDeploymentPrice(startTime time.Time, endTime time.Time, resolution time.Duration) (map[string]float64, error) {
duration := endTime.Sub(startTime)
podPrices := make(map[string]float64)
for _, node := range pricedNodes {
podsResourceUsages, warnings, err := prometheus.GetAvgPodResourceUsageOverTime(node.Node.Name, startTime, endTime, resolution)
if warnings != nil {
fmt.Println(warnings)
}
if err != nil {
fmt.Println(err)
os.Exit(-1)
}
for _, podsResourceUsage := range podsResourceUsages {
tmap := getPrice(podsResourceUsage, node, resolution)
for k, v := range tmap {
podPrices[k] += v
}
}
}
return groupPodPricesToDeployment(podPrices, endTime, duration), nil
}
func groupPodPricesToDeployment(podPrices map[string]float64, endTime time.Time, duration time.Duration) map[string]float64 {
deploymentMap := prometheus.GetPodsToDeployment(endTime, duration)
priceMap := make(map[string]float64)
for pod, deployment := range deploymentMap {
price, ok := podPrices[pod]
if !ok {
fmt.Println("NOT OK!")
fmt.Printf("The pod '%s' does not exist in podPrices\n", pod)
continue
}
priceMap[deployment] += price
}
//Print all the deployment costs.
for d, p := range priceMap {
fmt.Printf("%s has a cost of %f \n", d, p)
}
fmt.Printf("\nNode prices: \n")
sumNode := 0.0
for _, node := range pricedNodes {
fmt.Printf("Node %s costs %f.\n", node.Node.Name, duration.Hours()*node.Price)
sumNode += duration.Hours() * node.Price
}
sumPrice := 0.0
for _, v := range podPrices {
sumPrice += v
}
sumPriceMap := 0.0
for _, v := range priceMap {
sumPriceMap += v
}
fmt.Printf("Cost of nodes was %f. Total cost of pods was %f. \nThe pods being used in deployments amount to %f. \n", sumNode, sumPrice, sumPriceMap)
return priceMap
}
func getPrice(podsResourceUsage prometheus.ResourceUsageSample, node kubernetes.PricedNode, resolution time.Duration) map[string]float64 {
podPrices := make(map[string]float64)
pods := podsResourceUsage.ResourceUsages
t := podsResourceUsage.Time
monster := make([][]float64, len(pods))
index := 0
cpuUsage := 0.0
memUsage := 0.0
orderOfNames := make([]string, len(pods))
for name, resourceUsage := range pods {
orderOfNames[index] = name
monster[index] = []float64{resourceUsage.MemUsage, resourceUsage.CpuUsage}
cpuUsage += resourceUsage.CpuUsage
memUsage += resourceUsage.MemUsage
index += 1
}
//TODO: We get all the pods on a node, even those not belonging to a deployment.
//Calculate pods' cost
nodeMem, _, _ := prometheus.GetMemoryNodeCapacity(node.Node.Name, t)
nodeCPU, _, _ := prometheus.GetCPUNodeCapacity(node.Node.Name, t)
costCalculator := models.GoodModel{Balance: []float64{1, 1}}
price, wastedCost := costCalculator.CalculateCost(
[]float64{
nodeMem,
nodeCPU},
monster,
node.Price, resolution.Hours())
index = 0
totalPodPrice := 0.0
for _, pod := range orderOfNames {
if wastedCost[index] < 0 {
fmt.Printf("The wasted cost for pod %s is %f\n", pod, wastedCost[index])
}
totalPodPrice += price[index]
podPrices[pod] += price[index]
index += 1
}
if cpuUsage > nodeCPU {
fmt.Printf("Cpu usage too high for pods on node %s\n", node.Node.Name)
}
if memUsage > nodeMem {
fmt.Printf("Mem usage too high for pods on node %s\n", node.Node.Name)
}
if math.Abs(totalPodPrice-resolution.Hours()*node.Price) > 1e-10 {
fmt.Printf("The sum of the pod prices is %f. The node price is %f\n", totalPodPrice, resolution.Hours()*node.Price)
}
return podPrices
}
func printVector(v model.Vector) {
for _, sample := range v {
labelSet := model.LabelSet(sample.Metric)
metricName := labelSet[model.MetricNameLabel]
if metricName != "" {
fmt.Printf("Metric name: %s, time stamp: %s, value: %v\n", metricName, sample.Timestamp.Time(), sample.Value)
}
for key, value := range labelSet {
if key != model.MetricNameLabel {
fmt.Printf("\tLabel name: %s, value: %s\n", key, value)
}
}
fmt.Printf("\n")
}
}
func printMatrix(m model.Matrix) {
for _, sampleStream := range m {
fmt.Printf("Metric: %v\n", (*sampleStream).Metric.String())
for _, samplePair := range (*sampleStream).Values {
fmt.Printf("\tTime stamp; %v, value; %v\n", samplePair.Timestamp.Time(), samplePair.Value)
}
}
}
func printScalar(s model.Scalar) {
fmt.Printf("Time stamp: %v, value: %v\n", s.Timestamp, s.Value.String())
}
func printString(s model.String) {
fmt.Printf("Time stamp: %v, value: %v\n", s.Timestamp, s.Value)
}