clustercache_test.go 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717
  1. package scrape
  2. import (
  3. "reflect"
  4. "testing"
  5. "time"
  6. "github.com/opencost/opencost/core/pkg/clustercache"
  7. "github.com/opencost/opencost/core/pkg/external"
  8. "github.com/opencost/opencost/core/pkg/source"
  9. "github.com/opencost/opencost/modules/collector-source/pkg/metric"
  10. "github.com/opencost/opencost/modules/collector-source/pkg/util"
  11. v1 "k8s.io/api/core/v1"
  12. "k8s.io/apimachinery/pkg/api/resource"
  13. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  14. "k8s.io/apimachinery/pkg/types"
  15. )
  16. var Start1Str = "2025-01-01T00:00:00Z00:00"
  17. func Test_kubernetesScraper_scrapeNodes(t *testing.T) {
  18. start1, _ := time.Parse(time.RFC3339, Start1Str)
  19. type scrape struct {
  20. Nodes []*clustercache.Node
  21. Timestamp time.Time
  22. }
  23. tests := []struct {
  24. name string
  25. scrapes []scrape
  26. expected []metric.Update
  27. }{
  28. {
  29. name: "simple",
  30. scrapes: []scrape{
  31. {
  32. Nodes: []*clustercache.Node{
  33. {
  34. Name: "node1",
  35. UID: "uuid1",
  36. SpecProviderID: "i-1",
  37. Status: v1.NodeStatus{
  38. Capacity: v1.ResourceList{
  39. v1.ResourceCPU: resource.MustParse("2"),
  40. v1.ResourceMemory: resource.MustParse("2048"),
  41. },
  42. Allocatable: v1.ResourceList{
  43. v1.ResourceCPU: resource.MustParse("1"),
  44. v1.ResourceMemory: resource.MustParse("1024"),
  45. },
  46. },
  47. Labels: map[string]string{
  48. "test1": "blah",
  49. "test2": "blah2",
  50. },
  51. },
  52. },
  53. Timestamp: start1,
  54. },
  55. },
  56. expected: []metric.Update{
  57. {
  58. Name: metric.NodeInfo,
  59. Labels: map[string]string{
  60. source.NodeLabel: "node1",
  61. source.ProviderIDLabel: "i-1",
  62. source.UIDLabel: "uuid1",
  63. },
  64. Value: 0,
  65. AdditionalInfo: map[string]string{
  66. source.NodeLabel: "node1",
  67. source.ProviderIDLabel: "i-1",
  68. source.UIDLabel: "uuid1",
  69. },
  70. },
  71. {
  72. Name: metric.NodeResourceCapacities,
  73. Labels: map[string]string{
  74. source.NodeLabel: "node1",
  75. source.ProviderIDLabel: "i-1",
  76. source.UIDLabel: "uuid1",
  77. source.ResourceLabel: "cpu",
  78. source.UnitLabel: "core",
  79. },
  80. Value: 2.0,
  81. AdditionalInfo: nil,
  82. },
  83. {
  84. Name: metric.NodeResourceCapacities,
  85. Labels: map[string]string{
  86. source.NodeLabel: "node1",
  87. source.ProviderIDLabel: "i-1",
  88. source.UIDLabel: "uuid1",
  89. source.ResourceLabel: "memory",
  90. source.UnitLabel: "byte",
  91. },
  92. Value: 2048.0,
  93. AdditionalInfo: nil,
  94. },
  95. {
  96. Name: metric.KubeNodeStatusCapacityCPUCores,
  97. Labels: map[string]string{
  98. source.NodeLabel: "node1",
  99. source.ProviderIDLabel: "i-1",
  100. source.UIDLabel: "uuid1",
  101. },
  102. Value: 2.0,
  103. AdditionalInfo: nil,
  104. },
  105. {
  106. Name: metric.KubeNodeStatusCapacityMemoryBytes,
  107. Labels: map[string]string{
  108. source.NodeLabel: "node1",
  109. source.ProviderIDLabel: "i-1",
  110. source.UIDLabel: "uuid1",
  111. },
  112. Value: 2048.0,
  113. AdditionalInfo: nil,
  114. },
  115. {
  116. Name: metric.NodeResourcesAllocatable,
  117. Labels: map[string]string{
  118. source.NodeLabel: "node1",
  119. source.ProviderIDLabel: "i-1",
  120. source.UIDLabel: "uuid1",
  121. source.ResourceLabel: "cpu",
  122. source.UnitLabel: "core",
  123. },
  124. Value: 1.0,
  125. AdditionalInfo: nil,
  126. },
  127. {
  128. Name: metric.NodeResourcesAllocatable,
  129. Labels: map[string]string{
  130. source.NodeLabel: "node1",
  131. source.ProviderIDLabel: "i-1",
  132. source.UIDLabel: "uuid1",
  133. source.ResourceLabel: "memory",
  134. source.UnitLabel: "byte",
  135. },
  136. Value: 1024.0,
  137. AdditionalInfo: nil,
  138. },
  139. {
  140. Name: metric.KubeNodeStatusAllocatableCPUCores,
  141. Labels: map[string]string{
  142. source.NodeLabel: "node1",
  143. source.ProviderIDLabel: "i-1",
  144. source.UIDLabel: "uuid1",
  145. },
  146. Value: 1.0,
  147. AdditionalInfo: nil,
  148. },
  149. {
  150. Name: metric.KubeNodeStatusAllocatableMemoryBytes,
  151. Labels: map[string]string{
  152. source.NodeLabel: "node1",
  153. source.ProviderIDLabel: "i-1",
  154. source.UIDLabel: "uuid1",
  155. },
  156. Value: 1024.0,
  157. AdditionalInfo: nil,
  158. },
  159. {
  160. Name: metric.KubeNodeLabels,
  161. Labels: map[string]string{
  162. source.NodeLabel: "node1",
  163. source.ProviderIDLabel: "i-1",
  164. source.UIDLabel: "uuid1",
  165. },
  166. Value: 0,
  167. AdditionalInfo: map[string]string{
  168. "label_test1": "blah",
  169. "label_test2": "blah2",
  170. },
  171. },
  172. },
  173. },
  174. }
  175. for _, tt := range tests {
  176. t.Run(tt.name, func(t *testing.T) {
  177. ks := &ClusterCacheScraper{}
  178. var scrapeResults []metric.Update
  179. for _, s := range tt.scrapes {
  180. res := ks.scrapeNodes(s.Nodes)
  181. scrapeResults = append(scrapeResults, res...)
  182. }
  183. if len(scrapeResults) != len(tt.expected) {
  184. t.Errorf("Expected result length of %d, got %d", len(tt.expected), len(scrapeResults))
  185. }
  186. for i, expected := range tt.expected {
  187. got := scrapeResults[i]
  188. if !reflect.DeepEqual(expected, got) {
  189. t.Errorf("Result did not match expected at index %d: got %v, want %v", i, got, expected)
  190. }
  191. }
  192. })
  193. }
  194. }
  195. func Test_kubernetesScraper_scrapeNodesWithExternalLabels(t *testing.T) {
  196. start1, _ := time.Parse(time.RFC3339, Start1Str)
  197. const (
  198. testSource = "mock"
  199. testExternalLabelKey = "externalLabelKey"
  200. testExternalLabelValue = "externalLabelValue"
  201. )
  202. mockLabelProvider := external.NewNodeLabelProvider()
  203. err := mockLabelProvider.Update(testSource, map[string]string{testExternalLabelKey: testExternalLabelValue})
  204. if err != nil {
  205. t.Fatalf("failed to get test node labels: %s", err)
  206. }
  207. type scrape struct {
  208. Nodes []*clustercache.Node
  209. Timestamp time.Time
  210. }
  211. tests := []struct {
  212. name string
  213. scrapes []scrape
  214. expected []metric.Update
  215. }{
  216. {
  217. name: "simple",
  218. scrapes: []scrape{
  219. {
  220. Nodes: []*clustercache.Node{
  221. {
  222. Name: "node1",
  223. UID: "uuid1",
  224. SpecProviderID: "i-1",
  225. Status: v1.NodeStatus{
  226. Capacity: v1.ResourceList{
  227. v1.ResourceCPU: resource.MustParse("2"),
  228. v1.ResourceMemory: resource.MustParse("2048"),
  229. },
  230. Allocatable: v1.ResourceList{
  231. v1.ResourceCPU: resource.MustParse("1"),
  232. v1.ResourceMemory: resource.MustParse("1024"),
  233. },
  234. },
  235. Labels: map[string]string{
  236. "test1": "blah",
  237. "test2": "blah2",
  238. },
  239. },
  240. },
  241. Timestamp: start1,
  242. },
  243. },
  244. expected: []metric.Update{
  245. {
  246. Name: metric.NodeInfo,
  247. Labels: map[string]string{
  248. source.NodeLabel: "node1",
  249. source.ProviderIDLabel: "i-1",
  250. source.UIDLabel: "uuid1",
  251. },
  252. Value: 0,
  253. AdditionalInfo: map[string]string{
  254. source.NodeLabel: "node1",
  255. source.ProviderIDLabel: "i-1",
  256. source.UIDLabel: "uuid1",
  257. },
  258. },
  259. {
  260. Name: metric.NodeResourceCapacities,
  261. Labels: map[string]string{
  262. source.NodeLabel: "node1",
  263. source.ProviderIDLabel: "i-1",
  264. source.UIDLabel: "uuid1",
  265. source.ResourceLabel: "cpu",
  266. source.UnitLabel: "core",
  267. },
  268. Value: 2.0,
  269. AdditionalInfo: nil,
  270. },
  271. {
  272. Name: metric.NodeResourceCapacities,
  273. Labels: map[string]string{
  274. source.NodeLabel: "node1",
  275. source.ProviderIDLabel: "i-1",
  276. source.UIDLabel: "uuid1",
  277. source.ResourceLabel: "memory",
  278. source.UnitLabel: "byte",
  279. },
  280. Value: 2048.0,
  281. AdditionalInfo: nil,
  282. },
  283. {
  284. Name: metric.KubeNodeStatusCapacityCPUCores,
  285. Labels: map[string]string{
  286. source.NodeLabel: "node1",
  287. source.ProviderIDLabel: "i-1",
  288. source.UIDLabel: "uuid1",
  289. },
  290. Value: 2.0,
  291. AdditionalInfo: nil,
  292. },
  293. {
  294. Name: metric.KubeNodeStatusCapacityMemoryBytes,
  295. Labels: map[string]string{
  296. source.NodeLabel: "node1",
  297. source.ProviderIDLabel: "i-1",
  298. source.UIDLabel: "uuid1",
  299. },
  300. Value: 2048.0,
  301. AdditionalInfo: nil,
  302. },
  303. {
  304. Name: metric.NodeResourcesAllocatable,
  305. Labels: map[string]string{
  306. source.NodeLabel: "node1",
  307. source.ProviderIDLabel: "i-1",
  308. source.UIDLabel: "uuid1",
  309. source.ResourceLabel: "cpu",
  310. source.UnitLabel: "core",
  311. },
  312. Value: 1.0,
  313. AdditionalInfo: nil,
  314. },
  315. {
  316. Name: metric.NodeResourcesAllocatable,
  317. Labels: map[string]string{
  318. source.NodeLabel: "node1",
  319. source.ProviderIDLabel: "i-1",
  320. source.UIDLabel: "uuid1",
  321. source.ResourceLabel: "memory",
  322. source.UnitLabel: "byte",
  323. },
  324. Value: 1024.0,
  325. AdditionalInfo: nil,
  326. },
  327. {
  328. Name: metric.KubeNodeStatusAllocatableCPUCores,
  329. Labels: map[string]string{
  330. source.NodeLabel: "node1",
  331. source.ProviderIDLabel: "i-1",
  332. source.UIDLabel: "uuid1",
  333. },
  334. Value: 1.0,
  335. AdditionalInfo: nil,
  336. },
  337. {
  338. Name: metric.KubeNodeStatusAllocatableMemoryBytes,
  339. Labels: map[string]string{
  340. source.NodeLabel: "node1",
  341. source.ProviderIDLabel: "i-1",
  342. source.UIDLabel: "uuid1",
  343. },
  344. Value: 1024.0,
  345. AdditionalInfo: nil,
  346. },
  347. {
  348. Name: metric.KubeNodeLabels,
  349. Labels: map[string]string{
  350. source.NodeLabel: "node1",
  351. source.ProviderIDLabel: "i-1",
  352. source.UIDLabel: "uuid1",
  353. },
  354. Value: 0,
  355. AdditionalInfo: map[string]string{
  356. "label_test1": "blah",
  357. "label_test2": "blah2",
  358. // need label key with prefix label_ so the decoder does not exclude.
  359. "label_" + testExternalLabelKey: testExternalLabelValue,
  360. },
  361. },
  362. },
  363. },
  364. }
  365. for _, tt := range tests {
  366. t.Run(tt.name, func(t *testing.T) {
  367. ks := &ClusterCacheScraper{
  368. externalLabelProvider: mockLabelProvider,
  369. }
  370. var scrapeResults []metric.Update
  371. for _, s := range tt.scrapes {
  372. res := ks.scrapeNodes(s.Nodes)
  373. scrapeResults = append(scrapeResults, res...)
  374. }
  375. if len(scrapeResults) != len(tt.expected) {
  376. t.Errorf("Expected result length of %d, got %d", len(tt.expected), len(scrapeResults))
  377. }
  378. for i, expected := range tt.expected {
  379. got := scrapeResults[i]
  380. if !reflect.DeepEqual(expected, got) {
  381. t.Errorf("Result did not match expected at index %d: got %v, want %v", i, got, expected)
  382. }
  383. }
  384. })
  385. }
  386. }
  387. func Test_kubernetesScraper_scrapeDeployments(t *testing.T) {
  388. start1, _ := time.Parse(time.RFC3339, Start1Str)
  389. type scrape struct {
  390. Deployments []*clustercache.Deployment
  391. Timestamp time.Time
  392. }
  393. tests := []struct {
  394. name string
  395. scrapes []scrape
  396. expected []metric.Update
  397. }{
  398. {
  399. name: "simple",
  400. scrapes: []scrape{
  401. {
  402. Deployments: []*clustercache.Deployment{
  403. {
  404. Name: "deployment1",
  405. Namespace: "namespace1",
  406. UID: "uuid1",
  407. MatchLabels: map[string]string{
  408. "test1": "blah",
  409. "test2": "blah2",
  410. },
  411. },
  412. },
  413. Timestamp: start1,
  414. },
  415. },
  416. // deploymentInfo map is shared across all 4 metrics and has namespace
  417. // added to it before DeploymentMatchLabels is appended, so all 4 metrics
  418. // reflect the final state: {uid, namespace_uid, deployment, namespace}.
  419. expected: []metric.Update{
  420. {
  421. Name: metric.DeploymentInfo,
  422. Labels: map[string]string{
  423. source.UIDLabel: "uuid1",
  424. source.NamespaceUIDLabel: "ns-uuid1",
  425. source.DeploymentLabel: "deployment1",
  426. source.NamespaceLabel: "namespace1",
  427. },
  428. Value: 0,
  429. AdditionalInfo: map[string]string{
  430. source.UIDLabel: "uuid1",
  431. source.NamespaceUIDLabel: "ns-uuid1",
  432. source.DeploymentLabel: "deployment1",
  433. source.NamespaceLabel: "namespace1",
  434. },
  435. },
  436. {
  437. Name: metric.DeploymentLabels,
  438. Labels: map[string]string{
  439. source.UIDLabel: "uuid1",
  440. source.NamespaceUIDLabel: "ns-uuid1",
  441. source.DeploymentLabel: "deployment1",
  442. source.NamespaceLabel: "namespace1",
  443. },
  444. Value: 0,
  445. AdditionalInfo: map[string]string{},
  446. },
  447. {
  448. Name: metric.DeploymentAnnotations,
  449. Labels: map[string]string{
  450. source.UIDLabel: "uuid1",
  451. source.NamespaceUIDLabel: "ns-uuid1",
  452. source.DeploymentLabel: "deployment1",
  453. source.NamespaceLabel: "namespace1",
  454. },
  455. Value: 0,
  456. AdditionalInfo: map[string]string{},
  457. },
  458. {
  459. Name: metric.DeploymentMatchLabels,
  460. Labels: map[string]string{
  461. source.UIDLabel: "uuid1",
  462. source.NamespaceUIDLabel: "ns-uuid1",
  463. source.DeploymentLabel: "deployment1",
  464. source.NamespaceLabel: "namespace1",
  465. },
  466. Value: 0,
  467. AdditionalInfo: map[string]string{
  468. "label_test1": "blah",
  469. "label_test2": "blah2",
  470. },
  471. },
  472. },
  473. },
  474. }
  475. for _, tt := range tests {
  476. t.Run(tt.name, func(t *testing.T) {
  477. ks := &ClusterCacheScraper{}
  478. nsIndex := make(map[string]types.UID, 0)
  479. nsIndex["namespace1"] = "ns-uuid1"
  480. var scrapeResults []metric.Update
  481. for _, s := range tt.scrapes {
  482. res := ks.scrapeDeployments(s.Deployments, nsIndex)
  483. scrapeResults = append(scrapeResults, res...)
  484. }
  485. if len(scrapeResults) != len(tt.expected) {
  486. t.Errorf("Expected result length of %d, got %d", len(tt.expected), len(scrapeResults))
  487. }
  488. for i, expected := range tt.expected {
  489. got := scrapeResults[i]
  490. if !reflect.DeepEqual(expected, got) {
  491. t.Errorf("Result did not match expected at index %d: got %v, want %v", i, got, expected)
  492. }
  493. }
  494. })
  495. }
  496. }
  497. func Test_kubernetesScraper_scrapeNamespaces(t *testing.T) {
  498. start1, _ := time.Parse(time.RFC3339, Start1Str)
  499. type scrape struct {
  500. Namespaces []*clustercache.Namespace
  501. Timestamp time.Time
  502. }
  503. tests := []struct {
  504. name string
  505. scrapes []scrape
  506. expected []metric.Update
  507. }{
  508. {
  509. name: "simple",
  510. scrapes: []scrape{
  511. {
  512. Namespaces: []*clustercache.Namespace{
  513. {
  514. Name: "namespace1",
  515. UID: "uuid1",
  516. Labels: map[string]string{
  517. "test1": "blah",
  518. "test2": "blah2",
  519. },
  520. Annotations: map[string]string{
  521. "test3": "blah3",
  522. "test4": "blah4",
  523. },
  524. },
  525. },
  526. Timestamp: start1,
  527. },
  528. },
  529. expected: []metric.Update{
  530. {
  531. Name: metric.NamespaceInfo,
  532. Labels: map[string]string{
  533. source.NamespaceLabel: "namespace1",
  534. source.UIDLabel: "uuid1",
  535. },
  536. Value: 0,
  537. AdditionalInfo: map[string]string{
  538. source.NamespaceLabel: "namespace1",
  539. source.UIDLabel: "uuid1",
  540. },
  541. },
  542. {
  543. Name: metric.KubeNamespaceLabels,
  544. Labels: map[string]string{
  545. source.NamespaceLabel: "namespace1",
  546. source.UIDLabel: "uuid1",
  547. },
  548. Value: 0,
  549. AdditionalInfo: map[string]string{
  550. "label_test1": "blah",
  551. "label_test2": "blah2",
  552. },
  553. },
  554. {
  555. Name: metric.KubeNamespaceAnnotations,
  556. Labels: map[string]string{
  557. source.NamespaceLabel: "namespace1",
  558. source.UIDLabel: "uuid1",
  559. },
  560. Value: 0,
  561. AdditionalInfo: map[string]string{
  562. "annotation_test3": "blah3",
  563. "annotation_test4": "blah4",
  564. },
  565. },
  566. },
  567. },
  568. }
  569. for _, tt := range tests {
  570. t.Run(tt.name, func(t *testing.T) {
  571. ks := &ClusterCacheScraper{}
  572. var scrapeResults []metric.Update
  573. for _, s := range tt.scrapes {
  574. res := ks.scrapeNamespaces(s.Namespaces)
  575. scrapeResults = append(scrapeResults, res...)
  576. }
  577. if len(scrapeResults) != len(tt.expected) {
  578. t.Errorf("Expected result length of %d, got %d", len(tt.expected), len(scrapeResults))
  579. }
  580. for i, expected := range tt.expected {
  581. got := scrapeResults[i]
  582. if !reflect.DeepEqual(expected, got) {
  583. t.Errorf("Result did not match expected at index %d: got %v, want %v", i, got, expected)
  584. }
  585. }
  586. })
  587. }
  588. }
  589. func Test_kubernetesScraper_scrapePods(t *testing.T) {
  590. start1, _ := time.Parse(time.RFC3339, Start1Str)
  591. type scrape struct {
  592. PVCs []*clustercache.PersistentVolumeClaim
  593. Pods []*clustercache.Pod
  594. Timestamp time.Time
  595. }
  596. tests := []struct {
  597. name string
  598. nsSetup func(map[string]types.UID)
  599. scrapes []scrape
  600. expected []metric.Update
  601. }{
  602. {
  603. name: "simple",
  604. scrapes: []scrape{
  605. {
  606. PVCs: []*clustercache.PersistentVolumeClaim{
  607. {
  608. Name: "pvc1",
  609. Namespace: "namespace1",
  610. UID: "uuid1",
  611. Spec: v1.PersistentVolumeClaimSpec{
  612. VolumeName: "vol1",
  613. StorageClassName: util.Ptr("storageClass1"),
  614. Resources: v1.VolumeResourceRequirements{
  615. Requests: v1.ResourceList{
  616. v1.ResourceStorage: resource.MustParse("4096"),
  617. },
  618. },
  619. },
  620. },
  621. },
  622. Pods: []*clustercache.Pod{
  623. {
  624. Name: "pod1",
  625. Namespace: "namespace1",
  626. UID: "uuid1",
  627. Spec: clustercache.PodSpec{
  628. NodeName: "node1",
  629. Containers: []clustercache.Container{
  630. {
  631. Name: "container1",
  632. Resources: v1.ResourceRequirements{
  633. Requests: map[v1.ResourceName]resource.Quantity{
  634. v1.ResourceCPU: resource.MustParse("500m"),
  635. v1.ResourceMemory: resource.MustParse("512"),
  636. },
  637. Limits: map[v1.ResourceName]resource.Quantity{
  638. v1.ResourceCPU: resource.MustParse("1"),
  639. v1.ResourceMemory: resource.MustParse("1024"),
  640. },
  641. },
  642. },
  643. },
  644. },
  645. Labels: map[string]string{
  646. "test1": "blah",
  647. "test2": "blah2",
  648. },
  649. Annotations: map[string]string{
  650. "test3": "blah3",
  651. "test4": "blah4",
  652. },
  653. OwnerReferences: []metav1.OwnerReference{
  654. {
  655. Kind: source.DeploymentLabel,
  656. Name: "deployment1",
  657. Controller: nil,
  658. },
  659. },
  660. Status: clustercache.PodStatus{
  661. ContainerStatuses: []v1.ContainerStatus{
  662. {
  663. Name: "container1",
  664. State: v1.ContainerState{
  665. Running: &v1.ContainerStateRunning{},
  666. },
  667. },
  668. },
  669. },
  670. },
  671. },
  672. Timestamp: start1,
  673. },
  674. },
  675. // podInfo is mutated after PodInfo is appended (namespace/node/instance added),
  676. // so PodInfo.Labels also reflects those fields at assertion time.
  677. expected: []metric.Update{
  678. {
  679. Name: metric.PodInfo,
  680. Labels: map[string]string{
  681. source.UIDLabel: "uuid1",
  682. source.PodLabel: "pod1",
  683. source.NamespaceUIDLabel: "",
  684. source.NodeUIDLabel: "",
  685. source.NamespaceLabel: "namespace1",
  686. source.NodeLabel: "node1",
  687. source.InstanceLabel: "node1",
  688. },
  689. Value: 0,
  690. AdditionalInfo: map[string]string{
  691. source.UIDLabel: "uuid1",
  692. source.PodLabel: "pod1",
  693. source.NamespaceUIDLabel: "",
  694. source.NodeUIDLabel: "",
  695. source.NamespaceLabel: "namespace1",
  696. source.NodeLabel: "node1",
  697. source.InstanceLabel: "node1",
  698. },
  699. },
  700. {
  701. Name: metric.KubePodLabels,
  702. Labels: map[string]string{
  703. source.UIDLabel: "uuid1",
  704. source.PodLabel: "pod1",
  705. source.NamespaceUIDLabel: "",
  706. source.NodeUIDLabel: "",
  707. source.NamespaceLabel: "namespace1",
  708. source.NodeLabel: "node1",
  709. source.InstanceLabel: "node1",
  710. },
  711. Value: 0,
  712. AdditionalInfo: map[string]string{
  713. "label_test1": "blah",
  714. "label_test2": "blah2",
  715. },
  716. },
  717. {
  718. Name: metric.KubePodAnnotations,
  719. Labels: map[string]string{
  720. source.UIDLabel: "uuid1",
  721. source.PodLabel: "pod1",
  722. source.NamespaceUIDLabel: "",
  723. source.NodeUIDLabel: "",
  724. source.NamespaceLabel: "namespace1",
  725. source.NodeLabel: "node1",
  726. source.InstanceLabel: "node1",
  727. },
  728. Value: 0,
  729. AdditionalInfo: map[string]string{
  730. "annotation_test3": "blah3",
  731. "annotation_test4": "blah4",
  732. },
  733. },
  734. {
  735. Name: metric.KubePodOwner,
  736. Labels: map[string]string{
  737. source.UIDLabel: "uuid1",
  738. source.PodLabel: "pod1",
  739. source.NamespaceUIDLabel: "",
  740. source.NodeUIDLabel: "",
  741. source.NamespaceLabel: "namespace1",
  742. source.NodeLabel: "node1",
  743. source.InstanceLabel: "node1",
  744. source.OwnerKindLabel: "deployment",
  745. source.OwnerNameLabel: "deployment1",
  746. source.OwnerUIDLabel: "",
  747. source.ControllerLabel: "false",
  748. },
  749. Value: 0,
  750. AdditionalInfo: map[string]string{
  751. source.UIDLabel: "uuid1",
  752. source.PodLabel: "pod1",
  753. source.NamespaceUIDLabel: "",
  754. source.NodeUIDLabel: "",
  755. source.NamespaceLabel: "namespace1",
  756. source.NodeLabel: "node1",
  757. source.InstanceLabel: "node1",
  758. source.OwnerKindLabel: "deployment",
  759. source.OwnerNameLabel: "deployment1",
  760. source.OwnerUIDLabel: "",
  761. source.ControllerLabel: "false",
  762. },
  763. },
  764. {
  765. Name: metric.KubePodContainerStatusRunning,
  766. Labels: map[string]string{
  767. source.UIDLabel: "uuid1",
  768. source.PodLabel: "pod1",
  769. source.NamespaceUIDLabel: "",
  770. source.NodeUIDLabel: "",
  771. source.NamespaceLabel: "namespace1",
  772. source.NodeLabel: "node1",
  773. source.InstanceLabel: "node1",
  774. source.ContainerLabel: "container1",
  775. },
  776. Value: 0,
  777. AdditionalInfo: map[string]string{
  778. source.UIDLabel: "uuid1",
  779. source.PodLabel: "pod1",
  780. source.NamespaceUIDLabel: "",
  781. source.NodeUIDLabel: "",
  782. source.NamespaceLabel: "namespace1",
  783. source.NodeLabel: "node1",
  784. source.InstanceLabel: "node1",
  785. source.ContainerLabel: "container1",
  786. },
  787. },
  788. {
  789. Name: metric.KubePodContainerResourceRequests,
  790. Labels: map[string]string{
  791. source.UIDLabel: "uuid1",
  792. source.PodLabel: "pod1",
  793. source.NamespaceUIDLabel: "",
  794. source.NodeUIDLabel: "",
  795. source.NamespaceLabel: "namespace1",
  796. source.NodeLabel: "node1",
  797. source.InstanceLabel: "node1",
  798. source.ContainerLabel: "container1",
  799. source.ResourceLabel: "cpu",
  800. source.UnitLabel: "core",
  801. },
  802. Value: 0.5,
  803. AdditionalInfo: nil,
  804. },
  805. {
  806. Name: metric.KubePodContainerResourceRequests,
  807. Labels: map[string]string{
  808. source.UIDLabel: "uuid1",
  809. source.PodLabel: "pod1",
  810. source.NamespaceUIDLabel: "",
  811. source.NodeUIDLabel: "",
  812. source.NamespaceLabel: "namespace1",
  813. source.NodeLabel: "node1",
  814. source.InstanceLabel: "node1",
  815. source.ContainerLabel: "container1",
  816. source.ResourceLabel: "memory",
  817. source.UnitLabel: "byte",
  818. },
  819. Value: 512,
  820. AdditionalInfo: nil,
  821. },
  822. {
  823. Name: metric.KubePodContainerResourceLimits,
  824. Labels: map[string]string{
  825. source.UIDLabel: "uuid1",
  826. source.PodLabel: "pod1",
  827. source.NamespaceUIDLabel: "",
  828. source.NodeUIDLabel: "",
  829. source.NamespaceLabel: "namespace1",
  830. source.NodeLabel: "node1",
  831. source.InstanceLabel: "node1",
  832. source.ContainerLabel: "container1",
  833. source.ResourceLabel: "cpu",
  834. source.UnitLabel: "core",
  835. },
  836. Value: 1,
  837. AdditionalInfo: nil,
  838. },
  839. {
  840. Name: metric.KubePodContainerResourceLimits,
  841. Labels: map[string]string{
  842. source.UIDLabel: "uuid1",
  843. source.PodLabel: "pod1",
  844. source.NamespaceUIDLabel: "",
  845. source.NodeUIDLabel: "",
  846. source.NamespaceLabel: "namespace1",
  847. source.NodeLabel: "node1",
  848. source.InstanceLabel: "node1",
  849. source.ContainerLabel: "container1",
  850. source.ResourceLabel: "memory",
  851. source.UnitLabel: "byte",
  852. },
  853. Value: 1024,
  854. AdditionalInfo: nil,
  855. },
  856. {
  857. Name: metric.PodPVCAllocation,
  858. Labels: map[string]string{
  859. source.PodLabel: unmountedPVsContainer,
  860. source.NamespaceLabel: "namespace1",
  861. source.PVCLabel: "pvc1",
  862. source.PVLabel: "vol1",
  863. },
  864. Value: 4096,
  865. AdditionalInfo: nil,
  866. },
  867. },
  868. },
  869. {
  870. name: "with namespace index",
  871. nsSetup: func(nsIndex map[string]types.UID) {
  872. nsIndex["namespace1"] = "ns-uuid1"
  873. },
  874. scrapes: []scrape{
  875. {
  876. Pods: []*clustercache.Pod{
  877. {
  878. Name: "pod1",
  879. Namespace: "namespace1",
  880. UID: "uuid1",
  881. Spec: clustercache.PodSpec{
  882. NodeName: "node1",
  883. Containers: []clustercache.Container{
  884. {
  885. Name: "container1",
  886. Resources: v1.ResourceRequirements{
  887. Requests: map[v1.ResourceName]resource.Quantity{
  888. v1.ResourceCPU: resource.MustParse("500m"),
  889. v1.ResourceMemory: resource.MustParse("512"),
  890. },
  891. Limits: map[v1.ResourceName]resource.Quantity{
  892. v1.ResourceCPU: resource.MustParse("1"),
  893. v1.ResourceMemory: resource.MustParse("1024"),
  894. },
  895. },
  896. },
  897. },
  898. },
  899. Status: clustercache.PodStatus{
  900. ContainerStatuses: []v1.ContainerStatus{
  901. {
  902. Name: "container1",
  903. State: v1.ContainerState{
  904. Running: &v1.ContainerStateRunning{},
  905. },
  906. },
  907. },
  908. },
  909. },
  910. },
  911. Timestamp: start1,
  912. },
  913. },
  914. expected: []metric.Update{
  915. {
  916. Name: metric.PodInfo,
  917. Labels: map[string]string{
  918. source.UIDLabel: "uuid1",
  919. source.PodLabel: "pod1",
  920. source.NamespaceUIDLabel: "ns-uuid1",
  921. source.NodeUIDLabel: "",
  922. source.NamespaceLabel: "namespace1",
  923. source.NodeLabel: "node1",
  924. source.InstanceLabel: "node1",
  925. },
  926. Value: 0,
  927. AdditionalInfo: map[string]string{
  928. source.UIDLabel: "uuid1",
  929. source.PodLabel: "pod1",
  930. source.NamespaceUIDLabel: "ns-uuid1",
  931. source.NodeUIDLabel: "",
  932. source.NamespaceLabel: "namespace1",
  933. source.NodeLabel: "node1",
  934. source.InstanceLabel: "node1",
  935. },
  936. },
  937. {
  938. Name: metric.KubePodLabels,
  939. Labels: map[string]string{
  940. source.UIDLabel: "uuid1",
  941. source.PodLabel: "pod1",
  942. source.NamespaceUIDLabel: "ns-uuid1",
  943. source.NodeUIDLabel: "",
  944. source.NamespaceLabel: "namespace1",
  945. source.NodeLabel: "node1",
  946. source.InstanceLabel: "node1",
  947. },
  948. Value: 0,
  949. AdditionalInfo: map[string]string{},
  950. },
  951. {
  952. Name: metric.KubePodAnnotations,
  953. Labels: map[string]string{
  954. source.UIDLabel: "uuid1",
  955. source.PodLabel: "pod1",
  956. source.NamespaceUIDLabel: "ns-uuid1",
  957. source.NodeUIDLabel: "",
  958. source.NamespaceLabel: "namespace1",
  959. source.NodeLabel: "node1",
  960. source.InstanceLabel: "node1",
  961. },
  962. Value: 0,
  963. AdditionalInfo: map[string]string{},
  964. },
  965. {
  966. Name: metric.KubePodContainerStatusRunning,
  967. Labels: map[string]string{
  968. source.UIDLabel: "uuid1",
  969. source.PodLabel: "pod1",
  970. source.NamespaceUIDLabel: "ns-uuid1",
  971. source.NodeUIDLabel: "",
  972. source.NamespaceLabel: "namespace1",
  973. source.NodeLabel: "node1",
  974. source.InstanceLabel: "node1",
  975. source.ContainerLabel: "container1",
  976. },
  977. Value: 0,
  978. AdditionalInfo: map[string]string{
  979. source.UIDLabel: "uuid1",
  980. source.PodLabel: "pod1",
  981. source.NamespaceUIDLabel: "ns-uuid1",
  982. source.NodeUIDLabel: "",
  983. source.NamespaceLabel: "namespace1",
  984. source.NodeLabel: "node1",
  985. source.InstanceLabel: "node1",
  986. source.ContainerLabel: "container1",
  987. },
  988. },
  989. {
  990. Name: metric.KubePodContainerResourceRequests,
  991. Labels: map[string]string{
  992. source.UIDLabel: "uuid1",
  993. source.PodLabel: "pod1",
  994. source.NamespaceUIDLabel: "ns-uuid1",
  995. source.NodeUIDLabel: "",
  996. source.NamespaceLabel: "namespace1",
  997. source.NodeLabel: "node1",
  998. source.InstanceLabel: "node1",
  999. source.ContainerLabel: "container1",
  1000. source.ResourceLabel: "cpu",
  1001. source.UnitLabel: "core",
  1002. },
  1003. Value: 0.5,
  1004. AdditionalInfo: nil,
  1005. },
  1006. {
  1007. Name: metric.KubePodContainerResourceRequests,
  1008. Labels: map[string]string{
  1009. source.UIDLabel: "uuid1",
  1010. source.PodLabel: "pod1",
  1011. source.NamespaceUIDLabel: "ns-uuid1",
  1012. source.NodeUIDLabel: "",
  1013. source.NamespaceLabel: "namespace1",
  1014. source.NodeLabel: "node1",
  1015. source.InstanceLabel: "node1",
  1016. source.ContainerLabel: "container1",
  1017. source.ResourceLabel: "memory",
  1018. source.UnitLabel: "byte",
  1019. },
  1020. Value: 512,
  1021. AdditionalInfo: nil,
  1022. },
  1023. {
  1024. Name: metric.KubePodContainerResourceLimits,
  1025. Labels: map[string]string{
  1026. source.UIDLabel: "uuid1",
  1027. source.PodLabel: "pod1",
  1028. source.NamespaceUIDLabel: "ns-uuid1",
  1029. source.NodeUIDLabel: "",
  1030. source.NamespaceLabel: "namespace1",
  1031. source.NodeLabel: "node1",
  1032. source.InstanceLabel: "node1",
  1033. source.ContainerLabel: "container1",
  1034. source.ResourceLabel: "cpu",
  1035. source.UnitLabel: "core",
  1036. },
  1037. Value: 1,
  1038. AdditionalInfo: nil,
  1039. },
  1040. {
  1041. Name: metric.KubePodContainerResourceLimits,
  1042. Labels: map[string]string{
  1043. source.UIDLabel: "uuid1",
  1044. source.PodLabel: "pod1",
  1045. source.NamespaceUIDLabel: "ns-uuid1",
  1046. source.NodeUIDLabel: "",
  1047. source.NamespaceLabel: "namespace1",
  1048. source.NodeLabel: "node1",
  1049. source.InstanceLabel: "node1",
  1050. source.ContainerLabel: "container1",
  1051. source.ResourceLabel: "memory",
  1052. source.UnitLabel: "byte",
  1053. },
  1054. Value: 1024,
  1055. AdditionalInfo: nil,
  1056. },
  1057. },
  1058. },
  1059. }
  1060. for _, tt := range tests {
  1061. t.Run(tt.name, func(t *testing.T) {
  1062. ks := &ClusterCacheScraper{}
  1063. nodeIndex := make(map[string]types.UID, 0)
  1064. nsIndex := make(map[string]types.UID, 0)
  1065. if tt.nsSetup != nil {
  1066. tt.nsSetup(nsIndex)
  1067. }
  1068. pvcIndex := make(map[pvcKey]types.UID, 0)
  1069. var scrapeResults []metric.Update
  1070. for _, s := range tt.scrapes {
  1071. res := ks.scrapePods(s.Pods, s.PVCs, nodeIndex, nsIndex, pvcIndex)
  1072. scrapeResults = append(scrapeResults, res...)
  1073. }
  1074. if len(scrapeResults) != len(tt.expected) {
  1075. t.Errorf("Expected result length of %d, got %d", len(tt.expected), len(scrapeResults))
  1076. }
  1077. for i, expected := range tt.expected {
  1078. got := scrapeResults[i]
  1079. if !reflect.DeepEqual(expected, got) {
  1080. t.Errorf("Result did not match expected at index %d: got %v, want %v", i, got, expected)
  1081. }
  1082. }
  1083. })
  1084. }
  1085. }
  1086. func Test_kubernetesScraper_scrapePVCs(t *testing.T) {
  1087. start1, _ := time.Parse(time.RFC3339, Start1Str)
  1088. type scrape struct {
  1089. PVCs []*clustercache.PersistentVolumeClaim
  1090. Timestamp time.Time
  1091. }
  1092. tests := []struct {
  1093. name string
  1094. nsSetup func(map[string]types.UID)
  1095. scrapes []scrape
  1096. expected []metric.Update
  1097. }{
  1098. {
  1099. name: "simple",
  1100. scrapes: []scrape{
  1101. {
  1102. PVCs: []*clustercache.PersistentVolumeClaim{
  1103. {
  1104. Name: "pvc1",
  1105. Namespace: "namespace1",
  1106. UID: "uuid1",
  1107. Spec: v1.PersistentVolumeClaimSpec{
  1108. VolumeName: "vol1",
  1109. StorageClassName: util.Ptr("storageClass1"),
  1110. Resources: v1.VolumeResourceRequirements{
  1111. Requests: v1.ResourceList{
  1112. v1.ResourceStorage: resource.MustParse("4096"),
  1113. },
  1114. },
  1115. },
  1116. },
  1117. },
  1118. Timestamp: start1,
  1119. },
  1120. },
  1121. expected: []metric.Update{
  1122. {
  1123. Name: metric.KubePersistentVolumeClaimInfo,
  1124. Labels: map[string]string{
  1125. source.UIDLabel: "uuid1",
  1126. source.PVCLabel: "pvc1",
  1127. source.NamespaceUIDLabel: "",
  1128. source.NamespaceLabel: "namespace1",
  1129. source.VolumeNameLabel: "vol1",
  1130. source.PVUIDLabel: "",
  1131. source.StorageClassLabel: "storageClass1",
  1132. },
  1133. Value: 0,
  1134. AdditionalInfo: map[string]string{
  1135. source.UIDLabel: "uuid1",
  1136. source.PVCLabel: "pvc1",
  1137. source.NamespaceUIDLabel: "",
  1138. source.NamespaceLabel: "namespace1",
  1139. source.VolumeNameLabel: "vol1",
  1140. source.PVUIDLabel: "",
  1141. source.StorageClassLabel: "storageClass1",
  1142. },
  1143. },
  1144. {
  1145. Name: metric.KubePersistentVolumeClaimResourceRequestsStorageBytes,
  1146. Labels: map[string]string{
  1147. source.UIDLabel: "uuid1",
  1148. source.PVCLabel: "pvc1",
  1149. source.NamespaceUIDLabel: "",
  1150. source.NamespaceLabel: "namespace1",
  1151. source.VolumeNameLabel: "vol1",
  1152. source.PVUIDLabel: "",
  1153. source.StorageClassLabel: "storageClass1",
  1154. },
  1155. Value: 4096,
  1156. AdditionalInfo: nil,
  1157. },
  1158. },
  1159. },
  1160. {
  1161. name: "with namespace index",
  1162. nsSetup: func(nsIndex map[string]types.UID) {
  1163. nsIndex["namespace1"] = "ns-uuid1"
  1164. },
  1165. scrapes: []scrape{
  1166. {
  1167. PVCs: []*clustercache.PersistentVolumeClaim{
  1168. {
  1169. Name: "pvc1",
  1170. Namespace: "namespace1",
  1171. UID: "uuid1",
  1172. Spec: v1.PersistentVolumeClaimSpec{
  1173. VolumeName: "vol1",
  1174. StorageClassName: util.Ptr("storageClass1"),
  1175. Resources: v1.VolumeResourceRequirements{
  1176. Requests: v1.ResourceList{
  1177. v1.ResourceStorage: resource.MustParse("4096"),
  1178. },
  1179. },
  1180. },
  1181. },
  1182. },
  1183. Timestamp: start1,
  1184. },
  1185. },
  1186. expected: []metric.Update{
  1187. {
  1188. Name: metric.KubePersistentVolumeClaimInfo,
  1189. Labels: map[string]string{
  1190. source.UIDLabel: "uuid1",
  1191. source.PVCLabel: "pvc1",
  1192. source.NamespaceUIDLabel: "ns-uuid1",
  1193. source.NamespaceLabel: "namespace1",
  1194. source.VolumeNameLabel: "vol1",
  1195. source.PVUIDLabel: "",
  1196. source.StorageClassLabel: "storageClass1",
  1197. },
  1198. Value: 0,
  1199. AdditionalInfo: map[string]string{
  1200. source.UIDLabel: "uuid1",
  1201. source.PVCLabel: "pvc1",
  1202. source.NamespaceUIDLabel: "ns-uuid1",
  1203. source.NamespaceLabel: "namespace1",
  1204. source.VolumeNameLabel: "vol1",
  1205. source.PVUIDLabel: "",
  1206. source.StorageClassLabel: "storageClass1",
  1207. },
  1208. },
  1209. {
  1210. Name: metric.KubePersistentVolumeClaimResourceRequestsStorageBytes,
  1211. Labels: map[string]string{
  1212. source.UIDLabel: "uuid1",
  1213. source.PVCLabel: "pvc1",
  1214. source.NamespaceUIDLabel: "ns-uuid1",
  1215. source.NamespaceLabel: "namespace1",
  1216. source.VolumeNameLabel: "vol1",
  1217. source.PVUIDLabel: "",
  1218. source.StorageClassLabel: "storageClass1",
  1219. },
  1220. Value: 4096,
  1221. AdditionalInfo: nil,
  1222. },
  1223. },
  1224. },
  1225. }
  1226. for _, tt := range tests {
  1227. t.Run(tt.name, func(t *testing.T) {
  1228. ks := &ClusterCacheScraper{}
  1229. nsIndex := make(map[string]types.UID, 0)
  1230. if tt.nsSetup != nil {
  1231. tt.nsSetup(nsIndex)
  1232. }
  1233. pvIndex := make(map[string]types.UID, 0)
  1234. var scrapeResults []metric.Update
  1235. for _, s := range tt.scrapes {
  1236. res := ks.scrapePVCs(s.PVCs, nsIndex, pvIndex)
  1237. scrapeResults = append(scrapeResults, res...)
  1238. }
  1239. if len(scrapeResults) != len(tt.expected) {
  1240. t.Errorf("Expected result length of %d, got %d", len(tt.expected), len(scrapeResults))
  1241. }
  1242. for i, expected := range tt.expected {
  1243. got := scrapeResults[i]
  1244. if !reflect.DeepEqual(expected, got) {
  1245. t.Errorf("Result did not match expected at index %d: got %v, want %v", i, got, expected)
  1246. }
  1247. }
  1248. })
  1249. }
  1250. }
  1251. func Test_kubernetesScraper_scrapePVs(t *testing.T) {
  1252. start1, _ := time.Parse(time.RFC3339, Start1Str)
  1253. type scrape struct {
  1254. PVs []*clustercache.PersistentVolume
  1255. Timestamp time.Time
  1256. }
  1257. tests := []struct {
  1258. name string
  1259. scrapes []scrape
  1260. expected []metric.Update
  1261. }{
  1262. {
  1263. name: "simple",
  1264. scrapes: []scrape{
  1265. {
  1266. PVs: []*clustercache.PersistentVolume{
  1267. {
  1268. Name: "pv1",
  1269. UID: "uuid1",
  1270. Spec: v1.PersistentVolumeSpec{
  1271. StorageClassName: "storageClass1",
  1272. PersistentVolumeSource: v1.PersistentVolumeSource{
  1273. CSI: &v1.CSIPersistentVolumeSource{
  1274. VolumeHandle: "vol-1",
  1275. },
  1276. },
  1277. Capacity: v1.ResourceList{
  1278. v1.ResourceStorage: resource.MustParse("4096"),
  1279. },
  1280. },
  1281. },
  1282. },
  1283. Timestamp: start1,
  1284. },
  1285. },
  1286. expected: []metric.Update{
  1287. {
  1288. Name: metric.KubecostPVInfo,
  1289. Labels: map[string]string{
  1290. source.UIDLabel: "uuid1",
  1291. source.PVLabel: "pv1",
  1292. source.StorageClassLabel: "storageClass1",
  1293. source.ProviderIDLabel: "vol-1",
  1294. source.CSIVolumeHandleLabel: "vol-1",
  1295. },
  1296. Value: 0,
  1297. AdditionalInfo: map[string]string{
  1298. source.UIDLabel: "uuid1",
  1299. source.PVLabel: "pv1",
  1300. source.StorageClassLabel: "storageClass1",
  1301. source.ProviderIDLabel: "vol-1",
  1302. source.CSIVolumeHandleLabel: "vol-1",
  1303. },
  1304. },
  1305. {
  1306. Name: metric.KubePersistentVolumeCapacityBytes,
  1307. Labels: map[string]string{
  1308. source.UIDLabel: "uuid1",
  1309. source.PVLabel: "pv1",
  1310. source.StorageClassLabel: "storageClass1",
  1311. source.ProviderIDLabel: "vol-1",
  1312. source.CSIVolumeHandleLabel: "vol-1",
  1313. },
  1314. Value: 4096,
  1315. AdditionalInfo: nil,
  1316. },
  1317. },
  1318. },
  1319. }
  1320. for _, tt := range tests {
  1321. t.Run(tt.name, func(t *testing.T) {
  1322. ks := &ClusterCacheScraper{}
  1323. var scrapeResults []metric.Update
  1324. for _, s := range tt.scrapes {
  1325. res := ks.scrapePVs(s.PVs)
  1326. scrapeResults = append(scrapeResults, res...)
  1327. }
  1328. if len(scrapeResults) != len(tt.expected) {
  1329. t.Errorf("Expected result length of %d, got %d", len(tt.expected), len(scrapeResults))
  1330. }
  1331. for i, expected := range tt.expected {
  1332. got := scrapeResults[i]
  1333. if !reflect.DeepEqual(expected, got) {
  1334. t.Errorf("Result did not match expected at index %d: got %v, want %v", i, got, expected)
  1335. }
  1336. }
  1337. })
  1338. }
  1339. }
  1340. func Test_kubernetesScraper_scrapeServices(t *testing.T) {
  1341. start1, _ := time.Parse(time.RFC3339, Start1Str)
  1342. type scrape struct {
  1343. Services []*clustercache.Service
  1344. Timestamp time.Time
  1345. }
  1346. tests := []struct {
  1347. name string
  1348. nsSetup func(map[string]types.UID)
  1349. scrapes []scrape
  1350. expected []metric.Update
  1351. }{
  1352. {
  1353. name: "simple",
  1354. scrapes: []scrape{
  1355. {
  1356. Services: []*clustercache.Service{
  1357. {
  1358. Name: "service1",
  1359. Namespace: "namespace1",
  1360. UID: "uuid1",
  1361. SpecSelector: map[string]string{
  1362. "test1": "blah",
  1363. "test2": "blah2",
  1364. },
  1365. },
  1366. },
  1367. Timestamp: start1,
  1368. },
  1369. },
  1370. expected: []metric.Update{
  1371. {
  1372. Name: metric.ServiceInfo,
  1373. Labels: map[string]string{
  1374. source.UIDLabel: "uuid1",
  1375. source.ServiceLabel: "service1",
  1376. source.NamespaceLabel: "namespace1",
  1377. source.NamespaceUIDLabel: "",
  1378. source.ServiceTypeLabel: "",
  1379. source.LBIngressAddress: "",
  1380. },
  1381. Value: 0,
  1382. AdditionalInfo: map[string]string{
  1383. source.UIDLabel: "uuid1",
  1384. source.ServiceLabel: "service1",
  1385. source.NamespaceLabel: "namespace1",
  1386. source.NamespaceUIDLabel: "",
  1387. source.ServiceTypeLabel: "",
  1388. source.LBIngressAddress: "",
  1389. },
  1390. },
  1391. {
  1392. Name: metric.ServiceSelectorLabels,
  1393. Labels: map[string]string{
  1394. source.UIDLabel: "uuid1",
  1395. source.ServiceLabel: "service1",
  1396. source.NamespaceLabel: "namespace1",
  1397. source.NamespaceUIDLabel: "",
  1398. source.ServiceTypeLabel: "",
  1399. source.LBIngressAddress: "",
  1400. },
  1401. Value: 0,
  1402. AdditionalInfo: map[string]string{
  1403. "label_test1": "blah",
  1404. "label_test2": "blah2",
  1405. },
  1406. },
  1407. },
  1408. },
  1409. {
  1410. name: "with namespace index",
  1411. nsSetup: func(nsIndex map[string]types.UID) {
  1412. nsIndex["namespace1"] = "ns-uuid1"
  1413. },
  1414. scrapes: []scrape{
  1415. {
  1416. Services: []*clustercache.Service{
  1417. {
  1418. Name: "service1",
  1419. Namespace: "namespace1",
  1420. UID: "uuid1",
  1421. },
  1422. },
  1423. Timestamp: start1,
  1424. },
  1425. },
  1426. expected: []metric.Update{
  1427. {
  1428. Name: metric.ServiceInfo,
  1429. Labels: map[string]string{
  1430. source.UIDLabel: "uuid1",
  1431. source.ServiceLabel: "service1",
  1432. source.NamespaceLabel: "namespace1",
  1433. source.NamespaceUIDLabel: "ns-uuid1",
  1434. source.ServiceTypeLabel: "",
  1435. source.LBIngressAddress: "",
  1436. },
  1437. Value: 0,
  1438. AdditionalInfo: map[string]string{
  1439. source.UIDLabel: "uuid1",
  1440. source.ServiceLabel: "service1",
  1441. source.NamespaceLabel: "namespace1",
  1442. source.NamespaceUIDLabel: "ns-uuid1",
  1443. source.ServiceTypeLabel: "",
  1444. source.LBIngressAddress: "",
  1445. },
  1446. },
  1447. {
  1448. Name: metric.ServiceSelectorLabels,
  1449. Labels: map[string]string{
  1450. source.UIDLabel: "uuid1",
  1451. source.ServiceLabel: "service1",
  1452. source.NamespaceLabel: "namespace1",
  1453. source.NamespaceUIDLabel: "ns-uuid1",
  1454. source.ServiceTypeLabel: "",
  1455. source.LBIngressAddress: "",
  1456. },
  1457. Value: 0,
  1458. AdditionalInfo: map[string]string{},
  1459. },
  1460. },
  1461. },
  1462. {
  1463. name: "with LB ingress IP",
  1464. nsSetup: func(nsIndex map[string]types.UID) {
  1465. nsIndex["namespace1"] = "ns-uuid1"
  1466. },
  1467. scrapes: []scrape{
  1468. {
  1469. Services: []*clustercache.Service{
  1470. {
  1471. Name: "service1",
  1472. Namespace: "namespace1",
  1473. UID: "uuid1",
  1474. Type: v1.ServiceTypeLoadBalancer,
  1475. Status: v1.ServiceStatus{
  1476. LoadBalancer: v1.LoadBalancerStatus{
  1477. Ingress: []v1.LoadBalancerIngress{
  1478. {IP: "1.2.3.4"},
  1479. },
  1480. },
  1481. },
  1482. },
  1483. },
  1484. Timestamp: start1,
  1485. },
  1486. },
  1487. expected: []metric.Update{
  1488. {
  1489. Name: metric.ServiceInfo,
  1490. Labels: map[string]string{
  1491. source.UIDLabel: "uuid1",
  1492. source.ServiceLabel: "service1",
  1493. source.NamespaceLabel: "namespace1",
  1494. source.NamespaceUIDLabel: "ns-uuid1",
  1495. source.ServiceTypeLabel: "LoadBalancer",
  1496. source.LBIngressAddress: "1.2.3.4",
  1497. },
  1498. Value: 0,
  1499. AdditionalInfo: map[string]string{
  1500. source.UIDLabel: "uuid1",
  1501. source.ServiceLabel: "service1",
  1502. source.NamespaceLabel: "namespace1",
  1503. source.NamespaceUIDLabel: "ns-uuid1",
  1504. source.ServiceTypeLabel: "LoadBalancer",
  1505. source.LBIngressAddress: "1.2.3.4",
  1506. },
  1507. },
  1508. {
  1509. Name: metric.ServiceSelectorLabels,
  1510. Labels: map[string]string{
  1511. source.UIDLabel: "uuid1",
  1512. source.ServiceLabel: "service1",
  1513. source.NamespaceLabel: "namespace1",
  1514. source.NamespaceUIDLabel: "ns-uuid1",
  1515. source.ServiceTypeLabel: "LoadBalancer",
  1516. source.LBIngressAddress: "1.2.3.4",
  1517. },
  1518. Value: 0,
  1519. AdditionalInfo: map[string]string{},
  1520. },
  1521. },
  1522. },
  1523. }
  1524. for _, tt := range tests {
  1525. t.Run(tt.name, func(t *testing.T) {
  1526. ks := &ClusterCacheScraper{}
  1527. nsIndex := make(map[string]types.UID)
  1528. if tt.nsSetup != nil {
  1529. tt.nsSetup(nsIndex)
  1530. }
  1531. var scrapeResults []metric.Update
  1532. for _, s := range tt.scrapes {
  1533. res := ks.scrapeServices(s.Services, nsIndex)
  1534. scrapeResults = append(scrapeResults, res...)
  1535. }
  1536. if len(scrapeResults) != len(tt.expected) {
  1537. t.Errorf("Expected result length of %d, got %d", len(tt.expected), len(scrapeResults))
  1538. }
  1539. for i, expected := range tt.expected {
  1540. got := scrapeResults[i]
  1541. if !reflect.DeepEqual(expected, got) {
  1542. t.Errorf("Result did not match expected at index %d: got %v, want %v", i, got, expected)
  1543. }
  1544. }
  1545. })
  1546. }
  1547. }
  1548. func Test_kubernetesScraper_scrapeStatefulSets(t *testing.T) {
  1549. start1, _ := time.Parse(time.RFC3339, Start1Str)
  1550. type scrape struct {
  1551. StatefulSets []*clustercache.StatefulSet
  1552. Timestamp time.Time
  1553. }
  1554. tests := []struct {
  1555. name string
  1556. nsSetup func(map[string]types.UID)
  1557. scrapes []scrape
  1558. expected []metric.Update
  1559. }{
  1560. {
  1561. name: "simple",
  1562. scrapes: []scrape{
  1563. {
  1564. StatefulSets: []*clustercache.StatefulSet{
  1565. {
  1566. Name: "statefulSet1",
  1567. Namespace: "namespace1",
  1568. UID: "uuid1",
  1569. SpecSelector: &metav1.LabelSelector{
  1570. MatchLabels: map[string]string{
  1571. "test1": "blah",
  1572. "test2": "blah2",
  1573. },
  1574. },
  1575. },
  1576. },
  1577. Timestamp: start1,
  1578. },
  1579. },
  1580. // statefulSetInfo map is shared across all 4 metrics; namespace is added
  1581. // before StatefulSetMatchLabels is appended, so all 4 reflect the final state.
  1582. expected: []metric.Update{
  1583. {
  1584. Name: metric.StatefulSetInfo,
  1585. Labels: map[string]string{
  1586. source.UIDLabel: "uuid1",
  1587. source.NamespaceUIDLabel: "",
  1588. source.StatefulSetLabel: "statefulSet1",
  1589. source.NamespaceLabel: "namespace1",
  1590. },
  1591. Value: 0,
  1592. AdditionalInfo: map[string]string{
  1593. source.UIDLabel: "uuid1",
  1594. source.NamespaceUIDLabel: "",
  1595. source.StatefulSetLabel: "statefulSet1",
  1596. source.NamespaceLabel: "namespace1",
  1597. },
  1598. },
  1599. {
  1600. Name: metric.StatefulSetLabels,
  1601. Labels: map[string]string{
  1602. source.UIDLabel: "uuid1",
  1603. source.NamespaceUIDLabel: "",
  1604. source.StatefulSetLabel: "statefulSet1",
  1605. source.NamespaceLabel: "namespace1",
  1606. },
  1607. Value: 0,
  1608. AdditionalInfo: map[string]string{},
  1609. },
  1610. {
  1611. Name: metric.StatefulSetAnnotations,
  1612. Labels: map[string]string{
  1613. source.UIDLabel: "uuid1",
  1614. source.NamespaceUIDLabel: "",
  1615. source.StatefulSetLabel: "statefulSet1",
  1616. source.NamespaceLabel: "namespace1",
  1617. },
  1618. Value: 0,
  1619. AdditionalInfo: map[string]string{},
  1620. },
  1621. {
  1622. Name: metric.StatefulSetMatchLabels,
  1623. Labels: map[string]string{
  1624. source.UIDLabel: "uuid1",
  1625. source.NamespaceUIDLabel: "",
  1626. source.StatefulSetLabel: "statefulSet1",
  1627. source.NamespaceLabel: "namespace1",
  1628. },
  1629. Value: 0,
  1630. AdditionalInfo: map[string]string{
  1631. "label_test1": "blah",
  1632. "label_test2": "blah2",
  1633. },
  1634. },
  1635. },
  1636. },
  1637. {
  1638. // statefulSetInfo map is shared; NamespaceLabel is added before MatchLabels,
  1639. // so all 4 metrics reflect the final state including namespace_uid.
  1640. name: "with namespace index",
  1641. nsSetup: func(nsIndex map[string]types.UID) {
  1642. nsIndex["namespace1"] = "ns-uuid1"
  1643. },
  1644. scrapes: []scrape{
  1645. {
  1646. StatefulSets: []*clustercache.StatefulSet{
  1647. {
  1648. Name: "statefulSet1",
  1649. Namespace: "namespace1",
  1650. UID: "uuid1",
  1651. SpecSelector: &metav1.LabelSelector{},
  1652. },
  1653. },
  1654. Timestamp: start1,
  1655. },
  1656. },
  1657. expected: []metric.Update{
  1658. {
  1659. Name: metric.StatefulSetInfo,
  1660. Labels: map[string]string{
  1661. source.UIDLabel: "uuid1",
  1662. source.NamespaceUIDLabel: "ns-uuid1",
  1663. source.StatefulSetLabel: "statefulSet1",
  1664. source.NamespaceLabel: "namespace1",
  1665. },
  1666. Value: 0,
  1667. AdditionalInfo: map[string]string{
  1668. source.UIDLabel: "uuid1",
  1669. source.NamespaceUIDLabel: "ns-uuid1",
  1670. source.StatefulSetLabel: "statefulSet1",
  1671. source.NamespaceLabel: "namespace1",
  1672. },
  1673. },
  1674. {
  1675. Name: metric.StatefulSetLabels,
  1676. Labels: map[string]string{
  1677. source.UIDLabel: "uuid1",
  1678. source.NamespaceUIDLabel: "ns-uuid1",
  1679. source.StatefulSetLabel: "statefulSet1",
  1680. source.NamespaceLabel: "namespace1",
  1681. },
  1682. Value: 0,
  1683. AdditionalInfo: map[string]string{},
  1684. },
  1685. {
  1686. Name: metric.StatefulSetAnnotations,
  1687. Labels: map[string]string{
  1688. source.UIDLabel: "uuid1",
  1689. source.NamespaceUIDLabel: "ns-uuid1",
  1690. source.StatefulSetLabel: "statefulSet1",
  1691. source.NamespaceLabel: "namespace1",
  1692. },
  1693. Value: 0,
  1694. AdditionalInfo: map[string]string{},
  1695. },
  1696. {
  1697. Name: metric.StatefulSetMatchLabels,
  1698. Labels: map[string]string{
  1699. source.UIDLabel: "uuid1",
  1700. source.NamespaceUIDLabel: "ns-uuid1",
  1701. source.StatefulSetLabel: "statefulSet1",
  1702. source.NamespaceLabel: "namespace1",
  1703. },
  1704. Value: 0,
  1705. AdditionalInfo: map[string]string{},
  1706. },
  1707. },
  1708. },
  1709. }
  1710. for _, tt := range tests {
  1711. t.Run(tt.name, func(t *testing.T) {
  1712. ks := &ClusterCacheScraper{}
  1713. nsIndex := make(map[string]types.UID, 0)
  1714. if tt.nsSetup != nil {
  1715. tt.nsSetup(nsIndex)
  1716. }
  1717. var scrapeResults []metric.Update
  1718. for _, s := range tt.scrapes {
  1719. res := ks.scrapeStatefulSets(s.StatefulSets, nsIndex)
  1720. scrapeResults = append(scrapeResults, res...)
  1721. }
  1722. if len(scrapeResults) != len(tt.expected) {
  1723. t.Errorf("Expected result length of %d, got %d", len(tt.expected), len(scrapeResults))
  1724. }
  1725. for i, expected := range tt.expected {
  1726. got := scrapeResults[i]
  1727. if !reflect.DeepEqual(expected, got) {
  1728. t.Errorf("Result did not match expected at index %d: got %v, want %v", i, got, expected)
  1729. }
  1730. }
  1731. })
  1732. }
  1733. }
  1734. func Test_kubernetesScraper_scrapeReplicaSets(t *testing.T) {
  1735. start1, _ := time.Parse(time.RFC3339, Start1Str)
  1736. type scrape struct {
  1737. ReplicaSets []*clustercache.ReplicaSet
  1738. Timestamp time.Time
  1739. }
  1740. tests := []struct {
  1741. name string
  1742. nsSetup func(map[string]types.UID)
  1743. scrapes []scrape
  1744. expected []metric.Update
  1745. }{
  1746. {
  1747. name: "simple",
  1748. scrapes: []scrape{
  1749. {
  1750. ReplicaSets: []*clustercache.ReplicaSet{
  1751. {
  1752. Name: "replicaSet1",
  1753. Namespace: "namespace1",
  1754. UID: "uuid1",
  1755. OwnerReferences: []metav1.OwnerReference{
  1756. {
  1757. Name: "rollout1",
  1758. Kind: "Rollout",
  1759. },
  1760. },
  1761. },
  1762. {
  1763. Name: "pureReplicaSet",
  1764. Namespace: "namespace1",
  1765. UID: "uuid2",
  1766. OwnerReferences: []metav1.OwnerReference{},
  1767. },
  1768. },
  1769. Timestamp: start1,
  1770. },
  1771. },
  1772. expected: []metric.Update{
  1773. // replicaSet1: info/labels/annotations use replicaSetInfo (uid, namespace_uid, replicaset)
  1774. {
  1775. Name: metric.ReplicaSetInfo,
  1776. Labels: map[string]string{
  1777. source.UIDLabel: "uuid1",
  1778. source.NamespaceUIDLabel: "",
  1779. source.ReplicaSetLabel: "replicaSet1",
  1780. },
  1781. Value: 0,
  1782. AdditionalInfo: map[string]string{
  1783. source.UIDLabel: "uuid1",
  1784. source.NamespaceUIDLabel: "",
  1785. source.ReplicaSetLabel: "replicaSet1",
  1786. },
  1787. },
  1788. {
  1789. Name: metric.ReplicaSetLabels,
  1790. Labels: map[string]string{
  1791. source.UIDLabel: "uuid1",
  1792. source.NamespaceUIDLabel: "",
  1793. source.ReplicaSetLabel: "replicaSet1",
  1794. },
  1795. Value: 0,
  1796. AdditionalInfo: map[string]string{},
  1797. },
  1798. {
  1799. Name: metric.ReplicaSetAnnotations,
  1800. Labels: map[string]string{
  1801. source.UIDLabel: "uuid1",
  1802. source.NamespaceUIDLabel: "",
  1803. source.ReplicaSetLabel: "replicaSet1",
  1804. },
  1805. Value: 0,
  1806. AdditionalInfo: map[string]string{},
  1807. },
  1808. // replicaSet1 owner: uses replicaSetOwnerInfo (replicaset, namespace, uid) + owner fields
  1809. {
  1810. Name: metric.KubeReplicasetOwner,
  1811. Labels: map[string]string{
  1812. source.ReplicaSetLabel: "replicaSet1",
  1813. source.NamespaceLabel: "namespace1",
  1814. source.UIDLabel: "uuid1",
  1815. source.OwnerNameLabel: "rollout1",
  1816. source.OwnerKindLabel: "Rollout",
  1817. source.OwnerUIDLabel: "",
  1818. source.ControllerLabel: "false",
  1819. },
  1820. Value: 0,
  1821. AdditionalInfo: map[string]string{
  1822. source.ReplicaSetLabel: "replicaSet1",
  1823. source.NamespaceLabel: "namespace1",
  1824. source.UIDLabel: "uuid1",
  1825. source.OwnerNameLabel: "rollout1",
  1826. source.OwnerKindLabel: "Rollout",
  1827. source.OwnerUIDLabel: "",
  1828. source.ControllerLabel: "false",
  1829. },
  1830. },
  1831. // pureReplicaSet: info/labels/annotations
  1832. {
  1833. Name: metric.ReplicaSetInfo,
  1834. Labels: map[string]string{
  1835. source.UIDLabel: "uuid2",
  1836. source.NamespaceUIDLabel: "",
  1837. source.ReplicaSetLabel: "pureReplicaSet",
  1838. },
  1839. Value: 0,
  1840. AdditionalInfo: map[string]string{
  1841. source.UIDLabel: "uuid2",
  1842. source.NamespaceUIDLabel: "",
  1843. source.ReplicaSetLabel: "pureReplicaSet",
  1844. },
  1845. },
  1846. {
  1847. Name: metric.ReplicaSetLabels,
  1848. Labels: map[string]string{
  1849. source.UIDLabel: "uuid2",
  1850. source.NamespaceUIDLabel: "",
  1851. source.ReplicaSetLabel: "pureReplicaSet",
  1852. },
  1853. Value: 0,
  1854. AdditionalInfo: map[string]string{},
  1855. },
  1856. {
  1857. Name: metric.ReplicaSetAnnotations,
  1858. Labels: map[string]string{
  1859. source.UIDLabel: "uuid2",
  1860. source.NamespaceUIDLabel: "",
  1861. source.ReplicaSetLabel: "pureReplicaSet",
  1862. },
  1863. Value: 0,
  1864. AdditionalInfo: map[string]string{},
  1865. },
  1866. // pureReplicaSet owner: no owners path uses <none> sentinel, no owner_uid/controller
  1867. {
  1868. Name: metric.KubeReplicasetOwner,
  1869. Labels: map[string]string{
  1870. source.ReplicaSetLabel: "pureReplicaSet",
  1871. source.NamespaceLabel: "namespace1",
  1872. source.UIDLabel: "uuid2",
  1873. source.OwnerNameLabel: source.NoneLabelValue,
  1874. source.OwnerKindLabel: source.NoneLabelValue,
  1875. source.ControllerLabel: "false",
  1876. },
  1877. Value: 0,
  1878. AdditionalInfo: map[string]string{
  1879. source.ReplicaSetLabel: "pureReplicaSet",
  1880. source.NamespaceLabel: "namespace1",
  1881. source.UIDLabel: "uuid2",
  1882. source.OwnerNameLabel: source.NoneLabelValue,
  1883. source.OwnerKindLabel: source.NoneLabelValue,
  1884. source.ControllerLabel: "false",
  1885. },
  1886. },
  1887. },
  1888. },
  1889. {
  1890. name: "with namespace index",
  1891. nsSetup: func(nsIndex map[string]types.UID) {
  1892. nsIndex["namespace1"] = "ns-uuid1"
  1893. },
  1894. scrapes: []scrape{
  1895. {
  1896. ReplicaSets: []*clustercache.ReplicaSet{
  1897. {
  1898. Name: "replicaSet1",
  1899. Namespace: "namespace1",
  1900. UID: "uuid1",
  1901. OwnerReferences: []metav1.OwnerReference{},
  1902. },
  1903. },
  1904. Timestamp: start1,
  1905. },
  1906. },
  1907. expected: []metric.Update{
  1908. {
  1909. Name: metric.ReplicaSetInfo,
  1910. Labels: map[string]string{
  1911. source.UIDLabel: "uuid1",
  1912. source.NamespaceUIDLabel: "ns-uuid1",
  1913. source.ReplicaSetLabel: "replicaSet1",
  1914. },
  1915. Value: 0,
  1916. AdditionalInfo: map[string]string{
  1917. source.UIDLabel: "uuid1",
  1918. source.NamespaceUIDLabel: "ns-uuid1",
  1919. source.ReplicaSetLabel: "replicaSet1",
  1920. },
  1921. },
  1922. {
  1923. Name: metric.ReplicaSetLabels,
  1924. Labels: map[string]string{
  1925. source.UIDLabel: "uuid1",
  1926. source.NamespaceUIDLabel: "ns-uuid1",
  1927. source.ReplicaSetLabel: "replicaSet1",
  1928. },
  1929. Value: 0,
  1930. AdditionalInfo: map[string]string{},
  1931. },
  1932. {
  1933. Name: metric.ReplicaSetAnnotations,
  1934. Labels: map[string]string{
  1935. source.UIDLabel: "uuid1",
  1936. source.NamespaceUIDLabel: "ns-uuid1",
  1937. source.ReplicaSetLabel: "replicaSet1",
  1938. },
  1939. Value: 0,
  1940. AdditionalInfo: map[string]string{},
  1941. },
  1942. {
  1943. Name: metric.KubeReplicasetOwner,
  1944. Labels: map[string]string{
  1945. source.ReplicaSetLabel: "replicaSet1",
  1946. source.NamespaceLabel: "namespace1",
  1947. source.UIDLabel: "uuid1",
  1948. source.OwnerNameLabel: source.NoneLabelValue,
  1949. source.OwnerKindLabel: source.NoneLabelValue,
  1950. source.ControllerLabel: "false",
  1951. },
  1952. Value: 0,
  1953. AdditionalInfo: map[string]string{
  1954. source.ReplicaSetLabel: "replicaSet1",
  1955. source.NamespaceLabel: "namespace1",
  1956. source.UIDLabel: "uuid1",
  1957. source.OwnerNameLabel: source.NoneLabelValue,
  1958. source.OwnerKindLabel: source.NoneLabelValue,
  1959. source.ControllerLabel: "false",
  1960. },
  1961. },
  1962. },
  1963. },
  1964. }
  1965. for _, tt := range tests {
  1966. t.Run(tt.name, func(t *testing.T) {
  1967. ks := &ClusterCacheScraper{}
  1968. nsIndex := make(map[string]types.UID, 0)
  1969. if tt.nsSetup != nil {
  1970. tt.nsSetup(nsIndex)
  1971. }
  1972. var scrapeResults []metric.Update
  1973. for _, s := range tt.scrapes {
  1974. res := ks.scrapeReplicaSets(s.ReplicaSets, nsIndex)
  1975. scrapeResults = append(scrapeResults, res...)
  1976. }
  1977. if len(scrapeResults) != len(tt.expected) {
  1978. t.Errorf("Expected result length of %d, got %d", len(tt.expected), len(scrapeResults))
  1979. }
  1980. for i, expected := range tt.expected {
  1981. got := scrapeResults[i]
  1982. if !reflect.DeepEqual(expected, got) {
  1983. t.Errorf("Result did not match expected at index %d: got %v, want %v", i, got, expected)
  1984. }
  1985. }
  1986. })
  1987. }
  1988. }
  1989. func Test_kubernetesScraper_scrapeResourceQuotas(t *testing.T) {
  1990. start1, _ := time.Parse(time.RFC3339, Start1Str)
  1991. type scrape struct {
  1992. ResourceQuotas []*clustercache.ResourceQuota
  1993. Timestamp time.Time
  1994. }
  1995. tests := []struct {
  1996. name string
  1997. nsSetup func(map[string]types.UID)
  1998. scrapes []scrape
  1999. expected []metric.Update
  2000. }{
  2001. {
  2002. name: "simple",
  2003. scrapes: []scrape{
  2004. {
  2005. ResourceQuotas: []*clustercache.ResourceQuota{
  2006. {
  2007. Name: "resourceQuota1",
  2008. Namespace: "namespace1",
  2009. UID: "uuid1",
  2010. Spec: v1.ResourceQuotaSpec{
  2011. Hard: v1.ResourceList{
  2012. v1.ResourceRequestsCPU: resource.MustParse("1"),
  2013. v1.ResourceRequestsMemory: resource.MustParse("1024"),
  2014. v1.ResourceLimitsCPU: resource.MustParse("2"),
  2015. v1.ResourceLimitsMemory: resource.MustParse("2048"),
  2016. },
  2017. },
  2018. Status: v1.ResourceQuotaStatus{
  2019. Used: v1.ResourceList{
  2020. v1.ResourceRequestsCPU: resource.MustParse("0.5"),
  2021. v1.ResourceRequestsMemory: resource.MustParse("512"),
  2022. v1.ResourceLimitsCPU: resource.MustParse("1"),
  2023. v1.ResourceLimitsMemory: resource.MustParse("1024"),
  2024. },
  2025. },
  2026. },
  2027. },
  2028. Timestamp: start1,
  2029. },
  2030. },
  2031. expected: []metric.Update{
  2032. {
  2033. Name: metric.ResourceQuotaInfo,
  2034. Labels: map[string]string{
  2035. source.UIDLabel: "uuid1",
  2036. source.NamespaceUIDLabel: "",
  2037. source.ResourceQuotaLabel: "resourceQuota1",
  2038. },
  2039. Value: 0,
  2040. AdditionalInfo: map[string]string{
  2041. source.UIDLabel: "uuid1",
  2042. source.NamespaceUIDLabel: "",
  2043. source.ResourceQuotaLabel: "resourceQuota1",
  2044. },
  2045. },
  2046. {
  2047. Name: metric.KubeResourceQuotaSpecResourceRequests,
  2048. Labels: map[string]string{
  2049. source.UIDLabel: "uuid1",
  2050. source.NamespaceUIDLabel: "",
  2051. source.ResourceQuotaLabel: "resourceQuota1",
  2052. source.ResourceLabel: "cpu",
  2053. source.UnitLabel: "core",
  2054. },
  2055. Value: 1,
  2056. AdditionalInfo: nil,
  2057. },
  2058. {
  2059. Name: metric.KubeResourceQuotaSpecResourceRequests,
  2060. Labels: map[string]string{
  2061. source.UIDLabel: "uuid1",
  2062. source.NamespaceUIDLabel: "",
  2063. source.ResourceQuotaLabel: "resourceQuota1",
  2064. source.ResourceLabel: "memory",
  2065. source.UnitLabel: "byte",
  2066. },
  2067. Value: 1024,
  2068. AdditionalInfo: nil,
  2069. },
  2070. {
  2071. Name: metric.KubeResourceQuotaSpecResourceLimits,
  2072. Labels: map[string]string{
  2073. source.UIDLabel: "uuid1",
  2074. source.NamespaceUIDLabel: "",
  2075. source.ResourceQuotaLabel: "resourceQuota1",
  2076. source.ResourceLabel: "cpu",
  2077. source.UnitLabel: "core",
  2078. },
  2079. Value: 2,
  2080. AdditionalInfo: nil,
  2081. },
  2082. {
  2083. Name: metric.KubeResourceQuotaSpecResourceLimits,
  2084. Labels: map[string]string{
  2085. source.UIDLabel: "uuid1",
  2086. source.NamespaceUIDLabel: "",
  2087. source.ResourceQuotaLabel: "resourceQuota1",
  2088. source.ResourceLabel: "memory",
  2089. source.UnitLabel: "byte",
  2090. },
  2091. Value: 2048,
  2092. AdditionalInfo: nil,
  2093. },
  2094. {
  2095. Name: metric.KubeResourceQuotaStatusUsedResourceRequests,
  2096. Labels: map[string]string{
  2097. source.UIDLabel: "uuid1",
  2098. source.NamespaceUIDLabel: "",
  2099. source.ResourceQuotaLabel: "resourceQuota1",
  2100. source.ResourceLabel: "cpu",
  2101. source.UnitLabel: "core",
  2102. },
  2103. Value: 0.5,
  2104. AdditionalInfo: nil,
  2105. },
  2106. {
  2107. Name: metric.KubeResourceQuotaStatusUsedResourceRequests,
  2108. Labels: map[string]string{
  2109. source.UIDLabel: "uuid1",
  2110. source.NamespaceUIDLabel: "",
  2111. source.ResourceQuotaLabel: "resourceQuota1",
  2112. source.ResourceLabel: "memory",
  2113. source.UnitLabel: "byte",
  2114. },
  2115. Value: 512,
  2116. AdditionalInfo: nil,
  2117. },
  2118. {
  2119. Name: metric.KubeResourceQuotaStatusUsedResourceLimits,
  2120. Labels: map[string]string{
  2121. source.UIDLabel: "uuid1",
  2122. source.NamespaceUIDLabel: "",
  2123. source.ResourceQuotaLabel: "resourceQuota1",
  2124. source.ResourceLabel: "cpu",
  2125. source.UnitLabel: "core",
  2126. },
  2127. Value: 1,
  2128. AdditionalInfo: nil,
  2129. },
  2130. {
  2131. Name: metric.KubeResourceQuotaStatusUsedResourceLimits,
  2132. Labels: map[string]string{
  2133. source.UIDLabel: "uuid1",
  2134. source.NamespaceUIDLabel: "",
  2135. source.ResourceQuotaLabel: "resourceQuota1",
  2136. source.ResourceLabel: "memory",
  2137. source.UnitLabel: "byte",
  2138. },
  2139. Value: 1024,
  2140. AdditionalInfo: nil,
  2141. },
  2142. },
  2143. },
  2144. {
  2145. name: "with namespace index",
  2146. nsSetup: func(nsIndex map[string]types.UID) {
  2147. nsIndex["namespace1"] = "ns-uuid1"
  2148. },
  2149. scrapes: []scrape{
  2150. {
  2151. ResourceQuotas: []*clustercache.ResourceQuota{
  2152. {
  2153. Name: "resourceQuota1",
  2154. Namespace: "namespace1",
  2155. UID: "uuid1",
  2156. Spec: v1.ResourceQuotaSpec{
  2157. Hard: v1.ResourceList{
  2158. v1.ResourceRequestsCPU: resource.MustParse("1"),
  2159. },
  2160. },
  2161. },
  2162. },
  2163. Timestamp: start1,
  2164. },
  2165. },
  2166. expected: []metric.Update{
  2167. {
  2168. Name: metric.ResourceQuotaInfo,
  2169. Labels: map[string]string{
  2170. source.UIDLabel: "uuid1",
  2171. source.NamespaceUIDLabel: "ns-uuid1",
  2172. source.ResourceQuotaLabel: "resourceQuota1",
  2173. },
  2174. Value: 0,
  2175. AdditionalInfo: map[string]string{
  2176. source.UIDLabel: "uuid1",
  2177. source.NamespaceUIDLabel: "ns-uuid1",
  2178. source.ResourceQuotaLabel: "resourceQuota1",
  2179. },
  2180. },
  2181. {
  2182. Name: metric.KubeResourceQuotaSpecResourceRequests,
  2183. Labels: map[string]string{
  2184. source.UIDLabel: "uuid1",
  2185. source.NamespaceUIDLabel: "ns-uuid1",
  2186. source.ResourceQuotaLabel: "resourceQuota1",
  2187. source.ResourceLabel: "cpu",
  2188. source.UnitLabel: "core",
  2189. },
  2190. Value: 1,
  2191. AdditionalInfo: nil,
  2192. },
  2193. },
  2194. },
  2195. }
  2196. for _, tt := range tests {
  2197. t.Run(tt.name, func(t *testing.T) {
  2198. ks := &ClusterCacheScraper{}
  2199. nsIndex := make(map[string]types.UID, 0)
  2200. if tt.nsSetup != nil {
  2201. tt.nsSetup(nsIndex)
  2202. }
  2203. var scrapeResults []metric.Update
  2204. for _, s := range tt.scrapes {
  2205. res := ks.scrapeResourceQuotas(s.ResourceQuotas, nsIndex)
  2206. scrapeResults = append(scrapeResults, res...)
  2207. }
  2208. if len(scrapeResults) != len(tt.expected) {
  2209. t.Errorf("Expected result length of %d, got %d", len(tt.expected), len(scrapeResults))
  2210. }
  2211. for i, expected := range tt.expected {
  2212. got := scrapeResults[i]
  2213. if !reflect.DeepEqual(expected, got) {
  2214. t.Errorf("Result did not match expected at index %d: got %v, want %v", i, got, expected)
  2215. }
  2216. }
  2217. })
  2218. }
  2219. }
  2220. func Test_kubernetesScraper_scrapeDaemonSets(t *testing.T) {
  2221. start1, _ := time.Parse(time.RFC3339, Start1Str)
  2222. type scrape struct {
  2223. DaemonSets []*clustercache.DaemonSet
  2224. Timestamp time.Time
  2225. }
  2226. tests := []struct {
  2227. name string
  2228. nsSetup func(map[string]types.UID)
  2229. scrapes []scrape
  2230. expected []metric.Update
  2231. }{
  2232. {
  2233. name: "simple",
  2234. scrapes: []scrape{
  2235. {
  2236. DaemonSets: []*clustercache.DaemonSet{
  2237. {
  2238. Name: "daemonSet1",
  2239. Namespace: "namespace1",
  2240. UID: "uuid1",
  2241. },
  2242. },
  2243. Timestamp: start1,
  2244. },
  2245. },
  2246. expected: []metric.Update{
  2247. {
  2248. Name: metric.DaemonSetInfo,
  2249. Labels: map[string]string{
  2250. source.UIDLabel: "uuid1",
  2251. source.NamespaceUIDLabel: "",
  2252. source.DaemonSetLabel: "daemonSet1",
  2253. },
  2254. Value: 0,
  2255. AdditionalInfo: map[string]string{
  2256. source.UIDLabel: "uuid1",
  2257. source.NamespaceUIDLabel: "",
  2258. source.DaemonSetLabel: "daemonSet1",
  2259. },
  2260. },
  2261. {
  2262. Name: metric.DaemonSetLabels,
  2263. Labels: map[string]string{
  2264. source.UIDLabel: "uuid1",
  2265. source.NamespaceUIDLabel: "",
  2266. source.DaemonSetLabel: "daemonSet1",
  2267. },
  2268. Value: 0,
  2269. AdditionalInfo: map[string]string{},
  2270. },
  2271. {
  2272. Name: metric.DaemonSetAnnotations,
  2273. Labels: map[string]string{
  2274. source.UIDLabel: "uuid1",
  2275. source.NamespaceUIDLabel: "",
  2276. source.DaemonSetLabel: "daemonSet1",
  2277. },
  2278. Value: 0,
  2279. AdditionalInfo: map[string]string{},
  2280. },
  2281. },
  2282. },
  2283. {
  2284. name: "with namespace index",
  2285. nsSetup: func(nsIndex map[string]types.UID) {
  2286. nsIndex["namespace1"] = "ns-uuid1"
  2287. },
  2288. scrapes: []scrape{
  2289. {
  2290. DaemonSets: []*clustercache.DaemonSet{
  2291. {
  2292. Name: "daemonSet1",
  2293. Namespace: "namespace1",
  2294. UID: "uuid1",
  2295. },
  2296. },
  2297. Timestamp: start1,
  2298. },
  2299. },
  2300. expected: []metric.Update{
  2301. {
  2302. Name: metric.DaemonSetInfo,
  2303. Labels: map[string]string{
  2304. source.UIDLabel: "uuid1",
  2305. source.NamespaceUIDLabel: "ns-uuid1",
  2306. source.DaemonSetLabel: "daemonSet1",
  2307. },
  2308. Value: 0,
  2309. AdditionalInfo: map[string]string{
  2310. source.UIDLabel: "uuid1",
  2311. source.NamespaceUIDLabel: "ns-uuid1",
  2312. source.DaemonSetLabel: "daemonSet1",
  2313. },
  2314. },
  2315. {
  2316. Name: metric.DaemonSetLabels,
  2317. Labels: map[string]string{
  2318. source.UIDLabel: "uuid1",
  2319. source.NamespaceUIDLabel: "ns-uuid1",
  2320. source.DaemonSetLabel: "daemonSet1",
  2321. },
  2322. Value: 0,
  2323. AdditionalInfo: map[string]string{},
  2324. },
  2325. {
  2326. Name: metric.DaemonSetAnnotations,
  2327. Labels: map[string]string{
  2328. source.UIDLabel: "uuid1",
  2329. source.NamespaceUIDLabel: "ns-uuid1",
  2330. source.DaemonSetLabel: "daemonSet1",
  2331. },
  2332. Value: 0,
  2333. AdditionalInfo: map[string]string{},
  2334. },
  2335. },
  2336. },
  2337. }
  2338. for _, tt := range tests {
  2339. t.Run(tt.name, func(t *testing.T) {
  2340. ks := &ClusterCacheScraper{}
  2341. nsIndex := make(map[string]types.UID, 0)
  2342. if tt.nsSetup != nil {
  2343. tt.nsSetup(nsIndex)
  2344. }
  2345. var scrapeResults []metric.Update
  2346. for _, s := range tt.scrapes {
  2347. res := ks.scrapeDaemonSets(s.DaemonSets, nsIndex)
  2348. scrapeResults = append(scrapeResults, res...)
  2349. }
  2350. if len(scrapeResults) != len(tt.expected) {
  2351. t.Errorf("Expected result length of %d, got %d", len(tt.expected), len(scrapeResults))
  2352. }
  2353. for i, expected := range tt.expected {
  2354. got := scrapeResults[i]
  2355. if !reflect.DeepEqual(expected, got) {
  2356. t.Errorf("Result did not match expected at index %d: got %v, want %v", i, got, expected)
  2357. }
  2358. }
  2359. })
  2360. }
  2361. }
  2362. func Test_kubernetesScraper_scrapeJobs(t *testing.T) {
  2363. start1, _ := time.Parse(time.RFC3339, Start1Str)
  2364. type scrape struct {
  2365. Jobs []*clustercache.Job
  2366. Timestamp time.Time
  2367. }
  2368. tests := []struct {
  2369. name string
  2370. nsSetup func(map[string]types.UID)
  2371. scrapes []scrape
  2372. expected []metric.Update
  2373. }{
  2374. {
  2375. name: "simple",
  2376. scrapes: []scrape{
  2377. {
  2378. Jobs: []*clustercache.Job{
  2379. {
  2380. Name: "job1",
  2381. Namespace: "namespace1",
  2382. UID: "uuid1",
  2383. },
  2384. },
  2385. Timestamp: start1,
  2386. },
  2387. },
  2388. expected: []metric.Update{
  2389. {
  2390. Name: metric.JobInfo,
  2391. Labels: map[string]string{
  2392. source.UIDLabel: "uuid1",
  2393. source.NamespaceUIDLabel: "",
  2394. source.JobLabel: "job1",
  2395. },
  2396. Value: 0,
  2397. AdditionalInfo: map[string]string{
  2398. source.UIDLabel: "uuid1",
  2399. source.NamespaceUIDLabel: "",
  2400. source.JobLabel: "job1",
  2401. },
  2402. },
  2403. {
  2404. Name: metric.JobLabels,
  2405. Labels: map[string]string{
  2406. source.UIDLabel: "uuid1",
  2407. source.NamespaceUIDLabel: "",
  2408. source.JobLabel: "job1",
  2409. },
  2410. Value: 0,
  2411. AdditionalInfo: map[string]string{},
  2412. },
  2413. {
  2414. Name: metric.JobAnnotations,
  2415. Labels: map[string]string{
  2416. source.UIDLabel: "uuid1",
  2417. source.NamespaceUIDLabel: "",
  2418. source.JobLabel: "job1",
  2419. },
  2420. Value: 0,
  2421. AdditionalInfo: map[string]string{},
  2422. },
  2423. },
  2424. },
  2425. {
  2426. name: "with namespace index",
  2427. nsSetup: func(nsIndex map[string]types.UID) {
  2428. nsIndex["namespace1"] = "ns-uuid1"
  2429. },
  2430. scrapes: []scrape{
  2431. {
  2432. Jobs: []*clustercache.Job{
  2433. {
  2434. Name: "job1",
  2435. Namespace: "namespace1",
  2436. UID: "uuid1",
  2437. },
  2438. },
  2439. Timestamp: start1,
  2440. },
  2441. },
  2442. expected: []metric.Update{
  2443. {
  2444. Name: metric.JobInfo,
  2445. Labels: map[string]string{
  2446. source.UIDLabel: "uuid1",
  2447. source.NamespaceUIDLabel: "ns-uuid1",
  2448. source.JobLabel: "job1",
  2449. },
  2450. Value: 0,
  2451. AdditionalInfo: map[string]string{
  2452. source.UIDLabel: "uuid1",
  2453. source.NamespaceUIDLabel: "ns-uuid1",
  2454. source.JobLabel: "job1",
  2455. },
  2456. },
  2457. {
  2458. Name: metric.JobLabels,
  2459. Labels: map[string]string{
  2460. source.UIDLabel: "uuid1",
  2461. source.NamespaceUIDLabel: "ns-uuid1",
  2462. source.JobLabel: "job1",
  2463. },
  2464. Value: 0,
  2465. AdditionalInfo: map[string]string{},
  2466. },
  2467. {
  2468. Name: metric.JobAnnotations,
  2469. Labels: map[string]string{
  2470. source.UIDLabel: "uuid1",
  2471. source.NamespaceUIDLabel: "ns-uuid1",
  2472. source.JobLabel: "job1",
  2473. },
  2474. Value: 0,
  2475. AdditionalInfo: map[string]string{},
  2476. },
  2477. },
  2478. },
  2479. }
  2480. for _, tt := range tests {
  2481. t.Run(tt.name, func(t *testing.T) {
  2482. ks := &ClusterCacheScraper{}
  2483. nsIndex := make(map[string]types.UID, 0)
  2484. if tt.nsSetup != nil {
  2485. tt.nsSetup(nsIndex)
  2486. }
  2487. var scrapeResults []metric.Update
  2488. for _, s := range tt.scrapes {
  2489. res := ks.scrapeJobs(s.Jobs, nsIndex)
  2490. scrapeResults = append(scrapeResults, res...)
  2491. }
  2492. if len(scrapeResults) != len(tt.expected) {
  2493. t.Errorf("Expected result length of %d, got %d", len(tt.expected), len(scrapeResults))
  2494. }
  2495. for i, expected := range tt.expected {
  2496. got := scrapeResults[i]
  2497. if !reflect.DeepEqual(expected, got) {
  2498. t.Errorf("Result did not match expected at index %d: got %v, want %v", i, got, expected)
  2499. }
  2500. }
  2501. })
  2502. }
  2503. }
  2504. func Test_kubernetesScraper_scrapeCronJobs(t *testing.T) {
  2505. start1, _ := time.Parse(time.RFC3339, Start1Str)
  2506. type scrape struct {
  2507. CronJobs []*clustercache.CronJob
  2508. Timestamp time.Time
  2509. }
  2510. tests := []struct {
  2511. name string
  2512. nsSetup func(map[string]types.UID)
  2513. scrapes []scrape
  2514. expected []metric.Update
  2515. }{
  2516. {
  2517. name: "simple",
  2518. scrapes: []scrape{
  2519. {
  2520. CronJobs: []*clustercache.CronJob{
  2521. {
  2522. Name: "cronJob1",
  2523. Namespace: "namespace1",
  2524. UID: "uuid1",
  2525. },
  2526. },
  2527. Timestamp: start1,
  2528. },
  2529. },
  2530. expected: []metric.Update{
  2531. {
  2532. Name: metric.CronJobInfo,
  2533. Labels: map[string]string{
  2534. source.UIDLabel: "uuid1",
  2535. source.NamespaceUIDLabel: "",
  2536. source.CronJobLabel: "cronJob1",
  2537. },
  2538. Value: 0,
  2539. AdditionalInfo: map[string]string{
  2540. source.UIDLabel: "uuid1",
  2541. source.NamespaceUIDLabel: "",
  2542. source.CronJobLabel: "cronJob1",
  2543. },
  2544. },
  2545. {
  2546. Name: metric.CronJobLabels,
  2547. Labels: map[string]string{
  2548. source.UIDLabel: "uuid1",
  2549. source.NamespaceUIDLabel: "",
  2550. source.CronJobLabel: "cronJob1",
  2551. },
  2552. Value: 0,
  2553. AdditionalInfo: map[string]string{},
  2554. },
  2555. {
  2556. Name: metric.CronJobAnnotations,
  2557. Labels: map[string]string{
  2558. source.UIDLabel: "uuid1",
  2559. source.NamespaceUIDLabel: "",
  2560. source.CronJobLabel: "cronJob1",
  2561. },
  2562. Value: 0,
  2563. AdditionalInfo: map[string]string{},
  2564. },
  2565. },
  2566. },
  2567. {
  2568. name: "with namespace index",
  2569. nsSetup: func(nsIndex map[string]types.UID) {
  2570. nsIndex["namespace1"] = "ns-uuid1"
  2571. },
  2572. scrapes: []scrape{
  2573. {
  2574. CronJobs: []*clustercache.CronJob{
  2575. {
  2576. Name: "cronJob1",
  2577. Namespace: "namespace1",
  2578. UID: "uuid1",
  2579. },
  2580. },
  2581. Timestamp: start1,
  2582. },
  2583. },
  2584. expected: []metric.Update{
  2585. {
  2586. Name: metric.CronJobInfo,
  2587. Labels: map[string]string{
  2588. source.UIDLabel: "uuid1",
  2589. source.NamespaceUIDLabel: "ns-uuid1",
  2590. source.CronJobLabel: "cronJob1",
  2591. },
  2592. Value: 0,
  2593. AdditionalInfo: map[string]string{
  2594. source.UIDLabel: "uuid1",
  2595. source.NamespaceUIDLabel: "ns-uuid1",
  2596. source.CronJobLabel: "cronJob1",
  2597. },
  2598. },
  2599. {
  2600. Name: metric.CronJobLabels,
  2601. Labels: map[string]string{
  2602. source.UIDLabel: "uuid1",
  2603. source.NamespaceUIDLabel: "ns-uuid1",
  2604. source.CronJobLabel: "cronJob1",
  2605. },
  2606. Value: 0,
  2607. AdditionalInfo: map[string]string{},
  2608. },
  2609. {
  2610. Name: metric.CronJobAnnotations,
  2611. Labels: map[string]string{
  2612. source.UIDLabel: "uuid1",
  2613. source.NamespaceUIDLabel: "ns-uuid1",
  2614. source.CronJobLabel: "cronJob1",
  2615. },
  2616. Value: 0,
  2617. AdditionalInfo: map[string]string{},
  2618. },
  2619. },
  2620. },
  2621. }
  2622. for _, tt := range tests {
  2623. t.Run(tt.name, func(t *testing.T) {
  2624. ks := &ClusterCacheScraper{}
  2625. nsIndex := make(map[string]types.UID, 0)
  2626. if tt.nsSetup != nil {
  2627. tt.nsSetup(nsIndex)
  2628. }
  2629. var scrapeResults []metric.Update
  2630. for _, s := range tt.scrapes {
  2631. res := ks.scrapeCronJobs(s.CronJobs, nsIndex)
  2632. scrapeResults = append(scrapeResults, res...)
  2633. }
  2634. if len(scrapeResults) != len(tt.expected) {
  2635. t.Errorf("Expected result length of %d, got %d", len(tt.expected), len(scrapeResults))
  2636. }
  2637. for i, expected := range tt.expected {
  2638. got := scrapeResults[i]
  2639. if !reflect.DeepEqual(expected, got) {
  2640. t.Errorf("Result did not match expected at index %d: got %v, want %v", i, got, expected)
  2641. }
  2642. }
  2643. })
  2644. }
  2645. }