mock.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. package kubecost
  2. import (
  3. "fmt"
  4. "strconv"
  5. "time"
  6. )
  7. const gb = 1024 * 1024 * 1024
  8. const day = 24 * time.Hour
  9. var disk = PVKey{
  10. Cluster: "cluster1",
  11. Name: "pv1",
  12. }
  13. // NewMockUnitAllocation creates an *Allocation with all of its float64 values set to 1 and generic properties if not provided in arg
  14. func NewMockUnitAllocation(name string, start time.Time, resolution time.Duration, props *AllocationProperties) *Allocation {
  15. if name == "" {
  16. name = "cluster1/namespace1/pod1/container1"
  17. }
  18. properties := &AllocationProperties{}
  19. if props == nil {
  20. properties.Cluster = "cluster1"
  21. properties.Node = "node1"
  22. properties.Namespace = "namespace1"
  23. properties.ControllerKind = "deployment"
  24. properties.Controller = "deployment1"
  25. properties.Pod = "pod1"
  26. properties.Container = "container1"
  27. } else {
  28. properties = props
  29. }
  30. end := start.Add(resolution)
  31. alloc := &Allocation{
  32. Name: name,
  33. Properties: properties,
  34. Window: NewWindow(&start, &end).Clone(),
  35. Start: start,
  36. End: end,
  37. CPUCoreHours: 1,
  38. CPUCost: 1,
  39. CPUCoreRequestAverage: 1,
  40. CPUCoreUsageAverage: 1,
  41. GPUHours: 1,
  42. GPUCost: 1,
  43. NetworkCost: 1,
  44. LoadBalancerCost: 1,
  45. PVs: PVAllocations{
  46. disk: {
  47. ByteHours: 1,
  48. Cost: 1,
  49. },
  50. },
  51. RAMByteHours: 1,
  52. RAMCost: 1,
  53. RAMBytesRequestAverage: 1,
  54. RAMBytesUsageAverage: 1,
  55. RawAllocationOnly: &RawAllocationOnlyData{
  56. CPUCoreUsageMax: 1,
  57. RAMBytesUsageMax: 1,
  58. },
  59. }
  60. // If idle allocation, remove non-idle costs, but maintain total cost
  61. if alloc.IsIdle() {
  62. alloc.PVs = nil
  63. alloc.NetworkCost = 0.0
  64. alloc.LoadBalancerCost = 0.0
  65. alloc.CPUCoreHours += 1.0
  66. alloc.CPUCost += 1.0
  67. alloc.RAMByteHours += 1.0
  68. alloc.RAMCost += 1.0
  69. }
  70. return alloc
  71. }
  72. // GenerateMockAllocationSetClusterIdle creates generic allocation set which includes an idle set broken down by cluster
  73. func GenerateMockAllocationSetClusterIdle(start time.Time) *AllocationSet {
  74. // Cluster Idle allocations
  75. a1i := NewMockUnitAllocation(fmt.Sprintf("cluster1/%s", IdleSuffix), start, day, &AllocationProperties{
  76. Cluster: "cluster1",
  77. })
  78. a1i.CPUCost = 5.0
  79. a1i.RAMCost = 15.0
  80. a1i.GPUCost = 0.0
  81. a2i := NewMockUnitAllocation(fmt.Sprintf("cluster2/%s", IdleSuffix), start, day, &AllocationProperties{
  82. Cluster: "cluster2",
  83. })
  84. a2i.CPUCost = 5.0
  85. a2i.RAMCost = 5.0
  86. a2i.GPUCost = 0.0
  87. as := GenerateMockAllocationSet(start)
  88. as.Insert(a1i)
  89. as.Insert(a2i)
  90. return as
  91. }
  92. // GenerateMockAllocationSetNodeIdle creates generic allocation set which includes an idle set broken down by node
  93. func GenerateMockAllocationSetNodeIdle(start time.Time) *AllocationSet {
  94. // Node Idle allocations
  95. a11i := NewMockUnitAllocation(fmt.Sprintf("c1nodes/%s", IdleSuffix), start, day, &AllocationProperties{
  96. Cluster: "cluster1",
  97. Node: "c1nodes",
  98. ProviderID: "c1nodes",
  99. })
  100. a11i.CPUCost = 5.0
  101. a11i.RAMCost = 15.0
  102. a11i.GPUCost = 0.0
  103. a21i := NewMockUnitAllocation(fmt.Sprintf("node1/%s", IdleSuffix), start, day, &AllocationProperties{
  104. Cluster: "cluster2",
  105. Node: "node1",
  106. ProviderID: "node1",
  107. })
  108. a21i.CPUCost = 1.666667
  109. a21i.RAMCost = 1.666667
  110. a21i.GPUCost = 0.0
  111. a22i := NewMockUnitAllocation(fmt.Sprintf("node2/%s", IdleSuffix), start, day, &AllocationProperties{
  112. Cluster: "cluster2",
  113. Node: "node2",
  114. ProviderID: "node2",
  115. })
  116. a22i.CPUCost = 1.666667
  117. a22i.RAMCost = 1.666667
  118. a22i.GPUCost = 0.0
  119. a23i := NewMockUnitAllocation(fmt.Sprintf("node3/%s", IdleSuffix), start, day, &AllocationProperties{
  120. Cluster: "cluster2",
  121. Node: "node3",
  122. ProviderID: "node3",
  123. Namespace: "",
  124. })
  125. a23i.CPUCost = 1.666667
  126. a23i.RAMCost = 1.666667
  127. a23i.GPUCost = 0.0
  128. as := GenerateMockAllocationSet(start)
  129. as.Insert(a11i)
  130. as.Insert(a21i)
  131. as.Insert(a22i)
  132. as.Insert(a23i)
  133. return as
  134. }
  135. // GenerateMockAllocationSet creates generic allocation set without idle allocations
  136. func GenerateMockAllocationSet(start time.Time) *AllocationSet {
  137. // Active allocations
  138. a1111 := NewMockUnitAllocation("cluster1/namespace1/pod1/container1", start, day, &AllocationProperties{
  139. Cluster: "cluster1",
  140. Namespace: "namespace1",
  141. Pod: "pod1",
  142. Container: "container1",
  143. ProviderID: "c1nodes",
  144. Node: "c1nodes",
  145. })
  146. a1111.RAMCost = 11.00
  147. a11abc2 := NewMockUnitAllocation("cluster1/namespace1/pod-abc/container2", start, day, &AllocationProperties{
  148. Cluster: "cluster1",
  149. Namespace: "namespace1",
  150. Pod: "pod-abc",
  151. Container: "container2",
  152. ProviderID: "c1nodes",
  153. Node: "c1nodes",
  154. })
  155. a11def3 := NewMockUnitAllocation("cluster1/namespace1/pod-def/container3", start, day, &AllocationProperties{
  156. Cluster: "cluster1",
  157. Namespace: "namespace1",
  158. Pod: "pod-def",
  159. Container: "container3",
  160. ProviderID: "c1nodes",
  161. Node: "c1nodes",
  162. })
  163. a12ghi4 := NewMockUnitAllocation("cluster1/namespace2/pod-ghi/container4", start, day, &AllocationProperties{
  164. Cluster: "cluster1",
  165. Namespace: "namespace2",
  166. Pod: "pod-ghi",
  167. Container: "container4",
  168. ProviderID: "c1nodes",
  169. Node: "c1nodes",
  170. })
  171. a12ghi5 := NewMockUnitAllocation("cluster1/namespace2/pod-ghi/container5", start, day, &AllocationProperties{
  172. Cluster: "cluster1",
  173. Namespace: "namespace2",
  174. Pod: "pod-ghi",
  175. Container: "container5",
  176. ProviderID: "c1nodes",
  177. Node: "c1nodes",
  178. })
  179. a12jkl6 := NewMockUnitAllocation("cluster1/namespace2/pod-jkl/container6", start, day, &AllocationProperties{
  180. Cluster: "cluster1",
  181. Namespace: "namespace2",
  182. Pod: "pod-jkl",
  183. Container: "container6",
  184. ProviderID: "c1nodes",
  185. Node: "c1nodes",
  186. })
  187. a22mno4 := NewMockUnitAllocation("cluster2/namespace2/pod-mno/container4", start, day, &AllocationProperties{
  188. Cluster: "cluster2",
  189. Namespace: "namespace2",
  190. Pod: "pod-mno",
  191. Container: "container4",
  192. ProviderID: "node1",
  193. Node: "node1",
  194. })
  195. a22mno5 := NewMockUnitAllocation("cluster2/namespace2/pod-mno/container5", start, day, &AllocationProperties{
  196. Cluster: "cluster2",
  197. Namespace: "namespace2",
  198. Pod: "pod-mno",
  199. Container: "container5",
  200. ProviderID: "node1",
  201. Node: "node1",
  202. })
  203. a22pqr6 := NewMockUnitAllocation("cluster2/namespace2/pod-pqr/container6", start, day, &AllocationProperties{
  204. Cluster: "cluster2",
  205. Namespace: "namespace2",
  206. Pod: "pod-pqr",
  207. Container: "container6",
  208. ProviderID: "node2",
  209. Node: "node2",
  210. })
  211. a23stu7 := NewMockUnitAllocation("cluster2/namespace3/pod-stu/container7", start, day, &AllocationProperties{
  212. Cluster: "cluster2",
  213. Namespace: "namespace3",
  214. Pod: "pod-stu",
  215. Container: "container7",
  216. ProviderID: "node2",
  217. Node: "node2",
  218. })
  219. a23vwx8 := NewMockUnitAllocation("cluster2/namespace3/pod-vwx/container8", start, day, &AllocationProperties{
  220. Cluster: "cluster2",
  221. Namespace: "namespace3",
  222. Pod: "pod-vwx",
  223. Container: "container8",
  224. ProviderID: "node3",
  225. Node: "node3",
  226. })
  227. a23vwx9 := NewMockUnitAllocation("cluster2/namespace3/pod-vwx/container9", start, day, &AllocationProperties{
  228. Cluster: "cluster2",
  229. Namespace: "namespace3",
  230. Pod: "pod-vwx",
  231. Container: "container9",
  232. ProviderID: "node3",
  233. Node: "node3",
  234. })
  235. // Controllers
  236. a11abc2.Properties.ControllerKind = "deployment"
  237. a11abc2.Properties.Controller = "deployment1"
  238. a11def3.Properties.ControllerKind = "deployment"
  239. a11def3.Properties.Controller = "deployment1"
  240. a12ghi4.Properties.ControllerKind = "deployment"
  241. a12ghi4.Properties.Controller = "deployment2"
  242. a12ghi5.Properties.ControllerKind = "deployment"
  243. a12ghi5.Properties.Controller = "deployment2"
  244. a22mno4.Properties.ControllerKind = "deployment"
  245. a22mno4.Properties.Controller = "deployment2"
  246. a22mno5.Properties.ControllerKind = "deployment"
  247. a22mno5.Properties.Controller = "deployment2"
  248. a23stu7.Properties.ControllerKind = "deployment"
  249. a23stu7.Properties.Controller = "deployment3"
  250. a12jkl6.Properties.ControllerKind = "daemonset"
  251. a12jkl6.Properties.Controller = "daemonset1"
  252. a22pqr6.Properties.ControllerKind = "daemonset"
  253. a22pqr6.Properties.Controller = "daemonset1"
  254. a23vwx8.Properties.ControllerKind = "statefulset"
  255. a23vwx8.Properties.Controller = "statefulset1"
  256. a23vwx9.Properties.ControllerKind = "statefulset"
  257. a23vwx9.Properties.Controller = "statefulset1"
  258. // Labels
  259. a1111.Properties.Labels = map[string]string{"app": "app1", "env": "env1"}
  260. a12ghi4.Properties.Labels = map[string]string{"app": "app2", "env": "env2"}
  261. a12ghi5.Properties.Labels = map[string]string{"app": "app2", "env": "env2"}
  262. a22mno4.Properties.Labels = map[string]string{"app": "app2"}
  263. a22mno5.Properties.Labels = map[string]string{"app": "app2"}
  264. //Annotations
  265. a23stu7.Properties.Annotations = map[string]string{"team": "team1"}
  266. a23vwx8.Properties.Annotations = map[string]string{"team": "team2"}
  267. a23vwx9.Properties.Annotations = map[string]string{"team": "team1"}
  268. // Services
  269. a12jkl6.Properties.Services = []string{"service1"}
  270. a22pqr6.Properties.Services = []string{"service1"}
  271. return NewAllocationSet(start, start.Add(day),
  272. // cluster 1, namespace1
  273. a1111, a11abc2, a11def3,
  274. // cluster 1, namespace 2
  275. a12ghi4, a12ghi5, a12jkl6,
  276. // cluster 2, namespace 2
  277. a22mno4, a22mno5, a22pqr6,
  278. // cluster 2, namespace 3
  279. a23stu7, a23vwx8, a23vwx9,
  280. )
  281. }
  282. // GenerateMockAllocationSetWithAssetProperties with no idle and connections to Assets in properties
  283. func GenerateMockAllocationSetWithAssetProperties(start time.Time) *AllocationSet {
  284. as := GenerateMockAllocationSet(start)
  285. disk1 := PVKey{
  286. Cluster: "cluster2",
  287. Name: "disk1",
  288. }
  289. disk2 := PVKey{
  290. Cluster: "cluster2",
  291. Name: "disk2",
  292. }
  293. for _, a := range as.Allocations {
  294. // add reconcilable pvs to pod-mno
  295. if a.Properties.Pod == "pod-mno" {
  296. a.PVs = a.PVs.Add(PVAllocations{
  297. disk1: {
  298. Cost: 2.5,
  299. ByteHours: 2.5 * gb,
  300. },
  301. disk2: {
  302. Cost: 5,
  303. ByteHours: 5 * gb,
  304. },
  305. })
  306. }
  307. // add loadBalancer service to allocations
  308. if a.Name == "cluster2/namespace2/pod-mno/container4" {
  309. a.Properties.Services = append(a.Properties.Services, "loadBalancer1")
  310. }
  311. if a.Name == "cluster2/namespace2/pod-mno/container5" {
  312. a.Properties.Services = append(a.Properties.Services, "loadBalancer2")
  313. }
  314. if a.Name == "cluster2/namespace2/pod-pqr/container6" {
  315. a.Properties.Services = append(a.Properties.Services, "loadBalancer1")
  316. a.Properties.Services = append(a.Properties.Services, "loadBalancer2")
  317. }
  318. }
  319. return as
  320. }
  321. // GenerateMockAssetSets creates generic AssetSets
  322. func GenerateMockAssetSets(start, end time.Time) []*AssetSet {
  323. var assetSets []*AssetSet
  324. // Create an AssetSet representing cluster costs for two clusters (cluster1
  325. // and cluster2). Include Nodes and Disks for both, even though only
  326. // Nodes will be counted. Whereas in practice, Assets should be aggregated
  327. // by type, here we will provide multiple Nodes for one of the clusters to
  328. // make sure the function still holds.
  329. // NOTE: we're re-using GenerateMockAllocationSet so this has to line up with
  330. // the allocated node costs from that function. See table above.
  331. // | Hierarchy | Cost | CPU | RAM | GPU | Adjustment |
  332. // +-----------------------------------------+------+------+------+------+------------+
  333. // cluster1:
  334. // nodes 100.00 55.00 44.00 11.00 -10.00
  335. // +-----------------------------------------+------+------+------+------+------------+
  336. // cluster1 subtotal (adjusted) 100.00 50.00 40.00 10.00 0.00
  337. // +-----------------------------------------+------+------+------+------+------------+
  338. // cluster1 allocated 48.00 6.00 16.00 6.00 0.00
  339. // +-----------------------------------------+------+------+------+------+------------+
  340. // cluster1 idle 72.00 44.00 24.00 4.00 0.00
  341. // +-----------------------------------------+------+------+------+------+------------+
  342. // cluster2:
  343. // node1 35.00 20.00 15.00 0.00 0.00
  344. // node2 35.00 20.00 15.00 0.00 0.00
  345. // node3 30.00 10.00 10.00 10.00 0.00
  346. // (disks should not matter for idle)
  347. // +-----------------------------------------+------+------+------+------+------------+
  348. // cluster2 subtotal 100.00 50.00 40.00 10.00 0.00
  349. // +-----------------------------------------+------+------+------+------+------------+
  350. // cluster2 allocated 28.00 6.00 6.00 6.00 0.00
  351. // +-----------------------------------------+------+------+------+------+------------+
  352. // cluster2 idle 82.00 44.00 34.00 4.00 0.00
  353. // +-----------------------------------------+------+------+------+------+------------+
  354. cluster1Nodes := NewNode("c1nodes", "cluster1", "c1nodes", start, end, NewWindow(&start, &end))
  355. cluster1Nodes.CPUCost = 55.0
  356. cluster1Nodes.RAMCost = 44.0
  357. cluster1Nodes.GPUCost = 11.0
  358. cluster1Nodes.Adjustment = -10.00
  359. cluster1Nodes.CPUCoreHours = 8
  360. cluster1Nodes.RAMByteHours = 6
  361. cluster1Nodes.GPUHours = 24
  362. cluster2Node1 := NewNode("node1", "cluster2", "node1", start, end, NewWindow(&start, &end))
  363. cluster2Node1.CPUCost = 20.0
  364. cluster2Node1.RAMCost = 15.0
  365. cluster2Node1.GPUCost = 0.0
  366. cluster2Node1.CPUCoreHours = 4
  367. cluster2Node1.RAMByteHours = 3
  368. cluster2Node1.GPUHours = 0
  369. cluster2Node2 := NewNode("node2", "cluster2", "node2", start, end, NewWindow(&start, &end))
  370. cluster2Node2.CPUCost = 20.0
  371. cluster2Node2.RAMCost = 15.0
  372. cluster2Node2.GPUCost = 0.0
  373. cluster2Node2.CPUCoreHours = 3
  374. cluster2Node2.RAMByteHours = 2
  375. cluster2Node2.GPUHours = 0
  376. cluster2Node3 := NewNode("node3", "cluster2", "node3", start, end, NewWindow(&start, &end))
  377. cluster2Node3.CPUCost = 10.0
  378. cluster2Node3.RAMCost = 10.0
  379. cluster2Node3.GPUCost = 10.0
  380. cluster2Node3.CPUCoreHours = 2
  381. cluster2Node3.RAMByteHours = 2
  382. cluster2Node3.GPUHours = 24
  383. // Add PVs
  384. cluster2Disk1 := NewDisk("disk1", "cluster2", "disk1", start, end, NewWindow(&start, &end))
  385. cluster2Disk1.Cost = 5.0
  386. cluster2Disk1.Adjustment = 1.0
  387. cluster2Disk1.ByteHours = 5 * gb
  388. cluster2Disk2 := NewDisk("disk2", "cluster2", "disk2", start, end, NewWindow(&start, &end))
  389. cluster2Disk2.Cost = 10.0
  390. cluster2Disk2.Adjustment = 3.0
  391. cluster2Disk2.ByteHours = 10 * gb
  392. cluster2Node1Disk := NewDisk("node1", "cluster2", "node1", start, end, NewWindow(&start, &end))
  393. cluster2Node1Disk.Cost = 1.0
  394. cluster2Node1Disk.ByteHours = 5 * gb
  395. // Add Attached Disks
  396. cluster2Node2Disk := NewDisk("node2", "cluster2", "node2", start, end, NewWindow(&start, &end))
  397. cluster2Node2Disk.Cost = 2.0
  398. cluster2Node2Disk.ByteHours = 5 * gb
  399. cluster2Node3Disk := NewDisk("node3", "cluster2", "node3", start, end, NewWindow(&start, &end))
  400. cluster2Node3Disk.Cost = 3.0
  401. cluster2Node3Disk.ByteHours = 5 * gb
  402. // Add Cluster Management
  403. cluster1ClusterManagement := NewClusterManagement("", "cluster1", NewWindow(&start, &end))
  404. cluster1ClusterManagement.Cost = 2.0
  405. cluster2ClusterManagement := NewClusterManagement("", "cluster2", NewWindow(&start, &end))
  406. cluster2ClusterManagement.Cost = 2.0
  407. // Add Networks
  408. c1Network := NewNetwork("", "cluster1", "c1nodes", start, end, NewWindow(&start, &end))
  409. c1Network.Cost = 3.0
  410. node1Network := NewNetwork("node1", "cluster2", "node1", start, end, NewWindow(&start, &end))
  411. node1Network.Cost = 4.0
  412. node2Network := NewNetwork("node2", "cluster2", "node2", start, end, NewWindow(&start, &end))
  413. node2Network.Cost = 5.0
  414. node3Network := NewNetwork("node3", "cluster2", "node3", start, end, NewWindow(&start, &end))
  415. node3Network.Cost = 2.0
  416. // Add LoadBalancers
  417. cluster2LoadBalancer1 := NewLoadBalancer("namespace2/loadBalancer1", "cluster2", "lb1", start, end, NewWindow(&start, &end))
  418. cluster2LoadBalancer1.Cost = 10.0
  419. cluster2LoadBalancer2 := NewLoadBalancer("namespace2/loadBalancer2", "cluster2", "lb2", start, end, NewWindow(&start, &end))
  420. cluster2LoadBalancer2.Cost = 15.0
  421. assetSet1 := NewAssetSet(start, end, cluster1Nodes, cluster2Node1, cluster2Node2, cluster2Node3, cluster2Disk1,
  422. cluster2Disk2, cluster2Node1Disk, cluster2Node2Disk, cluster2Node3Disk, cluster1ClusterManagement,
  423. cluster2ClusterManagement, c1Network, node1Network, node2Network, node3Network, cluster2LoadBalancer1, cluster2LoadBalancer2)
  424. assetSets = append(assetSets, assetSet1)
  425. // NOTE: we're re-using GenerateMockAllocationSet so this has to line up with
  426. // the allocated node costs from that function. See table above.
  427. // | Hierarchy | Cost | CPU | RAM | GPU | Adjustment |
  428. // +-----------------------------------------+------+------+------+------+------------+
  429. // cluster1:
  430. // nodes 100.00 5.00 4.00 1.00 90.00
  431. // +-----------------------------------------+------+------+------+------+------------+
  432. // cluster1 subtotal (adjusted) 100.00 50.00 40.00 10.00 0.00
  433. // +-----------------------------------------+------+------+------+------+------------+
  434. // cluster1 allocated 48.00 6.00 16.00 6.00 0.00
  435. // +-----------------------------------------+------+------+------+------+------------+
  436. // cluster1 idle 72.00 44.00 24.00 4.00 0.00
  437. // +-----------------------------------------+------+------+------+------+------------+
  438. // cluster2:
  439. // node1 35.00 20.00 15.00 0.00 0.00
  440. // node2 35.00 20.00 15.00 0.00 0.00
  441. // node3 30.00 10.00 10.00 10.00 0.00
  442. // (disks should not matter for idle)
  443. // +-----------------------------------------+------+------+------+------+------------+
  444. // cluster2 subtotal 100.00 50.00 40.00 10.00 0.00
  445. // +-----------------------------------------+------+------+------+------+------------+
  446. // cluster2 allocated 28.00 6.00 6.00 6.00 0.00
  447. // +-----------------------------------------+------+------+------+------+------------+
  448. // cluster2 idle 82.00 44.00 34.00 4.00 0.00
  449. // +-----------------------------------------+------+------+------+------+------------+
  450. cluster1Nodes = NewNode("", "cluster1", "c1nodes", start, end, NewWindow(&start, &end))
  451. cluster1Nodes.CPUCost = 5.0
  452. cluster1Nodes.RAMCost = 4.0
  453. cluster1Nodes.GPUCost = 1.0
  454. cluster1Nodes.Adjustment = 90.00
  455. cluster1Nodes.CPUCoreHours = 8
  456. cluster1Nodes.RAMByteHours = 6
  457. cluster1Nodes.GPUHours = 24
  458. cluster2Node1 = NewNode("node1", "cluster2", "node1", start, end, NewWindow(&start, &end))
  459. cluster2Node1.CPUCost = 20.0
  460. cluster2Node1.RAMCost = 15.0
  461. cluster2Node1.GPUCost = 0.0
  462. cluster2Node1.CPUCoreHours = 4
  463. cluster2Node1.RAMByteHours = 3
  464. cluster2Node1.GPUHours = 0
  465. cluster2Node2 = NewNode("node2", "cluster2", "node2", start, end, NewWindow(&start, &end))
  466. cluster2Node2.CPUCost = 20.0
  467. cluster2Node2.RAMCost = 15.0
  468. cluster2Node2.GPUCost = 0.0
  469. cluster2Node2.CPUCoreHours = 3
  470. cluster2Node2.RAMByteHours = 2
  471. cluster2Node2.GPUHours = 0
  472. cluster2Node3 = NewNode("node3", "cluster2", "node3", start, end, NewWindow(&start, &end))
  473. cluster2Node3.CPUCost = 10.0
  474. cluster2Node3.RAMCost = 10.0
  475. cluster2Node3.GPUCost = 10.0
  476. cluster2Node3.CPUCoreHours = 2
  477. cluster2Node3.RAMByteHours = 2
  478. cluster2Node3.GPUHours = 24
  479. // Add PVs
  480. cluster2Disk1 = NewDisk("disk1", "cluster2", "disk1", start, end, NewWindow(&start, &end))
  481. cluster2Disk1.Cost = 5.0
  482. cluster2Disk1.Adjustment = 1.0
  483. cluster2Disk1.ByteHours = 5 * gb
  484. cluster2Disk2 = NewDisk("disk2", "cluster2", "disk2", start, end, NewWindow(&start, &end))
  485. cluster2Disk2.Cost = 12.0
  486. cluster2Disk2.Adjustment = 4.0
  487. cluster2Disk2.ByteHours = 20 * gb
  488. assetSet2 := NewAssetSet(start, end, cluster1Nodes, cluster2Node1, cluster2Node2, cluster2Node3, cluster2Disk1,
  489. cluster2Disk2, cluster2Node1Disk, cluster2Node2Disk, cluster2Node3Disk, cluster1ClusterManagement,
  490. cluster2ClusterManagement, c1Network, node1Network, node2Network, node3Network, cluster2LoadBalancer1, cluster2LoadBalancer2)
  491. assetSets = append(assetSets, assetSet2)
  492. return assetSets
  493. }
  494. // GenerateMockAssetSet generates the following topology:
  495. //
  496. // | Asset | Cost | Adj |
  497. // +------------------------------+------+------+
  498. //
  499. // cluster1:
  500. // node1: 6.00 1.00
  501. // node2: 4.00 1.50
  502. // node3: 7.00 -0.50
  503. // disk1: 2.50 0.00
  504. // disk2: 1.50 0.00
  505. // clusterManagement1: 3.00 0.00
  506. //
  507. // +------------------------------+------+------+
  508. //
  509. // cluster1 subtotal 24.00 2.00
  510. //
  511. // +------------------------------+------+------+
  512. //
  513. // cluster2:
  514. // node4: 12.00 -1.00
  515. // disk3: 2.50 0.00
  516. // disk4: 1.50 0.00
  517. // clusterManagement2: 0.00 0.00
  518. //
  519. // +------------------------------+------+------+
  520. //
  521. // cluster2 subtotal 16.00 -1.00
  522. //
  523. // +------------------------------+------+------+
  524. //
  525. // cluster3:
  526. // node5: 17.00 2.00
  527. //
  528. // +------------------------------+------+------+
  529. //
  530. // cluster3 subtotal 17.00 2.00
  531. //
  532. // +------------------------------+------+------+
  533. //
  534. // total 57.00 3.00
  535. //
  536. // +------------------------------+------+------+
  537. func GenerateMockAssetSet(start time.Time) *AssetSet {
  538. end := start.Add(day)
  539. window := NewWindow(&start, &end)
  540. hours := window.Duration().Hours()
  541. node1 := NewNode("node1", "cluster1", "gcp-node1", *window.Clone().start, *window.Clone().end, window.Clone())
  542. node1.CPUCost = 4.0
  543. node1.RAMCost = 4.0
  544. node1.GPUCost = 2.0
  545. node1.Discount = 0.5
  546. node1.CPUCoreHours = 2.0 * hours
  547. node1.RAMByteHours = 4.0 * gb * hours
  548. node1.GPUHours = 1.0 * hours
  549. node1.SetAdjustment(1.0)
  550. node1.SetLabels(map[string]string{"test": "test"})
  551. node2 := NewNode("node2", "cluster1", "gcp-node2", *window.Clone().start, *window.Clone().end, window.Clone())
  552. node2.CPUCost = 4.0
  553. node2.RAMCost = 4.0
  554. node2.GPUCost = 0.0
  555. node2.Discount = 0.5
  556. node2.CPUCoreHours = 2.0 * hours
  557. node2.RAMByteHours = 4.0 * gb * hours
  558. node2.GPUHours = 0.0 * hours
  559. node2.SetAdjustment(1.5)
  560. node3 := NewNode("node3", "cluster1", "gcp-node3", *window.Clone().start, *window.Clone().end, window.Clone())
  561. node3.CPUCost = 4.0
  562. node3.RAMCost = 4.0
  563. node3.GPUCost = 3.0
  564. node3.Discount = 0.5
  565. node3.CPUCoreHours = 2.0 * hours
  566. node3.RAMByteHours = 4.0 * gb * hours
  567. node3.GPUHours = 2.0 * hours
  568. node3.SetAdjustment(-0.5)
  569. node4 := NewNode("node4", "cluster2", "gcp-node4", *window.Clone().start, *window.Clone().end, window.Clone())
  570. node4.CPUCost = 10.0
  571. node4.RAMCost = 6.0
  572. node4.GPUCost = 0.0
  573. node4.Discount = 0.25
  574. node4.CPUCoreHours = 4.0 * hours
  575. node4.RAMByteHours = 12.0 * gb * hours
  576. node4.GPUHours = 0.0 * hours
  577. node4.SetAdjustment(-1.0)
  578. node5 := NewNode("node5", "cluster3", "aws-node5", *window.Clone().start, *window.Clone().end, window.Clone())
  579. node5.CPUCost = 10.0
  580. node5.RAMCost = 7.0
  581. node5.GPUCost = 0.0
  582. node5.Discount = 0.0
  583. node5.CPUCoreHours = 8.0 * hours
  584. node5.RAMByteHours = 24.0 * gb * hours
  585. node5.GPUHours = 0.0 * hours
  586. node5.SetAdjustment(2.0)
  587. disk1 := NewDisk("disk1", "cluster1", "gcp-disk1", *window.Clone().start, *window.Clone().end, window.Clone())
  588. disk1.Cost = 2.5
  589. disk1.ByteHours = 100 * gb * hours
  590. disk2 := NewDisk("disk2", "cluster1", "gcp-disk2", *window.Clone().start, *window.Clone().end, window.Clone())
  591. disk2.Cost = 1.5
  592. disk2.ByteHours = 60 * gb * hours
  593. disk3 := NewDisk("disk3", "cluster2", "gcp-disk3", *window.Clone().start, *window.Clone().end, window.Clone())
  594. disk3.Cost = 2.5
  595. disk3.ByteHours = 100 * gb * hours
  596. disk4 := NewDisk("disk4", "cluster2", "gcp-disk4", *window.Clone().start, *window.Clone().end, window.Clone())
  597. disk4.Cost = 1.5
  598. disk4.ByteHours = 100 * gb * hours
  599. cm1 := NewClusterManagement(GCPProvider, "cluster1", window.Clone())
  600. cm1.Cost = 3.0
  601. cm2 := NewClusterManagement(GCPProvider, "cluster2", window.Clone())
  602. cm2.Cost = 0.0
  603. return NewAssetSet(
  604. start, end,
  605. // cluster 1
  606. node1, node2, node3, disk1, disk2, cm1,
  607. // cluster 2
  608. node4, disk3, disk4, cm2,
  609. // cluster 3
  610. node5,
  611. )
  612. }
  613. func GenerateKubecostNodeAndPID(mockProviderIDInt int, provider string, mockClusterID int, setEndTime time.Time) (*Node, string) {
  614. providerID := "PID" + strconv.FormatInt(int64(mockProviderIDInt), 10)
  615. return &Node{
  616. Properties: &AssetProperties{
  617. Provider: provider,
  618. ProviderID: providerID,
  619. Cluster: "cluster" + strconv.FormatInt(int64(mockClusterID), 10),
  620. },
  621. End: setEndTime,
  622. }, providerID
  623. }
  624. func GenerateAWSMockCCIAndPID(mockProviderIDInt int, mockCloudIDInt int, labelKey string, resourceCategory string) (*CloudCostItem, string) {
  625. return &CloudCostItem{
  626. Properties: CloudCostItemProperties{
  627. ProviderID: "PID" + strconv.FormatInt(int64(mockProviderIDInt), 10),
  628. Provider: AWSProvider,
  629. Category: resourceCategory,
  630. Labels: map[string]string{
  631. labelKey: "cluster" + strconv.FormatInt(int64(mockCloudIDInt), 10),
  632. },
  633. },
  634. }, "cluster" + strconv.FormatInt(int64(mockCloudIDInt), 10)
  635. }
  636. func GenerateAlibabaMockCCIAndPID(mockProviderIDInt int, mockCloudIDInt int, labelKey string, resourceCategory string) (*CloudCostItem, string) {
  637. return &CloudCostItem{
  638. Properties: CloudCostItemProperties{
  639. ProviderID: "PID" + strconv.FormatInt(int64(mockProviderIDInt), 10),
  640. Provider: AlibabaProvider,
  641. Category: resourceCategory,
  642. Labels: map[string]string{
  643. labelKey: "cluster" + strconv.FormatInt(int64(mockCloudIDInt), 10),
  644. },
  645. },
  646. }, "cluster" + strconv.FormatInt(int64(mockCloudIDInt), 10)
  647. }
  648. func GenerateGCPMockCCIAndPID(mockProviderIDInt int, mockCloudIDInt int, labelKey string, resourceCategory string) (*CloudCostItem, string) {
  649. return &CloudCostItem{
  650. Properties: CloudCostItemProperties{
  651. ProviderID: "PID" + strconv.FormatInt(int64(mockProviderIDInt), 10),
  652. Provider: GCPProvider,
  653. Category: resourceCategory,
  654. },
  655. }, ""
  656. }