cluster_helpers_test.go 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232
  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/source"
  9. "github.com/opencost/opencost/core/pkg/util"
  10. "github.com/opencost/opencost/pkg/cloud/provider"
  11. "github.com/opencost/opencost/pkg/config"
  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. testCase.overheadMap,
  708. )
  709. if !reflect.DeepEqual(result, testCase.expected) {
  710. t.Errorf("buildNodeMap case %s failed. Got %+v but expected %+v", testCase.name, result, testCase.expected)
  711. // Use spew because we have to follow pointers to figure out
  712. // what isn't matching up
  713. t.Logf("Got: %s", spew.Sdump(result))
  714. t.Logf("Expected: %s", spew.Sdump(testCase.expected))
  715. }
  716. })
  717. }
  718. }
  719. func TestBuildGPUCostMap(t *testing.T) {
  720. cases := []struct {
  721. name string
  722. promResult []*source.QueryResult
  723. countMap map[NodeIdentifier]float64
  724. expected map[NodeIdentifier]float64
  725. }{
  726. {
  727. name: "All Zeros",
  728. promResult: []*source.QueryResult{
  729. source.NewQueryResult(
  730. map[string]interface{}{
  731. "cluster_id": "cluster1",
  732. "node": "node1",
  733. "instance_type": "type1",
  734. "provider_id": "provider1",
  735. },
  736. []*util.Vector{
  737. {
  738. Timestamp: 0,
  739. Value: 0,
  740. },
  741. },
  742. source.DefaultResultKeys(),
  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: []*source.QueryResult{
  763. source.NewQueryResult(
  764. map[string]interface{}{
  765. "cluster_id": "cluster1",
  766. "node": "node1",
  767. "instance_type": "type1",
  768. "provider_id": "provider1",
  769. },
  770. []*util.Vector{
  771. {
  772. Timestamp: 0,
  773. Value: 2,
  774. },
  775. },
  776. source.DefaultResultKeys(),
  777. ),
  778. },
  779. countMap: map[NodeIdentifier]float64{
  780. {
  781. Cluster: "cluster1",
  782. Name: "node1",
  783. ProviderID: "provider1",
  784. }: 0,
  785. },
  786. expected: map[NodeIdentifier]float64{
  787. {
  788. Cluster: "cluster1",
  789. Name: "node1",
  790. ProviderID: "provider1",
  791. }: 0,
  792. },
  793. },
  794. {
  795. name: "Missing Node Count",
  796. promResult: []*source.QueryResult{
  797. source.NewQueryResult(
  798. map[string]interface{}{
  799. "cluster_id": "cluster1",
  800. "node": "node1",
  801. "instance_type": "type1",
  802. "provider_id": "provider1",
  803. },
  804. []*util.Vector{
  805. {
  806. Timestamp: 0,
  807. Value: 2,
  808. },
  809. },
  810. source.DefaultResultKeys(),
  811. ),
  812. },
  813. countMap: map[NodeIdentifier]float64{},
  814. expected: map[NodeIdentifier]float64{
  815. {
  816. Cluster: "cluster1",
  817. Name: "node1",
  818. ProviderID: "provider1",
  819. }: 0,
  820. },
  821. },
  822. {
  823. name: "missing cost data",
  824. promResult: []*source.QueryResult{},
  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: []*source.QueryResult{
  837. source.NewQueryResult(
  838. map[string]interface{}{
  839. "cluster_id": "cluster1",
  840. "node": "node1",
  841. "instance_type": "type1",
  842. "provider_id": "provider1",
  843. },
  844. []*util.Vector{
  845. {
  846. Timestamp: 0,
  847. Value: 2,
  848. },
  849. },
  850. source.DefaultResultKeys(),
  851. ),
  852. },
  853. countMap: map[NodeIdentifier]float64{
  854. {
  855. Cluster: "cluster1",
  856. Name: "node1",
  857. ProviderID: "provider1",
  858. }: 2,
  859. },
  860. expected: map[NodeIdentifier]float64{
  861. {
  862. Cluster: "cluster1",
  863. Name: "node1",
  864. ProviderID: "provider1",
  865. }: 4,
  866. },
  867. },
  868. }
  869. for _, testCase := range cases {
  870. t.Run(testCase.name, func(t *testing.T) {
  871. testProvider := &provider.CustomProvider{
  872. Config: provider.NewProviderConfig(config.NewConfigFileManager(nil), "fakeFile"),
  873. }
  874. testPreemptible := make(map[NodeIdentifier]bool)
  875. gpuPrices := source.DecodeAll(testCase.promResult, source.DecodeNodeGPUPricePerHrResult)
  876. result, _ := buildGPUCostMap(gpuPrices, testCase.countMap, testProvider, testPreemptible)
  877. if !reflect.DeepEqual(result, testCase.expected) {
  878. t.Errorf("buildGPUCostMap case %s failed. Got %+v but expected %+v", testCase.name, result, testCase.expected)
  879. }
  880. })
  881. }
  882. }
  883. func TestAssetCustompricing(t *testing.T) {
  884. windowStart := time.Date(2020, time.April, 13, 0, 0, 0, 0, time.UTC)
  885. windowEnd := windowStart.Add(time.Hour)
  886. window := opencost.NewClosedWindow(windowStart, windowEnd)
  887. startTimestamp := float64(windowStart.Unix())
  888. pvCostPromResult := []*source.QueryResult{
  889. source.NewQueryResult(
  890. map[string]interface{}{
  891. "cluster_id": "cluster1",
  892. "persistentvolume": "pvc1",
  893. "provider_id": "provider1",
  894. },
  895. []*util.Vector{
  896. {
  897. Timestamp: startTimestamp,
  898. Value: 1.0,
  899. },
  900. },
  901. source.DefaultResultKeys(),
  902. ),
  903. }
  904. pvSizePromResult := []*source.QueryResult{
  905. source.NewQueryResult(
  906. map[string]interface{}{
  907. "cluster_id": "cluster1",
  908. "persistentvolume": "pvc1",
  909. "provider_id": "provider1",
  910. },
  911. []*util.Vector{
  912. {
  913. Timestamp: startTimestamp,
  914. Value: 1073741824.0,
  915. },
  916. },
  917. source.DefaultResultKeys(),
  918. ),
  919. }
  920. pvMinsPromResult := []*source.QueryResult{
  921. source.NewQueryResult(
  922. map[string]interface{}{
  923. "cluster_id": "cluster1",
  924. "persistentvolume": "pvc1",
  925. "provider_id": "provider1",
  926. },
  927. []*util.Vector{
  928. {
  929. Timestamp: startTimestamp,
  930. Value: 1.0,
  931. },
  932. {
  933. Timestamp: startTimestamp + (60.0 * 60.0),
  934. Value: 1.0,
  935. },
  936. },
  937. source.DefaultResultKeys(),
  938. ),
  939. }
  940. pvAvgUsagePromResult := []*source.QueryResult{
  941. source.NewQueryResult(
  942. map[string]interface{}{
  943. "cluster_id": "cluster1",
  944. "persistentvolumeclaim": "pv-claim1",
  945. "namespace": "ns1",
  946. },
  947. []*util.Vector{
  948. {
  949. Timestamp: startTimestamp,
  950. Value: 1.0,
  951. },
  952. {
  953. Timestamp: startTimestamp + (60.0 * 60.0),
  954. Value: 1.0,
  955. },
  956. },
  957. source.DefaultResultKeys(),
  958. ),
  959. }
  960. pvMaxUsagePromResult := []*source.QueryResult{
  961. source.NewQueryResult(
  962. map[string]interface{}{
  963. "cluster_id": "cluster1",
  964. "persistentvolumeclaim": "pv-claim1",
  965. "namespace": "ns1",
  966. },
  967. []*util.Vector{
  968. {
  969. Timestamp: startTimestamp,
  970. Value: 1.0,
  971. },
  972. {
  973. Timestamp: startTimestamp + (60.0 * 60.0),
  974. Value: 1.0,
  975. },
  976. },
  977. source.DefaultResultKeys(),
  978. ),
  979. }
  980. pvInfoPromResult := []*source.QueryResult{
  981. source.NewQueryResult(
  982. map[string]interface{}{
  983. "cluster_id": "cluster1",
  984. "persistentvolumeclaim": "pv-claim1",
  985. "volumename": "pvc1",
  986. "namespace": "ns1",
  987. },
  988. []*util.Vector{
  989. {
  990. Timestamp: startTimestamp,
  991. Value: 1.0,
  992. },
  993. },
  994. source.DefaultResultKeys(),
  995. ),
  996. }
  997. gpuCountMap := map[NodeIdentifier]float64{
  998. {
  999. Cluster: "cluster1",
  1000. Name: "node1",
  1001. ProviderID: "provider1",
  1002. }: 2,
  1003. }
  1004. nodeKey := NodeIdentifier{
  1005. Cluster: "cluster1",
  1006. Name: "node1",
  1007. ProviderID: "provider1",
  1008. }
  1009. cases := []struct {
  1010. name string
  1011. customPricingMap map[string]string
  1012. expectedPricing map[string]float64
  1013. zeroCollector bool // If true, simulate collector returning 0 (promless mode)
  1014. }{
  1015. {
  1016. name: "No custom pricing",
  1017. customPricingMap: map[string]string{},
  1018. expectedPricing: map[string]float64{
  1019. "CPU": 0.5,
  1020. "RAM": 0.5 / 1024.0 / 1024.0 / 1024.0,
  1021. "GPU": 1.0,
  1022. "Storage": 1.0,
  1023. },
  1024. zeroCollector: false,
  1025. },
  1026. {
  1027. name: "Custom pricing enabled",
  1028. customPricingMap: map[string]string{
  1029. "CPU": "20.0",
  1030. "RAM": "4.0",
  1031. "GPU": "500.0",
  1032. "Storage": "0.1",
  1033. "customPricesEnabled": "true",
  1034. },
  1035. expectedPricing: map[string]float64{
  1036. "CPU": 0.027397, // 20.0 / 730
  1037. "RAM": 5.102716386318207e-12, // 4.0 / 730 / 1024^3
  1038. "GPU": 1.369864, // 500.0 / 730 * 2
  1039. "Storage": 0.000137, // 0.1 / 730 * (1073741824.0 / 1024 / 1024 / 1024) * (60 / 60) => 0.1 / 730 * 1 * 1
  1040. },
  1041. zeroCollector: false,
  1042. },
  1043. {
  1044. name: "Collector returns 0, fallback to custom pricing",
  1045. customPricingMap: map[string]string{
  1046. "CPU": "20.0",
  1047. "RAM": "4.0",
  1048. "GPU": "500.0",
  1049. "Storage": "0.1",
  1050. // NOTE: customPricesEnabled is NOT set to "true"
  1051. // This tests the fallback behavior when collector returns 0
  1052. },
  1053. expectedPricing: map[string]float64{
  1054. "CPU": 0.027397, // 20.0 / 730 (fallback from 0)
  1055. "RAM": 5.102716386318207e-12, // 4.0 / 730 / 1024^3 (fallback from 0)
  1056. "GPU": 0.0, // GPU doesn't have fallback logic
  1057. "Storage": 1.0, // Storage uses separate PV pricing (pvCostPromResult), not affected by node pricing
  1058. },
  1059. zeroCollector: true,
  1060. },
  1061. }
  1062. for _, testCase := range cases {
  1063. t.Run(testCase.name, func(t *testing.T) {
  1064. testProvider := &provider.CustomProvider{
  1065. Config: provider.NewProviderConfig(config.NewConfigFileManager(nil), ""),
  1066. }
  1067. testProvider.UpdateConfigFromConfigMap(testCase.customPricingMap)
  1068. // Create test data - if zeroCollector is true, simulate collector returning 0
  1069. testValue := 0.5
  1070. if testCase.zeroCollector {
  1071. testValue = 0.0
  1072. }
  1073. zeroCollectorPromResult := []*source.QueryResult{
  1074. source.NewQueryResult(
  1075. map[string]interface{}{
  1076. "cluster_id": "cluster1",
  1077. "node": "node1",
  1078. "instance_type": "type1",
  1079. "provider_id": "provider1",
  1080. },
  1081. []*util.Vector{
  1082. {
  1083. Timestamp: startTimestamp,
  1084. Value: testValue,
  1085. },
  1086. },
  1087. source.DefaultResultKeys(),
  1088. ),
  1089. }
  1090. testPreemptible := make(map[NodeIdentifier]bool)
  1091. nodeCpuResult := source.DecodeAll(zeroCollectorPromResult, source.DecodeNodeCPUPricePerHrResult)
  1092. nodeRamResult := source.DecodeAll(zeroCollectorPromResult, source.DecodeNodeRAMPricePerGiBHrResult)
  1093. nodeGpuResult := source.DecodeAll(zeroCollectorPromResult, source.DecodeNodeGPUPricePerHrResult)
  1094. cpuMap, _ := buildCPUCostMap(nodeCpuResult, testProvider, testPreemptible)
  1095. ramMap, _ := buildRAMCostMap(nodeRamResult, testProvider, testPreemptible)
  1096. gpuMap, _ := buildGPUCostMap(nodeGpuResult, gpuCountMap, testProvider, testPreemptible)
  1097. cpuResult := cpuMap[nodeKey]
  1098. ramResult := ramMap[nodeKey]
  1099. gpuResult := gpuMap[nodeKey]
  1100. diskMap := map[DiskIdentifier]*Disk{}
  1101. pvMinsResult := source.DecodeAll(pvMinsPromResult, source.DecodePVActiveMinutesResult)
  1102. pvSizeResult := source.DecodeAll(pvSizePromResult, source.DecodePVBytesResult)
  1103. pvCostResult := source.DecodeAll(pvCostPromResult, source.DecodePVPricePerGiBHourResult)
  1104. pvUsedAvgResult := source.DecodeAll(pvAvgUsagePromResult, source.DecodePVUsedAvgResult)
  1105. pvMaxUsageResult := source.DecodeAll(pvMaxUsagePromResult, source.DecodePVUsedMaxResult)
  1106. pvcInfoResult := source.DecodeAll(pvInfoPromResult, source.DecodePVCInfoResult)
  1107. pvCosts(diskMap, time.Hour, pvMinsResult, pvSizeResult, pvCostResult, pvUsedAvgResult, pvMaxUsageResult, pvcInfoResult, testProvider, window)
  1108. diskResult := diskMap[DiskIdentifier{"cluster1", "pvc1"}].Cost
  1109. if !util.IsApproximately(cpuResult, testCase.expectedPricing["CPU"]) {
  1110. t.Errorf("CPU custom pricing error in %s. Got %v but expected %v", testCase.name, cpuResult, testCase.expectedPricing["CPU"])
  1111. }
  1112. if !util.IsApproximately(ramResult, testCase.expectedPricing["RAM"]) {
  1113. t.Errorf("RAM custom pricing error in %s. Got %v but expected %v", testCase.name, ramResult, testCase.expectedPricing["RAM"])
  1114. }
  1115. if !util.IsApproximately(gpuResult, testCase.expectedPricing["GPU"]) {
  1116. t.Errorf("GPU custom pricing error in %s. Got %v but expected %v", testCase.name, gpuResult, testCase.expectedPricing["GPU"])
  1117. }
  1118. if !util.IsApproximately(diskResult, testCase.expectedPricing["Storage"]) {
  1119. t.Errorf("Disk custom pricing error in %s. Got %v but expected %v", testCase.name, diskResult, testCase.expectedPricing["Storage"])
  1120. }
  1121. })
  1122. }
  1123. }
  1124. func TestBuildLabelsMap(t *testing.T) {
  1125. const (
  1126. labelKey1 = "testlabelkey1"
  1127. labelValue1 = "testlabel1-value"
  1128. labelKey2 = "test-label-key-2"
  1129. labelValue2 = "testlabel2.value"
  1130. nonLabelKey = "instance_type"
  1131. labelPrefix = "label_"
  1132. )
  1133. startTimestamp := float64(windowStart.Unix())
  1134. nodePromResult := []*source.QueryResult{
  1135. source.NewQueryResult(
  1136. map[string]interface{}{
  1137. "cluster_id": "cluster1",
  1138. "node": "node1",
  1139. "instance_type": "type1",
  1140. "provider_id": "provider1",
  1141. "label_testlabelkey1": "testlabel1-value",
  1142. "label_test-label-key-2": "testlabel2.value",
  1143. },
  1144. []*util.Vector{
  1145. {
  1146. Timestamp: startTimestamp,
  1147. Value: 0.5,
  1148. },
  1149. },
  1150. source.DefaultResultKeys(),
  1151. ),
  1152. source.NewQueryResult(
  1153. map[string]interface{}{
  1154. "cluster_id": "cluster1",
  1155. "node": "node2",
  1156. "instance_type": "type1",
  1157. "provider_id": "provider1",
  1158. "label_testlabelkey1": "testlabel1-value",
  1159. "label_test-label-key-2": "testlabel2.value",
  1160. },
  1161. []*util.Vector{
  1162. {
  1163. Timestamp: startTimestamp,
  1164. Value: 0.5,
  1165. },
  1166. },
  1167. source.DefaultResultKeys(),
  1168. ),
  1169. }
  1170. nodeLabelsResult := source.DecodeAll(nodePromResult, source.DecodeNodeLabelsResult)
  1171. nodeLabelMap := buildLabelsMap(nodeLabelsResult)
  1172. // Test that for all nodes and all label keys in the map there isn't a key with the label_ prefix.
  1173. for _, labelMap := range nodeLabelMap {
  1174. for key, value := range labelMap {
  1175. if strings.HasPrefix(key, labelPrefix) {
  1176. t.Errorf("Asset label maps aren't sanitized. Expected no '%v' prefix in %v", labelPrefix, key)
  1177. }
  1178. // Test that the label value isn't touched
  1179. if key == labelKey1 && value != labelValue1 {
  1180. t.Errorf("Label Value didn't match. Got %v, but Expected: %v", value, labelValue1)
  1181. }
  1182. // Test that the label value isn't touched
  1183. if key == labelKey2 && value != labelValue2 {
  1184. t.Errorf("Label Value didn't match. Got %v, but Expected: %v", value, labelValue2)
  1185. }
  1186. }
  1187. // Test that keys that don't have the label_ prefix aren't in the resultant label map.
  1188. _, ok := labelMap[nonLabelKey]
  1189. if ok {
  1190. t.Errorf("Non-label keys are included in label mapping for asset labels. Expected '%v' to not exist'.", nonLabelKey)
  1191. }
  1192. }
  1193. }