How-to guide / Debug dependency wiring
Use auto-instantiation with availablePackages, optional, and autoNamespace
These docs describe Kix's main branch, which can include changes not yet released. Read the docs for the latest release.
Use auto-instantiation when a cluster has a standard package for a dependency, but should include it only when something needs it. Use an optional instance when you also need to configure that dormant provider.
This guide assumes you already have packages whose required build arguments describe their dependencies.
Declare the dependencies
Section titled “Declare the dependencies”In the consuming package, make each dependency a required build argument. This
application reads values from database and metrics:
13 build =14 {15 self,16 database,17 metrics,18 ...19 }:20 {21 configMap = scope.mkResource {22 apiVersion = "v1";23 kind = "ConfigMap";24 name = scope.instanceName;25 data = {26 databaseResource = database.out.name;27 metricsResource = metrics.out.name;28 };29 };30
31 root = self.configMap;32 };Required arguments create demand. Kix first looks for a matching instance or alias, then consults the cluster’s package catalog.
Add a default package to the catalog
Section titled “Add a default package to the catalog”Add the package under the dependency name in availablePackages. Set
autoNamespace if auto-instantiated packages should live in a shared
namespace:
25 # Catalog entries stay absent until a required package argument asks26 # for one. autoNamespace chooses where Kix creates the instance.27 availablePackages.database = {28 package = databasePackage;29 };30 autoNamespace = "platform";When an instance requires database, Kix creates a database instance in
platform with the package’s default configuration. Without autoNamespace,
Kix creates it in the requesting instance’s namespace. A namespace set on
the individual catalog entry takes precedence over autoNamespace. A package
that runs once per cluster, such as an operator, needs a namespace of its own;
if Kix would place it in the requesting instance’s namespace, evaluation fails
with [K2377]. Declare the operator under instances, set autoNamespace, or
set namespace on its catalog entry.
Keep a configured provider dormant
Section titled “Keep a configured provider dormant”An availablePackages entry is instantiated with default configuration. When
the provider needs cluster-specific configuration, declare a normal instance
and set optional = true:
34 # This configured instance is dormant until another package requires35 # an argument named `metrics`.36 instances.platform.metrics = {37 package = metricsPackage;38 optional = true;39 config.retentionDays = 14;40 };Kix excludes this instance when nothing depends on metrics. A required
metrics argument activates it with the configuration shown above. So does a
consumer that wires it explicitly with deps.<arg> = ref.platform.metrics,
whatever the argument is called.
Add the consumer
Section titled “Add the consumer”Declare only the package you intend to run directly:
44 # applicationPackage requires both `database` and `metrics`. That45 # demand creates the catalog package and activates the optional one.46 instances.apps.api = {47 package = applicationPackage;48 };The package’s required arguments pull both providers into the evaluated cluster. This also works transitively when an activated provider has required dependencies of its own.
Check the result
Section titled “Check the result”Evaluate the cluster:
❱ kix check how-to-auto-instantiation
TOOL RESULT DETAILS
eval pass 12 manifests evaluated
scorecard pass 0 errors, 3 warnings, 0 info
governance.hasOwner (warning), 3:
apps/api: package 'api' has no meta.owner
platform/database: package 'database' has no meta.owner
platform/metrics: package 'metrics' has no meta.owner Inspect the dependency graph to confirm where the providers were created:
❱ kix graph how-to-auto-instantiation -o tree Show outputHide output · 29 lines
Namespace/apps
├── ConfigMap/api@apps
│ └── PackageInstance.kix.run/api@apps
│ └── Activation.kix.run/how-to-auto-instantiation-a0n9vk0rbrgk
└── PackageInstance.kix.run/api@apps
└── Activation.kix.run/how-to-auto-instantiation-a0n9vk0rbrgk
Namespace/kube-system
├── PackageInstance.kix.run/platform-storage@kube-system (import)
│ └── Activation.kix.run/how-to-auto-instantiation-a0n9vk0rbrgk
└── PackageInstance.kix.run/platform-dns@kube-system (import)
└── Activation.kix.run/how-to-auto-instantiation-a0n9vk0rbrgk
Namespace/platform
├── ConfigMap/metrics@platform
│ ├── ConfigMap/api@apps
│ │ └── PackageInstance.kix.run/api@apps
│ │ └── Activation.kix.run/how-to-auto-instantiation-a0n9vk0rbrgk
│ └── PackageInstance.kix.run/metrics@platform
│ └── Activation.kix.run/how-to-auto-instantiation-a0n9vk0rbrgk
├── ConfigMap/database@platform
│ ├── ConfigMap/api@apps
│ │ └── PackageInstance.kix.run/api@apps
│ │ └── Activation.kix.run/how-to-auto-instantiation-a0n9vk0rbrgk
│ └── PackageInstance.kix.run/database@platform
│ └── Activation.kix.run/how-to-auto-instantiation-a0n9vk0rbrgk
├── PackageInstance.kix.run/metrics@platform
│ └── Activation.kix.run/how-to-auto-instantiation-a0n9vk0rbrgk
└── PackageInstance.kix.run/database@platform
└── Activation.kix.run/how-to-auto-instantiation-a0n9vk0rbrgk
12 resources, 18 dependencies The graph contains database@platform, metrics@platform, and api@apps.
Both provider ConfigMaps appear before the application ConfigMap that consumes
their outputs.
Choose between catalog packages that answer one name
Section titled “Choose between catalog packages that answer one name”A catalog entry answers its own name, the package’s meta.roles and
meta.defaultAliases, and the entry’s aliases. When a required argument
names something more than one entry answers, Kix stops the build. Here a second entry, postgres, lists database among its aliases:
❱ kix check how-to-auto-instantiation
apps/api: needs dependency 'database', which has no active instance, and 2 packages in availablePackages match 'database':
database (package name)
postgres (availablePackages.postgres.aliases)
Fix: Declare the package this cluster should use by adding one of these lines. `optional = true` creates the instance only when something depends on it, and the namespace and name match what Kix creates automatically so any existing instance keeps its data:
If the other matching packages are not used in this cluster, remove them from availablePackages.
(exit code: 1) The error lists each entry and why it answers the name. To choose one, declare it as an optional instance named after the dependency. Kix looks for instances before the catalog, and still creates this one only when something depends on it:
1instances.platform.database = {2 package = databasePackage;3 optional = true;4};Alternatively, remove the other entries from availablePackages.
When the name is a role the environment already provides, set
cluster.roles.<role>.binding instead.
A name several entries answer is not an error while nothing requires it, so a
catalog built from a whole package set still evaluates. An argument the
consumer wires with deps.<arg> makes no demand on the catalog.