pod_test.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. package kubemodel
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/stretchr/testify/require"
  7. "github.com/opencost/opencost/core/pkg/model/kubemodel"
  8. "github.com/opencost/opencost/core/pkg/source"
  9. )
  10. func TestComputePods(t *testing.T) {
  11. start := time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC)
  12. end := start.Add(time.Hour)
  13. tests := []struct {
  14. name string
  15. overrides map[string]any
  16. want map[string]*kubemodel.Pod
  17. }{
  18. {
  19. name: "no data returns empty pod map",
  20. overrides: map[string]any{},
  21. want: map[string]*kubemodel.Pod{},
  22. },
  23. {
  24. name: "basic pod info and uptime",
  25. overrides: map[string]any{
  26. source.QueryPodInfo: []*source.PodInfoResult{
  27. {UID: "pod-1", Pod: "my-pod", NamespaceUID: "ns-1"},
  28. },
  29. source.QueryPodUptime: []*source.UptimeResult{
  30. {UID: "pod-1", First: start, Last: end},
  31. },
  32. },
  33. want: map[string]*kubemodel.Pod{
  34. "pod-1": {
  35. UID: "pod-1",
  36. Name: "my-pod",
  37. NamespaceUID: "ns-1",
  38. Start: start,
  39. End: end,
  40. },
  41. },
  42. },
  43. {
  44. name: "pod without uptime is not registered",
  45. overrides: map[string]any{
  46. source.QueryPodInfo: []*source.PodInfoResult{
  47. {UID: "pod-1", Pod: "my-pod", NamespaceUID: "ns-1"},
  48. },
  49. },
  50. want: map[string]*kubemodel.Pod{},
  51. },
  52. {
  53. name: "pod without namespace uid is not registered",
  54. overrides: map[string]any{
  55. source.QueryPodInfo: []*source.PodInfoResult{
  56. {UID: "pod-1", Pod: "my-pod"},
  57. },
  58. source.QueryPodUptime: []*source.UptimeResult{
  59. {UID: "pod-1", First: start, Last: end},
  60. },
  61. },
  62. want: map[string]*kubemodel.Pod{},
  63. },
  64. {
  65. name: "pod with node uid",
  66. overrides: map[string]any{
  67. source.QueryPodInfo: []*source.PodInfoResult{
  68. {UID: "pod-1", Pod: "my-pod", NamespaceUID: "ns-1", NodeUID: "node-1"},
  69. },
  70. source.QueryPodUptime: []*source.UptimeResult{
  71. {UID: "pod-1", First: start, Last: end},
  72. },
  73. },
  74. want: map[string]*kubemodel.Pod{
  75. "pod-1": {
  76. UID: "pod-1",
  77. Name: "my-pod",
  78. NamespaceUID: "ns-1",
  79. NodeUID: "node-1",
  80. Start: start,
  81. End: end,
  82. },
  83. },
  84. },
  85. {
  86. name: "pod owners are attached",
  87. overrides: map[string]any{
  88. source.QueryPodInfo: []*source.PodInfoResult{
  89. {UID: "pod-1", Pod: "my-pod", NamespaceUID: "ns-1"},
  90. },
  91. source.QueryPodUptime: []*source.UptimeResult{
  92. {UID: "pod-1", First: start, Last: end},
  93. },
  94. source.QueryPodOwners: []*source.OwnerResult{
  95. {UID: "pod-1", OwnerUID: "rs-1", OwnerKind: "ReplicaSet", Controller: true},
  96. },
  97. },
  98. want: map[string]*kubemodel.Pod{
  99. "pod-1": {
  100. UID: "pod-1",
  101. Name: "my-pod",
  102. NamespaceUID: "ns-1",
  103. Start: start,
  104. End: end,
  105. Owners: []kubemodel.Owner{
  106. {UID: "rs-1", Kind: kubemodel.OwnerKindReplicaSet, Controller: true},
  107. },
  108. },
  109. },
  110. },
  111. {
  112. name: "pod pvc volumes are attached",
  113. overrides: map[string]any{
  114. source.QueryPodInfo: []*source.PodInfoResult{
  115. {UID: "pod-1", Pod: "my-pod", NamespaceUID: "ns-1"},
  116. },
  117. source.QueryPodUptime: []*source.UptimeResult{
  118. {UID: "pod-1", First: start, Last: end},
  119. },
  120. source.QueryPodPVCVolumes: []*source.PodPVCVolumeResult{
  121. {UID: "pod-1", PVCUID: "pvc-1", PodVolumeName: "data"},
  122. },
  123. },
  124. want: map[string]*kubemodel.Pod{
  125. "pod-1": {
  126. UID: "pod-1",
  127. Name: "my-pod",
  128. NamespaceUID: "ns-1",
  129. Start: start,
  130. End: end,
  131. PVCVolumes: []kubemodel.PodPVCVolume{
  132. {Name: "data", PersistentVolumeClaimUID: "pvc-1"},
  133. },
  134. },
  135. },
  136. },
  137. {
  138. name: "pod labels and annotations are attached",
  139. overrides: map[string]any{
  140. source.QueryPodInfo: []*source.PodInfoResult{
  141. {UID: "pod-1", Pod: "my-pod", NamespaceUID: "ns-1"},
  142. },
  143. source.QueryPodUptime: []*source.UptimeResult{
  144. {UID: "pod-1", First: start, Last: end},
  145. },
  146. source.QueryPodLabels: []*source.PodLabelsResult{
  147. {UID: "pod-1", Labels: map[string]string{"app": "web"}},
  148. },
  149. source.QueryPodAnnotations: []*source.PodAnnotationsResult{
  150. {UID: "pod-1", Annotations: map[string]string{"team": "platform"}},
  151. },
  152. },
  153. want: map[string]*kubemodel.Pod{
  154. "pod-1": {
  155. UID: "pod-1",
  156. Name: "my-pod",
  157. NamespaceUID: "ns-1",
  158. Start: start,
  159. End: end,
  160. Labels: map[string]string{"app": "web"},
  161. Annotations: map[string]string{"team": "platform"},
  162. },
  163. },
  164. },
  165. {
  166. name: "uptime for unknown pod is ignored",
  167. overrides: map[string]any{
  168. source.QueryPodInfo: []*source.PodInfoResult{
  169. {UID: "pod-1", Pod: "my-pod", NamespaceUID: "ns-1"},
  170. },
  171. source.QueryPodUptime: []*source.UptimeResult{
  172. {UID: "pod-1", First: start, Last: end},
  173. {UID: "unknown-pod", First: start, Last: end},
  174. },
  175. },
  176. want: map[string]*kubemodel.Pod{
  177. "pod-1": {
  178. UID: "pod-1",
  179. Name: "my-pod",
  180. NamespaceUID: "ns-1",
  181. Start: start,
  182. End: end,
  183. },
  184. },
  185. },
  186. {
  187. name: "network egress: internet, cross-region, cross-zone traffic types",
  188. overrides: map[string]any{
  189. source.QueryPodInfo: []*source.PodInfoResult{
  190. {UID: "pod-1", Pod: "my-pod", NamespaceUID: "ns-1"},
  191. },
  192. source.QueryPodUptime: []*source.UptimeResult{
  193. {UID: "pod-1", First: start, Last: end},
  194. },
  195. source.QueryPodNetworkEgressBytes: []*source.PodNetworkBytesResult{
  196. {UID: "pod-1", Internet: true, Service: "svc-a", Value: 100},
  197. {UID: "pod-1", Internet: false, SameRegion: false, Service: "svc-b", Value: 200},
  198. {UID: "pod-1", Internet: false, SameRegion: true, SameZone: false, Service: "svc-c", Value: 300},
  199. {UID: "pod-1", Internet: false, SameRegion: true, SameZone: true, Service: "svc-d", Value: 400}, // no traffic type → skipped
  200. {UID: "pod-1", Internet: true, Service: "svc-e", Value: 0}, // zero bytes → skipped
  201. {UID: "unknown-pod", Internet: true, Service: "svc-f", Value: 500}, // unknown pod → skipped
  202. },
  203. },
  204. want: map[string]*kubemodel.Pod{
  205. "pod-1": {
  206. UID: "pod-1",
  207. Name: "my-pod",
  208. NamespaceUID: "ns-1",
  209. Start: start,
  210. End: end,
  211. NetworkTrafficDetails: []kubemodel.NetworkTrafficDetail{
  212. {PodUID: "pod-1", TrafficDirection: kubemodel.TrafficDirectionEgress, TrafficType: kubemodel.TrafficTypeInternet, Endpoint: "svc-a", Bytes: 100},
  213. {PodUID: "pod-1", TrafficDirection: kubemodel.TrafficDirectionEgress, TrafficType: kubemodel.TrafficTypeCrossRegion, Endpoint: "svc-b", Bytes: 200},
  214. {PodUID: "pod-1", TrafficDirection: kubemodel.TrafficDirectionEgress, TrafficType: kubemodel.TrafficTypeCrossZone, Endpoint: "svc-c", Bytes: 300},
  215. },
  216. },
  217. },
  218. },
  219. {
  220. name: "unknown uid in supplementary data is ignored",
  221. overrides: map[string]any{
  222. source.QueryPodInfo: []*source.PodInfoResult{
  223. {UID: "pod-1", Pod: "my-pod", NamespaceUID: "ns-1"},
  224. },
  225. source.QueryPodUptime: []*source.UptimeResult{
  226. {UID: "pod-1", First: start, Last: end},
  227. },
  228. // all of these carry an unknown UID — hit the !ok warn paths
  229. source.QueryPodOwners: []*source.OwnerResult{
  230. {UID: "unknown-pod", OwnerUID: "rs-1", OwnerKind: "ReplicaSet", Controller: true},
  231. },
  232. source.QueryPodPVCVolumes: []*source.PodPVCVolumeResult{
  233. {UID: "unknown-pod", PVCUID: "pvc-1", PodVolumeName: "data"},
  234. },
  235. source.QueryPodLabels: []*source.PodLabelsResult{
  236. {UID: "unknown-pod", Labels: map[string]string{"app": "web"}},
  237. },
  238. source.QueryPodAnnotations: []*source.PodAnnotationsResult{
  239. {UID: "unknown-pod", Annotations: map[string]string{"team": "platform"}},
  240. },
  241. // same-zone+same-region ingress: triggers !ok continue in ingress loop
  242. source.QueryPodNetworkIngressBytes: []*source.PodNetworkBytesResult{
  243. {UID: "pod-1", Internet: false, SameRegion: true, SameZone: true, Value: 100},
  244. },
  245. },
  246. want: map[string]*kubemodel.Pod{
  247. "pod-1": {
  248. UID: "pod-1",
  249. Name: "my-pod",
  250. NamespaceUID: "ns-1",
  251. Start: start,
  252. End: end,
  253. },
  254. },
  255. },
  256. {
  257. name: "network ingress traffic is recorded",
  258. overrides: map[string]any{
  259. source.QueryPodInfo: []*source.PodInfoResult{
  260. {UID: "pod-1", Pod: "my-pod", NamespaceUID: "ns-1"},
  261. },
  262. source.QueryPodUptime: []*source.UptimeResult{
  263. {UID: "pod-1", First: start, Last: end},
  264. },
  265. source.QueryPodNetworkIngressBytes: []*source.PodNetworkBytesResult{
  266. {UID: "pod-1", Internet: true, NatGateway: true, Service: "svc-a", Value: 512},
  267. {UID: "pod-1", Internet: false, SameRegion: false, Service: "svc-b", Value: 256},
  268. },
  269. },
  270. want: map[string]*kubemodel.Pod{
  271. "pod-1": {
  272. UID: "pod-1",
  273. Name: "my-pod",
  274. NamespaceUID: "ns-1",
  275. Start: start,
  276. End: end,
  277. NetworkTrafficDetails: []kubemodel.NetworkTrafficDetail{
  278. {PodUID: "pod-1", TrafficDirection: kubemodel.TrafficDirectionIngress, TrafficType: kubemodel.TrafficTypeInternet, IsNatGateway: true, Endpoint: "svc-a", Bytes: 512},
  279. {PodUID: "pod-1", TrafficDirection: kubemodel.TrafficDirectionIngress, TrafficType: kubemodel.TrafficTypeCrossRegion, Endpoint: "svc-b", Bytes: 256},
  280. },
  281. },
  282. },
  283. },
  284. }
  285. for _, tt := range tests {
  286. t.Run(tt.name, func(t *testing.T) {
  287. ds := source.NewMockOpenCostDataSource()
  288. ds.ResolutionValue = 5 * time.Minute
  289. seedCluster(ds, start, end)
  290. for method, result := range tt.overrides {
  291. ds.Querier.SetOverride(method, result)
  292. }
  293. km, err := NewKubeModel(testClusterUID, false, ds)
  294. require.NoError(t, err)
  295. kms, err := km.ComputeKubeModelSet(start, end)
  296. require.NoError(t, err)
  297. assert.Equal(t, tt.want, kms.Pods)
  298. })
  299. }
  300. }