allocationfilter.go 5.7 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 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. if a == nil {
  76. return false
  77. }
  78. if a.Properties == nil {
  79. return false
  80. }
  81. // The Allocation's value for the field to compare
  82. var valueToCompare string
  83. // toCompareMissing will be true if the value to be compared is missing in
  84. // the Allocation. For example, if we're filtering based on the value of
  85. // the "app" label, but the Allocation doesn't have an "app" label, this
  86. // will become true. This lets us deal with != gracefully.
  87. toCompareMissing := false
  88. // This switch maps the filter.Field to the field to be compared in
  89. // a.Properties and sets valueToCompare from the value in a.Properties.
  90. switch filter.Field {
  91. case FilterClusterID:
  92. valueToCompare = a.Properties.Cluster
  93. case FilterNode:
  94. valueToCompare = a.Properties.Node
  95. case FilterNamespace:
  96. valueToCompare = a.Properties.Namespace
  97. case FilterControllerKind:
  98. valueToCompare = a.Properties.ControllerKind
  99. case FilterControllerName:
  100. valueToCompare = a.Properties.Controller
  101. case FilterPod:
  102. valueToCompare = a.Properties.Pod
  103. case FilterContainer:
  104. valueToCompare = a.Properties.Container
  105. // Comes from GetAnnotation/LabelFilterFunc in KCM
  106. case FilterLabel:
  107. val, ok := a.Properties.Labels[filter.Key]
  108. if !ok {
  109. toCompareMissing = true
  110. } else {
  111. valueToCompare = val
  112. }
  113. case FilterAnnotation:
  114. val, ok := a.Properties.Annotations[filter.Key]
  115. if !ok {
  116. toCompareMissing = true
  117. } else {
  118. valueToCompare = val
  119. }
  120. default:
  121. log.Errorf("Allocation Filter: Unhandled filter field. This is a filter implementation error and requires immediate patching. Field: %s", filter.Field)
  122. return false
  123. }
  124. switch filter.Op {
  125. case FilterEquals:
  126. if toCompareMissing {
  127. return false
  128. }
  129. // namespace:"__unallocated__" should match a.Properties.Namespace = ""
  130. if valueToCompare == "" {
  131. return filter.Value == UnallocatedSuffix
  132. }
  133. if valueToCompare == filter.Value {
  134. return true
  135. }
  136. case FilterNotEquals:
  137. if toCompareMissing {
  138. return true
  139. }
  140. // namespace!:"__unallocated__" should match
  141. // a.Properties.Namespace != ""
  142. if filter.Value == UnallocatedSuffix {
  143. return valueToCompare != ""
  144. }
  145. if valueToCompare != filter.Value {
  146. return true
  147. }
  148. default:
  149. log.Errorf("Allocation Filter: Unhandled filter op. This is a filter implementation error and requires immediate patching. Op: %s", filter.Op)
  150. return false
  151. }
  152. return false
  153. }
  154. func (and AllocationFilterAnd) Matches(a *Allocation) bool {
  155. filters := and.Filters
  156. if len(filters) == 0 {
  157. return true
  158. }
  159. for _, filter := range filters {
  160. if !filter.Matches(a) {
  161. return false
  162. }
  163. }
  164. return true
  165. }
  166. func (or AllocationFilterOr) Matches(a *Allocation) bool {
  167. filters := or.Filters
  168. if len(filters) == 0 {
  169. return true
  170. }
  171. for _, filter := range filters {
  172. if filter.Matches(a) {
  173. return true
  174. }
  175. }
  176. return false
  177. }