Function
In ConfigHub, configuration data is separate from the code that operates on it.
A Function is a executable piece of code that operates on configuration data in Config Units that ConfigHub can invoke.
Functions operate on configuration elements that are (mostly) standard APIs with defined schemas and backward compatibility guarantees in well known stable formats, such as Kubernetes resources represented in YAML.
There are multiple ways one can think about functions:
- Each function is kind of a mini tool or command that performs a specific operation on specific kinds of configuration data. These mini tools should be reusable across all configuration Units containing the configuration elements of the same types. An example is the
set-image-referencefunction, which can update image references according to the OCI standard in all Kubernetes workload types. - Function invocation is a kind of an API extension mechanism. It enables the same functionality to be accessible via CLI, UI, and automation systems that call the ConfigHub API.
- When configured as a Trigger, a function acts similarly to a dynamic admission controller in Kubernetes.
- Functions are an extension point that enables ConfigHub to support additional ToolchainTypes for other configuration formats, by implementing them as external functions.
- Functions provide a means of making operations, such as validation and policy checks, with consistent behavior available to your organization.
Your preferred AI tools can be used to make ad hoc configuration changes directly that can be explained concisely and simply and are easy to validate, and those changes can be patched to similar units by using the original unit as a merge source for updates. We recommend using AI to help you create and test functions for operations on configurations that need to be performed repeatedly reliably and predictably, such as when making changes to many units, or automating changes in CI or in systems using ConfigHub's API as a control/management plane. Functions can also more easily encode conditional logic based on configuration data values.
There are 3 types of Functions:
- Readonly
- Mutating
- Validating
A Readonly Function extracts or analyzes specific pieces of data from a Config Unit and returns the results. For example it may extract the container image URI from a Unit containing a Kubernetes Deployment. A Mutating function takes a Config Unit as input, makes changes to it and writes and returns the mutated Unit. For example, a Mutating function may update the Postgres version in a set of ACK config Units. A Validating function takes Config Units as input and returns simple pass/fail for each Unit.
Functions most commonly run synchronously, hermetically, and idempotently, with no side effects. But it is possible for functions to interact with external systems. The main constraint is that functions are expected to return synchronously and therefore must complete without long delays.
ConfigHub has a library of Functions built in, which run inside the server. You can also write external functions of your own, served by a bot you run on a Channel, which ConfigHub invokes the same way. The SDK is what you build them with.
In addition to imperative Function invocation, ConfigHub supports automatic function Triggers, which is a major reason functions aren't entirely external to ConfigHub.
(ConfigHub functions were inspired by KRM functions and Kustomize transformations, but support more than just KRM YAML, transform configuration data in place rather than out of place, support more use cases than just transformation and validation, are API-driven rather than file- and CLI-driven, are not attached to individual configuration Units, have a model for passing data across configuration Units, and are executed by ConfigHub or by bots on Channels rather than in containers, among other differences.)
Common output types
Standard function output types used by built-in functions are defined in the function API in the SDK. The most common output types are ValidationResultList and AttributeValueList.
Validating functions return the ValidationResultList type. In Go, ValidationResult is currently:
type ValidationResult struct {
Passed bool // true if valid, false otherwise
Index int // index of the function invocation corresponding to the result
Details []string `json:",omitempty"` // optional list of failure details
FailedAttributes AttributeValueList `json:",omitempty"` // optional list of failed attributes; preferred over Details
}
Value-extracting functions, such as getters, return the AttributeValueList type, which is defined as follows. It contains the Value, Path, resource metadata, and Attribute metadata from the Path Registry.
// AttributeValue provides the value of an attribute in addition to information about the attribute.
type AttributeValue struct {
AttributeInfo
Value any
Comment string `json:",omitempty"`
Index int // index of the function invocation corresponding to the result
}
// AttributeInfo conveys both the identifying information about a resource attribute and its
// metadata.
type AttributeInfo struct {
AttributeIdentifier
AttributeMetadata
}
// An AttributeIdentifier identifies the resource type and name and resolved path of the
// resource attribute.
type AttributeIdentifier struct {
ResourceInfo
Path ResolvedPath `swaggertype:"string"`
}
// AttributeMetadata specifies the AttributeName, DataType, and other details, such as corresponding
// getter and setter functions for the attribute.
type AttributeMetadata struct {
AttributeName AttributeName `json:",omitempty" swaggertype:"string"`
DataType DataType `swaggertype:"string"`
Info *AttributeDetails `json:",omitempty"`
}
// ResourceInfo contains the ResourceName, ResourceNameWithoutScope, ResourceType, and ResourceCategory for a configuration Element within a configuration Unit.
type ResourceInfo struct {
ResourceName ResourceName `swaggertype:"string" description:"Name of a resource in the system under management represented in the configuration data; Kubernetes resources are represented in the form <metadata.namespace>/<metadata.name>; not all ToolchainTypes necessarily use '/' as a separator between any scope(s) and name or other client-chosen ID"`
ResourceNameWithoutScope ResourceName `swaggertype:"string" description:"Name of a resource in the system under management represented in the configuration data, without any uniquifying scope, such as Namespace, Project, Account, Region, etc.; Kubernetes resources are represented in the form <metadata.name>"`
ResourceType ResourceType `swaggertype:"string" description:"Type of a resource in the system under management represented in the configuration data; Kubernetes resources are represented in the form <apiVersion>/<kind> (aka group-version-kind)"`
ResourceCategory ResourceCategory `json:",omitempty" swaggertype:"string" description:"Category of configuration element represented in the configuration data; Kubernetes resources are of category Resource, and application configuration files are of category AppConfig"`
}