allocationfilter.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. package kubecost
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/kubecost/opencost/pkg/log"
  6. )
  7. // FilterField is an enum that represents Allocation-specific fields that can be
  8. // filtered on (namespace, label, etc.)
  9. type FilterField string
  10. // If you add a FilterField, MAKE SURE TO UPDATE ALL FILTER IMPLEMENTATIONS! Go
  11. // does not enforce exhaustive pattern matching on "enum" types.
  12. const (
  13. FilterClusterID FilterField = "clusterid"
  14. FilterNode = "node"
  15. FilterNamespace = "namespace"
  16. FilterControllerKind = "controllerkind"
  17. FilterControllerName = "controllername"
  18. FilterPod = "pod"
  19. FilterContainer = "container"
  20. // Filtering based on label aliases (team, department, etc.) should be a
  21. // responsibility of the query handler. By the time it reaches this
  22. // structured representation, we shouldn't have to be aware of what is
  23. // aliased to what.
  24. FilterLabel = "label"
  25. FilterAnnotation = "annotation"
  26. FilterServices = "services"
  27. )
  28. // FilterOp is an enum that represents operations that can be performed
  29. // when filtering (equality, inequality, etc.)
  30. type FilterOp string
  31. // If you add a FilterOp, MAKE SURE TO UPDATE ALL FILTER IMPLEMENTATIONS! Go
  32. // does not enforce exhaustive pattern matching on "enum" types.
  33. const (
  34. // FilterEquals is the equality operator
  35. // "kube-system" FilterEquals "kube-system" = true
  36. // "kube-syste" FilterEquals "kube-system" = false
  37. FilterEquals FilterOp = "equals"
  38. // FilterNotEquals is the inequality operator
  39. FilterNotEquals = "notequals"
  40. // FilterContains is an array/slice membership operator
  41. // ["a", "b", "c"] FilterContains "a" = true
  42. FilterContains = "contains"
  43. // FilterNotContains is an array/slice non-membership operator
  44. // ["a", "b", "c"] FilterNotContains "d" = true
  45. FilterNotContains = "notcontains"
  46. // FilterStartsWith matches strings with the given prefix.
  47. // "kube-system" StartsWith "kube" = true
  48. //
  49. // When comparing with a field represented by an array/slice, this is like
  50. // applying FilterContains to every element of the slice.
  51. FilterStartsWith = "startswith"
  52. // FilterContainsPrefix is like FilterContains, but using StartsWith instead
  53. // of Equals.
  54. // ["kube-system", "abc123"] ContainsPrefix ["kube"] = true
  55. FilterContainsPrefix = "containsprefix"
  56. )
  57. // AllocationFilter represents anything that can be used to filter an
  58. // Allocation.
  59. //
  60. // Implement this interface with caution. While it is generic, it
  61. // is intended to be introspectable so query handlers can perform various
  62. // optimizations. These optimizations include:
  63. // - Routing a query to the most optimal cache
  64. // - Querying backing data stores efficiently (e.g. translation to SQL)
  65. //
  66. // Custom implementations of this interface outside of this package should not
  67. // expect to receive these benefits. Passing a custom implementation to a
  68. // handler may in errors.
  69. type AllocationFilter interface {
  70. // Matches is the canonical in-Go function for determing if an Allocation
  71. // matches a filter.
  72. Matches(a *Allocation) bool
  73. String() string
  74. }
  75. // AllocationFilterCondition is the lowest-level type of filter. It represents
  76. // the a filter operation (equality, inequality, etc.) on a field (namespace,
  77. // label, etc.).
  78. type AllocationFilterCondition struct {
  79. Field FilterField
  80. Op FilterOp
  81. // Key is for filters that require key-value pairs, like labels or
  82. // annotations.
  83. //
  84. // A filter of 'label[app]:"foo"' has Key="app" and Value="foo"
  85. Key string
  86. // Value is for _all_ filters. A filter of 'namespace:"kubecost"' has
  87. // Value="kubecost"
  88. Value string
  89. }
  90. func (afc AllocationFilterCondition) String() string {
  91. if afc.Key == "" {
  92. return fmt.Sprintf(`(%s %s "%s")`, afc.Op, afc.Field, afc.Value)
  93. }
  94. return fmt.Sprintf(`(%s %s[%s] "%s")`, afc.Op, afc.Field, afc.Key, afc.Value)
  95. }
  96. // AllocationFilterOr is a set of filters that should be evaluated as a logical
  97. // OR.
  98. type AllocationFilterOr struct {
  99. Filters []AllocationFilter
  100. }
  101. func (af AllocationFilterOr) String() string {
  102. s := "(or"
  103. for _, f := range af.Filters {
  104. s += fmt.Sprintf(" %s", f)
  105. }
  106. s += ")"
  107. return s
  108. }
  109. // AllocationFilterOr is a set of filters that should be evaluated as a logical
  110. // AND.
  111. type AllocationFilterAnd struct {
  112. Filters []AllocationFilter
  113. }
  114. func (af AllocationFilterAnd) String() string {
  115. s := "(or"
  116. for _, f := range af.Filters {
  117. s += fmt.Sprintf(" %s", f)
  118. }
  119. s += ")"
  120. return s
  121. }
  122. func (filter AllocationFilterCondition) Matches(a *Allocation) bool {
  123. if a == nil {
  124. return false
  125. }
  126. if a.Properties == nil {
  127. return false
  128. }
  129. // The Allocation's value for the field to compare
  130. // We use an interface{} so this can contain the services []string slice
  131. var valueToCompare interface{}
  132. // toCompareMissing will be true if the value to be compared is missing in
  133. // the Allocation. For example, if we're filtering based on the value of
  134. // the "app" label, but the Allocation doesn't have an "app" label, this
  135. // will become true. This lets us deal with != gracefully.
  136. toCompareMissing := false
  137. // This switch maps the filter.Field to the field to be compared in
  138. // a.Properties and sets valueToCompare from the value in a.Properties.
  139. switch filter.Field {
  140. case FilterClusterID:
  141. valueToCompare = a.Properties.Cluster
  142. case FilterNode:
  143. valueToCompare = a.Properties.Node
  144. case FilterNamespace:
  145. valueToCompare = a.Properties.Namespace
  146. case FilterControllerKind:
  147. valueToCompare = a.Properties.ControllerKind
  148. case FilterControllerName:
  149. valueToCompare = a.Properties.Controller
  150. case FilterPod:
  151. valueToCompare = a.Properties.Pod
  152. case FilterContainer:
  153. valueToCompare = a.Properties.Container
  154. // Comes from GetAnnotation/LabelFilterFunc in KCM
  155. case FilterLabel:
  156. val, ok := a.Properties.Labels[filter.Key]
  157. if !ok {
  158. toCompareMissing = true
  159. } else {
  160. valueToCompare = val
  161. }
  162. case FilterAnnotation:
  163. val, ok := a.Properties.Annotations[filter.Key]
  164. if !ok {
  165. toCompareMissing = true
  166. } else {
  167. valueToCompare = val
  168. }
  169. case FilterServices:
  170. valueToCompare = a.Properties.Services
  171. default:
  172. log.Errorf("Allocation Filter: Unhandled filter field. This is a filter implementation error and requires immediate patching. Field: %s", filter.Field)
  173. return false
  174. }
  175. switch filter.Op {
  176. case FilterEquals:
  177. if toCompareMissing {
  178. return false
  179. }
  180. // namespace:"__unallocated__" should match a.Properties.Namespace = ""
  181. if valueToCompare == "" {
  182. return filter.Value == UnallocatedSuffix
  183. }
  184. if valueToCompare == filter.Value {
  185. return true
  186. }
  187. case FilterNotEquals:
  188. if toCompareMissing {
  189. return true
  190. }
  191. // namespace!:"__unallocated__" should match
  192. // a.Properties.Namespace != ""
  193. if filter.Value == UnallocatedSuffix {
  194. return valueToCompare != ""
  195. }
  196. if valueToCompare != filter.Value {
  197. return true
  198. }
  199. case FilterContains:
  200. if stringSlice, ok := valueToCompare.([]string); ok {
  201. if len(stringSlice) == 0 {
  202. return filter.Value == UnallocatedSuffix
  203. }
  204. for _, s := range stringSlice {
  205. if s == filter.Value {
  206. return true
  207. }
  208. }
  209. } else {
  210. log.Warnf("Allocation Filter: invalid 'contains' call for non-list filter value")
  211. }
  212. case FilterNotContains:
  213. if stringSlice, ok := valueToCompare.([]string); ok {
  214. // services!:"__unallocated__" should match
  215. // len(a.Properties.Services) > 0
  216. //
  217. // TODO: is this true?
  218. if filter.Value == UnallocatedSuffix {
  219. return len(stringSlice) > 0
  220. }
  221. for _, s := range stringSlice {
  222. if s == filter.Value {
  223. return false
  224. }
  225. }
  226. return true
  227. } else {
  228. log.Warnf("Allocation Filter: invalid 'notcontains' call for non-list filter value")
  229. }
  230. case FilterStartsWith:
  231. if toCompareMissing {
  232. return false
  233. }
  234. // We don't need special __unallocated__ logic here because a query
  235. // asking for "__unallocated__" won't have a wildcard and unallocated
  236. // properties are the empty string.
  237. s, ok := valueToCompare.(string)
  238. if !ok {
  239. log.Warnf("Allocation Filter: invalid 'startswith' call for field with unsupported type")
  240. return false
  241. }
  242. return strings.HasPrefix(s, filter.Value)
  243. case FilterContainsPrefix:
  244. if toCompareMissing {
  245. return false
  246. }
  247. // We don't need special __unallocated__ logic here because a query
  248. // asking for "__unallocated__" won't have a wildcard and unallocated
  249. // properties are the empty string.
  250. values, ok := valueToCompare.([]string)
  251. if !ok {
  252. log.Warnf("Allocation Filter: invalid '%s' call for field with unsupported type", FilterContainsPrefix)
  253. return false
  254. }
  255. for _, s := range values {
  256. if strings.HasPrefix(s, filter.Value) {
  257. return true
  258. }
  259. }
  260. return false
  261. default:
  262. log.Errorf("Allocation Filter: Unhandled filter op. This is a filter implementation error and requires immediate patching. Op: %s", filter.Op)
  263. return false
  264. }
  265. return false
  266. }
  267. func (and AllocationFilterAnd) Matches(a *Allocation) bool {
  268. filters := and.Filters
  269. if len(filters) == 0 {
  270. return true
  271. }
  272. for _, filter := range filters {
  273. if !filter.Matches(a) {
  274. return false
  275. }
  276. }
  277. return true
  278. }
  279. func (or AllocationFilterOr) Matches(a *Allocation) bool {
  280. filters := or.Filters
  281. if len(filters) == 0 {
  282. return true
  283. }
  284. for _, filter := range filters {
  285. if filter.Matches(a) {
  286. return true
  287. }
  288. }
  289. return false
  290. }