Skip to content

Placeholders

When you create a new configuration unit from scratch and perhaps start from an existing sample, most fields will have reasonable default values. But in some cases, you need to specify a specific value for a field before the config will work and there is no reasonable default.

Common examples of values without reasonable defaults in infrastructures resources are references to network addresses and generated identifiers. It's less common in Kubernetes, but setting the namespace field of resources is an example.

Such values cannot be known in advance so they cannot be included in the config sample that you start with. ConfigHub uses the concept of a special placeholder value to allow authors of config samples to indicate where required information needs to be filled in.

The default placeholder value for strings is "confighubplaceholder" and for integers is 999999999 (9 9s). Placeholder values must be syntactically valid for the data type of the attribute. This enables existing libraries to be able to parse the configuration, and existing linting and schema validation tools to work also. "confighubplaceholder" maximizes compatibility by not including punctuation or capital letters. The "confighub" prefix should ensure that it does not collide with normal configuration values. Placeholder values are not parameters and do not need to be unique. They are just values that can and should be changed.

For example a Kubernetes service account can look like this:

apiVersion: v1
kind: ServiceAccount
metadata:
  name: backend
  namespace: confighubplaceholder

This tells ConfigHub functions multiple things:

  1. This config cannot be applied yet. It is missing some information. This can be enforced using the vet-placeholders function as a trigger.
  2. Specifically, the value of namespace in the resource type v1/ServiceAccount needs to be set because it is not set to a reasonable default value.

Because ConfigHub functions understand the underlying schemas of the resources, functions targeting the namespace field can know that this value is a string and that it's a reference to a resource of type Namespace, much as command-line tools, UIs, and controllers would. Functions can be as specific or as general as the function author intends.

This eliminates:

  1. The need for a special, non-standard schema just to define the parameters. Such a schema would either have to include pretty much all possible fields in a resource type or it would only be applicable to a particular config sample.
  2. The need to insert those parameters. Configuration could originate from a wide variety of sources: imported from a cluster, generated by a tool, generated by AI, etc.
  3. The need for code that reads or writes the configuration to deal with whatever syntax would represent the parameters and to understand the mapping between the parameters and the underlying resource attributes. Parameters would just get in the way of tools operating on the configuration.

Named placeholders

Sometimes a value that needs to be filled in is embedded in a larger text string rather than occupying a field on its own — for example, within expressions, scripts, descriptions, or resource names. A named placeholder is the string placeholder value followed by a name of letters, digits, and underscores, with no intervening punctuation, whitespace, or other characters, such as confighubplaceholdersubdomain or confighubplaceholderapp_env. The trailing characters act as a name that distinguishes one placeholder from another, while the leading confighubplaceholder prefix still marks the value as something that must be filled in, and vet-placeholders will still flag it. Any other character ends the name, so confighubplaceholder-http is the bare placeholder followed by -http. A name of lowercase letters and digits keeps the placeholder a valid DNS label, as the bare placeholder is, so it remains syntactically valid wherever the bare placeholder would be. An underscore does not, and stands in for the dashes and dots of a resource name, as described below.

For example, an Ingress hostname might embed a named placeholder for the subdomain:

spec:
  rules:
  - host: confighubplaceholdersubdomain.example.com

Named placeholders are useful when several distinct values need to be filled into the same string or when otherwise-identical strings need to be told apart. For instance, two resource names that share a common base but must remain distinct can use different named placeholders, or the same named placeholder followed by different fixed suffixes (such as confighubplaceholder-http and confighubplaceholder-https).

Functions differ in how they handle named placeholders:

  • The set-default-names function replaces named placeholders in place, preserving the surrounding text. So confighubplaceholdersubdomain.example.com becomes <name>.example.com and confighubplaceholder-http becomes <name>-http, where <name> is the value supplied to the function.
  • Needs/Provides resolution does not currently replace named placeholders in place. It replaces the whole field, so it is only appropriate when the placeholder value occupies the entire field value. When it does, the name says which provided value to prefer, as described below.
  • The search-replace function can be used to explicitly replace named placeholder values, using the names to disambiguate them. Replacing the more specific named placeholders before the bare confighubplaceholder lets you give each one a different value.

Choosing what fills a named placeholder

When a needed value is a named placeholder and nothing else, Needs/Provides prefers a provided value from the resource the name identifies. The name is that resource's name with any generated part removed and its dashes and dots replaced by underscores, since a placeholder name can't contain them. A resource whose whole name is known doesn't need a placeholder: you would write its name. The name is useful where part of the name is generated, as it is for a ConfigMap rendered from AppConfig in immutable mode, which is named after its Unit plus a hash of its content. Each rendering of the Unit app-env offers the name app_env, so a reference of confighubplaceholderapp_env prefers whichever rendering is offered over a ConfigMap rendered from another Unit.

The preference lasts after the placeholder has been filled in. ConfigHub keeps the original value of a needed path, so when a newer rendering replaces an older one, only the references that named that Unit move to it. A provided value that offers no name, which is currently any resource other than a ConfigMap rendered from AppConfig, is matched as it would be for the bare placeholder.