allocationfilter.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package kubecost
  2. import "github.com/kubecost/cost-model/pkg/log"
  3. // FilterField is an enum that represents Allocation-specific fields that can be
  4. // filtered on (namespace, label, etc.)
  5. type FilterField int
  6. // If you add a FilterField, MAKE SURE TO UPDATE ALL FILTER IMPLEMENTATIONS! Go
  7. // does not enforce exhaustive pattern matching on "enum" types.
  8. const (
  9. FilterClusterID FilterField = iota
  10. FilterNode
  11. FilterNamespace
  12. FilterControllerKind
  13. FilterControllerName
  14. FilterPod
  15. FilterContainer
  16. // Filtering based on label aliases (team, department, etc.) should be a
  17. // responsibility of the query handler. By the time it reaches this
  18. // structured representation, we shouldn't have to be aware of what is
  19. // aliased to what.
  20. FilterLabel
  21. FilterAnnotation
  22. )
  23. // FilterOp is an enum that represents operations that can be performed
  24. // when filtering (equality, inequality, etc.)
  25. type FilterOp int
  26. // If you add a FilterOp, MAKE SURE TO UPDATE ALL FILTER IMPLEMENTATIONS! Go
  27. // does not enforce exhaustive pattern matching on "enum" types.
  28. const (
  29. FilterEquals FilterOp = iota
  30. FilterNotEquals
  31. )
  32. // AllocationFilter is a mini-DSL for filtering Allocation data by different
  33. // conditions. By specifying a more strict DSL instead of using arbitrary
  34. // functions we gain the ability to take advantage of storage-level filtering
  35. // performance improvements like indexes in databases. We can create a
  36. // transformation from our DSL to the storage's specific query language. We
  37. // also gain the ability to define a more feature-rich query language for
  38. // users, supporting more operators and more complex logic, if desired.
  39. type AllocationFilter interface {
  40. // Matches is the canonical in-Go function for determing if an Allocation
  41. // matches a filter.
  42. Matches(a *Allocation) bool
  43. }
  44. // AllocationFilterCondition is the lowest-level type of filter. It represents
  45. // the a filter operation (equality, inequality, etc.) on a field (namespace,
  46. // label, etc.).
  47. type AllocationFilterCondition struct {
  48. Field FilterField
  49. Op FilterOp
  50. // Key is for filters that require key-value pairs, like labels or
  51. // annotations.
  52. //
  53. // A filter of 'label[app]:"foo"' has Key="app" and Value="foo"
  54. Key string
  55. // Value is for _all_ filters. A filter of 'namespace:"kubecost"' has
  56. // Value="kubecost"
  57. Value string
  58. }
  59. // AllocationFilterOr is a set of filters that should be evaluated as a logical
  60. // OR.
  61. type AllocationFilterOr struct {
  62. Filters []AllocationFilter
  63. }
  64. // AllocationFilterOr is a set of filters that should be evaluated as a logical
  65. // AND.
  66. type AllocationFilterAnd struct {
  67. Filters []AllocationFilter
  68. }
  69. func (filter AllocationFilterCondition) Matches(a *Allocation) bool {
  70. // TODO: For these nil cases, what about != filters?
  71. if a == nil {
  72. return false
  73. }
  74. if a.Properties == nil {
  75. return false
  76. }
  77. // TODO Controller PARSING should allow controllerkind:controllername
  78. // syntax, converted to:
  79. // (AND (ControllerName Equals) (ControllerKind Equals))
  80. // The Allocation's value for the field to compare
  81. var valueToCompare string
  82. // toCompareMissing will be true if the value to be compared is missing in
  83. // the Allocation. For example, if we're filtering based on the value of
  84. // the "app" label, but the Allocation doesn't have an "app" label, this
  85. // will become true. This lets us deal with != gracefully.
  86. toCompareMissing := false
  87. // This switch maps the filter.Field to the field to be compared in
  88. // a.Properties and sets valueToCompare from the value in a.Properties.
  89. switch filter.Field {
  90. case FilterClusterID:
  91. valueToCompare = a.Properties.Cluster
  92. case FilterNode:
  93. valueToCompare = a.Properties.Node
  94. case FilterNamespace:
  95. valueToCompare = a.Properties.Namespace
  96. case FilterControllerKind:
  97. valueToCompare = a.Properties.ControllerKind
  98. case FilterControllerName:
  99. valueToCompare = a.Properties.Controller
  100. case FilterPod:
  101. valueToCompare = a.Properties.Pod
  102. case FilterContainer:
  103. valueToCompare = a.Properties.Container
  104. // Comes from GetAnnotation/LabelFilterFunc in KCM
  105. case FilterLabel:
  106. val, ok := a.Properties.Labels[filter.Key]
  107. if !ok {
  108. toCompareMissing = true
  109. } else {
  110. valueToCompare = val
  111. }
  112. case FilterAnnotation:
  113. val, ok := a.Properties.Annotations[filter.Key]
  114. if !ok {
  115. toCompareMissing = true
  116. } else {
  117. valueToCompare = val
  118. }
  119. default:
  120. log.Errorf("Allocation Filter: Unhandled filter field. This is a filter implementation error and requires immediate patching. Field: %d", filter.Field)
  121. return false
  122. }
  123. switch filter.Op {
  124. case FilterEquals:
  125. if toCompareMissing {
  126. return false
  127. }
  128. // namespace:"__unallocated__" should match a.Properties.Namespace = ""
  129. if valueToCompare == "" {
  130. return filter.Value == UnallocatedSuffix
  131. }
  132. if valueToCompare == filter.Value {
  133. return true
  134. }
  135. case FilterNotEquals:
  136. if toCompareMissing {
  137. return true
  138. }
  139. // namespace!:"__unallocated__" should match a.Properties.Namespace != ""
  140. if filter.Value == UnallocatedSuffix {
  141. return valueToCompare != ""
  142. }
  143. if valueToCompare != filter.Value {
  144. return true
  145. }
  146. default:
  147. log.Errorf("Allocation Filter: Unhandled filter op. This is a filter implementation error and requires immediate patching. Op: %d", filter.Op)
  148. return false
  149. }
  150. return false
  151. }
  152. func (and AllocationFilterAnd) Matches(a *Allocation) bool {
  153. filters := and.Filters
  154. if len(filters) == 0 {
  155. // TODO: Should an empty set of ANDs be true or false?
  156. return true
  157. }
  158. for _, filter := range filters {
  159. if !filter.Matches(a) {
  160. return false
  161. }
  162. }
  163. return true
  164. }
  165. func (or AllocationFilterOr) Matches(a *Allocation) bool {
  166. filters := or.Filters
  167. if len(filters) == 0 {
  168. return true
  169. }
  170. for _, filter := range filters {
  171. if filter.Matches(a) {
  172. return true
  173. }
  174. }
  175. return false
  176. }