cluster_helpers_test.go 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182
  1. package costmodel
  2. import (
  3. "reflect"
  4. "strings"
  5. "testing"
  6. "time"
  7. "github.com/opencost/opencost/core/pkg/opencost"
  8. "github.com/opencost/opencost/core/pkg/util"
  9. "github.com/opencost/opencost/pkg/cloud/provider"
  10. "github.com/opencost/opencost/pkg/config"
  11. "github.com/opencost/opencost/pkg/prom"
  12. "github.com/davecgh/go-spew/spew"
  13. )
  14. func TestMergeTypeMaps(t *testing.T) {
  15. cases := []struct {
  16. name string
  17. map1 map[nodeIdentifierNoProviderID]string
  18. map2 map[nodeIdentifierNoProviderID]string
  19. expected map[nodeIdentifierNoProviderID]string
  20. }{
  21. {
  22. name: "both empty",
  23. map1: map[nodeIdentifierNoProviderID]string{},
  24. map2: map[nodeIdentifierNoProviderID]string{},
  25. expected: map[nodeIdentifierNoProviderID]string{},
  26. },
  27. {
  28. name: "map2 empty",
  29. map1: map[nodeIdentifierNoProviderID]string{
  30. {
  31. Cluster: "cluster1",
  32. Name: "node1",
  33. }: "type1",
  34. },
  35. map2: map[nodeIdentifierNoProviderID]string{},
  36. expected: map[nodeIdentifierNoProviderID]string{
  37. {
  38. Cluster: "cluster1",
  39. Name: "node1",
  40. }: "type1",
  41. },
  42. },
  43. {
  44. name: "map1 empty",
  45. map1: map[nodeIdentifierNoProviderID]string{},
  46. map2: map[nodeIdentifierNoProviderID]string{
  47. {
  48. Cluster: "cluster1",
  49. Name: "node1",
  50. }: "type1",
  51. },
  52. expected: map[nodeIdentifierNoProviderID]string{
  53. {
  54. Cluster: "cluster1",
  55. Name: "node1",
  56. }: "type1",
  57. },
  58. },
  59. {
  60. name: "no overlap",
  61. map1: map[nodeIdentifierNoProviderID]string{
  62. {
  63. Cluster: "cluster1",
  64. Name: "node1",
  65. }: "type1",
  66. },
  67. map2: map[nodeIdentifierNoProviderID]string{
  68. {
  69. Cluster: "cluster1",
  70. Name: "node2",
  71. }: "type2",
  72. {
  73. Cluster: "cluster1",
  74. Name: "node4",
  75. }: "type4",
  76. },
  77. expected: map[nodeIdentifierNoProviderID]string{
  78. {
  79. Cluster: "cluster1",
  80. Name: "node1",
  81. }: "type1",
  82. {
  83. Cluster: "cluster1",
  84. Name: "node2",
  85. }: "type2",
  86. {
  87. Cluster: "cluster1",
  88. Name: "node4",
  89. }: "type4",
  90. },
  91. },
  92. {
  93. name: "with overlap",
  94. map1: map[nodeIdentifierNoProviderID]string{
  95. {
  96. Cluster: "cluster1",
  97. Name: "node1",
  98. }: "type1",
  99. },
  100. map2: map[nodeIdentifierNoProviderID]string{
  101. {
  102. Cluster: "cluster1",
  103. Name: "node2",
  104. }: "type2",
  105. {
  106. Cluster: "cluster1",
  107. Name: "node1",
  108. }: "type4",
  109. },
  110. expected: map[nodeIdentifierNoProviderID]string{
  111. {
  112. Cluster: "cluster1",
  113. Name: "node1",
  114. }: "type1",
  115. {
  116. Cluster: "cluster1",
  117. Name: "node2",
  118. }: "type2",
  119. },
  120. },
  121. }
  122. for _, testCase := range cases {
  123. t.Run(testCase.name, func(t *testing.T) {
  124. result := mergeTypeMaps(testCase.map1, testCase.map2)
  125. if !reflect.DeepEqual(result, testCase.expected) {
  126. t.Errorf("mergeTypeMaps case %s failed. Got %+v but expected %+v", testCase.name, result, testCase.expected)
  127. }
  128. })
  129. }
  130. }
  131. func TestBuildNodeMap(t *testing.T) {
  132. cases := []struct {
  133. name string
  134. cpuCostMap map[NodeIdentifier]float64
  135. ramCostMap map[NodeIdentifier]float64
  136. gpuCostMap map[NodeIdentifier]float64
  137. gpuCountMap map[NodeIdentifier]float64
  138. cpuCoresMap map[nodeIdentifierNoProviderID]float64
  139. ramBytesMap map[nodeIdentifierNoProviderID]float64
  140. ramUserPctMap map[nodeIdentifierNoProviderID]float64
  141. ramSystemPctMap map[nodeIdentifierNoProviderID]float64
  142. cpuBreakdownMap map[nodeIdentifierNoProviderID]*ClusterCostsBreakdown
  143. activeDataMap map[NodeIdentifier]activeData
  144. preemptibleMap map[NodeIdentifier]bool
  145. labelsMap map[nodeIdentifierNoProviderID]map[string]string
  146. clusterAndNameToType map[nodeIdentifierNoProviderID]string
  147. expected map[NodeIdentifier]*Node
  148. overheadMap map[nodeIdentifierNoProviderID]*NodeOverhead
  149. }{
  150. {
  151. name: "empty",
  152. expected: map[NodeIdentifier]*Node{},
  153. },
  154. {
  155. name: "just cpu cost",
  156. cpuCostMap: map[NodeIdentifier]float64{
  157. {
  158. Cluster: "cluster1",
  159. Name: "node1",
  160. ProviderID: "prov_node1",
  161. }: 0.048,
  162. },
  163. clusterAndNameToType: map[nodeIdentifierNoProviderID]string{
  164. {
  165. Cluster: "cluster1",
  166. Name: "node1",
  167. }: "type1",
  168. },
  169. expected: map[NodeIdentifier]*Node{
  170. {
  171. Cluster: "cluster1",
  172. Name: "node1",
  173. ProviderID: "prov_node1",
  174. }: {
  175. Cluster: "cluster1",
  176. Name: "node1",
  177. ProviderID: "prov_node1",
  178. NodeType: "type1",
  179. CPUCost: 0.048,
  180. CPUBreakdown: &ClusterCostsBreakdown{},
  181. RAMBreakdown: &ClusterCostsBreakdown{},
  182. Overhead: &NodeOverhead{},
  183. },
  184. },
  185. },
  186. {
  187. name: "just cpu cost with empty provider ID",
  188. cpuCostMap: map[NodeIdentifier]float64{
  189. {
  190. Cluster: "cluster1",
  191. Name: "node1",
  192. }: 0.048,
  193. },
  194. clusterAndNameToType: map[nodeIdentifierNoProviderID]string{
  195. {
  196. Cluster: "cluster1",
  197. Name: "node1",
  198. }: "type1",
  199. },
  200. expected: map[NodeIdentifier]*Node{
  201. {
  202. Cluster: "cluster1",
  203. Name: "node1",
  204. }: {
  205. Cluster: "cluster1",
  206. Name: "node1",
  207. NodeType: "type1",
  208. CPUCost: 0.048,
  209. CPUBreakdown: &ClusterCostsBreakdown{},
  210. RAMBreakdown: &ClusterCostsBreakdown{},
  211. Overhead: &NodeOverhead{},
  212. },
  213. },
  214. },
  215. {
  216. name: "cpu cost with overlapping node names",
  217. cpuCostMap: map[NodeIdentifier]float64{
  218. {
  219. Cluster: "cluster1",
  220. Name: "node1",
  221. ProviderID: "prov_node1_A",
  222. }: 0.048,
  223. {
  224. Cluster: "cluster1",
  225. Name: "node1",
  226. ProviderID: "prov_node1_B",
  227. }: 0.087,
  228. },
  229. clusterAndNameToType: map[nodeIdentifierNoProviderID]string{
  230. {
  231. Cluster: "cluster1",
  232. Name: "node1",
  233. }: "type1",
  234. },
  235. expected: map[NodeIdentifier]*Node{
  236. {
  237. Cluster: "cluster1",
  238. Name: "node1",
  239. ProviderID: "prov_node1_A",
  240. }: {
  241. Cluster: "cluster1",
  242. Name: "node1",
  243. ProviderID: "prov_node1_A",
  244. NodeType: "type1",
  245. CPUCost: 0.048,
  246. CPUBreakdown: &ClusterCostsBreakdown{},
  247. RAMBreakdown: &ClusterCostsBreakdown{},
  248. Overhead: &NodeOverhead{},
  249. },
  250. {
  251. Cluster: "cluster1",
  252. Name: "node1",
  253. ProviderID: "prov_node1_B",
  254. }: {
  255. Cluster: "cluster1",
  256. Name: "node1",
  257. ProviderID: "prov_node1_B",
  258. NodeType: "type1",
  259. CPUCost: 0.087,
  260. CPUBreakdown: &ClusterCostsBreakdown{},
  261. RAMBreakdown: &ClusterCostsBreakdown{},
  262. Overhead: &NodeOverhead{},
  263. },
  264. },
  265. },
  266. {
  267. name: "all fields + overlapping node names",
  268. cpuCostMap: map[NodeIdentifier]float64{
  269. {
  270. Cluster: "cluster1",
  271. Name: "node1",
  272. ProviderID: "prov_node1_A",
  273. }: 0.048,
  274. {
  275. Cluster: "cluster1",
  276. Name: "node1",
  277. ProviderID: "prov_node1_B",
  278. }: 0.087,
  279. {
  280. Cluster: "cluster1",
  281. Name: "node2",
  282. ProviderID: "prov_node2_A",
  283. }: 0.033,
  284. },
  285. ramCostMap: map[NodeIdentifier]float64{
  286. {
  287. Cluster: "cluster1",
  288. Name: "node1",
  289. ProviderID: "prov_node1_A",
  290. }: 0.09,
  291. {
  292. Cluster: "cluster1",
  293. Name: "node1",
  294. ProviderID: "prov_node1_B",
  295. }: 0.3,
  296. {
  297. Cluster: "cluster1",
  298. Name: "node2",
  299. ProviderID: "prov_node2_A",
  300. }: 0.024,
  301. },
  302. gpuCostMap: map[NodeIdentifier]float64{
  303. {
  304. Cluster: "cluster1",
  305. Name: "node1",
  306. ProviderID: "prov_node1_A",
  307. }: 0.8,
  308. {
  309. Cluster: "cluster1",
  310. Name: "node1",
  311. ProviderID: "prov_node1_B",
  312. }: 1.4,
  313. {
  314. Cluster: "cluster1",
  315. Name: "node2",
  316. ProviderID: "prov_node2_A",
  317. }: 3.1,
  318. },
  319. gpuCountMap: map[NodeIdentifier]float64{
  320. {
  321. Cluster: "cluster1",
  322. Name: "node1",
  323. ProviderID: "prov_node1_A",
  324. }: 1.0,
  325. {
  326. Cluster: "cluster1",
  327. Name: "node1",
  328. ProviderID: "prov_node1_B",
  329. }: 1.0,
  330. {
  331. Cluster: "cluster1",
  332. Name: "node2",
  333. ProviderID: "prov_node2_A",
  334. }: 2.0,
  335. },
  336. cpuCoresMap: map[nodeIdentifierNoProviderID]float64{
  337. {
  338. Cluster: "cluster1",
  339. Name: "node1",
  340. }: 2.0,
  341. {
  342. Cluster: "cluster1",
  343. Name: "node2",
  344. }: 5.0,
  345. },
  346. ramBytesMap: map[nodeIdentifierNoProviderID]float64{
  347. {
  348. Cluster: "cluster1",
  349. Name: "node1",
  350. }: 2048.0,
  351. {
  352. Cluster: "cluster1",
  353. Name: "node2",
  354. }: 6303.0,
  355. },
  356. ramUserPctMap: map[nodeIdentifierNoProviderID]float64{
  357. {
  358. Cluster: "cluster1",
  359. Name: "node1",
  360. }: 30.0,
  361. {
  362. Cluster: "cluster1",
  363. Name: "node2",
  364. }: 42.6,
  365. },
  366. ramSystemPctMap: map[nodeIdentifierNoProviderID]float64{
  367. {
  368. Cluster: "cluster1",
  369. Name: "node1",
  370. }: 15.0,
  371. {
  372. Cluster: "cluster1",
  373. Name: "node2",
  374. }: 20.1,
  375. },
  376. cpuBreakdownMap: map[nodeIdentifierNoProviderID]*ClusterCostsBreakdown{
  377. {
  378. Cluster: "cluster1",
  379. Name: "node1",
  380. }: {
  381. System: 20.2,
  382. User: 68.0,
  383. },
  384. {
  385. Cluster: "cluster1",
  386. Name: "node2",
  387. }: {
  388. System: 28.9,
  389. User: 34.0,
  390. },
  391. },
  392. activeDataMap: map[NodeIdentifier]activeData{
  393. {
  394. Cluster: "cluster1",
  395. Name: "node1",
  396. ProviderID: "prov_node1_A",
  397. }: {
  398. start: time.Date(2020, 6, 16, 3, 45, 28, 0, time.UTC),
  399. end: time.Date(2020, 6, 16, 9, 20, 39, 0, time.UTC),
  400. minutes: 5*60 + 35 + (11.0 / 60.0),
  401. },
  402. {
  403. Cluster: "cluster1",
  404. Name: "node1",
  405. ProviderID: "prov_node1_B",
  406. }: {
  407. start: time.Date(2020, 6, 16, 3, 45, 28, 0, time.UTC),
  408. end: time.Date(2020, 6, 16, 9, 21, 39, 0, time.UTC),
  409. minutes: 5*60 + 36 + (11.0 / 60.0),
  410. },
  411. {
  412. Cluster: "cluster1",
  413. Name: "node2",
  414. ProviderID: "prov_node2_A",
  415. }: {
  416. start: time.Date(2020, 6, 16, 3, 45, 28, 0, time.UTC),
  417. end: time.Date(2020, 6, 16, 9, 10, 39, 0, time.UTC),
  418. minutes: 5*60 + 25 + (11.0 / 60.0),
  419. },
  420. },
  421. preemptibleMap: map[NodeIdentifier]bool{
  422. {
  423. Cluster: "cluster1",
  424. Name: "node1",
  425. ProviderID: "prov_node1_A",
  426. }: true,
  427. {
  428. Cluster: "cluster1",
  429. Name: "node1",
  430. ProviderID: "prov_node1_B",
  431. }: false,
  432. {
  433. Cluster: "cluster1",
  434. Name: "node2",
  435. ProviderID: "prov_node2_A",
  436. }: false,
  437. },
  438. labelsMap: map[nodeIdentifierNoProviderID]map[string]string{
  439. {
  440. Cluster: "cluster1",
  441. Name: "node1",
  442. }: {
  443. "labelname1_A": "labelvalue1_A",
  444. "labelname1_B": "labelvalue1_B",
  445. },
  446. {
  447. Cluster: "cluster1",
  448. Name: "node2",
  449. }: {
  450. "labelname2_A": "labelvalue2_A",
  451. "labelname2_B": "labelvalue2_B",
  452. },
  453. },
  454. clusterAndNameToType: map[nodeIdentifierNoProviderID]string{
  455. {
  456. Cluster: "cluster1",
  457. Name: "node1",
  458. }: "type1",
  459. {
  460. Cluster: "cluster1",
  461. Name: "node2",
  462. }: "type2",
  463. },
  464. expected: map[NodeIdentifier]*Node{
  465. {
  466. Cluster: "cluster1",
  467. Name: "node1",
  468. ProviderID: "prov_node1_A",
  469. }: {
  470. Cluster: "cluster1",
  471. Name: "node1",
  472. ProviderID: "prov_node1_A",
  473. NodeType: "type1",
  474. CPUCost: 0.048,
  475. RAMCost: 0.09,
  476. GPUCost: 0.8,
  477. CPUCores: 2.0,
  478. GPUCount: 1.0,
  479. RAMBytes: 2048.0,
  480. RAMBreakdown: &ClusterCostsBreakdown{
  481. User: 30.0,
  482. System: 15.0,
  483. },
  484. CPUBreakdown: &ClusterCostsBreakdown{
  485. System: 20.2,
  486. User: 68.0,
  487. },
  488. Start: time.Date(2020, 6, 16, 3, 45, 28, 0, time.UTC),
  489. End: time.Date(2020, 6, 16, 9, 20, 39, 0, time.UTC),
  490. Minutes: 5*60 + 35 + (11.0 / 60.0),
  491. Preemptible: true,
  492. Overhead: &NodeOverhead{},
  493. Labels: map[string]string{
  494. "labelname1_A": "labelvalue1_A",
  495. "labelname1_B": "labelvalue1_B",
  496. },
  497. },
  498. {
  499. Cluster: "cluster1",
  500. Name: "node1",
  501. ProviderID: "prov_node1_B",
  502. }: {
  503. Cluster: "cluster1",
  504. Name: "node1",
  505. ProviderID: "prov_node1_B",
  506. NodeType: "type1",
  507. CPUCost: 0.087,
  508. RAMCost: 0.3,
  509. GPUCost: 1.4,
  510. CPUCores: 2.0,
  511. GPUCount: 1.0,
  512. RAMBytes: 2048.0,
  513. RAMBreakdown: &ClusterCostsBreakdown{
  514. User: 30.0,
  515. System: 15.0,
  516. },
  517. CPUBreakdown: &ClusterCostsBreakdown{
  518. System: 20.2,
  519. User: 68.0,
  520. },
  521. Start: time.Date(2020, 6, 16, 3, 45, 28, 0, time.UTC),
  522. End: time.Date(2020, 6, 16, 9, 21, 39, 0, time.UTC),
  523. Minutes: 5*60 + 36 + (11.0 / 60.0),
  524. Preemptible: false,
  525. Labels: map[string]string{
  526. "labelname1_A": "labelvalue1_A",
  527. "labelname1_B": "labelvalue1_B",
  528. },
  529. Overhead: &NodeOverhead{},
  530. },
  531. {
  532. Cluster: "cluster1",
  533. Name: "node2",
  534. ProviderID: "prov_node2_A",
  535. }: {
  536. Cluster: "cluster1",
  537. Name: "node2",
  538. ProviderID: "prov_node2_A",
  539. NodeType: "type2",
  540. CPUCost: 0.033,
  541. RAMCost: 0.024,
  542. GPUCost: 3.1,
  543. CPUCores: 5.0,
  544. GPUCount: 2.0,
  545. RAMBytes: 6303.0,
  546. RAMBreakdown: &ClusterCostsBreakdown{
  547. User: 42.6,
  548. System: 20.1,
  549. },
  550. CPUBreakdown: &ClusterCostsBreakdown{
  551. System: 28.9,
  552. User: 34.0,
  553. },
  554. Start: time.Date(2020, 6, 16, 3, 45, 28, 0, time.UTC),
  555. End: time.Date(2020, 6, 16, 9, 10, 39, 0, time.UTC),
  556. Minutes: 5*60 + 25 + (11.0 / 60.0),
  557. Preemptible: false,
  558. Labels: map[string]string{
  559. "labelname2_A": "labelvalue2_A",
  560. "labelname2_B": "labelvalue2_B",
  561. },
  562. Overhead: &NodeOverhead{},
  563. },
  564. },
  565. },
  566. {
  567. name: "e2-micro cpu cost adjustment",
  568. cpuCostMap: map[NodeIdentifier]float64{
  569. {
  570. Cluster: "cluster1",
  571. Name: "node1",
  572. ProviderID: "prov_node1",
  573. }: 0.048,
  574. },
  575. cpuCoresMap: map[nodeIdentifierNoProviderID]float64{
  576. {
  577. Cluster: "cluster1",
  578. Name: "node1",
  579. }: 6.0, // GKE lies about number of cores
  580. },
  581. clusterAndNameToType: map[nodeIdentifierNoProviderID]string{
  582. {
  583. Cluster: "cluster1",
  584. Name: "node1",
  585. }: "e2-micro", // for this node type
  586. },
  587. expected: map[NodeIdentifier]*Node{
  588. {
  589. Cluster: "cluster1",
  590. Name: "node1",
  591. ProviderID: "prov_node1",
  592. }: {
  593. Cluster: "cluster1",
  594. Name: "node1",
  595. ProviderID: "prov_node1",
  596. NodeType: "e2-micro",
  597. CPUCost: 0.048 * (partialCPUMap["e2-micro"] / 6.0), // adjustmentFactor is (v / GKE cores)
  598. CPUCores: partialCPUMap["e2-micro"],
  599. CPUBreakdown: &ClusterCostsBreakdown{},
  600. RAMBreakdown: &ClusterCostsBreakdown{},
  601. Overhead: &NodeOverhead{},
  602. },
  603. },
  604. },
  605. {
  606. name: "e2-small cpu cost adjustment",
  607. cpuCostMap: map[NodeIdentifier]float64{
  608. {
  609. Cluster: "cluster1",
  610. Name: "node1",
  611. ProviderID: "prov_node1",
  612. }: 0.048,
  613. },
  614. cpuCoresMap: map[nodeIdentifierNoProviderID]float64{
  615. {
  616. Cluster: "cluster1",
  617. Name: "node1",
  618. }: 6.0, // GKE lies about number of cores
  619. },
  620. clusterAndNameToType: map[nodeIdentifierNoProviderID]string{
  621. {
  622. Cluster: "cluster1",
  623. Name: "node1",
  624. }: "e2-small", // for this node type
  625. },
  626. expected: map[NodeIdentifier]*Node{
  627. {
  628. Cluster: "cluster1",
  629. Name: "node1",
  630. ProviderID: "prov_node1",
  631. }: {
  632. Cluster: "cluster1",
  633. Name: "node1",
  634. ProviderID: "prov_node1",
  635. NodeType: "e2-small",
  636. CPUCost: 0.048 * (partialCPUMap["e2-small"] / 6.0), // adjustmentFactor is (v / GKE cores)
  637. CPUCores: partialCPUMap["e2-small"],
  638. CPUBreakdown: &ClusterCostsBreakdown{},
  639. RAMBreakdown: &ClusterCostsBreakdown{},
  640. Overhead: &NodeOverhead{},
  641. },
  642. },
  643. },
  644. {
  645. name: "e2-medium cpu cost adjustment",
  646. cpuCostMap: map[NodeIdentifier]float64{
  647. {
  648. Cluster: "cluster1",
  649. Name: "node1",
  650. ProviderID: "prov_node1",
  651. }: 0.048,
  652. },
  653. cpuCoresMap: map[nodeIdentifierNoProviderID]float64{
  654. {
  655. Cluster: "cluster1",
  656. Name: "node1",
  657. }: 6.0, // GKE lies about number of cores
  658. },
  659. clusterAndNameToType: map[nodeIdentifierNoProviderID]string{
  660. {
  661. Cluster: "cluster1",
  662. Name: "node1",
  663. }: "e2-medium", // for this node type
  664. },
  665. overheadMap: map[nodeIdentifierNoProviderID]*NodeOverhead{
  666. {
  667. Cluster: "cluster1",
  668. Name: "node1",
  669. }: {
  670. CpuOverheadFraction: 0.5,
  671. RamOverheadFraction: 0.25,
  672. }, // for this node type
  673. },
  674. expected: map[NodeIdentifier]*Node{
  675. {
  676. Cluster: "cluster1",
  677. Name: "node1",
  678. ProviderID: "prov_node1",
  679. }: {
  680. Cluster: "cluster1",
  681. Name: "node1",
  682. ProviderID: "prov_node1",
  683. NodeType: "e2-medium",
  684. CPUCost: 0.048 * (partialCPUMap["e2-medium"] / 6.0), // adjustmentFactor is (v / GKE cores)
  685. CPUCores: partialCPUMap["e2-medium"],
  686. CPUBreakdown: &ClusterCostsBreakdown{},
  687. RAMBreakdown: &ClusterCostsBreakdown{},
  688. Overhead: &NodeOverhead{
  689. CpuOverheadFraction: 0.5,
  690. RamOverheadFraction: 0.25,
  691. },
  692. },
  693. },
  694. },
  695. }
  696. for _, testCase := range cases {
  697. t.Run(testCase.name, func(t *testing.T) {
  698. result := buildNodeMap(
  699. testCase.cpuCostMap, testCase.ramCostMap, testCase.gpuCostMap, testCase.gpuCountMap,
  700. testCase.cpuCoresMap, testCase.ramBytesMap, testCase.ramUserPctMap,
  701. testCase.ramSystemPctMap,
  702. testCase.cpuBreakdownMap,
  703. testCase.activeDataMap,
  704. testCase.preemptibleMap,
  705. testCase.labelsMap,
  706. testCase.clusterAndNameToType,
  707. time.Minute,
  708. testCase.overheadMap,
  709. )
  710. if !reflect.DeepEqual(result, testCase.expected) {
  711. t.Errorf("buildNodeMap case %s failed. Got %+v but expected %+v", testCase.name, result, testCase.expected)
  712. // Use spew because we have to follow pointers to figure out
  713. // what isn't matching up
  714. t.Logf("Got: %s", spew.Sdump(result))
  715. t.Logf("Expected: %s", spew.Sdump(testCase.expected))
  716. }
  717. })
  718. }
  719. }
  720. func TestBuildGPUCostMap(t *testing.T) {
  721. cases := []struct {
  722. name string
  723. promResult []*prom.QueryResult
  724. countMap map[NodeIdentifier]float64
  725. expected map[NodeIdentifier]float64
  726. }{
  727. {
  728. name: "All Zeros",
  729. promResult: []*prom.QueryResult{
  730. {
  731. Metric: map[string]interface{}{
  732. "cluster_id": "cluster1",
  733. "node": "node1",
  734. "instance_type": "type1",
  735. "provider_id": "provider1",
  736. },
  737. Values: []*util.Vector{
  738. {
  739. Timestamp: 0,
  740. Value: 0,
  741. },
  742. },
  743. },
  744. },
  745. countMap: map[NodeIdentifier]float64{
  746. {
  747. Cluster: "cluster1",
  748. Name: "node1",
  749. ProviderID: "provider1",
  750. }: 0,
  751. },
  752. expected: map[NodeIdentifier]float64{
  753. {
  754. Cluster: "cluster1",
  755. Name: "node1",
  756. ProviderID: "provider1",
  757. }: 0,
  758. },
  759. },
  760. {
  761. name: "Zero Node Count",
  762. promResult: []*prom.QueryResult{
  763. {
  764. Metric: map[string]interface{}{
  765. "cluster_id": "cluster1",
  766. "node": "node1",
  767. "instance_type": "type1",
  768. "provider_id": "provider1",
  769. },
  770. Values: []*util.Vector{
  771. {
  772. Timestamp: 0,
  773. Value: 2,
  774. },
  775. },
  776. },
  777. },
  778. countMap: map[NodeIdentifier]float64{
  779. {
  780. Cluster: "cluster1",
  781. Name: "node1",
  782. ProviderID: "provider1",
  783. }: 0,
  784. },
  785. expected: map[NodeIdentifier]float64{
  786. {
  787. Cluster: "cluster1",
  788. Name: "node1",
  789. ProviderID: "provider1",
  790. }: 0,
  791. },
  792. },
  793. {
  794. name: "Missing Node Count",
  795. promResult: []*prom.QueryResult{
  796. {
  797. Metric: map[string]interface{}{
  798. "cluster_id": "cluster1",
  799. "node": "node1",
  800. "instance_type": "type1",
  801. "provider_id": "provider1",
  802. },
  803. Values: []*util.Vector{
  804. {
  805. Timestamp: 0,
  806. Value: 2,
  807. },
  808. },
  809. },
  810. },
  811. countMap: map[NodeIdentifier]float64{},
  812. expected: map[NodeIdentifier]float64{
  813. {
  814. Cluster: "cluster1",
  815. Name: "node1",
  816. ProviderID: "provider1",
  817. }: 0,
  818. },
  819. },
  820. {
  821. name: "missing cost data",
  822. promResult: []*prom.QueryResult{
  823. {},
  824. },
  825. countMap: map[NodeIdentifier]float64{
  826. {
  827. Cluster: "cluster1",
  828. Name: "node1",
  829. ProviderID: "provider1",
  830. }: 0,
  831. },
  832. expected: map[NodeIdentifier]float64{},
  833. },
  834. {
  835. name: "All values present",
  836. promResult: []*prom.QueryResult{
  837. {
  838. Metric: map[string]interface{}{
  839. "cluster_id": "cluster1",
  840. "node": "node1",
  841. "instance_type": "type1",
  842. "provider_id": "provider1",
  843. },
  844. Values: []*util.Vector{
  845. {
  846. Timestamp: 0,
  847. Value: 2,
  848. },
  849. },
  850. },
  851. },
  852. countMap: map[NodeIdentifier]float64{
  853. {
  854. Cluster: "cluster1",
  855. Name: "node1",
  856. ProviderID: "provider1",
  857. }: 2,
  858. },
  859. expected: map[NodeIdentifier]float64{
  860. {
  861. Cluster: "cluster1",
  862. Name: "node1",
  863. ProviderID: "provider1",
  864. }: 4,
  865. },
  866. },
  867. }
  868. for _, testCase := range cases {
  869. t.Run(testCase.name, func(t *testing.T) {
  870. testProvider := &provider.CustomProvider{
  871. Config: provider.NewProviderConfig(config.NewConfigFileManager(nil), "fakeFile"),
  872. }
  873. testPreemptible := make(map[NodeIdentifier]bool)
  874. result, _ := buildGPUCostMap(testCase.promResult, testCase.countMap, testProvider, testPreemptible)
  875. if !reflect.DeepEqual(result, testCase.expected) {
  876. t.Errorf("buildGPUCostMap case %s failed. Got %+v but expected %+v", testCase.name, result, testCase.expected)
  877. }
  878. })
  879. }
  880. }
  881. func TestAssetCustompricing(t *testing.T) {
  882. windowStart := time.Date(2020, time.April, 13, 0, 0, 0, 0, time.UTC)
  883. windowEnd := windowStart.Add(time.Hour)
  884. window := opencost.NewClosedWindow(windowStart, windowEnd)
  885. startTimestamp := float64(windowStart.Unix())
  886. nodePromResult := []*prom.QueryResult{
  887. {
  888. Metric: map[string]interface{}{
  889. "cluster_id": "cluster1",
  890. "node": "node1",
  891. "instance_type": "type1",
  892. "provider_id": "provider1",
  893. },
  894. Values: []*util.Vector{
  895. {
  896. Timestamp: startTimestamp,
  897. Value: 0.5,
  898. },
  899. },
  900. },
  901. }
  902. pvCostPromResult := []*prom.QueryResult{
  903. {
  904. Metric: map[string]interface{}{
  905. "cluster_id": "cluster1",
  906. "persistentvolume": "pvc1",
  907. "provider_id": "provider1",
  908. },
  909. Values: []*util.Vector{
  910. {
  911. Timestamp: startTimestamp,
  912. Value: 1.0,
  913. },
  914. },
  915. },
  916. }
  917. pvSizePromResult := []*prom.QueryResult{
  918. {
  919. Metric: map[string]interface{}{
  920. "cluster_id": "cluster1",
  921. "persistentvolume": "pvc1",
  922. "provider_id": "provider1",
  923. },
  924. Values: []*util.Vector{
  925. {
  926. Timestamp: startTimestamp,
  927. Value: 1073741824.0,
  928. },
  929. },
  930. },
  931. }
  932. pvMinsPromResult := []*prom.QueryResult{
  933. {
  934. Metric: map[string]interface{}{
  935. "cluster_id": "cluster1",
  936. "persistentvolume": "pvc1",
  937. "provider_id": "provider1",
  938. },
  939. Values: []*util.Vector{
  940. {
  941. Timestamp: startTimestamp,
  942. Value: 1.0,
  943. },
  944. {
  945. Timestamp: startTimestamp + (60.0 * 60.0),
  946. Value: 1.0,
  947. },
  948. },
  949. },
  950. }
  951. pvAvgUsagePromResult := []*prom.QueryResult{
  952. {
  953. Metric: map[string]interface{}{
  954. "cluster_id": "cluster1",
  955. "persistentvolumeclaim": "pv-claim1",
  956. "namespace": "ns1",
  957. },
  958. Values: []*util.Vector{
  959. {
  960. Timestamp: startTimestamp,
  961. Value: 1.0,
  962. },
  963. {
  964. Timestamp: startTimestamp + (60.0 * 60.0),
  965. Value: 1.0,
  966. },
  967. },
  968. },
  969. }
  970. pvMaxUsagePromResult := []*prom.QueryResult{
  971. {
  972. Metric: map[string]interface{}{
  973. "cluster_id": "cluster1",
  974. "persistentvolumeclaim": "pv-claim1",
  975. "namespace": "ns1",
  976. },
  977. Values: []*util.Vector{
  978. {
  979. Timestamp: startTimestamp,
  980. Value: 1.0,
  981. },
  982. {
  983. Timestamp: startTimestamp + (60.0 * 60.0),
  984. Value: 1.0,
  985. },
  986. },
  987. },
  988. }
  989. pvInfoPromResult := []*prom.QueryResult{
  990. {
  991. Metric: map[string]interface{}{
  992. "cluster_id": "cluster1",
  993. "persistentvolumeclaim": "pv-claim1",
  994. "volumename": "pvc1",
  995. "namespace": "ns1",
  996. },
  997. Values: []*util.Vector{
  998. {
  999. Timestamp: startTimestamp,
  1000. Value: 1.0,
  1001. },
  1002. },
  1003. },
  1004. }
  1005. gpuCountMap := map[NodeIdentifier]float64{
  1006. {
  1007. Cluster: "cluster1",
  1008. Name: "node1",
  1009. ProviderID: "provider1",
  1010. }: 2,
  1011. }
  1012. nodeKey := NodeIdentifier{
  1013. Cluster: "cluster1",
  1014. Name: "node1",
  1015. ProviderID: "provider1",
  1016. }
  1017. cases := []struct {
  1018. name string
  1019. customPricingMap map[string]string
  1020. expectedPricing map[string]float64
  1021. }{
  1022. {
  1023. name: "No custom pricing",
  1024. customPricingMap: map[string]string{},
  1025. expectedPricing: map[string]float64{
  1026. "CPU": 0.5,
  1027. "RAM": 0.5,
  1028. "GPU": 1.0,
  1029. "Storage": 1.0,
  1030. },
  1031. },
  1032. {
  1033. name: "Custom pricing enabled",
  1034. customPricingMap: map[string]string{
  1035. "CPU": "20.0",
  1036. "RAM": "4.0",
  1037. "GPU": "500.0",
  1038. "Storage": "0.1",
  1039. "customPricesEnabled": "true",
  1040. },
  1041. expectedPricing: map[string]float64{
  1042. "CPU": 0.027397, // 20.0 / 730
  1043. "RAM": 5.102716386318207e-12, // 4.0 / 730 / 1024^3
  1044. "GPU": 1.369864, // 500.0 / 730 * 2
  1045. "Storage": 0.000137, // 0.1 / 730 * (1073741824.0 / 1024 / 1024 / 1024) * (60 / 60) => 0.1 / 730 * 1 * 1
  1046. },
  1047. },
  1048. }
  1049. for _, testCase := range cases {
  1050. t.Run(testCase.name, func(t *testing.T) {
  1051. testProvider := &provider.CustomProvider{
  1052. Config: provider.NewProviderConfig(config.NewConfigFileManager(nil), ""),
  1053. }
  1054. testProvider.UpdateConfigFromConfigMap(testCase.customPricingMap)
  1055. testPreemptible := make(map[NodeIdentifier]bool)
  1056. cpuMap, _ := buildCPUCostMap(nodePromResult, testProvider, testPreemptible)
  1057. ramMap, _ := buildRAMCostMap(nodePromResult, testProvider, testPreemptible)
  1058. gpuMap, _ := buildGPUCostMap(nodePromResult, gpuCountMap, testProvider, testPreemptible)
  1059. cpuResult := cpuMap[nodeKey]
  1060. ramResult := ramMap[nodeKey]
  1061. gpuResult := gpuMap[nodeKey]
  1062. diskMap := map[DiskIdentifier]*Disk{}
  1063. pvCosts(diskMap, time.Hour, pvMinsPromResult, pvSizePromResult, pvCostPromResult, pvAvgUsagePromResult, pvMaxUsagePromResult, pvInfoPromResult, testProvider, window)
  1064. diskResult := diskMap[DiskIdentifier{"cluster1", "pvc1"}].Cost
  1065. if !util.IsApproximately(cpuResult, testCase.expectedPricing["CPU"]) {
  1066. t.Errorf("CPU custom pricing error in %s. Got %v but expected %v", testCase.name, cpuResult, testCase.expectedPricing["CPU"])
  1067. }
  1068. if !util.IsApproximately(ramResult, testCase.expectedPricing["RAM"]) {
  1069. t.Errorf("RAM custom pricing error in %s. Got %v but expected %v", testCase.name, ramResult, testCase.expectedPricing["RAM"])
  1070. }
  1071. if !util.IsApproximately(gpuResult, testCase.expectedPricing["GPU"]) {
  1072. t.Errorf("GPU custom pricing error in %s. Got %v but expected %v", testCase.name, gpuResult, testCase.expectedPricing["GPU"])
  1073. }
  1074. if !util.IsApproximately(diskResult, testCase.expectedPricing["Storage"]) {
  1075. t.Errorf("Disk custom pricing error in %s. Got %v but expected %v", testCase.name, diskResult, testCase.expectedPricing["Storage"])
  1076. }
  1077. })
  1078. }
  1079. }
  1080. func TestBuildLabelsMap(t *testing.T) {
  1081. const (
  1082. labelKey1 = "testlabelkey1"
  1083. labelValue1 = "testlabel1-value"
  1084. labelKey2 = "test-label-key-2"
  1085. labelValue2 = "testlabel2.value"
  1086. nonLabelKey = "instance_type"
  1087. labelPrefix = "label_"
  1088. )
  1089. startTimestamp := float64(windowStart.Unix())
  1090. nodePromResult := []*prom.QueryResult{
  1091. {
  1092. Metric: map[string]interface{}{
  1093. "cluster_id": "cluster1",
  1094. "node": "node1",
  1095. "instance_type": "type1",
  1096. "provider_id": "provider1",
  1097. "label_testlabelkey1": "testlabel1-value",
  1098. "label_test-label-key-2": "testlabel2.value",
  1099. },
  1100. Values: []*util.Vector{
  1101. {
  1102. Timestamp: startTimestamp,
  1103. Value: 0.5,
  1104. },
  1105. },
  1106. },
  1107. {
  1108. Metric: map[string]interface{}{
  1109. "cluster_id": "cluster1",
  1110. "node": "node2",
  1111. "instance_type": "type1",
  1112. "provider_id": "provider1",
  1113. "label_testlabelkey1": "testlabel1-value",
  1114. "label_test-label-key-2": "testlabel2.value",
  1115. },
  1116. Values: []*util.Vector{
  1117. {
  1118. Timestamp: startTimestamp,
  1119. Value: 0.5,
  1120. },
  1121. },
  1122. },
  1123. }
  1124. nodeLabelMap := buildLabelsMap(nodePromResult)
  1125. // Test that for all nodes and all label keys in the map there isn't a key with the label_ prefix.
  1126. for _, labelMap := range nodeLabelMap {
  1127. for key, value := range labelMap {
  1128. if strings.HasPrefix(key, labelPrefix) {
  1129. t.Errorf("Asset label maps aren't sanitized. Expected no '%v' prefix in %v", labelPrefix, key)
  1130. }
  1131. // Test that the label value isn't touched
  1132. if key == labelKey1 && value != labelValue1 {
  1133. t.Errorf("Label Value didn't match. Got %v, but Expected: %v", value, labelValue1)
  1134. }
  1135. // Test that the label value isn't touched
  1136. if key == labelKey2 && value != labelValue2 {
  1137. t.Errorf("Label Value didn't match. Got %v, but Expected: %v", value, labelValue2)
  1138. }
  1139. }
  1140. // Test that keys that don't have the label_ prefix aren't in the resultant label map.
  1141. _, ok := labelMap[nonLabelKey]
  1142. if ok {
  1143. t.Errorf("Non-label keys are included in label mapping for asset labels. Expected '%v' to not exist'.", nonLabelKey)
  1144. }
  1145. }
  1146. }