cub function reduce
cub function reduce
Summarize what a function returns across units
Synopsis
Run a function on units in a space, or across all spaces without --space, and summarize what it returns.
The function named first, the map, is run on each unit selected. It must read configuration and return values (an AttributeValueList), as get-starlark, get-replicas and the other get- functions do; a missing get- prefix is supplied. Each value it returns is a row, which carries where it came from: the unit, its space, and the target the unit is released to.
--group-by is a CEL expression evaluated on each row, as row, returning the key of the row's group, or a list of keys for a row that belongs to several groups. Without it, every row is in one group. A row has the fields of the value (ResourceName, ResourceType, Path, Value, AttributeName, DataType, FunctionName) and Origin (UnitID, UnitSlug, UnitLabels, SpaceID, SpaceSlug, SpaceLabels, TargetID, TargetSlug, TargetSpaceSlug, RevisionNum). TargetID is null and the target's slugs empty for a unit with no target. A target's slug is unique only in its space, so name a cluster with both, as in row.Origin.TargetSpaceSlug + "/" + row.Origin.TargetSlug. Read a label a unit or space may not have with ?, as in row.Origin.SpaceLabels[?"Environment"].orValue("none"): a unit with a row the expression fails on is reported, and none of its rows is grouped.
The reducer, --reducer, is run once on each group's rows. reduce-starlark takes a Starlark program defining reduce(rows), which returns a list of values, each a dict with at least Path and Value; 'group' is the group's key. Pass the reducer's arguments with --reducer-arg, once each, in order.
Nothing is written. The default output lists each value the reducer returned for each group. -o json and the other output formats show the whole response.
Examples:
# Count Deployments per space across the organization
cub function reduce \
--resource-type apps/v1/Deployment \
--group-by 'row.Origin.SpaceSlug' \
--reducer reduce-starlark \
--reducer-arg 'def reduce(rows): return [{"Path": "deployments", "Value": len(rows)}]' \
get-replicas
# Total replicas per Environment label of the spaces, where total-replicas.star is
# def reduce(rows):
# total = 0
# for r in rows:
# total += r["Value"]
# return [{"Path": "replicas", "Value": total}]
cub function reduce \
--group-by 'row.Origin.SpaceLabels[?"Environment"].orValue("none")' \
--reducer reduce-starlark \
--reducer-arg "$(cat total-replicas.star)" \
get-replicas
# Distinct container images per target, read with a Starlark map
cub function reduce --space my-space \
--group-by 'row.Origin.TargetSpaceSlug + "/" + row.Origin.TargetSlug' \
--reducer reduce-starlark \
--reducer-arg "$(cat images-per-target.star)" \
get-starlark "$(cat container-images.star)"
cub function reduce <map-function> [<arg1> ...] --reducer <function> [--reducer-arg <arg> ...] [--group-by <expression>] [flags]
Options
--filter string Filter entity to apply to the list. Specify as 'space/filter' for cross-space filters or just 'filter' for current space. Supports both slugs and UUIDs. The filter will be combined with any --where clause using AND logic. Examples: "production-filters/security-check", "my-filter-uuid", "validation-rules"
--group-by string CEL expression evaluated on each row, as row, returning the key of its group or a list of keys; without it, every row is in one group
-h, --help help for reduce
--include-hidden string[="*"] Also select hidden entities, to list or to act on: those hidden for the given HiddenReasons, comma-separated, as in --include-hidden=BackingUnit, or for any reason but Archived when given no value or "*". ConfigHub/YAML Units, which hold the configuration of other entities, are hidden with the reason BackingUnit. Archived entities are selected only when the reasons name Archived, as in --include-hidden=Archived or --include-hidden='*,Archived'. A --where naming entities by Slug or ID lists them whether hidden or not, but a bulk operation acts on archived ones only when --include-hidden names Archived
-o, --output string Output format. One of: json, yaml, name, wide, mutations, jq=<expr>, yq=<expr>, custom-columns=<spec>
--quiet No default output.
--reducer string the function run once on each group's rows, such as reduce-starlark
--reducer-arg stringArray an argument of the reducer, in order; repeat for each, or use --name=value to name its parameter
--resource-type string select only the units that contain a resource of this type, such as apps/v1/Deployment (with --where-data, one that matches it); it does not narrow which resources the function operates on, so a function such as set-label still changes every resource of each unit selected: use --where-resource for that
--toolchain string ToolchainType of the units to run the map on (default "Kubernetes/YAML")
--unit strings select specific units by slug or UUID (can be repeated or comma-separated)
--verbose Detailed output, additive with default output
--where string Filter expression using SQL-inspired syntax. Supports conjunctions with AND. String operators: =, !=, <, >, <=, >=, LIKE, NOT LIKE, ILIKE, ~~, !~~, ~, ~*, !~, !~*. Pattern matching with LIKE/ILIKE uses % and _ wildcards. Regex operators (~, ~*, !~, !~*) support POSIX regular expressions. A related entity is referenced by prefix, as in "UpstreamUnit.Slug = 'base'"; when the reference names a list, a * segment matches any element, as in "FromLink.*.Slug = 'upgrade-app'". Examples: "Slug LIKE 'app-%'", "DisplayName ILIKE '%backend%'", "Slug ~ '^[a-z]+-[0-9]+$'". 'cub <entity> explain --where' lists the attributes an entity's expressions can name
--where-data string where data filter
--where-resource string filter which resources the map reads; the map runs only on the units holding such a resource, which makes a reduce over a few kinds of resource across many units much faster
Options inherited from parent commands
--component string the Component of --variant, when that names the variant alone; --variant <component>/<variant> says the same in one flag
--context string The context to use for this command
--debug Debug output
--executor-space string Space ID or slug whose executor to use for builtin functions (org-level only)
--space string space to operate in, by slug or UUID. Omitted, a list or bulk operation spans the organization and a single entity is named as <space>/<slug>; "*" means the same as omitting it
--variant string variant to operate in, as <component>/<variant>, or as <variant> with --component: the space naming that Component whose Variant label is <variant>. The same scope as --space, named by what it is rather than by the space's slug; the two cannot be combined
SEE ALSO
- cub function - Function commands