allocationfilter.go 6.2 KB

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