Create and Manage Variants
A common task is to manage variants of the same configuration — for example, development, staging, and production variants, or per-region and per-tenant variants.
A variant is a space that was cloned from an upstream space — typically a base — and customized for a different environment, region, tenant, or experiment. Cloning links each unit in the variant space to the upstream unit it was copied from, so the variant can later be brought up to date as the upstream changes.
The cub variant commands manage variants at the space level and are the recommended way to work with them. Under the hood, the upstream relationships they establish are maintained per unit, and the lower-level unit commands remain fully supported — see working with variants at the unit level.
Managing variant spaces
The cub variant subcommands operate on a whole space and its units at once: seed a base space from rendered manifests, clone it into a variant, and later reconcile the variant with its upstream.
Seeding a base space with cub variant upload
When you already have rendered Kubernetes manifests — from the Helm plugin, kustomize build, helm template, or an external pipeline — cub variant upload ingests them into a space as units. It does not render anything; it stores what you give it.
kustomize build overlays/base | cub variant upload \
--component web --variant base \
--target web-base/cluster -
--component is required and names the component the space belongs to; --variant (default base) becomes the well-known Variant space label, and --stage, --environment, --region, --layer, and --owner set the rest. Set --stage for any space a ChangeWorkflow will promote into, since that is the label its Stages select on. The space slug comes from --space-pattern (default template:{{.Component.Slug}}-{{.Labels.Variant}}) or from an explicit --space, and the space is created if it does not exist. Every resource becomes its own unit, named for the resource rather than for the file it came from: a workload keeps its bare name (web), everything else takes its kind (web-service), and each AppConfig file becomes its own unit as well. The space records what it was uploaded from, in its ExternalSourceRef and ExternalSourceDigest annotations. --target sets the space's release target, which every unit that is not Unreleasable is released to. A workload gets a hash annotation for each ConfigMap in the bundle it uses, so that it rolls when the ConfigMap changes, and links that keep those hashes, as application configuration describes. Rendered Secrets are never uploaded — apply them out of band.
--namespace is the release namespace: where namespaced resources that name no namespace, and cluster-scoped resources, belong. Give a component one namespace per space — that release namespace — and everything downstream stays unambiguous. A space already has a single delivery destination in its ReleaseTargetID, and set-namespace is space-wide, so cub variant create --namespace fills exactly one namespace in and needs no per-resource selection. --create-namespace synthesizes the Namespace resource when the bundle lacks it, and is off by default: a bare Namespace carries none of the pod-security labels, NetworkPolicy, ResourceQuota or LimitRange that make a namespace usable, and a platform usually provisions those together. Asking it to synthesize a Namespace named after a placeholder is an error, because that object must never reach a cluster. If the bundle already carries the release Namespace, none is synthesized and the upload reports the collision rather than failing.
A bundle that puts a stray resource in a second namespace — a RoleBinding in kube-system, say — is the case one namespace per space does not cover. Uploading it is fine, but cub variant create --namespace would move that resource too, so scope set-namespace yourself with --where-resource. It takes two invocations, because a metadata.namespace filter cannot match a cluster-scoped Namespace resource, and --where-resource has no OR:
# the namespaced resources in the release namespace
cub function set --space web-prod \
--where-resource "metadata.namespace = 'confighubplaceholder'" \
set-namespace web-prod
# and the Namespace resource itself
cub function set --space web-prod \
--where-resource "ConfigHub.ResourceType = 'v1/Namespace' AND metadata.name = 'confighubplaceholder'" \
set-namespace web-prod
This is the recommended way to seed a promotable base space.
Every upload is create-or-update, so there is no separate re-upload mode: units whose resources are unchanged are left alone, changed ones are merged, and new ones are created. A unit whose resource is no longer in the input is emptied rather than deleted — it keeps its id, its links and its history, and the next release withdraws the object — and an upload whose plan would empty anything asks first unless you pass --yes. The merge takes the difference between what the source uploaded last time and what it uploads now, and applies it to the unit as it is, much as kubectl apply merges with its last-applied configuration. A change you made in ConfigHub after the upload — a replica count, an annotation — survives it, unless the source changed the same field, in which case the source's new value wins. To hold a value even against that, give the path an Override=External guard: the upload then withholds the source's change and reports it as a conflict.
# Hold this value even when the source changes it
cub function set --space web-base --unit web --guard Override=External set-replicas 5
Letting the source win is usually what you want for a space whose content is generated elsewhere. If you find yourself guarding the same path after every render, the value belongs in the source instead.
Creating a variant with cub variant create
cub variant create clones an upstream space and all of its units into a new downstream space, linking each clone to its upstream unit so the variant can be promoted later.
cub variant create prod web-base \
--environment Prod --region us-east2 \
--target web-prod/cluster \
--namespace web-prod \
--unit-delete-gate critical --unit-destroy-gate critical
The first argument is the variant name, which becomes the new space's Variant label; the second is the upstream space to clone from. The new space's name defaults to <component>-<variant> from the cloned space's labels — web-prod here — and can be overridden with --space-pattern. The new space inherits the upstream's labels (with Variant overridden), along with its permissions. It selects the triggers the upstream selects: it inherits the upstream's WhereTrigger and TriggerFilterID, or, when the upstream has neither and so runs the triggers in it, gets a WhereTrigger selecting the upstream space's triggers. The triggers are not copied, so a trigger you add to the new space runs only if its selector matches it. The new space starts with no delete gates; --space-delete-gate adds gates, and then the upstream's gates are kept as well. --target sets the new space's release target and stamps its TargetID annotation, and --namespace replaces the placeholder namespace from the base so each variant lands in its own namespace. The UpstreamSpaceID annotation it records is what makes the space promotable.
To automatically customize the clones, define PostClone triggers in the upstream space, or select them with its WhereTrigger or TriggerFilterID. The new space selects the same triggers, so they run during the clone. See managing dependencies and validation and policies.
Promoting a variant with cub variant promote
cub variant promote reconciles a variant space with the upstream space recorded in its UpstreamSpaceID annotation. In one command it upgrades every unit whose upstream has advanced (merging the upstream changes), clones any units added to the upstream since the variant was created or last promoted, and copies the new units' links.
# Preview what would change first.
cub variant promote web-prod --dry-run -o mutations
# Promote, optionally within a changeset and with a change description.
cub variant promote web-prod \
--changeset release-2024-06 \
--change-desc "Promote web to prod"
A promotion takes the options a cub unit update does, and applies them to every write it makes, including the set-namespace that places units it adds:
--clearancenames the guarded reasons the writes are cleared for. A guarded path it does not cover is withheld and reported as a conflict.--guard KEY=VALUErecords a reason on the paths written.--clearancehas to cover it.--subgrouprecords a category on the writes' mutations, so they can be found together afterwards.--tagputs a tag on the revision each unit the promotion writes is left at, added units included. Units it does not change are not tagged. A variant with a unit to write that the tag already marks is not promoted, since a tag marks one revision of a unit.
A promotion is a merge, so it is not cleared for the Override guards that keep a variant's own values unless --clearance says so.
cub variant promote web-prod --tag web-base/release-2024-06 --clearance owner=platform
Promotion only updates the configuration data; it does not make the change live. Publish a new Release afterward with cub release publish <space> so the GitOps operator picks it up.
A promotion that brings several upstream revisions records one revision in each unit for every upstream revision that has an effect there, re-running the functions each one recorded (see merging and rebasing). So there is no single revision before the promotion to go back to, and cub unit update --restore -1 undoes only its last step. Give the promotion a name to undo it by: promote within a ChangeSet with --changeset, and restore to Before:ChangeSet:<slug>, or promote a ChangeOrder with --change-order, and take it back out with cub variant demote. --squash merges each unit's range as one revision instead.
To promote a named change rather than everything the upstream has reached — one image bump, through dev then staging then prod, gated on each stage actually running it — pass --change-order. See tracking and promoting changes.
Only spaces created by cub variant create can be promoted this way. For finer-grained or cross-space promotions, use the unit-level --upgrade mechanism described under keeping variants in sync, typically wrapped in a ChangeSet.
Protecting a variant's own values
By default, an upstream change to a path reaches the variant at its next merge, even if the variant had set that path to something else. A value the variant has chosen as its own, such as production's replica count, should say so with an Override guard, so that upgrades and promotions leave it alone and report what they withheld as a conflict:
cub function set --space prod --unit backend-clone --guard Override=Upstream set-replicas 5
Keep a Variant's Own Values covers the Override guard, guards for other reasons and the clearances that write past them, and dealing with conflicts.
Base units
You may choose to maintain base units which you copy to create all of the variants you plan to deploy. Base units are similar to abstract classes in that they aren't intended to be deployed. For example, their space need not have a release target. A unit without a target is never in a Release, and you can select only units with targets with TargetID IS NOT NULL, or skip base units with labels.
This approach enables a clearer separation of common attributes and custom attributes.
Placeholders for undetermined values
Base units, and sometimes other new config units, contain default values for most fields, but some may require specific values that cannot yet be provided. This is indicated by placeholders.
A placeholder is a special string or number in the config data which indicates that you must replace it with a real value before the configuration is valid. ConfigHub uses the string "confighubplaceholder" to indicate where a string field needs to be supplied with a value and 9 9s (999999999) for integer fields.
The vet-placeholders function can be installed as a trigger to prevent applying configuration units with unreplaced placeholders, and get-placeholders can return a list of field paths containing placeholders.
Triggers are good for setting general properties of an environment: scale/cost (e.g., replicated vs not, backups vs not), privacy (e.g., public vs not, data with real PII vs not), security (e.g. must be encrypted vs not, can't run as root), and the like.
Links better address more specific resource properties like hostnames, IP addresses, etc.
See managing dependencies for guidance regarding how to replace placeholders with variant-specific values.
Going further
- Working with variants at the unit level — cloning, reshaping upstream links, ad hoc merges, and merging external sources
- Tracking and promoting changes — moving one named change through declared stages with a ChangeWorkflow and a ChangeOrder
- Keep a Variant's Own Values — guards, clearances, and the conflicts a merge records
- Advanced merging techniques — the merge engine itself: how a range is walked and replayed, what
--squashchanges, whatMutationSourcesrecords, and theWhereMutation, subtraction, andWhereResourcecontrols