This document outlines the filtering language added to the Allocation API in v1.96 of Kubecost, superseding the original filtering parameters (e.g. filterNamespaces=
). One of the primary goals of the new filter language was to introduce support for "not equals" (e.g. namespace != kubecost
) queries while maintaining extensibility.
Note: V1 filters will continue to be supported in all relevant APIs. APIs will first check for the
filter=
parameter. If it is present, V2 filters will be used. If it is not present, APIs will attempt to use V1 filters.
The supported filter fields as of v1.96 are:
cluster
node
namespace
controllerName
controllerKind
(e.g.deployment
,daemonset
)container
pod
services
label
annotation
(same syntax as label, see examples)
The supported filter ops as of v1.96 are:
:
(equality, or "contains" if an array type)!:
(inequality, or "not contains" if an array type)
Filter values are strings surrounded by "
. Multiple values can be separated by commas ,
, representing logical OR.
Each individual filter is separated by a +
, representing logical AND.
Filters exist under the filter=
parameter in supported APIs. Supported APIs are currently:
- Allocation API
- Request Sizing APIs (Note: This is currently only supported in beta UI view.)
Here are some example filters to see how the filtering language works:
namespace:"kubecost"+container:"cost-model"
Return only results that are in thekubecost
namespace and are for thecost-model
container.cluster:"cluster-one"+label[app]="cost-analyzer"
Return only results in clustercluster-one
that are labeled withapp=cost-analyzer
.cluster!:"cluster-one"
Ignore results from clustercluster-one
namespace:"kubecost","kube-system"
Return only results from namespaceskubecost
andkube-system
.namespace!:"kubecost","kube-system"
Return results for all namespaces exceptkubecost
andkube-system
.
For example, in an Allocation query:
http://localhost:9090/model/allocation?window=1d&accumulate=true&aggregate=namespace&filter=cluster!:%22cluster-one%22
The format is essentially: <filter field> <filter op> <filter value>
To see the filter language's formal grammar and lexer/parser implementation, check out OpenCost's pkg/util/allocationfilterutil/v2
.