clustercache_test.go 89 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270
  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. // Non-CSI PV: provider ID comes from the in-tree AWS EBS source and
  1321. // the csi_volume_handle label must not be present at all.
  1322. name: "aws ebs non-csi pv omits csi_volume_handle label",
  1323. scrapes: []scrape{
  1324. {
  1325. PVs: []*clustercache.PersistentVolume{
  1326. {
  1327. Name: "pv-aws",
  1328. UID: "uuid-aws",
  1329. Spec: v1.PersistentVolumeSpec{
  1330. StorageClassName: "gp2",
  1331. PersistentVolumeSource: v1.PersistentVolumeSource{
  1332. AWSElasticBlockStore: &v1.AWSElasticBlockStoreVolumeSource{
  1333. VolumeID: "aws://us-east-2a/vol-0fc54c5e83b8d2b76",
  1334. },
  1335. },
  1336. Capacity: v1.ResourceList{
  1337. v1.ResourceStorage: resource.MustParse("8192"),
  1338. },
  1339. },
  1340. },
  1341. },
  1342. Timestamp: start1,
  1343. },
  1344. },
  1345. expected: []metric.Update{
  1346. {
  1347. Name: metric.KubecostPVInfo,
  1348. Labels: map[string]string{
  1349. source.UIDLabel: "uuid-aws",
  1350. source.PVLabel: "pv-aws",
  1351. source.StorageClassLabel: "gp2",
  1352. source.ProviderIDLabel: "vol-0fc54c5e83b8d2b76",
  1353. },
  1354. Value: 0,
  1355. AdditionalInfo: map[string]string{
  1356. source.UIDLabel: "uuid-aws",
  1357. source.PVLabel: "pv-aws",
  1358. source.StorageClassLabel: "gp2",
  1359. source.ProviderIDLabel: "vol-0fc54c5e83b8d2b76",
  1360. },
  1361. },
  1362. {
  1363. Name: metric.KubePersistentVolumeCapacityBytes,
  1364. Labels: map[string]string{
  1365. source.UIDLabel: "uuid-aws",
  1366. source.PVLabel: "pv-aws",
  1367. source.StorageClassLabel: "gp2",
  1368. source.ProviderIDLabel: "vol-0fc54c5e83b8d2b76",
  1369. },
  1370. Value: 8192,
  1371. AdditionalInfo: nil,
  1372. },
  1373. },
  1374. },
  1375. {
  1376. // GCE PD in-tree source: provider ID is the PD name, no csi label.
  1377. name: "gce pd non-csi pv omits csi_volume_handle label",
  1378. scrapes: []scrape{
  1379. {
  1380. PVs: []*clustercache.PersistentVolume{
  1381. {
  1382. Name: "pv-gce",
  1383. UID: "uuid-gce",
  1384. Spec: v1.PersistentVolumeSpec{
  1385. StorageClassName: "standard",
  1386. PersistentVolumeSource: v1.PersistentVolumeSource{
  1387. GCEPersistentDisk: &v1.GCEPersistentDiskVolumeSource{
  1388. PDName: "gke-pvc-abc123",
  1389. },
  1390. },
  1391. },
  1392. },
  1393. },
  1394. Timestamp: start1,
  1395. },
  1396. },
  1397. expected: []metric.Update{
  1398. {
  1399. Name: metric.KubecostPVInfo,
  1400. Labels: map[string]string{
  1401. source.UIDLabel: "uuid-gce",
  1402. source.PVLabel: "pv-gce",
  1403. source.StorageClassLabel: "standard",
  1404. source.ProviderIDLabel: "gke-pvc-abc123",
  1405. },
  1406. Value: 0,
  1407. AdditionalInfo: map[string]string{
  1408. source.UIDLabel: "uuid-gce",
  1409. source.PVLabel: "pv-gce",
  1410. source.StorageClassLabel: "standard",
  1411. source.ProviderIDLabel: "gke-pvc-abc123",
  1412. },
  1413. },
  1414. },
  1415. },
  1416. {
  1417. // CSI source present but VolumeHandle empty: csi_volume_handle label
  1418. // is omitted, and provider_id currently ends up empty (documents the
  1419. // gap where the old code fell back to pv.Name).
  1420. name: "csi pv with empty volume handle omits csi label and has empty provider id",
  1421. scrapes: []scrape{
  1422. {
  1423. PVs: []*clustercache.PersistentVolume{
  1424. {
  1425. Name: "pv-csi-empty",
  1426. UID: "uuid-csi-empty",
  1427. Spec: v1.PersistentVolumeSpec{
  1428. StorageClassName: "gp3",
  1429. PersistentVolumeSource: v1.PersistentVolumeSource{
  1430. CSI: &v1.CSIPersistentVolumeSource{
  1431. VolumeHandle: "",
  1432. },
  1433. },
  1434. },
  1435. },
  1436. },
  1437. Timestamp: start1,
  1438. },
  1439. },
  1440. expected: []metric.Update{
  1441. {
  1442. Name: metric.KubecostPVInfo,
  1443. Labels: map[string]string{
  1444. source.UIDLabel: "uuid-csi-empty",
  1445. source.PVLabel: "pv-csi-empty",
  1446. source.StorageClassLabel: "gp3",
  1447. source.ProviderIDLabel: "",
  1448. },
  1449. Value: 0,
  1450. AdditionalInfo: map[string]string{
  1451. source.UIDLabel: "uuid-csi-empty",
  1452. source.PVLabel: "pv-csi-empty",
  1453. source.StorageClassLabel: "gp3",
  1454. source.ProviderIDLabel: "",
  1455. },
  1456. },
  1457. },
  1458. },
  1459. {
  1460. // No recognized volume source: provider ID falls back to pv.Name.
  1461. name: "pv with no known volume source falls back to name",
  1462. scrapes: []scrape{
  1463. {
  1464. PVs: []*clustercache.PersistentVolume{
  1465. {
  1466. Name: "pv-nfs",
  1467. UID: "uuid-nfs",
  1468. Spec: v1.PersistentVolumeSpec{
  1469. StorageClassName: "nfs",
  1470. },
  1471. },
  1472. },
  1473. Timestamp: start1,
  1474. },
  1475. },
  1476. expected: []metric.Update{
  1477. {
  1478. Name: metric.KubecostPVInfo,
  1479. Labels: map[string]string{
  1480. source.UIDLabel: "uuid-nfs",
  1481. source.PVLabel: "pv-nfs",
  1482. source.StorageClassLabel: "nfs",
  1483. source.ProviderIDLabel: "pv-nfs",
  1484. },
  1485. Value: 0,
  1486. AdditionalInfo: map[string]string{
  1487. source.UIDLabel: "uuid-nfs",
  1488. source.PVLabel: "pv-nfs",
  1489. source.StorageClassLabel: "nfs",
  1490. source.ProviderIDLabel: "pv-nfs",
  1491. },
  1492. },
  1493. },
  1494. },
  1495. }
  1496. for _, tt := range tests {
  1497. t.Run(tt.name, func(t *testing.T) {
  1498. ks := &ClusterCacheScraper{}
  1499. var scrapeResults []metric.Update
  1500. for _, s := range tt.scrapes {
  1501. res := ks.scrapePVs(s.PVs)
  1502. scrapeResults = append(scrapeResults, res...)
  1503. }
  1504. if len(scrapeResults) != len(tt.expected) {
  1505. t.Fatalf("Expected result length of %d, got %d: %+v", len(tt.expected), len(scrapeResults), scrapeResults)
  1506. }
  1507. for i, expected := range tt.expected {
  1508. got := scrapeResults[i]
  1509. if !reflect.DeepEqual(expected, got) {
  1510. t.Errorf("Result did not match expected at index %d: got %v, want %v", i, got, expected)
  1511. }
  1512. // csi_volume_handle must only be present for CSI volumes with a
  1513. // non-empty handle.
  1514. if _, ok := got.Labels[source.CSIVolumeHandleLabel]; ok {
  1515. if got.Labels[source.CSIVolumeHandleLabel] == "" {
  1516. t.Errorf("index %d: csi_volume_handle label present but empty", i)
  1517. }
  1518. }
  1519. }
  1520. })
  1521. }
  1522. }
  1523. func Test_getPVProviderID(t *testing.T) {
  1524. tests := []struct {
  1525. name string
  1526. pv *clustercache.PersistentVolume
  1527. want string
  1528. }{
  1529. {
  1530. name: "gce persistent disk uses pd name",
  1531. pv: &clustercache.PersistentVolume{
  1532. Name: "pv-gce",
  1533. Spec: v1.PersistentVolumeSpec{
  1534. PersistentVolumeSource: v1.PersistentVolumeSource{
  1535. GCEPersistentDisk: &v1.GCEPersistentDiskVolumeSource{PDName: "gke-pd-1"},
  1536. },
  1537. },
  1538. },
  1539. want: "gke-pd-1",
  1540. },
  1541. {
  1542. name: "azure disk uses disk name",
  1543. pv: &clustercache.PersistentVolume{
  1544. Name: "pv-azure",
  1545. Spec: v1.PersistentVolumeSpec{
  1546. PersistentVolumeSource: v1.PersistentVolumeSource{
  1547. AzureDisk: &v1.AzureDiskVolumeSource{DiskName: "azure-disk-1"},
  1548. },
  1549. },
  1550. },
  1551. want: "azure-disk-1",
  1552. },
  1553. {
  1554. name: "aws ebs with aws:// prefixed volume id is parsed",
  1555. pv: &clustercache.PersistentVolume{
  1556. Name: "pv-aws",
  1557. Spec: v1.PersistentVolumeSpec{
  1558. PersistentVolumeSource: v1.PersistentVolumeSource{
  1559. AWSElasticBlockStore: &v1.AWSElasticBlockStoreVolumeSource{
  1560. VolumeID: "aws://us-east-2a/vol-0fc54c5e83b8d2b76",
  1561. },
  1562. },
  1563. },
  1564. },
  1565. want: "vol-0fc54c5e83b8d2b76",
  1566. },
  1567. {
  1568. name: "aws ebs with bare volume id is left unchanged",
  1569. pv: &clustercache.PersistentVolume{
  1570. Name: "pv-aws",
  1571. Spec: v1.PersistentVolumeSpec{
  1572. PersistentVolumeSource: v1.PersistentVolumeSource{
  1573. AWSElasticBlockStore: &v1.AWSElasticBlockStoreVolumeSource{
  1574. VolumeID: "vol-abc123",
  1575. },
  1576. },
  1577. },
  1578. },
  1579. want: "vol-abc123",
  1580. },
  1581. {
  1582. name: "aws ebs with empty volume id yields empty string",
  1583. pv: &clustercache.PersistentVolume{
  1584. Name: "pv-aws",
  1585. Spec: v1.PersistentVolumeSpec{
  1586. PersistentVolumeSource: v1.PersistentVolumeSource{
  1587. AWSElasticBlockStore: &v1.AWSElasticBlockStoreVolumeSource{VolumeID: ""},
  1588. },
  1589. },
  1590. },
  1591. want: "",
  1592. },
  1593. {
  1594. name: "csi uses volume handle",
  1595. pv: &clustercache.PersistentVolume{
  1596. Name: "pv-csi",
  1597. Spec: v1.PersistentVolumeSpec{
  1598. PersistentVolumeSource: v1.PersistentVolumeSource{
  1599. CSI: &v1.CSIPersistentVolumeSource{VolumeHandle: "vol-csi-1"},
  1600. },
  1601. },
  1602. },
  1603. want: "vol-csi-1",
  1604. },
  1605. {
  1606. // Documents current behavior: a CSI source with an empty handle
  1607. // returns "" rather than falling back to pv.Name.
  1608. name: "csi with empty volume handle returns empty string",
  1609. pv: &clustercache.PersistentVolume{
  1610. Name: "pv-csi",
  1611. Spec: v1.PersistentVolumeSpec{
  1612. PersistentVolumeSource: v1.PersistentVolumeSource{
  1613. CSI: &v1.CSIPersistentVolumeSource{VolumeHandle: ""},
  1614. },
  1615. },
  1616. },
  1617. want: "",
  1618. },
  1619. {
  1620. name: "no recognized source falls back to pv name",
  1621. pv: &clustercache.PersistentVolume{
  1622. Name: "pv-nfs",
  1623. Spec: v1.PersistentVolumeSpec{},
  1624. },
  1625. want: "pv-nfs",
  1626. },
  1627. {
  1628. // GCE branch is checked before CSI, so GCE wins when both are set.
  1629. name: "gce takes precedence over csi",
  1630. pv: &clustercache.PersistentVolume{
  1631. Name: "pv-both",
  1632. Spec: v1.PersistentVolumeSpec{
  1633. PersistentVolumeSource: v1.PersistentVolumeSource{
  1634. GCEPersistentDisk: &v1.GCEPersistentDiskVolumeSource{PDName: "gce-wins"},
  1635. CSI: &v1.CSIPersistentVolumeSource{VolumeHandle: "csi-loses"},
  1636. },
  1637. },
  1638. },
  1639. want: "gce-wins",
  1640. },
  1641. }
  1642. for _, tt := range tests {
  1643. t.Run(tt.name, func(t *testing.T) {
  1644. if got := getPVProviderID(tt.pv); got != tt.want {
  1645. t.Errorf("getPVProviderID() = %q, want %q", got, tt.want)
  1646. }
  1647. })
  1648. }
  1649. }
  1650. func Test_persistentVolumeAWSRegex(t *testing.T) {
  1651. tests := []struct {
  1652. name string
  1653. input string
  1654. want string // expected capture group 1, or "" for no match
  1655. }{
  1656. {
  1657. name: "standard aws:// volume id",
  1658. input: "aws://us-east-2a/vol-0fc54c5e83b8d2b76",
  1659. want: "vol-0fc54c5e83b8d2b76",
  1660. },
  1661. {
  1662. name: "trailing path segment stops at slash",
  1663. input: "aws://us-east-2a/vol-123/extra",
  1664. want: "vol-123",
  1665. },
  1666. {
  1667. name: "bare volume id does not match",
  1668. input: "vol-abc123",
  1669. want: "",
  1670. },
  1671. {
  1672. name: "too few segments does not match",
  1673. input: "aws://vol-123",
  1674. want: "",
  1675. },
  1676. {
  1677. name: "empty string does not match",
  1678. input: "",
  1679. want: "",
  1680. },
  1681. }
  1682. for _, tt := range tests {
  1683. t.Run(tt.name, func(t *testing.T) {
  1684. match := persistentVolumeAWSRegex.FindStringSubmatch(tt.input)
  1685. got := ""
  1686. if len(match) >= 2 {
  1687. got = match[1]
  1688. }
  1689. if got != tt.want {
  1690. t.Errorf("persistentVolumeAWSRegex on %q = %q, want %q", tt.input, got, tt.want)
  1691. }
  1692. })
  1693. }
  1694. }
  1695. func TestToResourceUnitValue(t *testing.T) {
  1696. tests := []struct {
  1697. name string
  1698. resourceName v1.ResourceName
  1699. quantity resource.Quantity
  1700. wantResource string
  1701. wantUnit string
  1702. wantValue float64
  1703. }{
  1704. {
  1705. name: "cpu is reported in cores",
  1706. resourceName: v1.ResourceCPU,
  1707. quantity: resource.MustParse("500m"),
  1708. wantResource: "cpu",
  1709. wantUnit: "core",
  1710. wantValue: 0.5,
  1711. },
  1712. {
  1713. name: "memory is reported in bytes",
  1714. resourceName: v1.ResourceMemory,
  1715. quantity: resource.MustParse("1Ki"),
  1716. wantResource: "memory",
  1717. wantUnit: "byte",
  1718. wantValue: 1024,
  1719. },
  1720. {
  1721. name: "storage is reported in bytes",
  1722. resourceName: v1.ResourceStorage,
  1723. quantity: resource.MustParse("2Ki"),
  1724. wantResource: "storage",
  1725. wantUnit: "byte",
  1726. wantValue: 2048,
  1727. },
  1728. {
  1729. name: "ephemeral storage is reported in bytes",
  1730. resourceName: v1.ResourceEphemeralStorage,
  1731. quantity: resource.MustParse("3Ki"),
  1732. wantResource: "ephemeral-storage",
  1733. wantUnit: "byte",
  1734. wantValue: 3072,
  1735. },
  1736. {
  1737. name: "pods are reported as integers",
  1738. resourceName: v1.ResourcePods,
  1739. quantity: resource.MustParse("10"),
  1740. wantResource: "pods",
  1741. wantUnit: "integer",
  1742. wantValue: 10,
  1743. },
  1744. {
  1745. // Regression guard: the resource name is no longer sanitized, so the
  1746. // hyphen and case are preserved verbatim ("hugepages-2Mi", not
  1747. // "hugepages_2Mi").
  1748. name: "huge pages keep their raw name and are bytes",
  1749. resourceName: v1.ResourceName(v1.ResourceHugePagesPrefix + "2Mi"),
  1750. quantity: resource.MustParse("4Ki"),
  1751. wantResource: "hugepages-2Mi",
  1752. wantUnit: "byte",
  1753. wantValue: 4096,
  1754. },
  1755. {
  1756. // Regression guard: extended resource names keep '.' and '/'
  1757. // ("nvidia.com/gpu", not "nvidia_com_gpu").
  1758. name: "extended resource keeps dotted slashed name and is integer",
  1759. resourceName: v1.ResourceName("nvidia.com/gpu"),
  1760. quantity: resource.MustParse("2"),
  1761. wantResource: "nvidia.com/gpu",
  1762. wantUnit: "integer",
  1763. wantValue: 2,
  1764. },
  1765. {
  1766. name: "attachable volume resource keeps raw name and is bytes",
  1767. resourceName: v1.ResourceName(v1.ResourceAttachableVolumesPrefix + "aws-ebs"),
  1768. quantity: resource.MustParse("5"),
  1769. wantResource: "attachable-volumes-aws-ebs",
  1770. wantUnit: "byte",
  1771. wantValue: 5,
  1772. },
  1773. {
  1774. name: "unrecognized native resource returns empty",
  1775. resourceName: v1.ResourceName("kubernetes.io/somethingelse"),
  1776. quantity: resource.MustParse("1"),
  1777. wantResource: "",
  1778. wantUnit: "",
  1779. wantValue: 0,
  1780. },
  1781. }
  1782. for _, tt := range tests {
  1783. t.Run(tt.name, func(t *testing.T) {
  1784. gotResource, gotUnit, gotValue := toResourceUnitValue(tt.resourceName, tt.quantity)
  1785. if gotResource != tt.wantResource {
  1786. t.Errorf("resource = %q, want %q", gotResource, tt.wantResource)
  1787. }
  1788. if gotUnit != tt.wantUnit {
  1789. t.Errorf("unit = %q, want %q", gotUnit, tt.wantUnit)
  1790. }
  1791. if gotValue != tt.wantValue {
  1792. t.Errorf("value = %v, want %v", gotValue, tt.wantValue)
  1793. }
  1794. })
  1795. }
  1796. }
  1797. func Test_kubernetesScraper_scrapeServices(t *testing.T) {
  1798. start1, _ := time.Parse(time.RFC3339, Start1Str)
  1799. type scrape struct {
  1800. Services []*clustercache.Service
  1801. Timestamp time.Time
  1802. }
  1803. tests := []struct {
  1804. name string
  1805. nsSetup func(map[string]types.UID)
  1806. scrapes []scrape
  1807. expected []metric.Update
  1808. }{
  1809. {
  1810. name: "simple",
  1811. scrapes: []scrape{
  1812. {
  1813. Services: []*clustercache.Service{
  1814. {
  1815. Name: "service1",
  1816. Namespace: "namespace1",
  1817. UID: "uuid1",
  1818. SpecSelector: map[string]string{
  1819. "test1": "blah",
  1820. "test2": "blah2",
  1821. },
  1822. },
  1823. },
  1824. Timestamp: start1,
  1825. },
  1826. },
  1827. expected: []metric.Update{
  1828. {
  1829. Name: metric.ServiceInfo,
  1830. Labels: map[string]string{
  1831. source.UIDLabel: "uuid1",
  1832. source.ServiceLabel: "service1",
  1833. source.NamespaceLabel: "namespace1",
  1834. source.NamespaceUIDLabel: "",
  1835. source.ServiceTypeLabel: "",
  1836. source.LBIngressAddress: "",
  1837. },
  1838. Value: 0,
  1839. AdditionalInfo: map[string]string{
  1840. source.UIDLabel: "uuid1",
  1841. source.ServiceLabel: "service1",
  1842. source.NamespaceLabel: "namespace1",
  1843. source.NamespaceUIDLabel: "",
  1844. source.ServiceTypeLabel: "",
  1845. source.LBIngressAddress: "",
  1846. },
  1847. },
  1848. {
  1849. Name: metric.ServiceSelectorLabels,
  1850. Labels: map[string]string{
  1851. source.UIDLabel: "uuid1",
  1852. source.ServiceLabel: "service1",
  1853. source.NamespaceLabel: "namespace1",
  1854. source.NamespaceUIDLabel: "",
  1855. source.ServiceTypeLabel: "",
  1856. source.LBIngressAddress: "",
  1857. },
  1858. Value: 0,
  1859. AdditionalInfo: map[string]string{
  1860. "label_test1": "blah",
  1861. "label_test2": "blah2",
  1862. },
  1863. },
  1864. },
  1865. },
  1866. {
  1867. name: "with namespace index",
  1868. nsSetup: func(nsIndex map[string]types.UID) {
  1869. nsIndex["namespace1"] = "ns-uuid1"
  1870. },
  1871. scrapes: []scrape{
  1872. {
  1873. Services: []*clustercache.Service{
  1874. {
  1875. Name: "service1",
  1876. Namespace: "namespace1",
  1877. UID: "uuid1",
  1878. },
  1879. },
  1880. Timestamp: start1,
  1881. },
  1882. },
  1883. expected: []metric.Update{
  1884. {
  1885. Name: metric.ServiceInfo,
  1886. Labels: map[string]string{
  1887. source.UIDLabel: "uuid1",
  1888. source.ServiceLabel: "service1",
  1889. source.NamespaceLabel: "namespace1",
  1890. source.NamespaceUIDLabel: "ns-uuid1",
  1891. source.ServiceTypeLabel: "",
  1892. source.LBIngressAddress: "",
  1893. },
  1894. Value: 0,
  1895. AdditionalInfo: map[string]string{
  1896. source.UIDLabel: "uuid1",
  1897. source.ServiceLabel: "service1",
  1898. source.NamespaceLabel: "namespace1",
  1899. source.NamespaceUIDLabel: "ns-uuid1",
  1900. source.ServiceTypeLabel: "",
  1901. source.LBIngressAddress: "",
  1902. },
  1903. },
  1904. {
  1905. Name: metric.ServiceSelectorLabels,
  1906. Labels: map[string]string{
  1907. source.UIDLabel: "uuid1",
  1908. source.ServiceLabel: "service1",
  1909. source.NamespaceLabel: "namespace1",
  1910. source.NamespaceUIDLabel: "ns-uuid1",
  1911. source.ServiceTypeLabel: "",
  1912. source.LBIngressAddress: "",
  1913. },
  1914. Value: 0,
  1915. AdditionalInfo: map[string]string{},
  1916. },
  1917. },
  1918. },
  1919. {
  1920. name: "with LB ingress IP",
  1921. nsSetup: func(nsIndex map[string]types.UID) {
  1922. nsIndex["namespace1"] = "ns-uuid1"
  1923. },
  1924. scrapes: []scrape{
  1925. {
  1926. Services: []*clustercache.Service{
  1927. {
  1928. Name: "service1",
  1929. Namespace: "namespace1",
  1930. UID: "uuid1",
  1931. Type: v1.ServiceTypeLoadBalancer,
  1932. Status: v1.ServiceStatus{
  1933. LoadBalancer: v1.LoadBalancerStatus{
  1934. Ingress: []v1.LoadBalancerIngress{
  1935. {IP: "1.2.3.4"},
  1936. },
  1937. },
  1938. },
  1939. },
  1940. },
  1941. Timestamp: start1,
  1942. },
  1943. },
  1944. expected: []metric.Update{
  1945. {
  1946. Name: metric.ServiceInfo,
  1947. Labels: map[string]string{
  1948. source.UIDLabel: "uuid1",
  1949. source.ServiceLabel: "service1",
  1950. source.NamespaceLabel: "namespace1",
  1951. source.NamespaceUIDLabel: "ns-uuid1",
  1952. source.ServiceTypeLabel: "LoadBalancer",
  1953. source.LBIngressAddress: "1.2.3.4",
  1954. },
  1955. Value: 0,
  1956. AdditionalInfo: map[string]string{
  1957. source.UIDLabel: "uuid1",
  1958. source.ServiceLabel: "service1",
  1959. source.NamespaceLabel: "namespace1",
  1960. source.NamespaceUIDLabel: "ns-uuid1",
  1961. source.ServiceTypeLabel: "LoadBalancer",
  1962. source.LBIngressAddress: "1.2.3.4",
  1963. },
  1964. },
  1965. {
  1966. Name: metric.ServiceSelectorLabels,
  1967. Labels: map[string]string{
  1968. source.UIDLabel: "uuid1",
  1969. source.ServiceLabel: "service1",
  1970. source.NamespaceLabel: "namespace1",
  1971. source.NamespaceUIDLabel: "ns-uuid1",
  1972. source.ServiceTypeLabel: "LoadBalancer",
  1973. source.LBIngressAddress: "1.2.3.4",
  1974. },
  1975. Value: 0,
  1976. AdditionalInfo: map[string]string{},
  1977. },
  1978. },
  1979. },
  1980. }
  1981. for _, tt := range tests {
  1982. t.Run(tt.name, func(t *testing.T) {
  1983. ks := &ClusterCacheScraper{}
  1984. nsIndex := make(map[string]types.UID)
  1985. if tt.nsSetup != nil {
  1986. tt.nsSetup(nsIndex)
  1987. }
  1988. var scrapeResults []metric.Update
  1989. for _, s := range tt.scrapes {
  1990. res := ks.scrapeServices(s.Services, nsIndex)
  1991. scrapeResults = append(scrapeResults, res...)
  1992. }
  1993. if len(scrapeResults) != len(tt.expected) {
  1994. t.Errorf("Expected result length of %d, got %d", len(tt.expected), len(scrapeResults))
  1995. }
  1996. for i, expected := range tt.expected {
  1997. got := scrapeResults[i]
  1998. if !reflect.DeepEqual(expected, got) {
  1999. t.Errorf("Result did not match expected at index %d: got %v, want %v", i, got, expected)
  2000. }
  2001. }
  2002. })
  2003. }
  2004. }
  2005. func Test_kubernetesScraper_scrapeStatefulSets(t *testing.T) {
  2006. start1, _ := time.Parse(time.RFC3339, Start1Str)
  2007. type scrape struct {
  2008. StatefulSets []*clustercache.StatefulSet
  2009. Timestamp time.Time
  2010. }
  2011. tests := []struct {
  2012. name string
  2013. nsSetup func(map[string]types.UID)
  2014. scrapes []scrape
  2015. expected []metric.Update
  2016. }{
  2017. {
  2018. name: "simple",
  2019. scrapes: []scrape{
  2020. {
  2021. StatefulSets: []*clustercache.StatefulSet{
  2022. {
  2023. Name: "statefulSet1",
  2024. Namespace: "namespace1",
  2025. UID: "uuid1",
  2026. SpecSelector: &metav1.LabelSelector{
  2027. MatchLabels: map[string]string{
  2028. "test1": "blah",
  2029. "test2": "blah2",
  2030. },
  2031. },
  2032. },
  2033. },
  2034. Timestamp: start1,
  2035. },
  2036. },
  2037. // statefulSetInfo map is shared across all 4 metrics; namespace is added
  2038. // before StatefulSetMatchLabels is appended, so all 4 reflect the final state.
  2039. expected: []metric.Update{
  2040. {
  2041. Name: metric.StatefulSetInfo,
  2042. Labels: map[string]string{
  2043. source.UIDLabel: "uuid1",
  2044. source.NamespaceUIDLabel: "",
  2045. source.StatefulSetLabel: "statefulSet1",
  2046. source.NamespaceLabel: "namespace1",
  2047. },
  2048. Value: 0,
  2049. AdditionalInfo: map[string]string{
  2050. source.UIDLabel: "uuid1",
  2051. source.NamespaceUIDLabel: "",
  2052. source.StatefulSetLabel: "statefulSet1",
  2053. source.NamespaceLabel: "namespace1",
  2054. },
  2055. },
  2056. {
  2057. Name: metric.StatefulSetLabels,
  2058. Labels: map[string]string{
  2059. source.UIDLabel: "uuid1",
  2060. source.NamespaceUIDLabel: "",
  2061. source.StatefulSetLabel: "statefulSet1",
  2062. source.NamespaceLabel: "namespace1",
  2063. },
  2064. Value: 0,
  2065. AdditionalInfo: map[string]string{},
  2066. },
  2067. {
  2068. Name: metric.StatefulSetAnnotations,
  2069. Labels: map[string]string{
  2070. source.UIDLabel: "uuid1",
  2071. source.NamespaceUIDLabel: "",
  2072. source.StatefulSetLabel: "statefulSet1",
  2073. source.NamespaceLabel: "namespace1",
  2074. },
  2075. Value: 0,
  2076. AdditionalInfo: map[string]string{},
  2077. },
  2078. {
  2079. Name: metric.StatefulSetMatchLabels,
  2080. Labels: map[string]string{
  2081. source.UIDLabel: "uuid1",
  2082. source.NamespaceUIDLabel: "",
  2083. source.StatefulSetLabel: "statefulSet1",
  2084. source.NamespaceLabel: "namespace1",
  2085. },
  2086. Value: 0,
  2087. AdditionalInfo: map[string]string{
  2088. "label_test1": "blah",
  2089. "label_test2": "blah2",
  2090. },
  2091. },
  2092. },
  2093. },
  2094. {
  2095. // statefulSetInfo map is shared; NamespaceLabel is added before MatchLabels,
  2096. // so all 4 metrics reflect the final state including namespace_uid.
  2097. name: "with namespace index",
  2098. nsSetup: func(nsIndex map[string]types.UID) {
  2099. nsIndex["namespace1"] = "ns-uuid1"
  2100. },
  2101. scrapes: []scrape{
  2102. {
  2103. StatefulSets: []*clustercache.StatefulSet{
  2104. {
  2105. Name: "statefulSet1",
  2106. Namespace: "namespace1",
  2107. UID: "uuid1",
  2108. SpecSelector: &metav1.LabelSelector{},
  2109. },
  2110. },
  2111. Timestamp: start1,
  2112. },
  2113. },
  2114. expected: []metric.Update{
  2115. {
  2116. Name: metric.StatefulSetInfo,
  2117. Labels: map[string]string{
  2118. source.UIDLabel: "uuid1",
  2119. source.NamespaceUIDLabel: "ns-uuid1",
  2120. source.StatefulSetLabel: "statefulSet1",
  2121. source.NamespaceLabel: "namespace1",
  2122. },
  2123. Value: 0,
  2124. AdditionalInfo: map[string]string{
  2125. source.UIDLabel: "uuid1",
  2126. source.NamespaceUIDLabel: "ns-uuid1",
  2127. source.StatefulSetLabel: "statefulSet1",
  2128. source.NamespaceLabel: "namespace1",
  2129. },
  2130. },
  2131. {
  2132. Name: metric.StatefulSetLabels,
  2133. Labels: map[string]string{
  2134. source.UIDLabel: "uuid1",
  2135. source.NamespaceUIDLabel: "ns-uuid1",
  2136. source.StatefulSetLabel: "statefulSet1",
  2137. source.NamespaceLabel: "namespace1",
  2138. },
  2139. Value: 0,
  2140. AdditionalInfo: map[string]string{},
  2141. },
  2142. {
  2143. Name: metric.StatefulSetAnnotations,
  2144. Labels: map[string]string{
  2145. source.UIDLabel: "uuid1",
  2146. source.NamespaceUIDLabel: "ns-uuid1",
  2147. source.StatefulSetLabel: "statefulSet1",
  2148. source.NamespaceLabel: "namespace1",
  2149. },
  2150. Value: 0,
  2151. AdditionalInfo: map[string]string{},
  2152. },
  2153. {
  2154. Name: metric.StatefulSetMatchLabels,
  2155. Labels: map[string]string{
  2156. source.UIDLabel: "uuid1",
  2157. source.NamespaceUIDLabel: "ns-uuid1",
  2158. source.StatefulSetLabel: "statefulSet1",
  2159. source.NamespaceLabel: "namespace1",
  2160. },
  2161. Value: 0,
  2162. AdditionalInfo: map[string]string{},
  2163. },
  2164. },
  2165. },
  2166. }
  2167. for _, tt := range tests {
  2168. t.Run(tt.name, func(t *testing.T) {
  2169. ks := &ClusterCacheScraper{}
  2170. nsIndex := make(map[string]types.UID, 0)
  2171. if tt.nsSetup != nil {
  2172. tt.nsSetup(nsIndex)
  2173. }
  2174. var scrapeResults []metric.Update
  2175. for _, s := range tt.scrapes {
  2176. res := ks.scrapeStatefulSets(s.StatefulSets, nsIndex)
  2177. scrapeResults = append(scrapeResults, res...)
  2178. }
  2179. if len(scrapeResults) != len(tt.expected) {
  2180. t.Errorf("Expected result length of %d, got %d", len(tt.expected), len(scrapeResults))
  2181. }
  2182. for i, expected := range tt.expected {
  2183. got := scrapeResults[i]
  2184. if !reflect.DeepEqual(expected, got) {
  2185. t.Errorf("Result did not match expected at index %d: got %v, want %v", i, got, expected)
  2186. }
  2187. }
  2188. })
  2189. }
  2190. }
  2191. func Test_kubernetesScraper_scrapeReplicaSets(t *testing.T) {
  2192. start1, _ := time.Parse(time.RFC3339, Start1Str)
  2193. type scrape struct {
  2194. ReplicaSets []*clustercache.ReplicaSet
  2195. Timestamp time.Time
  2196. }
  2197. tests := []struct {
  2198. name string
  2199. nsSetup func(map[string]types.UID)
  2200. scrapes []scrape
  2201. expected []metric.Update
  2202. }{
  2203. {
  2204. name: "simple",
  2205. scrapes: []scrape{
  2206. {
  2207. ReplicaSets: []*clustercache.ReplicaSet{
  2208. {
  2209. Name: "replicaSet1",
  2210. Namespace: "namespace1",
  2211. UID: "uuid1",
  2212. OwnerReferences: []metav1.OwnerReference{
  2213. {
  2214. Name: "rollout1",
  2215. Kind: "Rollout",
  2216. },
  2217. },
  2218. },
  2219. {
  2220. Name: "pureReplicaSet",
  2221. Namespace: "namespace1",
  2222. UID: "uuid2",
  2223. OwnerReferences: []metav1.OwnerReference{},
  2224. },
  2225. },
  2226. Timestamp: start1,
  2227. },
  2228. },
  2229. expected: []metric.Update{
  2230. // replicaSet1: info/labels/annotations use replicaSetInfo (uid, namespace_uid, replicaset)
  2231. {
  2232. Name: metric.ReplicaSetInfo,
  2233. Labels: map[string]string{
  2234. source.UIDLabel: "uuid1",
  2235. source.NamespaceUIDLabel: "",
  2236. source.ReplicaSetLabel: "replicaSet1",
  2237. },
  2238. Value: 0,
  2239. AdditionalInfo: map[string]string{
  2240. source.UIDLabel: "uuid1",
  2241. source.NamespaceUIDLabel: "",
  2242. source.ReplicaSetLabel: "replicaSet1",
  2243. },
  2244. },
  2245. {
  2246. Name: metric.ReplicaSetLabels,
  2247. Labels: map[string]string{
  2248. source.UIDLabel: "uuid1",
  2249. source.NamespaceUIDLabel: "",
  2250. source.ReplicaSetLabel: "replicaSet1",
  2251. },
  2252. Value: 0,
  2253. AdditionalInfo: map[string]string{},
  2254. },
  2255. {
  2256. Name: metric.ReplicaSetAnnotations,
  2257. Labels: map[string]string{
  2258. source.UIDLabel: "uuid1",
  2259. source.NamespaceUIDLabel: "",
  2260. source.ReplicaSetLabel: "replicaSet1",
  2261. },
  2262. Value: 0,
  2263. AdditionalInfo: map[string]string{},
  2264. },
  2265. // replicaSet1 owner: uses replicaSetOwnerInfo (replicaset, namespace, uid) + owner fields
  2266. {
  2267. Name: metric.KubeReplicasetOwner,
  2268. Labels: map[string]string{
  2269. source.ReplicaSetLabel: "replicaSet1",
  2270. source.NamespaceLabel: "namespace1",
  2271. source.UIDLabel: "uuid1",
  2272. source.OwnerNameLabel: "rollout1",
  2273. source.OwnerKindLabel: "Rollout",
  2274. source.OwnerUIDLabel: "",
  2275. source.ControllerLabel: "false",
  2276. },
  2277. Value: 0,
  2278. AdditionalInfo: map[string]string{
  2279. source.ReplicaSetLabel: "replicaSet1",
  2280. source.NamespaceLabel: "namespace1",
  2281. source.UIDLabel: "uuid1",
  2282. source.OwnerNameLabel: "rollout1",
  2283. source.OwnerKindLabel: "Rollout",
  2284. source.OwnerUIDLabel: "",
  2285. source.ControllerLabel: "false",
  2286. },
  2287. },
  2288. // pureReplicaSet: info/labels/annotations
  2289. {
  2290. Name: metric.ReplicaSetInfo,
  2291. Labels: map[string]string{
  2292. source.UIDLabel: "uuid2",
  2293. source.NamespaceUIDLabel: "",
  2294. source.ReplicaSetLabel: "pureReplicaSet",
  2295. },
  2296. Value: 0,
  2297. AdditionalInfo: map[string]string{
  2298. source.UIDLabel: "uuid2",
  2299. source.NamespaceUIDLabel: "",
  2300. source.ReplicaSetLabel: "pureReplicaSet",
  2301. },
  2302. },
  2303. {
  2304. Name: metric.ReplicaSetLabels,
  2305. Labels: map[string]string{
  2306. source.UIDLabel: "uuid2",
  2307. source.NamespaceUIDLabel: "",
  2308. source.ReplicaSetLabel: "pureReplicaSet",
  2309. },
  2310. Value: 0,
  2311. AdditionalInfo: map[string]string{},
  2312. },
  2313. {
  2314. Name: metric.ReplicaSetAnnotations,
  2315. Labels: map[string]string{
  2316. source.UIDLabel: "uuid2",
  2317. source.NamespaceUIDLabel: "",
  2318. source.ReplicaSetLabel: "pureReplicaSet",
  2319. },
  2320. Value: 0,
  2321. AdditionalInfo: map[string]string{},
  2322. },
  2323. // pureReplicaSet owner: no owners path uses <none> sentinel, no owner_uid/controller
  2324. {
  2325. Name: metric.KubeReplicasetOwner,
  2326. Labels: map[string]string{
  2327. source.ReplicaSetLabel: "pureReplicaSet",
  2328. source.NamespaceLabel: "namespace1",
  2329. source.UIDLabel: "uuid2",
  2330. source.OwnerNameLabel: source.NoneLabelValue,
  2331. source.OwnerKindLabel: source.NoneLabelValue,
  2332. source.ControllerLabel: "false",
  2333. },
  2334. Value: 0,
  2335. AdditionalInfo: map[string]string{
  2336. source.ReplicaSetLabel: "pureReplicaSet",
  2337. source.NamespaceLabel: "namespace1",
  2338. source.UIDLabel: "uuid2",
  2339. source.OwnerNameLabel: source.NoneLabelValue,
  2340. source.OwnerKindLabel: source.NoneLabelValue,
  2341. source.ControllerLabel: "false",
  2342. },
  2343. },
  2344. },
  2345. },
  2346. {
  2347. name: "with namespace index",
  2348. nsSetup: func(nsIndex map[string]types.UID) {
  2349. nsIndex["namespace1"] = "ns-uuid1"
  2350. },
  2351. scrapes: []scrape{
  2352. {
  2353. ReplicaSets: []*clustercache.ReplicaSet{
  2354. {
  2355. Name: "replicaSet1",
  2356. Namespace: "namespace1",
  2357. UID: "uuid1",
  2358. OwnerReferences: []metav1.OwnerReference{},
  2359. },
  2360. },
  2361. Timestamp: start1,
  2362. },
  2363. },
  2364. expected: []metric.Update{
  2365. {
  2366. Name: metric.ReplicaSetInfo,
  2367. Labels: map[string]string{
  2368. source.UIDLabel: "uuid1",
  2369. source.NamespaceUIDLabel: "ns-uuid1",
  2370. source.ReplicaSetLabel: "replicaSet1",
  2371. },
  2372. Value: 0,
  2373. AdditionalInfo: map[string]string{
  2374. source.UIDLabel: "uuid1",
  2375. source.NamespaceUIDLabel: "ns-uuid1",
  2376. source.ReplicaSetLabel: "replicaSet1",
  2377. },
  2378. },
  2379. {
  2380. Name: metric.ReplicaSetLabels,
  2381. Labels: map[string]string{
  2382. source.UIDLabel: "uuid1",
  2383. source.NamespaceUIDLabel: "ns-uuid1",
  2384. source.ReplicaSetLabel: "replicaSet1",
  2385. },
  2386. Value: 0,
  2387. AdditionalInfo: map[string]string{},
  2388. },
  2389. {
  2390. Name: metric.ReplicaSetAnnotations,
  2391. Labels: map[string]string{
  2392. source.UIDLabel: "uuid1",
  2393. source.NamespaceUIDLabel: "ns-uuid1",
  2394. source.ReplicaSetLabel: "replicaSet1",
  2395. },
  2396. Value: 0,
  2397. AdditionalInfo: map[string]string{},
  2398. },
  2399. {
  2400. Name: metric.KubeReplicasetOwner,
  2401. Labels: map[string]string{
  2402. source.ReplicaSetLabel: "replicaSet1",
  2403. source.NamespaceLabel: "namespace1",
  2404. source.UIDLabel: "uuid1",
  2405. source.OwnerNameLabel: source.NoneLabelValue,
  2406. source.OwnerKindLabel: source.NoneLabelValue,
  2407. source.ControllerLabel: "false",
  2408. },
  2409. Value: 0,
  2410. AdditionalInfo: map[string]string{
  2411. source.ReplicaSetLabel: "replicaSet1",
  2412. source.NamespaceLabel: "namespace1",
  2413. source.UIDLabel: "uuid1",
  2414. source.OwnerNameLabel: source.NoneLabelValue,
  2415. source.OwnerKindLabel: source.NoneLabelValue,
  2416. source.ControllerLabel: "false",
  2417. },
  2418. },
  2419. },
  2420. },
  2421. }
  2422. for _, tt := range tests {
  2423. t.Run(tt.name, func(t *testing.T) {
  2424. ks := &ClusterCacheScraper{}
  2425. nsIndex := make(map[string]types.UID, 0)
  2426. if tt.nsSetup != nil {
  2427. tt.nsSetup(nsIndex)
  2428. }
  2429. var scrapeResults []metric.Update
  2430. for _, s := range tt.scrapes {
  2431. res := ks.scrapeReplicaSets(s.ReplicaSets, nsIndex)
  2432. scrapeResults = append(scrapeResults, res...)
  2433. }
  2434. if len(scrapeResults) != len(tt.expected) {
  2435. t.Errorf("Expected result length of %d, got %d", len(tt.expected), len(scrapeResults))
  2436. }
  2437. for i, expected := range tt.expected {
  2438. got := scrapeResults[i]
  2439. if !reflect.DeepEqual(expected, got) {
  2440. t.Errorf("Result did not match expected at index %d: got %v, want %v", i, got, expected)
  2441. }
  2442. }
  2443. })
  2444. }
  2445. }
  2446. func Test_kubernetesScraper_scrapeResourceQuotas(t *testing.T) {
  2447. start1, _ := time.Parse(time.RFC3339, Start1Str)
  2448. type scrape struct {
  2449. ResourceQuotas []*clustercache.ResourceQuota
  2450. Timestamp time.Time
  2451. }
  2452. tests := []struct {
  2453. name string
  2454. nsSetup func(map[string]types.UID)
  2455. scrapes []scrape
  2456. expected []metric.Update
  2457. }{
  2458. {
  2459. name: "simple",
  2460. scrapes: []scrape{
  2461. {
  2462. ResourceQuotas: []*clustercache.ResourceQuota{
  2463. {
  2464. Name: "resourceQuota1",
  2465. Namespace: "namespace1",
  2466. UID: "uuid1",
  2467. Spec: v1.ResourceQuotaSpec{
  2468. Hard: v1.ResourceList{
  2469. v1.ResourceRequestsCPU: resource.MustParse("1"),
  2470. v1.ResourceRequestsMemory: resource.MustParse("1024"),
  2471. v1.ResourceLimitsCPU: resource.MustParse("2"),
  2472. v1.ResourceLimitsMemory: resource.MustParse("2048"),
  2473. },
  2474. },
  2475. Status: v1.ResourceQuotaStatus{
  2476. Used: v1.ResourceList{
  2477. v1.ResourceRequestsCPU: resource.MustParse("0.5"),
  2478. v1.ResourceRequestsMemory: resource.MustParse("512"),
  2479. v1.ResourceLimitsCPU: resource.MustParse("1"),
  2480. v1.ResourceLimitsMemory: resource.MustParse("1024"),
  2481. },
  2482. },
  2483. },
  2484. },
  2485. Timestamp: start1,
  2486. },
  2487. },
  2488. expected: []metric.Update{
  2489. {
  2490. Name: metric.ResourceQuotaInfo,
  2491. Labels: map[string]string{
  2492. source.UIDLabel: "uuid1",
  2493. source.NamespaceUIDLabel: "",
  2494. source.ResourceQuotaLabel: "resourceQuota1",
  2495. },
  2496. Value: 0,
  2497. AdditionalInfo: map[string]string{
  2498. source.UIDLabel: "uuid1",
  2499. source.NamespaceUIDLabel: "",
  2500. source.ResourceQuotaLabel: "resourceQuota1",
  2501. },
  2502. },
  2503. {
  2504. Name: metric.KubeResourceQuotaSpecResourceRequests,
  2505. Labels: map[string]string{
  2506. source.UIDLabel: "uuid1",
  2507. source.NamespaceUIDLabel: "",
  2508. source.ResourceQuotaLabel: "resourceQuota1",
  2509. source.ResourceLabel: "cpu",
  2510. source.UnitLabel: "core",
  2511. },
  2512. Value: 1,
  2513. AdditionalInfo: nil,
  2514. },
  2515. {
  2516. Name: metric.KubeResourceQuotaSpecResourceRequests,
  2517. Labels: map[string]string{
  2518. source.UIDLabel: "uuid1",
  2519. source.NamespaceUIDLabel: "",
  2520. source.ResourceQuotaLabel: "resourceQuota1",
  2521. source.ResourceLabel: "memory",
  2522. source.UnitLabel: "byte",
  2523. },
  2524. Value: 1024,
  2525. AdditionalInfo: nil,
  2526. },
  2527. {
  2528. Name: metric.KubeResourceQuotaSpecResourceLimits,
  2529. Labels: map[string]string{
  2530. source.UIDLabel: "uuid1",
  2531. source.NamespaceUIDLabel: "",
  2532. source.ResourceQuotaLabel: "resourceQuota1",
  2533. source.ResourceLabel: "cpu",
  2534. source.UnitLabel: "core",
  2535. },
  2536. Value: 2,
  2537. AdditionalInfo: nil,
  2538. },
  2539. {
  2540. Name: metric.KubeResourceQuotaSpecResourceLimits,
  2541. Labels: map[string]string{
  2542. source.UIDLabel: "uuid1",
  2543. source.NamespaceUIDLabel: "",
  2544. source.ResourceQuotaLabel: "resourceQuota1",
  2545. source.ResourceLabel: "memory",
  2546. source.UnitLabel: "byte",
  2547. },
  2548. Value: 2048,
  2549. AdditionalInfo: nil,
  2550. },
  2551. {
  2552. Name: metric.KubeResourceQuotaStatusUsedResourceRequests,
  2553. Labels: map[string]string{
  2554. source.UIDLabel: "uuid1",
  2555. source.NamespaceUIDLabel: "",
  2556. source.ResourceQuotaLabel: "resourceQuota1",
  2557. source.ResourceLabel: "cpu",
  2558. source.UnitLabel: "core",
  2559. },
  2560. Value: 0.5,
  2561. AdditionalInfo: nil,
  2562. },
  2563. {
  2564. Name: metric.KubeResourceQuotaStatusUsedResourceRequests,
  2565. Labels: map[string]string{
  2566. source.UIDLabel: "uuid1",
  2567. source.NamespaceUIDLabel: "",
  2568. source.ResourceQuotaLabel: "resourceQuota1",
  2569. source.ResourceLabel: "memory",
  2570. source.UnitLabel: "byte",
  2571. },
  2572. Value: 512,
  2573. AdditionalInfo: nil,
  2574. },
  2575. {
  2576. Name: metric.KubeResourceQuotaStatusUsedResourceLimits,
  2577. Labels: map[string]string{
  2578. source.UIDLabel: "uuid1",
  2579. source.NamespaceUIDLabel: "",
  2580. source.ResourceQuotaLabel: "resourceQuota1",
  2581. source.ResourceLabel: "cpu",
  2582. source.UnitLabel: "core",
  2583. },
  2584. Value: 1,
  2585. AdditionalInfo: nil,
  2586. },
  2587. {
  2588. Name: metric.KubeResourceQuotaStatusUsedResourceLimits,
  2589. Labels: map[string]string{
  2590. source.UIDLabel: "uuid1",
  2591. source.NamespaceUIDLabel: "",
  2592. source.ResourceQuotaLabel: "resourceQuota1",
  2593. source.ResourceLabel: "memory",
  2594. source.UnitLabel: "byte",
  2595. },
  2596. Value: 1024,
  2597. AdditionalInfo: nil,
  2598. },
  2599. },
  2600. },
  2601. {
  2602. name: "with namespace index",
  2603. nsSetup: func(nsIndex map[string]types.UID) {
  2604. nsIndex["namespace1"] = "ns-uuid1"
  2605. },
  2606. scrapes: []scrape{
  2607. {
  2608. ResourceQuotas: []*clustercache.ResourceQuota{
  2609. {
  2610. Name: "resourceQuota1",
  2611. Namespace: "namespace1",
  2612. UID: "uuid1",
  2613. Spec: v1.ResourceQuotaSpec{
  2614. Hard: v1.ResourceList{
  2615. v1.ResourceRequestsCPU: resource.MustParse("1"),
  2616. },
  2617. },
  2618. },
  2619. },
  2620. Timestamp: start1,
  2621. },
  2622. },
  2623. expected: []metric.Update{
  2624. {
  2625. Name: metric.ResourceQuotaInfo,
  2626. Labels: map[string]string{
  2627. source.UIDLabel: "uuid1",
  2628. source.NamespaceUIDLabel: "ns-uuid1",
  2629. source.ResourceQuotaLabel: "resourceQuota1",
  2630. },
  2631. Value: 0,
  2632. AdditionalInfo: map[string]string{
  2633. source.UIDLabel: "uuid1",
  2634. source.NamespaceUIDLabel: "ns-uuid1",
  2635. source.ResourceQuotaLabel: "resourceQuota1",
  2636. },
  2637. },
  2638. {
  2639. Name: metric.KubeResourceQuotaSpecResourceRequests,
  2640. Labels: map[string]string{
  2641. source.UIDLabel: "uuid1",
  2642. source.NamespaceUIDLabel: "ns-uuid1",
  2643. source.ResourceQuotaLabel: "resourceQuota1",
  2644. source.ResourceLabel: "cpu",
  2645. source.UnitLabel: "core",
  2646. },
  2647. Value: 1,
  2648. AdditionalInfo: nil,
  2649. },
  2650. },
  2651. },
  2652. }
  2653. for _, tt := range tests {
  2654. t.Run(tt.name, func(t *testing.T) {
  2655. ks := &ClusterCacheScraper{}
  2656. nsIndex := make(map[string]types.UID, 0)
  2657. if tt.nsSetup != nil {
  2658. tt.nsSetup(nsIndex)
  2659. }
  2660. var scrapeResults []metric.Update
  2661. for _, s := range tt.scrapes {
  2662. res := ks.scrapeResourceQuotas(s.ResourceQuotas, nsIndex)
  2663. scrapeResults = append(scrapeResults, res...)
  2664. }
  2665. if len(scrapeResults) != len(tt.expected) {
  2666. t.Errorf("Expected result length of %d, got %d", len(tt.expected), len(scrapeResults))
  2667. }
  2668. for i, expected := range tt.expected {
  2669. got := scrapeResults[i]
  2670. if !reflect.DeepEqual(expected, got) {
  2671. t.Errorf("Result did not match expected at index %d: got %v, want %v", i, got, expected)
  2672. }
  2673. }
  2674. })
  2675. }
  2676. }
  2677. func Test_kubernetesScraper_scrapeDaemonSets(t *testing.T) {
  2678. start1, _ := time.Parse(time.RFC3339, Start1Str)
  2679. type scrape struct {
  2680. DaemonSets []*clustercache.DaemonSet
  2681. Timestamp time.Time
  2682. }
  2683. tests := []struct {
  2684. name string
  2685. nsSetup func(map[string]types.UID)
  2686. scrapes []scrape
  2687. expected []metric.Update
  2688. }{
  2689. {
  2690. name: "simple",
  2691. scrapes: []scrape{
  2692. {
  2693. DaemonSets: []*clustercache.DaemonSet{
  2694. {
  2695. Name: "daemonSet1",
  2696. Namespace: "namespace1",
  2697. UID: "uuid1",
  2698. },
  2699. },
  2700. Timestamp: start1,
  2701. },
  2702. },
  2703. expected: []metric.Update{
  2704. {
  2705. Name: metric.DaemonSetInfo,
  2706. Labels: map[string]string{
  2707. source.UIDLabel: "uuid1",
  2708. source.NamespaceUIDLabel: "",
  2709. source.DaemonSetLabel: "daemonSet1",
  2710. },
  2711. Value: 0,
  2712. AdditionalInfo: map[string]string{
  2713. source.UIDLabel: "uuid1",
  2714. source.NamespaceUIDLabel: "",
  2715. source.DaemonSetLabel: "daemonSet1",
  2716. },
  2717. },
  2718. {
  2719. Name: metric.DaemonSetLabels,
  2720. Labels: map[string]string{
  2721. source.UIDLabel: "uuid1",
  2722. source.NamespaceUIDLabel: "",
  2723. source.DaemonSetLabel: "daemonSet1",
  2724. },
  2725. Value: 0,
  2726. AdditionalInfo: map[string]string{},
  2727. },
  2728. {
  2729. Name: metric.DaemonSetAnnotations,
  2730. Labels: map[string]string{
  2731. source.UIDLabel: "uuid1",
  2732. source.NamespaceUIDLabel: "",
  2733. source.DaemonSetLabel: "daemonSet1",
  2734. },
  2735. Value: 0,
  2736. AdditionalInfo: map[string]string{},
  2737. },
  2738. },
  2739. },
  2740. {
  2741. name: "with namespace index",
  2742. nsSetup: func(nsIndex map[string]types.UID) {
  2743. nsIndex["namespace1"] = "ns-uuid1"
  2744. },
  2745. scrapes: []scrape{
  2746. {
  2747. DaemonSets: []*clustercache.DaemonSet{
  2748. {
  2749. Name: "daemonSet1",
  2750. Namespace: "namespace1",
  2751. UID: "uuid1",
  2752. },
  2753. },
  2754. Timestamp: start1,
  2755. },
  2756. },
  2757. expected: []metric.Update{
  2758. {
  2759. Name: metric.DaemonSetInfo,
  2760. Labels: map[string]string{
  2761. source.UIDLabel: "uuid1",
  2762. source.NamespaceUIDLabel: "ns-uuid1",
  2763. source.DaemonSetLabel: "daemonSet1",
  2764. },
  2765. Value: 0,
  2766. AdditionalInfo: map[string]string{
  2767. source.UIDLabel: "uuid1",
  2768. source.NamespaceUIDLabel: "ns-uuid1",
  2769. source.DaemonSetLabel: "daemonSet1",
  2770. },
  2771. },
  2772. {
  2773. Name: metric.DaemonSetLabels,
  2774. Labels: map[string]string{
  2775. source.UIDLabel: "uuid1",
  2776. source.NamespaceUIDLabel: "ns-uuid1",
  2777. source.DaemonSetLabel: "daemonSet1",
  2778. },
  2779. Value: 0,
  2780. AdditionalInfo: map[string]string{},
  2781. },
  2782. {
  2783. Name: metric.DaemonSetAnnotations,
  2784. Labels: map[string]string{
  2785. source.UIDLabel: "uuid1",
  2786. source.NamespaceUIDLabel: "ns-uuid1",
  2787. source.DaemonSetLabel: "daemonSet1",
  2788. },
  2789. Value: 0,
  2790. AdditionalInfo: map[string]string{},
  2791. },
  2792. },
  2793. },
  2794. {
  2795. name: "with container arguments",
  2796. scrapes: []scrape{
  2797. {
  2798. DaemonSets: []*clustercache.DaemonSet{
  2799. {
  2800. Name: "daemonSet1",
  2801. Namespace: "namespace1",
  2802. UID: "uuid1",
  2803. SpecContainers: []v1.Container{
  2804. {Args: []string{"--vgpu=2", "--bare-flag"}},
  2805. },
  2806. },
  2807. },
  2808. Timestamp: start1,
  2809. },
  2810. },
  2811. expected: []metric.Update{
  2812. {
  2813. Name: metric.DaemonSetInfo,
  2814. Labels: map[string]string{
  2815. source.UIDLabel: "uuid1",
  2816. source.NamespaceUIDLabel: "",
  2817. source.DaemonSetLabel: "daemonSet1",
  2818. },
  2819. Value: 0,
  2820. AdditionalInfo: map[string]string{
  2821. source.UIDLabel: "uuid1",
  2822. source.NamespaceUIDLabel: "",
  2823. source.DaemonSetLabel: "daemonSet1",
  2824. },
  2825. },
  2826. {
  2827. Name: metric.DaemonSetLabels,
  2828. Labels: map[string]string{
  2829. source.UIDLabel: "uuid1",
  2830. source.NamespaceUIDLabel: "",
  2831. source.DaemonSetLabel: "daemonSet1",
  2832. },
  2833. Value: 0,
  2834. AdditionalInfo: map[string]string{},
  2835. },
  2836. {
  2837. Name: metric.DaemonSetAnnotations,
  2838. Labels: map[string]string{
  2839. source.UIDLabel: "uuid1",
  2840. source.NamespaceUIDLabel: "",
  2841. source.DaemonSetLabel: "daemonSet1",
  2842. },
  2843. Value: 0,
  2844. AdditionalInfo: map[string]string{},
  2845. },
  2846. {
  2847. Name: metric.DaemonSetArguments,
  2848. Labels: map[string]string{
  2849. source.UIDLabel: "uuid1",
  2850. source.NamespaceUIDLabel: "",
  2851. source.DaemonSetLabel: "daemonSet1",
  2852. source.ArgLabel: "bare-flag",
  2853. source.ValueLabel: "",
  2854. },
  2855. Value: 0,
  2856. AdditionalInfo: map[string]string{
  2857. source.UIDLabel: "uuid1",
  2858. source.NamespaceUIDLabel: "",
  2859. source.DaemonSetLabel: "daemonSet1",
  2860. source.ArgLabel: "bare-flag",
  2861. source.ValueLabel: "",
  2862. },
  2863. },
  2864. {
  2865. Name: metric.DaemonSetArguments,
  2866. Labels: map[string]string{
  2867. source.UIDLabel: "uuid1",
  2868. source.NamespaceUIDLabel: "",
  2869. source.DaemonSetLabel: "daemonSet1",
  2870. source.ArgLabel: "vgpu",
  2871. source.ValueLabel: "2",
  2872. },
  2873. Value: 0,
  2874. AdditionalInfo: map[string]string{
  2875. source.UIDLabel: "uuid1",
  2876. source.NamespaceUIDLabel: "",
  2877. source.DaemonSetLabel: "daemonSet1",
  2878. source.ArgLabel: "vgpu",
  2879. source.ValueLabel: "2",
  2880. },
  2881. },
  2882. },
  2883. },
  2884. }
  2885. for _, tt := range tests {
  2886. t.Run(tt.name, func(t *testing.T) {
  2887. ks := &ClusterCacheScraper{}
  2888. nsIndex := make(map[string]types.UID, 0)
  2889. if tt.nsSetup != nil {
  2890. tt.nsSetup(nsIndex)
  2891. }
  2892. var scrapeResults []metric.Update
  2893. for _, s := range tt.scrapes {
  2894. res := ks.scrapeDaemonSets(s.DaemonSets, nsIndex)
  2895. scrapeResults = append(scrapeResults, res...)
  2896. }
  2897. if len(scrapeResults) != len(tt.expected) {
  2898. t.Errorf("Expected result length of %d, got %d", len(tt.expected), len(scrapeResults))
  2899. }
  2900. for i, expected := range tt.expected {
  2901. got := scrapeResults[i]
  2902. if !reflect.DeepEqual(expected, got) {
  2903. t.Errorf("Result did not match expected at index %d: got %v, want %v", i, got, expected)
  2904. }
  2905. }
  2906. })
  2907. }
  2908. }
  2909. func Test_kubernetesScraper_scrapeJobs(t *testing.T) {
  2910. start1, _ := time.Parse(time.RFC3339, Start1Str)
  2911. type scrape struct {
  2912. Jobs []*clustercache.Job
  2913. Timestamp time.Time
  2914. }
  2915. tests := []struct {
  2916. name string
  2917. nsSetup func(map[string]types.UID)
  2918. scrapes []scrape
  2919. expected []metric.Update
  2920. }{
  2921. {
  2922. name: "simple",
  2923. scrapes: []scrape{
  2924. {
  2925. Jobs: []*clustercache.Job{
  2926. {
  2927. Name: "job1",
  2928. Namespace: "namespace1",
  2929. UID: "uuid1",
  2930. },
  2931. },
  2932. Timestamp: start1,
  2933. },
  2934. },
  2935. expected: []metric.Update{
  2936. {
  2937. Name: metric.JobInfo,
  2938. Labels: map[string]string{
  2939. source.UIDLabel: "uuid1",
  2940. source.NamespaceUIDLabel: "",
  2941. source.JobLabel: "job1",
  2942. },
  2943. Value: 0,
  2944. AdditionalInfo: map[string]string{
  2945. source.UIDLabel: "uuid1",
  2946. source.NamespaceUIDLabel: "",
  2947. source.JobLabel: "job1",
  2948. },
  2949. },
  2950. {
  2951. Name: metric.JobLabels,
  2952. Labels: map[string]string{
  2953. source.UIDLabel: "uuid1",
  2954. source.NamespaceUIDLabel: "",
  2955. source.JobLabel: "job1",
  2956. },
  2957. Value: 0,
  2958. AdditionalInfo: map[string]string{},
  2959. },
  2960. {
  2961. Name: metric.JobAnnotations,
  2962. Labels: map[string]string{
  2963. source.UIDLabel: "uuid1",
  2964. source.NamespaceUIDLabel: "",
  2965. source.JobLabel: "job1",
  2966. },
  2967. Value: 0,
  2968. AdditionalInfo: map[string]string{},
  2969. },
  2970. },
  2971. },
  2972. {
  2973. name: "with namespace index",
  2974. nsSetup: func(nsIndex map[string]types.UID) {
  2975. nsIndex["namespace1"] = "ns-uuid1"
  2976. },
  2977. scrapes: []scrape{
  2978. {
  2979. Jobs: []*clustercache.Job{
  2980. {
  2981. Name: "job1",
  2982. Namespace: "namespace1",
  2983. UID: "uuid1",
  2984. },
  2985. },
  2986. Timestamp: start1,
  2987. },
  2988. },
  2989. expected: []metric.Update{
  2990. {
  2991. Name: metric.JobInfo,
  2992. Labels: map[string]string{
  2993. source.UIDLabel: "uuid1",
  2994. source.NamespaceUIDLabel: "ns-uuid1",
  2995. source.JobLabel: "job1",
  2996. },
  2997. Value: 0,
  2998. AdditionalInfo: map[string]string{
  2999. source.UIDLabel: "uuid1",
  3000. source.NamespaceUIDLabel: "ns-uuid1",
  3001. source.JobLabel: "job1",
  3002. },
  3003. },
  3004. {
  3005. Name: metric.JobLabels,
  3006. Labels: map[string]string{
  3007. source.UIDLabel: "uuid1",
  3008. source.NamespaceUIDLabel: "ns-uuid1",
  3009. source.JobLabel: "job1",
  3010. },
  3011. Value: 0,
  3012. AdditionalInfo: map[string]string{},
  3013. },
  3014. {
  3015. Name: metric.JobAnnotations,
  3016. Labels: map[string]string{
  3017. source.UIDLabel: "uuid1",
  3018. source.NamespaceUIDLabel: "ns-uuid1",
  3019. source.JobLabel: "job1",
  3020. },
  3021. Value: 0,
  3022. AdditionalInfo: map[string]string{},
  3023. },
  3024. },
  3025. },
  3026. }
  3027. for _, tt := range tests {
  3028. t.Run(tt.name, func(t *testing.T) {
  3029. ks := &ClusterCacheScraper{}
  3030. nsIndex := make(map[string]types.UID, 0)
  3031. if tt.nsSetup != nil {
  3032. tt.nsSetup(nsIndex)
  3033. }
  3034. var scrapeResults []metric.Update
  3035. for _, s := range tt.scrapes {
  3036. res := ks.scrapeJobs(s.Jobs, nsIndex)
  3037. scrapeResults = append(scrapeResults, res...)
  3038. }
  3039. if len(scrapeResults) != len(tt.expected) {
  3040. t.Errorf("Expected result length of %d, got %d", len(tt.expected), len(scrapeResults))
  3041. }
  3042. for i, expected := range tt.expected {
  3043. got := scrapeResults[i]
  3044. if !reflect.DeepEqual(expected, got) {
  3045. t.Errorf("Result did not match expected at index %d: got %v, want %v", i, got, expected)
  3046. }
  3047. }
  3048. })
  3049. }
  3050. }
  3051. func Test_kubernetesScraper_scrapeCronJobs(t *testing.T) {
  3052. start1, _ := time.Parse(time.RFC3339, Start1Str)
  3053. type scrape struct {
  3054. CronJobs []*clustercache.CronJob
  3055. Timestamp time.Time
  3056. }
  3057. tests := []struct {
  3058. name string
  3059. nsSetup func(map[string]types.UID)
  3060. scrapes []scrape
  3061. expected []metric.Update
  3062. }{
  3063. {
  3064. name: "simple",
  3065. scrapes: []scrape{
  3066. {
  3067. CronJobs: []*clustercache.CronJob{
  3068. {
  3069. Name: "cronJob1",
  3070. Namespace: "namespace1",
  3071. UID: "uuid1",
  3072. },
  3073. },
  3074. Timestamp: start1,
  3075. },
  3076. },
  3077. expected: []metric.Update{
  3078. {
  3079. Name: metric.CronJobInfo,
  3080. Labels: map[string]string{
  3081. source.UIDLabel: "uuid1",
  3082. source.NamespaceUIDLabel: "",
  3083. source.CronJobLabel: "cronJob1",
  3084. },
  3085. Value: 0,
  3086. AdditionalInfo: map[string]string{
  3087. source.UIDLabel: "uuid1",
  3088. source.NamespaceUIDLabel: "",
  3089. source.CronJobLabel: "cronJob1",
  3090. },
  3091. },
  3092. {
  3093. Name: metric.CronJobLabels,
  3094. Labels: map[string]string{
  3095. source.UIDLabel: "uuid1",
  3096. source.NamespaceUIDLabel: "",
  3097. source.CronJobLabel: "cronJob1",
  3098. },
  3099. Value: 0,
  3100. AdditionalInfo: map[string]string{},
  3101. },
  3102. {
  3103. Name: metric.CronJobAnnotations,
  3104. Labels: map[string]string{
  3105. source.UIDLabel: "uuid1",
  3106. source.NamespaceUIDLabel: "",
  3107. source.CronJobLabel: "cronJob1",
  3108. },
  3109. Value: 0,
  3110. AdditionalInfo: map[string]string{},
  3111. },
  3112. },
  3113. },
  3114. {
  3115. name: "with namespace index",
  3116. nsSetup: func(nsIndex map[string]types.UID) {
  3117. nsIndex["namespace1"] = "ns-uuid1"
  3118. },
  3119. scrapes: []scrape{
  3120. {
  3121. CronJobs: []*clustercache.CronJob{
  3122. {
  3123. Name: "cronJob1",
  3124. Namespace: "namespace1",
  3125. UID: "uuid1",
  3126. },
  3127. },
  3128. Timestamp: start1,
  3129. },
  3130. },
  3131. expected: []metric.Update{
  3132. {
  3133. Name: metric.CronJobInfo,
  3134. Labels: map[string]string{
  3135. source.UIDLabel: "uuid1",
  3136. source.NamespaceUIDLabel: "ns-uuid1",
  3137. source.CronJobLabel: "cronJob1",
  3138. },
  3139. Value: 0,
  3140. AdditionalInfo: map[string]string{
  3141. source.UIDLabel: "uuid1",
  3142. source.NamespaceUIDLabel: "ns-uuid1",
  3143. source.CronJobLabel: "cronJob1",
  3144. },
  3145. },
  3146. {
  3147. Name: metric.CronJobLabels,
  3148. Labels: map[string]string{
  3149. source.UIDLabel: "uuid1",
  3150. source.NamespaceUIDLabel: "ns-uuid1",
  3151. source.CronJobLabel: "cronJob1",
  3152. },
  3153. Value: 0,
  3154. AdditionalInfo: map[string]string{},
  3155. },
  3156. {
  3157. Name: metric.CronJobAnnotations,
  3158. Labels: map[string]string{
  3159. source.UIDLabel: "uuid1",
  3160. source.NamespaceUIDLabel: "ns-uuid1",
  3161. source.CronJobLabel: "cronJob1",
  3162. },
  3163. Value: 0,
  3164. AdditionalInfo: map[string]string{},
  3165. },
  3166. },
  3167. },
  3168. }
  3169. for _, tt := range tests {
  3170. t.Run(tt.name, func(t *testing.T) {
  3171. ks := &ClusterCacheScraper{}
  3172. nsIndex := make(map[string]types.UID, 0)
  3173. if tt.nsSetup != nil {
  3174. tt.nsSetup(nsIndex)
  3175. }
  3176. var scrapeResults []metric.Update
  3177. for _, s := range tt.scrapes {
  3178. res := ks.scrapeCronJobs(s.CronJobs, nsIndex)
  3179. scrapeResults = append(scrapeResults, res...)
  3180. }
  3181. if len(scrapeResults) != len(tt.expected) {
  3182. t.Errorf("Expected result length of %d, got %d", len(tt.expected), len(scrapeResults))
  3183. }
  3184. for i, expected := range tt.expected {
  3185. got := scrapeResults[i]
  3186. if !reflect.DeepEqual(expected, got) {
  3187. t.Errorf("Result did not match expected at index %d: got %v, want %v", i, got, expected)
  3188. }
  3189. }
  3190. })
  3191. }
  3192. }