Skip to content

Storing and Using Application Configuration

ConfigHub enables application configuration files to be represented in their native formats while supporting standard ConfigHub configuration functionality. Application configuration units can be operated upon by standard ConfigHub functions, filtered using ConfigHub's filter syntax, and managed as variants with upstream/downstream relationships. Values can be resolved across links via the Needs/Provides mechanism. Application configuration units can be rendered and deployed as Kubernetes ConfigMaps, mounted as ConfigMap volumes, or -- in the case of Env files -- injected as environment variables using envFrom.

Supported formats

ConfigHub supports the following ToolchainType values for application configuration:

ToolchainType Format File suffix
AppConfig/YAML YAML .yaml
AppConfig/JSON JSON .json
AppConfig/INI INI .ini
AppConfig/TOML TOML .toml
AppConfig/Properties Java Properties .properties
AppConfig/Env Environment variables .env
AppConfig/Text Text .txt

Required metadata fields

Each application configuration file must include ConfigHub metadata fields, represented consistently with its format. These fields are stripped when rendering a ConfigMap for use within a Kubernetes workload.

  • configHub.configName -- Uniquely identifies the configuration file. Required for most ConfigHub functionality. Treated similarly to Kubernetes resource names. Also used to generate the data key in the rendered ConfigMap, with the format's file suffix appended (e.g., MyApplicationConfig.ini).
  • configHub.configSchema -- Uniquely identifies the set of valid values. Required. Conceptually similar to a Kubernetes resource type (e.g., apps/v1/Deployment). Can be used with the vet-jsonschema function to validate application configuration units.

For AppConfig/Text, these metadata fields are stored as standard YAML frontmatter at the beginning of the file, delimited by ---, with the fields nested under a configHub key (see the Text example below). For all other formats, the fields use the configHub. prefix in the format's native syntax.

Example configuration files

Below are examples showing the same application configuration in each supported format.

YAML (AppConfig/YAML)

configHub:
  configName: MyApplicationConfig
  configSchema: SimpleApp
app:
  features:
    - authentication
    - logging
  name: MyApplication
  version: 1.0.0
database:
  host: localhost
  port: 5432
  ssl:
    enabled: true

JSON (AppConfig/JSON)

{
  "configHub": {
    "configName": "MyApplicationConfig",
    "configSchema": "SimpleApp"
  },
  "app": {
    "features": ["authentication", "logging"],
    "name": "MyApplication",
    "version": "1.0.0"
  },
  "database": {
    "host": "localhost",
    "port": 5432,
    "ssl": {
      "enabled": true
    }
  }
}

TOML (AppConfig/TOML)

[configHub]
configName = "MyApplicationConfig"
configSchema = "SimpleApp"

[app]
features = ["authentication", "logging"]
name = "MyApplication"
version = "1.0.0"

[database]
host = "localhost"
port = 5432

[database.ssl]
enabled = true

INI (AppConfig/INI)

[configHub]
configName = MyApplicationConfig
configSchema = SimpleApp

[app]
features.0 = authentication
features.1 = logging
name = MyApplication
version = 1.0.0

[database]
host = localhost
port = 5432

[database.ssl]
enabled = true

Properties (AppConfig/Properties)

configHub.configName=MyApplicationConfig
configHub.configSchema=SimpleApp
app.features.0=authentication
app.features.1=logging
app.name=MyApplication
app.version=1.0.0
database.host=localhost
database.port=5432
database.ssl.enabled=true

Env (AppConfig/Env)

configHub.configName=MyApplicationConfig
configHub.configSchema=SimpleApp
APP_FEATURES_0=authentication
APP_FEATURES_1=logging
APP_NAME=MyApplication
APP_VERSION=1.0.0
DATABASE_HOST=localhost
DATABASE_PORT=5432
DATABASE_SSL_ENABLED=true

Env values are always strings, since shell variables have no types. The other formats keep integers and booleans as such.

Text (AppConfig/Text)

Text files use standard YAML frontmatter (delimited by ---) to store ConfigHub metadata:

---
configHub:
  configName: MyApplicationConfig
  configSchema: SimpleApp
---
# My Application Config

Valid config options are:
* Database Host
* Database Port
* Database SSL Enabled

Changing values with functions

The path functions work on application configuration as they do on Kubernetes resources, with the Unit's configSchema in place of the resource type. Keys are paths into the parsed configuration, with nested keys separated by dots:

cub function set --space my-space --unit my-config --toolchain AppConfig/Env \
    set-string-path SimpleApp DATABASE_HOST postgres.example.com

get-string-path, get-int-path and get-bool-path read values the same way, and set-int-path and set-bool-path write typed values in the formats that have them. See configuration path syntax.

Deploying as a Kubernetes ConfigMap

Application configuration units are rendered as Kubernetes ConfigMaps by attaching a render-configmap Invocation as the TransformInvocation on an Upsert Link from a downstream Kubernetes/YAML Unit. Rendering is a normal ConfigHub function that runs as part of resolution, so no Target and no bot are involved, and the same mechanism generalizes to other transformation pipelines.

Two rendering modes are supported:

Immutable mode (default, --immutable=true on render-configmap): Each rendering produces an immutable ConfigMap with a unique name generated by appending a content hash to the unit slug (similar to ConfigMaps generated by Kustomize). The most recent ConfigMap has the annotation confighub.com/RenderRevision: Latest. The workload must use confighubplaceholder in its ConfigMap references, and the Needs/Provides mechanism replaces them with the generated name. To bound how many historical ConfigMaps remain in the downstream Unit, register the prune-configmaps function as a Mutation Trigger on the Space (see Pruning old ConfigMaps below).

Mutable mode (--immutable=false on render-configmap): Produces a single mutable ConfigMap with a stable, predictable name equal to the unit slug (no hash suffix). The workload can reference the ConfigMap by name directly. The ConfigMap carries a confighub.com/Hash annotation containing the content hash. This annotation is registered as a provided value on the ConfigMap and as a needed value on the hash annotations of workload pod templates, so it is automatically propagated via the Needs/Provides mechanism. When the ConfigMap content changes, the hash annotation on the workload's pod template changes, triggering a rolling update. Mutable mode is simpler to set up but does not preserve old ConfigMap versions during rollouts.

cub unit create --space my-space my-config app.env --toolchain AppConfig/Env
cub unit create --space my-space my-configmap
cub link create --space my-space - my-configmap my-namespace

# Create the render-configmap Invocation (one per (toolchain, mode) combination).
cub invocation create --space my-space render-env-immutable AppConfig/Env -- render-configmap --immutable=true

# Wire the upstream AppConfig unit to the downstream Kubernetes/YAML unit via Upsert.
cub link create --space my-space --wait - my-configmap my-config \
    --update-type Upsert --auto-update \
    --transform-invocation my-space/render-env-immutable

For AppConfig/Env envFrom injection (each variable becomes a separate ConfigMap data entry), add --as-key-value=true after render-configmap in cub invocation create.

For mutable rendering, use --immutable=false instead of --immutable=true.

After the link resolves, cub unit data --space my-space my-configmap shows the rendered ConfigMap.

Pruning old ConfigMaps

Because Upsert appends new immutable ConfigMaps to the downstream Unit each time the upstream changes, register prune-configmaps as a Mutation Trigger on the Space (or on the downstream Unit's Space, scoped to ConfigMaps) to bound retention. cub variant upload creates this Trigger for the immutable ConfigMaps it renders:

cub trigger create --space my-space prune-configmaps Mutation Kubernetes/YAML prune-configmaps 10 \
    --where-resource "ConfigHub.ResourceType = 'v1/ConfigMap'"

The retention limit is a function argument, so it is passed positionally (or as -- --revision-history-limit=10), not as a flag on cub trigger create. The resource-type predicate belongs to --where-resource, which filters the resources a Trigger operates on; --where-unit-field filters Units and has no resource-type attribute to match against.

The trigger groups ConfigMaps by their confighub.com/ResourceNameStableCore annotation, orders each group by confighub.com/RevisionNum, keeps the newest entries, and removes the rest. The newest keeps confighub.com/RenderRevision: Latest, and the older entries it keeps are marked confighub.com/RenderRevision: Superseded and confighub.com/VisitorOptions: IgnoreProvided, so that Needs/Provides offers workloads only the newest. Mutable ConfigMaps are ignored.

Link the unit containing your Kubernetes workload (e.g., a Deployment) to the ConfigMap unit:

cub link create --space my-space - my-deployment my-configmap

Immutable mode workload configuration

In immutable mode, the workload should use confighubplaceholder in its ConfigMap references. Needs/Provides will replace the placeholder with the generated ConfigMap name. You may optionally use --where-resource on the link to match only the most recently generated ConfigMap:

cub link create --space my-space - my-deployment my-configmap --where-resource "metadata.annotations.confighub~1com/RenderRevision = 'Latest'"

Note: ~1 escapes the dots within a single path segment (an encoding inspired by JSON Pointer), so that they are not read as path separators. The annotation key confighub.com/RenderRevision therefore appears as confighub~1com/RenderRevision: the dot in confighub.com is escaped, while the / is left alone.

spec:
  template:
    spec:
      containers:
        - name: nginx
          volumeMounts:
            - name: config-volume
              mountPath: /etc/nginx/app.properties
              subPath: app.properties
      volumes:
        - name: config-volume
          configMap:
            name: confighubplaceholder

When the workload unit is resolved, confighubplaceholder will be replaced with the name of the latest rendered ConfigMap.

Workloads that use several immutable ConfigMaps

When one Link offers a workload several immutable ConfigMaps, as when Upsert renders the ConfigMaps of several AppConfig Units into one downstream Unit, a bare confighubplaceholder can't say which ConfigMap a reference is for, and references are paired with ConfigMaps in whatever order each comes. Use a named placeholder instead: confighubplaceholder followed by the AppConfig Unit's slug, with dashes and dots replaced by underscores. Each reference is then filled with a rendering of the Unit it names, and a newer rendering of another Unit doesn't move it:

spec:
  template:
    spec:
      containers:
        - name: app
          envFrom:
            - configMapRef:
                name: confighubplaceholderapp_env
            - configMapRef:
                name: confighubplaceholderfeature_flags

Here confighubplaceholderapp_env is filled with the ConfigMap rendered from the Unit app-env, and confighubplaceholderfeature_flags with the one rendered from feature-flags. With prune-configmaps registered, each moves to its Unit's newest rendering when that Unit changes.

Mutable mode workload configuration

In mutable mode, the ConfigMap has a stable name equal to the unit slug, so you can reference it directly. Give the pod template an annotation hash.confighub.com/<configmap> for the ConfigMap, whose value is the named placeholder for the ConfigMap's name: confighubplaceholder followed by the name with dashes and dots replaced by underscores. Needs/Provides replaces it with that ConfigMap's content hash, triggering a rolling update whenever the ConfigMap changes:

spec:
  template:
    metadata:
      annotations:
        hash.confighub.com/my-config: confighubplaceholdermy_config
    spec:
      containers:
        - name: nginx
          volumeMounts:
            - name: config-volume
              mountPath: /etc/nginx/app.properties
              subPath: app.properties
      volumes:
        - name: config-volume
          configMap:
            name: my-config

Environment variable injection using envFrom

For both modes, environment variable injection works the same way (use confighubplaceholder in immutable mode or the stable name in mutable mode):

spec:
  template:
    spec:
      containers:
        - name: nginx
          envFrom:
            - configMapRef:
                name: confighubplaceholder

Hash annotations for several ConfigMaps

A pod template has one hash annotation per ConfigMap it uses, and each named placeholder is filled only with the hash of the ConfigMap it names, so a change to one ConfigMap moves its own annotation and leaves the others alone. A placeholder whose ConfigMap isn't linked stays a placeholder, which vet-placeholders reports, rather than being filled with another ConfigMap's hash. A name longer than an annotation key allows is shortened and given a hash suffix in the key; the value still names the ConfigMap.

set-configmap-hash-annotations adds these annotations for you. It annotates each pod template for each ConfigMap it references, through volumes, projected volumes, envFrom or env[].valueFrom.configMapKeyRef, keeps annotations already present, and skips references that are placeholders, which name immutable renderings. Repeat --configmap-name to annotate only for the ConfigMaps named:

cub function do --space my-space --where "Slug = 'my-deployment'" set-configmap-hash-annotations

A pod template with a single confighub.com/Hash: confighubplaceholder annotation, the form used before per-ConfigMap annotations, is still filled, from whichever linked ConfigMap offers a hash, and set-configmap-hash-annotations leaves such a pod template alone.

Rolling a workload when editing a ConfigMap directly

The modes above render a ConfigMap from an AppConfig/* Unit. If instead you author the ConfigMap as ordinary Kubernetes/YAML and want to keep editing it directly, you can still have workloads roll when it changes, in one of two ways: keep a hash on the ConfigMap with a set-hash Trigger, which Needs/Provides carries to the workload as it does in mutable mode, or compute the hash while a TransformPaths Link resolves, with get-hash.

Keeping a hash on the ConfigMap with a Trigger

set-hash stores a hash of the values at the paths it is given in the resource's confighub.com/Hash annotation, the annotation render-configmap writes in mutable mode. Registered as a Mutation Trigger, it recomputes the hash on every change:

cub trigger create --space my-space hash-configmaps Mutation Kubernetes/YAML set-hash data binaryData \
    --where-resource "ConfigHub.ResourceType = 'v1/ConfigMap' AND metadata.annotations.confighub~1com/RenderRevision IS NULL"

Hashing data and binaryData together covers ConfigMaps holding either; a ConfigMap without binaryData gets the same hash as with set-hash data. --where-resource selects resources rather than Units, so the Trigger applies to every ConfigMap in the Space, whichever Unit holds it, including Units created later. The RenderRevision clause leaves out the ConfigMaps render-configmap produces, which carry a hash of the rendered file already, including the older immutable ones prune-configmaps has marked Superseded.

Needs/Provides then propagates the hash to the workloads that use the ConfigMap, as in mutable mode: give the pod template a hash.confighub.com/<configmap> annotation, which set-configmap-hash-annotations adds, and link the workload's Unit to the ConfigMap's Unit. cub variant upload infers that Link from the workload's reference to the ConfigMap; otherwise create it:

cub link create --space my-space - my-deployment my-configmap

The Trigger writes the new hash in a Revision of its own, after the edit that changed the ConfigMap, and that Revision resolves the workload again, so the pod template carries the hash of the current data.

What cub variant upload sets up

cub variant upload does this for the ConfigMaps in the manifests it uploads. It runs set-configmap-hash-annotations on each workload for the ConfigMaps in the bundle and links the workload's Unit to each ConfigMap's Unit with a Needs/Provides Link. How each annotation is filled depends on the ConfigMap:

  • A ConfigMap authored as Kubernetes/YAML gets a second Link from the workload's Unit, a TransformPaths Link that computes its hash with get-hash data binaryData and writes it to the workload's hash.confighub.com/<configmap> annotation. The ConfigMap itself is left as uploaded, with no hash of its own.
  • A ConfigMap rendered mutable from an AppConfig/* Unit carries the hash render-configmap writes, which the Needs/Provides Link carries to the annotation, as in mutable mode.
  • A ConfigMap rendered immutable gets no annotation, since its name changes with its content. The upload makes sure the Space runs a prune-configmaps Trigger for these, labeled Pack: configmaps.

A ConfigMap outside the bundle gets no annotation, since nothing would fill it, and a pod template with confighub.com/Hash of its own is left alone. Uploading the same manifests again leaves the Links as they are.

Trigger Needed when Function --where-resource
prune-configmaps the upload renders an immutable ConfigMap prune-configmaps 10 ConfigHub.ResourceType = 'v1/ConfigMap'

The upload checks the Triggers the Space runs. If one of them already runs prune-configmaps as a Mutation Trigger, whatever its slug or Space, nothing is created: a Trigger you created by following this guide counts, as do a base Space's Triggers that a variant selects. Otherwise, if the Space runs the Triggers defined in it, which is the case for a Space the upload creates, the upload creates the Trigger there before writing any Unit. If the Space selects its Triggers from a policy Space instead, the upload creates nothing and lists the missing Trigger in its output, to install where the Space selects Triggers from. The upload never updates or deletes the Trigger, so you can change it, or move it to a policy Space, and later uploads find it there.

A TransformPaths Link computes the hash while it resolves and leaves the ConfigMap unchanged. get-hash returns the hash that set-hash would store, without storing it. As an UpstreamGetter it runs against the upstream ConfigMap, and a DownstreamPaths entry writes the result into the workload's pod template — which is what makes Kubernetes restart the pods:

cub function do --space my-space --where "Slug = 'my-deployment'" set-configmap-hash-annotations
cub link create --space my-space my-deployment-my-config-hash my-deployment my-configmap \
    --update-type TransformPaths --auto-update \
    --upstream-getter 'configHash=get-hash data binaryData' \
    --downstream-path 'apps/v1/Deployment:default/my-deployment:spec.template.metadata.annotations.hash~1confighub~1com/my-config={{.Params.configHash}}'

set-configmap-hash-annotations gives the pod template a hash.confighub.com/my-config annotation for the ConfigMap my-config, which the Link fills. A workload that uses several ConfigMaps gets one annotation and one such Link for each, so a change to one ConfigMap moves only its own annotation. This is what cub variant upload creates for the ConfigMaps it uploads.

The same annotation is filled by Needs/Provides when the ConfigMap carries a hash of its own, from render-configmap in mutable mode or a set-hash Trigger. Since get-hash and set-hash compute the same hash of the same paths, both write the same value, so a Link and a Trigger on the same ConfigMap don't disagree; one of them is enough.