mock.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. package kubemodel
  2. import (
  3. "time"
  4. "github.com/opencost/opencost/core/pkg/model/shared"
  5. )
  6. // NewMockKubeModelSet returns a KubeModelSet populated with one instance of every
  7. // type. All time values are derived from start/end so the caller controls
  8. // monotonic-clock safety (use Truncate before passing in).
  9. func NewMockKubeModelSet(start, end time.Time) *KubeModelSet {
  10. kms := NewKubeModelSet(start, end)
  11. // --- Cluster ---
  12. kms.RegisterCluster(&Cluster{
  13. UID: "cluster-uid",
  14. Provider: shared.ProviderAWS,
  15. Account: "123456789012",
  16. Name: "test-cluster",
  17. Region: "us-east-1",
  18. Start: start,
  19. End: end,
  20. })
  21. // --- Namespace ---
  22. kms.RegisterNamespace(&Namespace{
  23. UID: "ns-uid",
  24. Name: "default",
  25. Labels: map[string]string{"env": "test"},
  26. Annotations: map[string]string{"note": "mock"},
  27. Start: start,
  28. End: end,
  29. })
  30. // --- ResourceQuota ---
  31. kms.RegisterResourceQuota(&ResourceQuota{
  32. UID: "rq-uid",
  33. NamespaceUID: "ns-uid",
  34. Name: "default-quota",
  35. Spec: &ResourceQuotaSpec{
  36. Hard: &ResourceQuotaSpecHard{
  37. Requests: ResourceQuantities{
  38. ResourceCPU: {Resource: ResourceCPU, Unit: UnitMillicore, Values: Stats{StatAvg: 4000, StatMax: 4000}},
  39. ResourceMemory: {Resource: ResourceMemory, Unit: UnitByte, Values: Stats{StatAvg: 8e9, StatMax: 8e9}},
  40. },
  41. Limits: ResourceQuantities{
  42. ResourceCPU: {Resource: ResourceCPU, Unit: UnitMillicore, Values: Stats{StatAvg: 8000, StatMax: 8000}},
  43. ResourceMemory: {Resource: ResourceMemory, Unit: UnitByte, Values: Stats{StatAvg: 16e9, StatMax: 16e9}},
  44. },
  45. },
  46. },
  47. Status: &ResourceQuotaStatus{
  48. Used: &ResourceQuotaStatusUsed{
  49. Requests: ResourceQuantities{
  50. ResourceCPU: {Resource: ResourceCPU, Unit: UnitMillicore, Values: Stats{StatAvg: 500, StatMax: 800}},
  51. ResourceMemory: {Resource: ResourceMemory, Unit: UnitByte, Values: Stats{StatAvg: 1e9, StatMax: 2e9}},
  52. },
  53. },
  54. },
  55. Start: start,
  56. End: end,
  57. })
  58. // --- Node ---
  59. kms.RegisterNode(&Node{
  60. UID: "node-uid",
  61. ProviderID: "aws:///us-east-1a/i-0abc123def456",
  62. Name: "node-1",
  63. Labels: map[string]string{"node.kubernetes.io/instance-type": "m5.large"},
  64. ResourceCapacities: ResourceQuantities{
  65. ResourceCPU: {Resource: ResourceCPU, Unit: UnitMillicore, Values: Stats{StatAvg: 2000, StatMax: 2000}},
  66. ResourceMemory: {Resource: ResourceMemory, Unit: UnitByte, Values: Stats{StatAvg: 8e9, StatMax: 8e9}},
  67. },
  68. ResourcesAllocatable: ResourceQuantities{
  69. ResourceCPU: {Resource: ResourceCPU, Unit: UnitMillicore, Values: Stats{StatAvg: 1900, StatMax: 1900}},
  70. ResourceMemory: {Resource: ResourceMemory, Unit: UnitByte, Values: Stats{StatAvg: 7e9, StatMax: 7e9}},
  71. },
  72. FileSystem: FileSystem{
  73. CapacityBytes: 100e9,
  74. UsageByteAvg: 40e9,
  75. UsageByteMax: 55e9,
  76. },
  77. Start: start,
  78. End: end,
  79. })
  80. // --- Pod ---
  81. kms.RegisterPod(&Pod{
  82. UID: "pod-uid",
  83. NamespaceUID: "ns-uid",
  84. NodeUID: "node-uid",
  85. Name: "my-pod-abc12",
  86. Owners: []Owner{{UID: "dep-uid", Kind: OwnerKindDeployment, Controller: true}},
  87. PVCVolumes: []PodPVCVolume{{Name: "data", PersistentVolumeClaimUID: "pvc-uid"}},
  88. Labels: map[string]string{"app": "my-app", "version": "v1"},
  89. Annotations: map[string]string{"prometheus.io/scrape": "true"},
  90. NetworkTrafficDetails: []NetworkTrafficDetail{
  91. {
  92. PodUID: "pod-uid",
  93. Endpoint: "10.0.0.5:443",
  94. TrafficDirection: TrafficDirectionEgress,
  95. TrafficType: TrafficTypeCrossZone,
  96. IsNatGateway: false,
  97. Bytes: 1024,
  98. },
  99. },
  100. Start: start,
  101. End: end,
  102. })
  103. // --- Container ---
  104. kms.RegisterContainer(&Container{
  105. PodUID: "pod-uid",
  106. Name: "app",
  107. ResourceRequests: ResourceQuantities{
  108. ResourceCPU: {Resource: ResourceCPU, Unit: UnitMillicore, Values: Stats{StatAvg: 250, StatMax: 250}},
  109. ResourceMemory: {Resource: ResourceMemory, Unit: UnitByte, Values: Stats{StatAvg: 512e6, StatMax: 512e6}},
  110. },
  111. ResourceLimits: ResourceQuantities{
  112. ResourceCPU: {Resource: ResourceCPU, Unit: UnitMillicore, Values: Stats{StatAvg: 500, StatMax: 500}},
  113. ResourceMemory: {Resource: ResourceMemory, Unit: UnitByte, Values: Stats{StatAvg: 1e9, StatMax: 1e9}},
  114. },
  115. CPUCoresAllocated: 0.25,
  116. CPUCoreUsageAvg: 0.18,
  117. CPUCoreUsageMax: 0.42,
  118. RAMBytesAllocated: 512e6,
  119. RAMBytesUsageAvg: 300e6,
  120. RAMBytesUsageMax: 480e6,
  121. Start: start,
  122. End: end,
  123. })
  124. // --- Deployment ---
  125. kms.RegisterDeployment(&Deployment{
  126. UID: "dep-uid",
  127. NamespaceUID: "ns-uid",
  128. Name: "my-app",
  129. Labels: map[string]string{"app": "my-app"},
  130. Annotations: map[string]string{"deployment.kubernetes.io/revision": "3"},
  131. MatchLabels: map[string]string{"app": "my-app"},
  132. Start: start,
  133. End: end,
  134. })
  135. // --- StatefulSet ---
  136. kms.RegisterStatefulSet(&StatefulSet{
  137. UID: "sts-uid",
  138. NamespaceUID: "ns-uid",
  139. Name: "my-statefulset",
  140. Labels: map[string]string{"app": "my-statefulset"},
  141. Annotations: map[string]string{"note": "test"},
  142. MatchLabels: map[string]string{"app": "my-statefulset"},
  143. Start: start,
  144. End: end,
  145. })
  146. // --- DaemonSet ---
  147. kms.RegisterDaemonSet(&DaemonSet{
  148. UID: "ds-uid",
  149. NamespaceUID: "ns-uid",
  150. Name: "my-daemonset",
  151. Labels: map[string]string{"app": "my-daemonset"},
  152. Annotations: map[string]string{"note": "test"},
  153. Start: start,
  154. End: end,
  155. })
  156. // --- Job ---
  157. kms.RegisterJob(&Job{
  158. UID: "job-uid",
  159. NamespaceUID: "ns-uid",
  160. Name: "my-job",
  161. Labels: map[string]string{"app": "my-job"},
  162. Annotations: map[string]string{"note": "test"},
  163. Start: start,
  164. End: end,
  165. })
  166. // --- CronJob ---
  167. kms.RegisterCronJob(&CronJob{
  168. UID: "cj-uid",
  169. NamespaceUID: "ns-uid",
  170. Name: "my-cronjob",
  171. Labels: map[string]string{"app": "my-cronjob"},
  172. Annotations: map[string]string{"note": "test"},
  173. Start: start,
  174. End: end,
  175. })
  176. // --- ReplicaSet ---
  177. kms.RegisterReplicaSet(&ReplicaSet{
  178. UID: "rs-uid",
  179. NamespaceUID: "ns-uid",
  180. Name: "my-app-7d9f8b",
  181. Owners: []Owner{{UID: "dep-uid", Kind: OwnerKindDeployment, Controller: true}},
  182. Labels: map[string]string{"app": "my-app", "pod-template-hash": "7d9f8b"},
  183. Annotations: map[string]string{"note": "test"},
  184. Start: start,
  185. End: end,
  186. })
  187. // --- Service ---
  188. kms.RegisterService(&Service{
  189. UID: "svc-uid",
  190. NamespaceUID: "ns-uid",
  191. Name: "my-app",
  192. Type: ServiceTypeClusterIP,
  193. Selector: map[string]string{"app": "my-app"},
  194. Start: start,
  195. End: end,
  196. })
  197. // --- PersistentVolumeClaim ---
  198. kms.RegisterPVC(&PersistentVolumeClaim{
  199. UID: "pvc-uid",
  200. NamespaceUID: "ns-uid",
  201. Name: "data",
  202. PersistentVolumeUID: "pv-uid",
  203. StorageClass: "gp2",
  204. RequestedBytes: 50e9,
  205. UsageBytesAvg: 20e9,
  206. UsageBytesMax: 35e9,
  207. Start: start,
  208. End: end,
  209. })
  210. // --- PersistentVolume ---
  211. kms.RegisterPersistentVolume(&PersistentVolume{
  212. UID: "pv-uid",
  213. Name: "pvc-abc123",
  214. StorageClass: "gp2",
  215. CSIVolumeHandle: "vol-0abc123def456789",
  216. SizeBytes: 50e9,
  217. Start: start,
  218. End: end,
  219. })
  220. // --- DCGMDevice ---
  221. kms.RegisterDCGMDevice(&DCGMDevice{
  222. UUID: "GPU-abc123def-456-789",
  223. Device: "0",
  224. ModelName: "Tesla T4",
  225. PodUsages: map[string]DCGMPod{
  226. "pod-uid": {
  227. ContainerUsages: map[string]DCGMContainer{
  228. "app": {UsageAvg: 0.65, UsageMax: 0.92},
  229. },
  230. },
  231. },
  232. Start: start,
  233. End: end,
  234. })
  235. // --- Diagnostics ---
  236. kms.Error(errMock("mock error"))
  237. kms.Warn("mock warning")
  238. kms.Info("mock info")
  239. return kms
  240. }
  241. // errMock is a minimal error implementation that avoids importing errors/fmt in the mock.
  242. type errMock string
  243. func (e errMock) Error() string { return string(e) }