2
0

properties.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. package kubecost
  2. import (
  3. "fmt"
  4. "sort"
  5. "strings"
  6. util "github.com/kubecost/cost-model/pkg/util"
  7. )
  8. type Property string
  9. const (
  10. NilProp Property = ""
  11. ClusterProp Property = "cluster"
  12. NodeProp Property = "node"
  13. ContainerProp Property = "container"
  14. ControllerProp Property = "controller"
  15. ControllerKindProp Property = "controllerKind"
  16. LabelProp Property = "label"
  17. AnnotationProp Property = "annotation"
  18. NamespaceProp Property = "namespace"
  19. PodProp Property = "pod"
  20. ServiceProp Property = "service"
  21. )
  22. var availableProperties []Property = []Property{
  23. NilProp,
  24. ClusterProp,
  25. NodeProp,
  26. ContainerProp,
  27. ControllerProp,
  28. ControllerKindProp,
  29. LabelProp,
  30. AnnotationProp,
  31. NamespaceProp,
  32. PodProp,
  33. ServiceProp,
  34. }
  35. func ParseProperty(prop string) Property {
  36. for _, property := range availableProperties {
  37. if strings.ToLower(string(property)) == strings.ToLower(prop) {
  38. return property
  39. }
  40. }
  41. return NilProp
  42. }
  43. func (p Property) String() string {
  44. return string(p)
  45. }
  46. type PropertyValue struct {
  47. Property Property
  48. Value interface{}
  49. }
  50. // Properties describes a set of Kubernetes objects.
  51. // TODO make this a struct smdh [TODO:CLEANUP]
  52. type Properties map[Property]interface{}
  53. // TODO niko/etl make sure Services deep copy works correctly
  54. func (p *Properties) Clone() Properties {
  55. if p == nil {
  56. return nil
  57. }
  58. clone := make(Properties, len(*p))
  59. for k, v := range *p {
  60. clone[k] = v
  61. }
  62. return clone
  63. }
  64. func (p *Properties) Equal(that *Properties) bool {
  65. if p == nil || that == nil {
  66. return false
  67. }
  68. if p.Length() != that.Length() {
  69. return false
  70. }
  71. pCluster, _ := p.GetCluster()
  72. thatCluster, _ := that.GetCluster()
  73. if pCluster != thatCluster {
  74. return false
  75. }
  76. pNode, _ := p.GetNode()
  77. thatNode, _ := that.GetNode()
  78. if pNode != thatNode {
  79. return false
  80. }
  81. pContainer, _ := p.GetContainer()
  82. thatContainer, _ := that.GetContainer()
  83. if pContainer != thatContainer {
  84. return false
  85. }
  86. pController, _ := p.GetController()
  87. thatController, _ := that.GetController()
  88. if pController != thatController {
  89. return false
  90. }
  91. pControllerKind, _ := p.GetControllerKind()
  92. thatControllerKind, _ := that.GetControllerKind()
  93. if pControllerKind != thatControllerKind {
  94. return false
  95. }
  96. pNamespace, _ := p.GetNamespace()
  97. thatNamespace, _ := that.GetNamespace()
  98. if pNamespace != thatNamespace {
  99. return false
  100. }
  101. pPod, _ := p.GetPod()
  102. thatPod, _ := that.GetPod()
  103. if pPod != thatPod {
  104. return false
  105. }
  106. pLabels, _ := p.GetLabels()
  107. thatLabels, _ := that.GetLabels()
  108. if len(pLabels) != len(thatLabels) {
  109. for k, pv := range pLabels {
  110. tv, ok := thatLabels[k]
  111. if !ok || tv != pv {
  112. return false
  113. }
  114. }
  115. return false
  116. }
  117. pAnnotations, _ := p.GetAnnotations()
  118. thatAnnotations, _ := that.GetAnnotations()
  119. if len(pAnnotations) != len(thatAnnotations) {
  120. for k, pv := range pAnnotations {
  121. tv, ok := thatAnnotations[k]
  122. if !ok || tv != pv {
  123. return false
  124. }
  125. }
  126. return false
  127. }
  128. pServices, _ := p.GetServices()
  129. thatServices, _ := that.GetServices()
  130. if len(pServices) != len(thatServices) {
  131. sort.Strings(pServices)
  132. sort.Strings(thatServices)
  133. for i, pv := range pServices {
  134. tv := thatServices[i]
  135. if tv != pv {
  136. return false
  137. }
  138. }
  139. return false
  140. }
  141. return true
  142. }
  143. func (p *Properties) Intersection(that Properties) Properties {
  144. spec := &Properties{}
  145. sCluster, sErr := p.GetCluster()
  146. tCluster, tErr := that.GetCluster()
  147. if sErr == nil && tErr == nil && sCluster == tCluster {
  148. spec.SetCluster(sCluster)
  149. }
  150. sNode, sErr := p.GetNode()
  151. tNode, tErr := that.GetNode()
  152. if sErr == nil && tErr == nil && sNode == tNode {
  153. spec.SetNode(sNode)
  154. }
  155. sContainer, sErr := p.GetContainer()
  156. tContainer, tErr := that.GetContainer()
  157. if sErr == nil && tErr == nil && sContainer == tContainer {
  158. spec.SetContainer(sContainer)
  159. }
  160. sController, sErr := p.GetController()
  161. tController, tErr := that.GetController()
  162. if sErr == nil && tErr == nil && sController == tController {
  163. spec.SetController(sController)
  164. }
  165. sControllerKind, sErr := p.GetControllerKind()
  166. tControllerKind, tErr := that.GetControllerKind()
  167. if sErr == nil && tErr == nil && sControllerKind == tControllerKind {
  168. spec.SetControllerKind(sControllerKind)
  169. }
  170. sNamespace, sErr := p.GetNamespace()
  171. tNamespace, tErr := that.GetNamespace()
  172. if sErr == nil && tErr == nil && sNamespace == tNamespace {
  173. spec.SetNamespace(sNamespace)
  174. }
  175. sPod, sErr := p.GetPod()
  176. tPod, tErr := that.GetPod()
  177. if sErr == nil && tErr == nil && sPod == tPod {
  178. spec.SetPod(sPod)
  179. }
  180. // TODO niko/etl intersection of services and labels and annotations
  181. return *spec
  182. }
  183. // Length returns the number of Properties
  184. func (p *Properties) Length() int {
  185. if p == nil {
  186. return 0
  187. }
  188. return len(*p)
  189. }
  190. func (p *Properties) String() string {
  191. if p == nil {
  192. return "<nil>"
  193. }
  194. strs := []string{}
  195. for key, prop := range *p {
  196. strs = append(strs, fmt.Sprintf("%s:%s", key, prop))
  197. }
  198. return fmt.Sprintf("{%s}", strings.Join(strs, "; "))
  199. }
  200. // AggregationStrings converts a Properties object into a slice of strings
  201. // representing a request to aggregate by certain properties.
  202. // NOTE: today, the ordering of the properties *has to match the ordering
  203. // of the allocaiton function generateKey*
  204. func (p *Properties) AggregationStrings() []string {
  205. if p == nil {
  206. return []string{}
  207. }
  208. aggStrs := []string{}
  209. if p.HasCluster() {
  210. aggStrs = append(aggStrs, ClusterProp.String())
  211. }
  212. if p.HasNode() {
  213. aggStrs = append(aggStrs, NodeProp.String())
  214. }
  215. if p.HasNamespace() {
  216. aggStrs = append(aggStrs, NamespaceProp.String())
  217. }
  218. if p.HasControllerKind() {
  219. aggStrs = append(aggStrs, ControllerKindProp.String())
  220. }
  221. if p.HasController() {
  222. aggStrs = append(aggStrs, ControllerProp.String())
  223. }
  224. if p.HasPod() {
  225. aggStrs = append(aggStrs, PodProp.String())
  226. }
  227. if p.HasContainer() {
  228. aggStrs = append(aggStrs, ContainerProp.String())
  229. }
  230. if p.HasService() {
  231. aggStrs = append(aggStrs, ServiceProp.String())
  232. }
  233. if p.HasLabel() {
  234. // e.g. expect format map[string]string{
  235. // "env":""
  236. // "app":"",
  237. // }
  238. // for aggregating by "label:app,label:env"
  239. labels, _ := p.GetLabels()
  240. labelAggStrs := []string{}
  241. for labelName := range labels {
  242. labelAggStrs = append(labelAggStrs, fmt.Sprintf("label:%s", labelName))
  243. }
  244. if len(labelAggStrs) > 0 {
  245. // Enforce alphabetical ordering, then append to aggStrs
  246. sort.Strings(labelAggStrs)
  247. for _, labelName := range labelAggStrs {
  248. aggStrs = append(aggStrs, labelName)
  249. }
  250. }
  251. }
  252. return aggStrs
  253. }
  254. func (p *Properties) Get(prop Property) (string, error) {
  255. if raw, ok := (*p)[prop]; ok {
  256. if result, ok := raw.(string); ok {
  257. return result, nil
  258. }
  259. return "", fmt.Errorf("%s is not a string", prop)
  260. }
  261. return "", fmt.Errorf("%s not set", prop)
  262. }
  263. func (p *Properties) Has(prop Property) bool {
  264. _, ok := (*p)[prop]
  265. return ok
  266. }
  267. func (p *Properties) Set(prop Property, value string) {
  268. (*p)[prop] = value
  269. }
  270. func (p *Properties) GetCluster() (string, error) {
  271. if raw, ok := (*p)[ClusterProp]; ok {
  272. if cluster, ok := raw.(string); ok {
  273. return cluster, nil
  274. }
  275. return "", fmt.Errorf("ClusterProp is not a string")
  276. }
  277. return "", fmt.Errorf("ClusterProp not set")
  278. }
  279. func (p *Properties) HasCluster() bool {
  280. _, ok := (*p)[ClusterProp]
  281. return ok
  282. }
  283. func (p *Properties) SetCluster(cluster string) {
  284. (*p)[ClusterProp] = cluster
  285. }
  286. func (p *Properties) GetNode() (string, error) {
  287. if raw, ok := (*p)[NodeProp]; ok {
  288. if node, ok := raw.(string); ok {
  289. return node, nil
  290. }
  291. return "", fmt.Errorf("NodeProp is not a string")
  292. }
  293. return "", fmt.Errorf("NodeProp not set")
  294. }
  295. func (p *Properties) HasNode() bool {
  296. _, ok := (*p)[NodeProp]
  297. return ok
  298. }
  299. func (p *Properties) SetNode(node string) {
  300. (*p)[NodeProp] = node
  301. }
  302. func (p *Properties) GetContainer() (string, error) {
  303. if raw, ok := (*p)[ContainerProp]; ok {
  304. if container, ok := raw.(string); ok {
  305. return container, nil
  306. }
  307. return "", fmt.Errorf("ContainerProp is not a string")
  308. }
  309. return "", fmt.Errorf("ContainerProp not set")
  310. }
  311. func (p *Properties) HasContainer() bool {
  312. _, ok := (*p)[ContainerProp]
  313. return ok
  314. }
  315. func (p *Properties) SetContainer(container string) {
  316. (*p)[ContainerProp] = container
  317. }
  318. func (p *Properties) GetController() (string, error) {
  319. if raw, ok := (*p)[ControllerProp]; ok {
  320. if controller, ok := raw.(string); ok {
  321. return controller, nil
  322. }
  323. return "", fmt.Errorf("ControllerProp is not a string")
  324. }
  325. return "", fmt.Errorf("ControllerProp not set")
  326. }
  327. func (p *Properties) HasController() bool {
  328. _, ok := (*p)[ControllerProp]
  329. return ok
  330. }
  331. func (p *Properties) SetController(controller string) {
  332. (*p)[ControllerProp] = controller
  333. }
  334. func (p *Properties) GetControllerKind() (string, error) {
  335. if raw, ok := (*p)[ControllerKindProp]; ok {
  336. if controllerKind, ok := raw.(string); ok {
  337. return controllerKind, nil
  338. }
  339. return "", fmt.Errorf("ControllerKindProp is not a string")
  340. }
  341. return "", fmt.Errorf("ControllerKindProp not set")
  342. }
  343. func (p *Properties) HasControllerKind() bool {
  344. _, ok := (*p)[ControllerKindProp]
  345. return ok
  346. }
  347. func (p *Properties) SetControllerKind(controllerKind string) {
  348. (*p)[ControllerKindProp] = controllerKind
  349. }
  350. func (p *Properties) GetLabels() (map[string]string, error) {
  351. if raw, ok := (*p)[LabelProp]; ok {
  352. if labels, ok := raw.(map[string]string); ok {
  353. return labels, nil
  354. }
  355. return map[string]string{}, fmt.Errorf("LabelProp is not a map[string]string")
  356. }
  357. return map[string]string{}, fmt.Errorf("LabelProp not set")
  358. }
  359. func (p *Properties) HasLabel() bool {
  360. _, ok := (*p)[LabelProp]
  361. return ok
  362. }
  363. func (p *Properties) SetLabels(labels map[string]string) {
  364. (*p)[LabelProp] = labels
  365. }
  366. func (p *Properties) GetAnnotations() (map[string]string, error) {
  367. if raw, ok := (*p)[AnnotationProp]; ok {
  368. if annotations, ok := raw.(map[string]string); ok {
  369. return annotations, nil
  370. }
  371. return map[string]string{}, fmt.Errorf("AnnotationProp is not a map[string]string")
  372. }
  373. return map[string]string{}, fmt.Errorf("AnnotationProp not set")
  374. }
  375. func (p *Properties) HasAnnotations() bool {
  376. _, ok := (*p)[AnnotationProp]
  377. return ok
  378. }
  379. func (p *Properties) SetAnnotations(annotations map[string]string) {
  380. (*p)[AnnotationProp] = annotations
  381. }
  382. func (p *Properties) GetNamespace() (string, error) {
  383. if raw, ok := (*p)[NamespaceProp]; ok {
  384. if namespace, ok := raw.(string); ok {
  385. return namespace, nil
  386. }
  387. return "", fmt.Errorf("NamespaceProp is not a string")
  388. }
  389. return "", fmt.Errorf("NamespaceProp not set")
  390. }
  391. func (p *Properties) HasNamespace() bool {
  392. _, ok := (*p)[NamespaceProp]
  393. return ok
  394. }
  395. func (p *Properties) SetNamespace(namespace string) {
  396. (*p)[NamespaceProp] = namespace
  397. }
  398. func (p *Properties) GetPod() (string, error) {
  399. if raw, ok := (*p)[PodProp]; ok {
  400. if pod, ok := raw.(string); ok {
  401. return pod, nil
  402. }
  403. return "", fmt.Errorf("PodProp is not a string")
  404. }
  405. return "", fmt.Errorf("PodProp not set")
  406. }
  407. func (p *Properties) HasPod() bool {
  408. _, ok := (*p)[PodProp]
  409. return ok
  410. }
  411. func (p *Properties) SetPod(pod string) {
  412. (*p)[PodProp] = pod
  413. }
  414. func (p *Properties) GetServices() ([]string, error) {
  415. if raw, ok := (*p)[ServiceProp]; ok {
  416. if services, ok := raw.([]string); ok {
  417. return services, nil
  418. }
  419. return []string{}, fmt.Errorf("ServiceProp is not a string")
  420. }
  421. return []string{}, fmt.Errorf("ServiceProp not set")
  422. }
  423. func (p *Properties) HasService() bool {
  424. _, ok := (*p)[ServiceProp]
  425. return ok
  426. }
  427. func (p *Properties) SetServices(services []string) {
  428. (*p)[ServiceProp] = services
  429. }
  430. func (p *Properties) MarshalBinary() (data []byte, err error) {
  431. buff := util.NewBuffer()
  432. buff.WriteUInt8(CodecVersion) // version
  433. // ClusterProp
  434. cluster, err := p.GetCluster()
  435. if err != nil {
  436. buff.WriteUInt8(uint8(0)) // write nil byte
  437. } else {
  438. buff.WriteUInt8(uint8(1)) // write non-nil byte
  439. buff.WriteString(cluster) // write string
  440. }
  441. // NodeProp
  442. node, err := p.GetNode()
  443. if err != nil {
  444. buff.WriteUInt8(uint8(0)) // write nil byte
  445. } else {
  446. buff.WriteUInt8(uint8(1)) // write non-nil byte
  447. buff.WriteString(node) // write string
  448. }
  449. // ContainerProp
  450. container, err := p.GetContainer()
  451. if err != nil {
  452. buff.WriteUInt8(uint8(0)) // write nil byte
  453. } else {
  454. buff.WriteUInt8(uint8(1)) // write non-nil byte
  455. buff.WriteString(container) // write string
  456. }
  457. // ControllerProp
  458. controller, err := p.GetController()
  459. if err != nil {
  460. buff.WriteUInt8(uint8(0)) // write nil byte
  461. } else {
  462. buff.WriteUInt8(uint8(1)) // write non-nil byte
  463. buff.WriteString(controller) // write string
  464. }
  465. // ControllerKindProp
  466. controllerKind, err := p.GetControllerKind()
  467. if err != nil {
  468. buff.WriteUInt8(uint8(0)) // write nil byte
  469. } else {
  470. buff.WriteUInt8(uint8(1)) // write non-nil byte
  471. buff.WriteString(controllerKind) // write string
  472. }
  473. // NamespaceProp
  474. namespace, err := p.GetNamespace()
  475. if err != nil {
  476. buff.WriteUInt8(uint8(0)) // write nil byte
  477. } else {
  478. buff.WriteUInt8(uint8(1)) // write non-nil byte
  479. buff.WriteString(namespace) // write string
  480. }
  481. // PodProp
  482. pod, err := p.GetPod()
  483. if err != nil {
  484. buff.WriteUInt8(uint8(0)) // write nil byte
  485. } else {
  486. buff.WriteUInt8(uint8(1)) // write non-nil byte
  487. buff.WriteString(pod) // write string
  488. }
  489. // LabelProp
  490. labels, err := p.GetLabels()
  491. if err != nil {
  492. buff.WriteUInt8(uint8(0)) // write nil byte
  493. } else {
  494. buff.WriteUInt8(uint8(1)) // write non-nil byte
  495. buff.WriteInt(len(labels)) // map length
  496. for k, v := range labels {
  497. buff.WriteString(k) // write string
  498. buff.WriteString(v) // write string
  499. }
  500. }
  501. // AnnotationProp
  502. annotations, err := p.GetAnnotations()
  503. if err != nil {
  504. buff.WriteUInt8(uint8(0)) // write nil byte
  505. } else {
  506. buff.WriteUInt8(uint8(1)) // write non-nil byte
  507. buff.WriteInt(len(annotations)) // map length
  508. for k, v := range annotations {
  509. buff.WriteString(k) // write string
  510. buff.WriteString(v) // write string
  511. }
  512. }
  513. // ServiceProp
  514. services, err := p.GetServices()
  515. if err != nil {
  516. buff.WriteUInt8(uint8(0)) // write nil byte
  517. } else {
  518. buff.WriteUInt8(uint8(1)) // write non-nil byte
  519. buff.WriteInt(len(services)) // slice length
  520. for _, v := range services {
  521. buff.WriteString(v) // write string
  522. }
  523. }
  524. return buff.Bytes(), nil
  525. }
  526. func (p *Properties) UnmarshalBinary(data []byte) error {
  527. buff := util.NewBufferFromBytes(data)
  528. v := buff.ReadUInt8() // version
  529. if v != CodecVersion {
  530. return fmt.Errorf("Invalid Version. Expected %d, got %d", CodecVersion, v)
  531. }
  532. *p = Properties{}
  533. // ClusterProp
  534. if buff.ReadUInt8() == 1 { // read nil byte
  535. cluster := buff.ReadString() // read string
  536. p.SetCluster(cluster)
  537. }
  538. // NodeProp
  539. if buff.ReadUInt8() == 1 { // read nil byte
  540. node := buff.ReadString() // read string
  541. p.SetNode(node)
  542. }
  543. // ContainerProp
  544. if buff.ReadUInt8() == 1 { // read nil byte
  545. container := buff.ReadString() // read string
  546. p.SetContainer(container)
  547. }
  548. // ControllerProp
  549. if buff.ReadUInt8() == 1 { // read nil byte
  550. controller := buff.ReadString() // read string
  551. p.SetController(controller)
  552. }
  553. // ControllerKindProp
  554. if buff.ReadUInt8() == 1 { // read nil byte
  555. controllerKind := buff.ReadString() // read string
  556. p.SetControllerKind(controllerKind)
  557. }
  558. // NamespaceProp
  559. if buff.ReadUInt8() == 1 { // read nil byte
  560. namespace := buff.ReadString() // read string
  561. p.SetNamespace(namespace)
  562. }
  563. // PodProp
  564. if buff.ReadUInt8() == 1 { // read nil byte
  565. pod := buff.ReadString() // read string
  566. p.SetPod(pod)
  567. }
  568. // LabelProp
  569. if buff.ReadUInt8() == 1 { // read nil byte
  570. length := buff.ReadInt() // read map len
  571. labels := make(map[string]string, length)
  572. for idx := 0; idx < length; idx++ {
  573. key := buff.ReadString()
  574. val := buff.ReadString()
  575. labels[key] = val
  576. }
  577. p.SetLabels(labels)
  578. }
  579. // AnnotationProp
  580. if buff.ReadUInt8() == 1 { // read nil byte
  581. length := buff.ReadInt() // read map len
  582. annotations := make(map[string]string, length)
  583. for idx := 0; idx < length; idx++ {
  584. key := buff.ReadString()
  585. val := buff.ReadString()
  586. annotations[key] = val
  587. }
  588. p.SetAnnotations(annotations)
  589. }
  590. // ServiceProp
  591. if buff.ReadUInt8() == 1 { // read nil byte
  592. length := buff.ReadInt() // read map len
  593. services := make([]string, length)
  594. for idx := 0; idx < length; idx++ {
  595. val := buff.ReadString()
  596. services[idx] = val
  597. }
  598. p.SetServices(services)
  599. }
  600. return nil
  601. }