-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
941 lines (806 loc) · 29.2 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
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
package main
import (
"bytes"
"compress/gzip"
"crypto/md5"
"encoding/csv"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
"github.com/caarlos0/env/v11"
)
type (
KubecostAllocationResponse struct {
Code int64 `json:"code"`
Message string `json:"message"`
Data []map[string]KubecostAllocation `json:"data"`
}
KubecostAllocation struct {
Name string `json:"name"`
Properties Properties `json:"properties"`
Window Window `json:"window"`
Start string `json:"start"`
End string `json:"end"`
Minutes float64 `json:"minutes"`
CPUCores float64 `json:"cpuCores"`
CPUCoreRequestAverage float64 `json:"cpuCoreRequestAverage"`
CPUCoreUsageAverage float64 `json:"cpuCoreUsageAverage"`
CPUCoreHours float64 `json:"cpuCoreHours"`
CPUCost float64 `json:"cpuCost"`
CPUCostAdjustment float64 `json:"cpuCostAdjustment"`
CPUEfficiency float64 `json:"cpuEfficiency"`
GPUCount float64 `json:"gpuCount"`
GPUHours float64 `json:"gpuHours"`
GPUCost float64 `json:"gpuCost"`
GPUCostAdjustment float64 `json:"gpuCostAdjustment"`
NetworkTransferBytes float64 `json:"networkTransferBytes"`
NetworkReceiveBytes float64 `json:"networkReceiveBytes"`
NetworkCost float64 `json:"networkCost"`
NetworkCrossZoneCost float64 `json:"networkCrossZoneCost"`
NetworkCrossRegionCost float64 `json:"networkCrossRegionCost"`
NetworkInternetCost float64 `json:"networkInternetCost"`
NetworkCostAdjustment float64 `json:"networkCostAdjustment"`
LoadBalancerCost float64 `json:"loadBalancerCost"`
LoadBalancerCostAdjustment float64 `json:"loadBalancerCostAdjustment"`
PVBytes float64 `json:"pvBytes"`
PVByteHours float64 `json:"pvByteHours"`
PVCost float64 `json:"pvCost"`
PVs map[string]PV `json:"pvs"`
PVCostAdjustment float64 `json:"pvCostAdjustment"`
RAMBytes float64 `json:"ramBytes"`
RAMByteRequestAverage float64 `json:"ramByteRequestAverage"`
RAMByteUsageAverage float64 `json:"ramByteUsageAverage"`
RAMByteHours float64 `json:"ramByteHours"`
RAMCost float64 `json:"ramCost"`
RAMCostAdjustment float64 `json:"ramCostAdjustment"`
RAMEfficiency float64 `json:"ramEfficiency"`
SharedCost float64 `json:"sharedCost"`
ExternalCost float64 `json:"externalCost"`
TotalCost float64 `json:"totalCost"`
TotalEfficiency float64 `json:"totalEfficiency"`
RawAllocationOnly interface{} `json:"rawAllocationOnly"`
}
PV struct {
ByteHours float64 `json:"byteHours"`
Cost float64 `json:"cost"`
}
Properties struct {
Cluster string `json:"cluster"`
Container string `json:"container"`
Namespace string `json:"namespace"`
Pod string `json:"pod"`
Node string `json:"node"`
Controller string `json:"controller"`
ControllerKind string `json:"controllerKind"`
Services []string `json:"services"`
ProviderID string `json:"providerID"`
Labels map[string]string `json:"labels"`
NamespaceLabels map[string]string `json:"namespaceLabels"`
}
Window struct {
Start string `json:"start"`
End string `json:"end"`
}
KubecostConfig struct {
Data struct {
CurrencyCode string `json:"currencyCode"`
}
}
OptimaFileUploadResponse struct {
ID string `json:"id"`
Status string `json:"status"`
BillUploadID string `json:"billUploadId"`
MD5 string `json:"md5"`
}
Config struct {
RefreshToken string `env:"REFRESH_TOKEN"`
ServiceClientId string `env:"SERVICE_APP_CLIENT_ID"`
ServiceClientSecret string `env:"SERVICE_APP_CLIENT_SECRET"`
OrgID string `env:"ORG_ID"`
BillConnectID string `env:"BILL_CONNECT_ID"`
Shard string `env:"SHARD" envDefault:"NAM"`
KubecostHost string `env:"KUBECOST_HOST" envDefault:"localhost:9090"`
KubecostAPIPath string `env:"KUBECOST_API_PATH" envDefault:"/model/"`
KubecostConfigHost string `env:"KUBECOST_CONFIG_HOST"`
KubecostConfigAPIPath string `env:"KUBECOST_CONFIG_API_PATH"`
Aggregation string `env:"AGGREGATION" envDefault:"pod"`
ShareNamespaces string `env:"SHARE_NAMESPACES" envDefault:"kube-system,cadvisor"`
Idle bool `env:"IDLE" envDefault:"true"`
IdleByNode bool `env:"IDLE_BY_NODE" envDefault:"false"`
ShareIdle bool `env:"SHARE_IDLE" envDefault:"false"`
ShareTenancyCosts bool `env:"SHARE_TENANCY_COSTS" envDefault:"true"`
Multiplier float64 `env:"MULTIPLIER" envDefault:"1.0"`
FileRotation bool `env:"FILE_ROTATION" envDefault:"true"`
FilePath string `env:"FILE_PATH" envDefault:"/var/kubecost"`
IncludePreviousMonth bool `env:"INCLUDE_PREVIOUS_MONTH" envDefault:"true"`
RequestTimeout int `env:"REQUEST_TIMEOUT" envDefault:"5"`
MaxFileRows int `env:"MAX_FILE_ROWS" envDefault:"1000000"`
CreateBillConnectIfNotExist bool `env:"CREATE_BILL_CONNECT_IF_NOT_EXIST" envDefault:"false"`
VendorName string `env:"VENDOR_NAME" envDefault:"Kubecost"`
PageSize int `env:"PAGE_SIZE" envDefault:"500"`
DefaultCurrency string `env:"DEFAULT_CURRENCY" envDefault:"USD"`
}
App struct {
Config
aggregation string
filesToUpload map[string]map[string]struct{}
client *http.Client
lastInvoiceDate time.Time
invoiceMonths []string
mandatoryFileSavingPeriodStartDate time.Time
billUploadURL string
}
)
var uuidPattern = regexp.MustCompile(`an existing billUpload \(ID: ([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})`)
var fileNameRe = regexp.MustCompile(`kubecost-(\d{4}-\d{2}-\d{2})(?:-(\d+))?\.csv(\.gz)?`)
func main() {
exporter := newApp()
exporter.updateFileList()
exporter.updateFromKubecost()
exporter.uploadToFlexera()
}
func (a *App) updateFromKubecost() {
now := time.Now().Local()
now = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
err := os.MkdirAll(a.FilePath, os.ModePerm)
if err != nil {
log.Fatal(err)
}
currency := a.getCurrency()
for d := range dateIter(now.AddDate(0, -(len(a.invoiceMonths)), 0)) {
if d.After(now) || !a.dateInInvoiceRange(d) {
continue
}
tomorrow := d.AddDate(0, 0, 1)
requestNewPage := true
limit := a.PageSize
page := 0
allCostRecords := make([]KubecostAllocation, 0)
idleCostRecords := make(map[string]KubecostAllocation)
for requestNewPage {
// https://github.com/kubecost/docs/blob/master/allocation.md#querying
reqUrl := fmt.Sprintf("http://%s%sallocation", a.KubecostHost, a.KubecostAPIPath)
req, err := http.NewRequest("GET", reqUrl, nil)
if err != nil {
log.Fatal(err)
}
q := req.URL.Query()
q.Add("window", fmt.Sprintf("%s,%s", d.Format("2006-01-02T15:04:05Z"), tomorrow.Format("2006-01-02T15:04:05Z")))
q.Add("aggregate", a.aggregation)
q.Add("idle", fmt.Sprintf("%t", a.Idle))
q.Add("includeIdle", fmt.Sprintf("%t", a.Idle))
q.Add("idleByNode", fmt.Sprintf("%t", a.IdleByNode))
q.Add("shareIdle", fmt.Sprintf("%t", a.ShareIdle))
q.Add("shareNamespaces", a.ShareNamespaces)
q.Add("shareSplit", "weighted")
q.Add("shareTenancyCosts", fmt.Sprintf("%t", a.ShareTenancyCosts))
q.Add("step", "1d")
q.Add("accumulate", "true")
q.Add("offset", fmt.Sprintf("%d", page*limit))
q.Add("limit", fmt.Sprintf("%d", limit))
req.URL.RawQuery = q.Encode()
log.Printf("Request: %+v?%s\n", reqUrl, q.Encode())
resp, err := a.client.Do(req)
if err != nil {
log.Fatal(err)
}
log.Printf("Response Status Code: %+v\n", resp.StatusCode)
var j KubecostAllocationResponse
err = json.NewDecoder(resp.Body).Decode(&j)
if err != nil {
log.Fatal(err)
}
resp.Body.Close()
if j.Code != http.StatusOK {
log.Println("Kubecost API response code different than 200, skipping")
continue
}
for _, allocation := range j.Data {
for id, record := range allocation {
if a.isIdleRecord(record) {
idleCostRecords[id] = record
} else {
allCostRecords = append(allCostRecords, record)
}
}
totalRecords := len(allocation)
if a.ShareIdle && totalRecords < a.PageSize || !a.ShareIdle && totalRecords < a.PageSize+1 {
requestNewPage = false
}
}
page++
}
for _, idleRecord := range idleCostRecords {
allCostRecords = append(allCostRecords, idleRecord)
}
monthOfData := d.Format("2006-01")
var csvFile = fmt.Sprintf(path.Join(a.FilePath, "kubecost-%v.csv.gz"), d.Format("2006-01-02"))
// If the data obtained is empty, skip the iteration, because it might overwrite a previously obtained file for the same range time
dataExist := false
if len(allCostRecords) > 0 {
dataExist = true
}
if dataExist == false {
log.Printf(
"Kubecost doesn't have data for date range %s to %s, skipping\n",
d.Format("2006-01-02T15:04:05Z"),
tomorrow.Format("2006-01-02T15:04:05Z"))
continue
}
var fileIndex int = 1
var rowCount int = 0
b := new(bytes.Buffer)
zipWriter := gzip.NewWriter(b)
writer := csv.NewWriter(zipWriter)
writer.Write(a.getCSVHeaders())
// Logs to validate date range requested and date range gotten in the data
log.Printf("Requested date range, from %s to %s \n", d.Format("2006-01-02T15:04:05Z"), tomorrow.Format("2006-01-02T15:04:05Z"))
for _, row := range a.getCSVRows(currency, monthOfData, allCostRecords) {
if rowCount >= a.MaxFileRows {
a.closeAndSaveFile(writer, zipWriter, b, monthOfData, csvFile)
fileIndex++
csvFile = fmt.Sprintf(path.Join(a.FilePath, "kubecost-%v-%d.csv.gz"), d.Format("2006-01-02"), fileIndex)
b = new(bytes.Buffer)
zipWriter = gzip.NewWriter(b)
writer = csv.NewWriter(zipWriter)
writer.Write(a.getCSVHeaders())
rowCount = 1
}
writer.Write(row)
rowCount++
}
a.closeAndSaveFile(writer, zipWriter, b, monthOfData, csvFile)
}
}
func (a *App) isIdleRecord(record KubecostAllocation) bool {
return strings.Contains(record.Name, "_idle_")
}
func (a *App) closeAndSaveFile(writer *csv.Writer, zipWriter *gzip.Writer, b *bytes.Buffer, monthOfData, csvGzFile string) {
writer.Flush()
zipWriter.Flush()
zipWriter.Close()
a.filesToUpload[monthOfData][csvGzFile] = struct{}{}
err := os.WriteFile(csvGzFile, b.Bytes(), 0644)
if err != nil {
log.Fatal(err)
}
log.Println("Retrieved", csvGzFile)
b.Reset()
// delete previous files for this day, except for the current one, in case there are fewer files than on the disk
matches := fileNameRe.FindStringSubmatch(csvGzFile)
if len(matches) >= 3 && len(matches[2]) == 0 { // filename without index
currentDate := matches[1]
for filename := range a.filesToUpload[monthOfData] {
if strings.Contains(filename, currentDate) && filename != csvGzFile {
_ = os.Remove(filename)
delete(a.filesToUpload[monthOfData], filename)
}
}
}
}
func (a *App) uploadToFlexera() {
accessToken, err := a.generateAccessToken()
if err != nil {
log.Fatalf("Error generating access token: %v", err)
}
authHeaders := map[string]string{"Authorization": "Bearer " + accessToken}
atLeastOneError := false
for month, files := range a.filesToUpload {
if len(files) == 0 {
log.Println("No files to upload for month", month)
continue
}
// if we try to upload files for previous month, we need to check if we have files for all days in the month
if !a.isCurrentMonth(month) {
// Since there may be more than one file for the same day, we must ensure that there is at least one file for each day.
daysToUpload := map[string]struct{}{}
for filename := range files {
matches := fileNameRe.FindStringSubmatch(filename)
if matches != nil && len(matches) >= 2 {
daysToUpload[matches[1]] = struct{}{}
}
}
if a.DaysInMonth(month) > len(daysToUpload) {
log.Println("Skipping month", month, "because not all days have a file to upload")
continue
}
}
billUploadID, err := a.StartBillUploadProcess(month, authHeaders)
if err != nil {
log.Println(err)
atLeastOneError = true
continue
}
for fileName := range files {
err = a.UploadFile(billUploadID, fileName, authHeaders)
if err != nil {
log.Printf("Error uploading file: %s. %s\n", fileName, err.Error())
atLeastOneError = true
break
}
}
if err != nil {
err = a.AbortBillUploadProcess(billUploadID, authHeaders)
} else {
err = a.CommitBillUploadProcess(billUploadID, authHeaders)
}
if err != nil {
log.Println(err)
atLeastOneError = true
}
}
//If at least one error in bill processing exit with code 1
if atLeastOneError {
//the below method internally uses os.Exit(1)
log.Fatal("Error during bill upload. Internal server error")
}
}
func (a *App) StartBillUploadProcess(month string, authHeaders map[string]string) (billUploadID string, err error) {
//Before the upload process create bill connect
a.createBillConnectIfNotExist(authHeaders)
billUpload := map[string]string{"billConnectId": a.BillConnectID, "billingPeriod": month}
billUploadJSON, _ := json.Marshal(billUpload)
response, err := a.doPost(a.billUploadURL, string(billUploadJSON), authHeaders)
if err != nil {
return "", err
}
switch response.StatusCode {
case 429:
time.Sleep(120 * time.Second)
return a.StartBillUploadProcess(month, authHeaders)
case 409:
bodyBytes, err := io.ReadAll(response.Body)
if err != nil {
return "", err
}
uuidMatch := uuidPattern.FindStringSubmatch(string(bodyBytes))
if len(uuidMatch) < 2 {
return "", fmt.Errorf("billUpload ID not found")
}
inProgressBillUploadID := uuidMatch[1]
err = a.AbortBillUploadProcess(inProgressBillUploadID, authHeaders)
if err != nil {
return "", err
}
return a.StartBillUploadProcess(month, authHeaders)
}
err = checkForError(response)
if err != nil {
return "", err
}
bodyBytes, err := io.ReadAll(response.Body)
if err != nil {
return "", err
}
var jsonResponse map[string]interface{}
if err = json.Unmarshal(bodyBytes, &jsonResponse); err != nil {
return "", err
}
return jsonResponse["id"].(string), nil
}
func (a *App) createBillConnectIfNotExist(authHeaders map[string]string) {
//If the flag is not enabled, do not attempt to create the bill connect
if !a.CreateBillConnectIfNotExist {
return
}
integrationId := "cbi-oi-kubecost"
//Split the billConnectId using the integrationId based on the bill identifier
if !strings.HasPrefix(a.BillConnectID, integrationId) {
log.Fatal("billConnectId does not start with the required prefix")
}
billIdentifier := strings.TrimPrefix(a.BillConnectID, integrationId+"-")
//Vendor name is same as display name
params := map[string]string{"displayName": a.VendorName, "vendorName": a.VendorName}
//name field has same value as bill identifier
createBillConnectPayload := map[string]interface{}{"billIdentifier": billIdentifier, "integrationId": integrationId, "name": billIdentifier, "params": params}
url := fmt.Sprintf("https://api.%s/%s/%s/%s", a.getFlexeraDomain(), "finops-onboarding/v1/orgs", a.OrgID, "bill-connects/cbi")
billConnectJson, _ := json.Marshal(createBillConnectPayload)
response, err := a.doPost(url, string(billConnectJson), authHeaders)
if err != nil {
//When the bill connect id is not provided, abort the process
log.Fatalf("Error while creating the bill connect : %v", err)
}
switch response.StatusCode {
case 201:
log.Printf("Bill Connect Id is created %s", a.BillConnectID)
break
case 409:
log.Printf("Bill Connect Id already exists %s", a.BillConnectID)
break
default:
log.Fatalf("Error while creating the bill connect : %v", err)
}
}
func (a *App) CommitBillUploadProcess(billUploadID string, headers map[string]string) error {
url := fmt.Sprintf("%s/%s/operations", a.billUploadURL, billUploadID)
response, err := a.doPost(url, `{"operation":"commit"}`, headers)
if err != nil {
return err
}
log.Println("commit upload bill process with id", billUploadID)
return checkForError(response)
}
func (a *App) AbortBillUploadProcess(billUploadID string, headers map[string]string) error {
url := fmt.Sprintf("%s/%s/operations", a.billUploadURL, billUploadID)
response, err := a.doPost(url, `{"operation":"abort"}`, headers)
if err != nil {
return err
}
log.Println("aborting upload bill process with id", billUploadID)
return checkForError(response)
}
func (a *App) UploadFile(billUploadID, fileName string, authHeaders map[string]string) error {
baseName := filepath.Base(fileName)
uploadFileURL := fmt.Sprintf("%s/%s/files/%s", a.billUploadURL, billUploadID, baseName)
fileData, err := os.ReadFile(fileName)
if err != nil {
return err
}
response, err := a.doPost(uploadFileURL, string(fileData), authHeaders)
if err != nil {
return err
}
err = checkForError(response)
if err != nil {
return err
}
bodyBytes, err := io.ReadAll(response.Body)
if err != nil {
return fmt.Errorf("error reading response: %s", err.Error())
}
defer response.Body.Close()
var uploadResponse OptimaFileUploadResponse
if err = json.Unmarshal(bodyBytes, &uploadResponse); err != nil {
return fmt.Errorf("error parsing response: %s", err.Error())
}
md5Hash := getMD5FromFileBytes(fileData)
if md5Hash != uploadResponse.MD5 {
return fmt.Errorf("MD5 of file %s does not match MD5 of uploaded file", fileName)
}
log.Printf("File %s uploaded and MD5 of file matches MD5 of uploaded file\n", fileName)
return nil
}
func (a *App) doPost(url, data string, headers map[string]string) (*http.Response, error) {
request, _ := http.NewRequest("POST", url, strings.NewReader(data))
log.Printf("Request: %+v\n", url)
for key, value := range headers {
request.Header.Set(key, value)
}
response, err := a.client.Do(request)
if err != nil {
return nil, err
}
log.Printf("Response Status Code: %+v\n", response.StatusCode)
return response, nil
}
// generateAccessToken returns an access token from the Flexera One API using a given refreshToken or service account.
func (a *App) generateAccessToken() (string, error) {
accessTokenUrl := fmt.Sprintf("https://login.%s/oidc/token", a.getFlexeraDomain())
reqBody := url.Values{}
if len(a.RefreshToken) > 0 {
reqBody.Set("grant_type", "refresh_token")
reqBody.Set("refresh_token", a.RefreshToken)
} else {
reqBody.Set("grant_type", "client_credentials")
reqBody.Set("client_id", a.ServiceClientId)
reqBody.Set("client_secret", a.ServiceClientSecret)
}
req, err := http.NewRequest("POST", accessTokenUrl, strings.NewReader(reqBody.Encode()))
if err != nil {
return "", fmt.Errorf("error creating access token request: %v", err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := a.client.Do(req)
if err != nil {
return "", fmt.Errorf("error retrieving access token: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("error retrieving access token: %v", resp.Status)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("error reading access token response body: %v", err)
}
var tokenResp struct {
AccessToken string `json:"access_token"`
}
if err := json.Unmarshal(body, &tokenResp); err != nil {
return "", fmt.Errorf("error parsing access token response body: %v", err)
}
return tokenResp.AccessToken, nil
}
// update file list and remove old files
func (a *App) updateFileList() {
files, err := os.ReadDir(a.FilePath)
if err != nil {
log.Fatal(err)
}
for _, file := range files {
if file.Type().IsRegular() {
matches := fileNameRe.FindStringSubmatch(file.Name())
if matches != nil {
if t, err := time.Parse("2006-01-02", matches[1]); err == nil {
if a.dateInInvoiceRange(t) {
a.filesToUpload[t.Format("2006-01")][path.Join(a.FilePath, file.Name())] = struct{}{}
} else if a.FileRotation && !a.dateInMandatoryFileSavingPeriod(t) {
if err = os.Remove(path.Join(a.FilePath, file.Name())); err != nil {
log.Printf("error removing file %s: %v", file.Name(), err)
}
}
}
}
}
}
}
func (a *App) getCurrency() string {
reqUrl := fmt.Sprintf("http://%s%sgetConfigs", a.KubecostConfigHost, a.KubecostConfigAPIPath)
resp, err := a.client.Get(reqUrl)
log.Printf("Request: %+v\n", reqUrl)
if err != nil {
log.Printf("Something went wrong, taking default value '%s'. \n Error: %s.\n", a.DefaultCurrency, err.Error())
return a.DefaultCurrency
}
log.Printf("Response Status Code: %+v\n", resp.StatusCode)
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
log.Printf("Unexpected http status at get config request, taking default value '%s'.\n", a.DefaultCurrency)
return a.DefaultCurrency
}
var config KubecostConfig
err = json.NewDecoder(resp.Body).Decode(&config)
if err != nil {
log.Printf("Something went wrong during decoding, taking default value '%s'. \n Error: %s.\n", a.DefaultCurrency, err.Error())
return a.DefaultCurrency
}
if config.Data.CurrencyCode == "" {
log.Printf("Currency has no value in the config, taking default value '%s'.\n", a.DefaultCurrency)
return a.DefaultCurrency
}
return config.Data.CurrencyCode
}
func (a *App) dateInInvoiceRange(date time.Time) bool {
for _, month := range a.invoiceMonths {
if date.Format("2006-01") == month {
return true
}
}
return false
}
func (a *App) dateInMandatoryFileSavingPeriod(date time.Time) bool {
return !date.Before(a.mandatoryFileSavingPeriodStartDate)
}
func (a *App) isCurrentMonth(month string) bool {
return time.Now().Local().Format("2006-01") == month
}
func (a *App) DaysInMonth(month string) int {
date, err := time.Parse("2006-01", month)
if err != nil {
return 0
}
numDays := date.AddDate(0, 1, 0).Sub(date).Hours() / 24
return int(numDays)
}
func (a *App) getOptimaAPIDomain() string {
optimaAPIDomainsDict := map[string]string{
"NAM": "api.optima.flexeraeng.com",
"EU": "api.optima-eu.flexeraeng.com",
"AU": "api.optima-apac.flexeraeng.com",
"DEV": "api.optima.flexeraengdev.com",
}
return optimaAPIDomainsDict[a.Shard]
}
func (a *App) getFlexeraDomain() string {
domainsDict := map[string]string{
"NAM": "flexera.com",
"EU": "flexera.eu",
"AU": "flexera.au",
"DEV": "flexeratest.com",
}
return domainsDict[a.Shard]
}
func (a *App) validateAppConfiguration() error {
switch a.Shard {
case "NAM", "EU", "AU", "DEV":
default:
return fmt.Errorf("shard: %s is wrong", a.Shard)
}
a.Aggregation = strings.ToLower(a.Aggregation)
switch a.Aggregation {
case "namespace":
a.aggregation = "cluster," + a.Aggregation
case "controller":
a.aggregation = "cluster,namespace,controllerKind," + a.Aggregation
case "node":
a.aggregation = "cluster,namespace,controllerKind,controller," + a.Aggregation
case "pod":
a.aggregation = "cluster,namespace,controllerKind,controller,node," + a.Aggregation
default:
return fmt.Errorf("aggregation type: %s is wrong", a.Aggregation)
}
if a.KubecostConfigHost == "" {
a.KubecostConfigHost = a.KubecostHost
}
if a.KubecostConfigAPIPath == "" {
a.KubecostConfigAPIPath = a.KubecostAPIPath
}
return nil
}
func newApp() *App {
lastInvoiceDate := time.Now().Local().AddDate(0, 0, -1)
a := App{
filesToUpload: make(map[string]map[string]struct{}),
client: &http.Client{},
lastInvoiceDate: lastInvoiceDate,
}
if err := env.Parse(&a.Config); err != nil {
log.Fatal(err)
}
if err := a.validateAppConfiguration(); err != nil {
log.Fatal(err)
}
a.client.Timeout = time.Duration(a.RequestTimeout) * time.Minute
a.billUploadURL = fmt.Sprintf("https://%s/optima/orgs/%s/billUploads", a.getOptimaAPIDomain(), a.OrgID)
a.invoiceMonths = []string{lastInvoiceDate.Format("2006-01")}
if a.IncludePreviousMonth {
a.invoiceMonths = append(a.invoiceMonths, a.lastInvoiceDate.AddDate(0, -1, 0).Format("2006-01"))
}
// The mandatory file saving period is the period since the first day of the previous month of last invoice date
previousMonthOfLastInvoiceDate := lastInvoiceDate.AddDate(0, -1, 0)
a.mandatoryFileSavingPeriodStartDate = time.Date(previousMonthOfLastInvoiceDate.Year(), previousMonthOfLastInvoiceDate.Month(), 1, 0, 0, 0, 0, previousMonthOfLastInvoiceDate.Location())
for _, month := range a.invoiceMonths {
a.filesToUpload[month] = make(map[string]struct{})
}
return &a
}
func (a *App) getCSVHeaders() []string {
return []string{
"ResourceID",
"Cost",
"CurrencyCode",
"Aggregation",
"UsageType",
"UsageAmount",
"UsageUnit",
"Cluster",
"Container",
"Namespace",
"Pod",
"Node",
"Controller",
"ControllerKind",
"ProviderID",
"Labels",
"InvoiceYearMonth",
"InvoiceDate",
"StartTime",
"EndTime",
}
}
func (a *App) getCSVRows(currency string, month string, data []KubecostAllocation) [][]string {
rows := make([][]string, 0)
mapDatesGotten := make(map[string]string)
for _, v := range data {
mapDatesGotten[v.Start] = v.End
labels := extractLabels(v.Properties)
types := []string{"cpuCost", "gpuCost", "ramCost", "pvCost", "networkCost", "sharedCost", "externalCost", "loadBalancerCost"}
vals := []float64{
v.CPUCost + v.CPUCostAdjustment,
v.GPUCost + v.GPUCostAdjustment,
v.RAMCost + v.RAMCostAdjustment,
v.PVCost + v.PVCostAdjustment,
v.NetworkCost + v.NetworkCostAdjustment,
v.SharedCost,
v.ExternalCost,
v.LoadBalancerCost + v.LoadBalancerCostAdjustment,
}
units := []string{"cpuCoreHours", "gpuHours", "ramByteHours", "pvByteHours", "networkTransferBytes", "minutes", "minutes", "minutes"}
amounts := []float64{v.CPUCoreHours, v.GPUHours, v.RAMByteHours, v.PVByteHours, v.NetworkTransferBytes, v.Minutes, v.Minutes, v.Minutes}
for i, c := range types {
multiplierFloat := a.Multiplier * vals[i]
if v.Properties.Cluster == "" {
v.Properties.Cluster = "Cluster"
}
rows = append(rows, []string{
v.Name,
strconv.FormatFloat(multiplierFloat, 'f', 5, 64),
currency,
a.Aggregation,
c,
strconv.FormatFloat(amounts[i], 'f', 5, 64),
units[i],
v.Properties.Cluster,
v.Properties.Container,
v.Properties.Namespace,
v.Properties.Pod,
v.Properties.Node,
v.Properties.Controller,
v.Properties.ControllerKind,
v.Properties.ProviderID,
labels,
strings.ReplaceAll(month, "-", ""),
v.Window.Start,
v.Start,
v.End,
})
}
}
log.Printf("Gotten dates range: %v \n", mapDatesGotten)
return rows
}
func checkForError(response *http.Response) error {
if response.StatusCode < 200 || response.StatusCode > 299 {
if bodyBytes, err := io.ReadAll(response.Body); err == nil {
log.Println(string(bodyBytes))
}
log.Printf("Request failed with status code: %d \n", response.StatusCode)
return fmt.Errorf("request failed with status code: %d", response.StatusCode)
}
return nil
}
// dateIter is a generator function that yields a sequence of dates starting
// from start_year and start_month (formatted as strings) until today.
func dateIter(startDate time.Time) <-chan time.Time {
c := make(chan time.Time)
go func() {
defer close(c)
for !time.Now().Local().Before(startDate) {
c <- startDate
startDate = startDate.AddDate(0, 0, 1)
}
}()
return c
}
// extractLabels returns a JSON string with all the properties labels, merging labels and namespace labels
// and adding labels for the container, controller, pod and provider.
func extractLabels(properties Properties) string {
mapLabels := make(map[string]string)
if properties.Labels != nil {
mapLabels = properties.Labels
}
if properties.NamespaceLabels != nil {
for k, v := range properties.NamespaceLabels {
mapLabels[k] = v
}
}
if properties.Container != "" {
mapLabels["kc-container"] = properties.Container
}
if properties.Controller != "" {
mapLabels["kc-controller"] = properties.Controller
}
if properties.Node != "" {
mapLabels["kc-node"] = properties.Node
}
if properties.Pod != "" {
mapLabels["kc-pod-id"] = properties.Pod
}
if properties.ProviderID != "" {
mapLabels["kc-provider-id"] = properties.ProviderID
}
//Map labels with cluster and namespace.
if properties.Cluster != "" {
mapLabels["kc-cluster"] = properties.Cluster
}
if properties.Namespace != "" {
mapLabels["kc-namespace"] = properties.Namespace
}
labelsJSON, _ := json.Marshal(mapLabels)
return string(labelsJSON)
}
func getMD5FromFileBytes(fileBytes []byte) string {
hash := md5.New()
hash.Write(fileBytes)
return hex.EncodeToString(hash.Sum(nil))
}