allocationfilter.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 string
  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 = "clusterid"
  10. FilterNode = "node"
  11. FilterNamespace = "namespace"
  12. FilterControllerKind = "controllerkind"
  13. FilterControllerName = "controllername"
  14. FilterPod = "pod"
  15. FilterContainer = "container"
  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 = "label"
  21. FilterAnnotation = "annotation"
  22. )
  23. // FilterOp is an enum that represents operations that can be performed
  24. // when filtering (equality, inequality, etc.)
  25. type FilterOp string
  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 = "equals"
  30. FilterNotEquals = "notequals"
  31. )
  32. // AllocationFilter represents anything that can be used to filter an
  33. // Allocation.
  34. //
  35. // Implement this interface with caution. While it is generic, it
  36. // is intended to be introspectable so query handlers can perform various
  37. // optimizations. These optimizations include:
  38. // - Routing a query to the most optimal cache
  39. // - Querying backing data stores efficiently (e.g. translation to SQL)
  40. //
  41. // Custom implementations of this interface outside of this package should not
  42. // expect to receive these benefits. Passing a custom implementation to a
  43. // handler may in errors.
  44. type AllocationFilter interface {
  45. // Matches is the canonical in-Go function for determing if an Allocation
  46. // matches a filter.
  47. Matches(a *Allocation) bool
  48. }
  49. // AllocationFilterCondition is the lowest-level type of filter. It represents
  50. // the a filter operation (equality, inequality, etc.) on a field (namespace,
  51. // label, etc.).
  52. type AllocationFilterCondition struct {
  53. Field FilterField
  54. Op FilterOp
  55. // Key is for filters that require key-value pairs, like labels or
  56. // annotations.
  57. //
  58. // A filter of 'label[app]:"foo"' has Key="app" and Value="foo"
  59. Key string
  60. // Value is for _all_ filters. A filter of 'namespace:"kubecost"' has
  61. // Value="kubecost"
  62. Value string
  63. }
  64. // AllocationFilterOr is a set of filters that should be evaluated as a logical
  65. // OR.
  66. type AllocationFilterOr struct {
  67. Filters []AllocationFilter
  68. }
  69. // AllocationFilterOr is a set of filters that should be evaluated as a logical
  70. // AND.
  71. type AllocationFilterAnd struct {
  72. Filters []AllocationFilter
  73. }
  74. func (filter AllocationFilterCondition) Matches(a *Allocation) bool {
  75. // TODO: For these nil cases, what about != filters?
  76. if a == nil {
  77. return false
  78. }
  79. if a.Properties == nil {
  80. return false
  81. }
  82. // The Allocation's value for the field to compare
  83. var valueToCompare string
  84. // toCompareMissing will be true if the value to be compared is missing in
  85. // the Allocation. For example, if we're filtering based on the value of
  86. // the "app" label, but the Allocation doesn't have an "app" label, this
  87. // will become true. This lets us deal with != gracefully.
  88. toCompareMissing := false
  89. // This switch maps the filter.Field to the field to be compared in
  90. // a.Properties and sets valueToCompare from the value in a.Properties.
  91. switch filter.Field {
  92. case FilterClusterID:
  93. valueToCompare = a.Properties.Cluster
  94. case FilterNode:
  95. valueToCompare = a.Properties.Node
  96. case FilterNamespace:
  97. valueToCompare = a.Properties.Namespace
  98. case FilterControllerKind:
  99. valueToCompare = a.Properties.ControllerKind
  100. case FilterControllerName:
  101. valueToCompare = a.Properties.Controller
  102. case FilterPod:
  103. valueToCompare = a.Properties.Pod
  104. case FilterContainer:
  105. valueToCompare = a.Properties.Container
  106. // Comes from GetAnnotation/LabelFilterFunc in KCM
  107. case FilterLabel:
  108. val, ok := a.Properties.Labels[filter.Key]
  109. if !ok {
  110. toCompareMissing = true
  111. } else {
  112. valueToCompare = val
  113. }
  114. case FilterAnnotation:
  115. val, ok := a.Properties.Annotations[filter.Key]
  116. if !ok {
  117. toCompareMissing = true
  118. } else {
  119. valueToCompare = val
  120. }
  121. default:
  122. log.Errorf("Allocation Filter: Unhandled filter field. This is a filter implementation error and requires immediate patching. Field: %d", filter.Field)
  123. return false
  124. }
  125. switch filter.Op {
  126. case FilterEquals:
  127. if toCompareMissing {
  128. return false
  129. }
  130. // namespace:"__unallocated__" should match a.Properties.Namespace = ""
  131. if valueToCompare == "" {
  132. return filter.Value == UnallocatedSuffix
  133. }
  134. if valueToCompare == filter.Value {
  135. return true
  136. }
  137. case FilterNotEquals:
  138. if toCompareMissing {
  139. return true
  140. }
  141. // namespace!:"__unallocated__" should match
  142. // a.Properties.Namespace != ""
  143. if filter.Value == UnallocatedSuffix {
  144. return valueToCompare != ""
  145. }
  146. if valueToCompare != filter.Value {
  147. return true
  148. }
  149. default:
  150. log.Errorf("Allocation Filter: Unhandled filter op. This is a filter implementation error and requires immediate patching. Op: %d", filter.Op)
  151. return false
  152. }
  153. return false
  154. }
  155. func (and AllocationFilterAnd) Matches(a *Allocation) bool {
  156. filters := and.Filters
  157. if len(filters) == 0 {
  158. // TODO: Should an empty set of ANDs be true or false?
  159. return true
  160. }
  161. for _, filter := range filters {
  162. if !filter.Matches(a) {
  163. return false
  164. }
  165. }
  166. return true
  167. }
  168. func (or AllocationFilterOr) Matches(a *Allocation) bool {
  169. filters := or.Filters
  170. if len(filters) == 0 {
  171. return true
  172. }
  173. for _, filter := range filters {
  174. if filter.Matches(a) {
  175. return true
  176. }
  177. }
  178. return false
  179. }