config.ts
Overview
The [config.ts](/projects/291/68809) file is a utility module responsible for retrieving and validating configuration settings related to the monitoring environment within a Pulumi-managed infrastructure project. It defines TypeScript interfaces to type-check configuration data, and exposes an asynchronous function `getConfig` that loads, verifies, and returns essential configuration details such as Kubernetes cluster access credentials, namespaces, and domain names.
This file plays a vital role in the deployment pipeline by ensuring that the necessary configuration is present and correctly formed before infrastructure provisioning or application deployment proceeds. It abstracts away the complexity of fetching configuration values from Pulumi's configuration and stack references, providing a single source of truth for environment-specific settings.
Interfaces
MonitoringConfig
Defines the shape of the monitoring-related configuration object loaded from Pulumi's configuration.
Property | Type | Description |
|---|---|---|
`stack` | string | The Pulumi stack name where cluster info resides. |
`environment` | string | The target environment identifier (e.g., "dev", "prod"). |
LoopConfig
Defines the configuration object returned by `getConfig`, aggregating cluster access and environment details.
Property | Type | Description |
|---|---|---|
`kubeconfig` | string | Kubernetes cluster access credentials in kubeconfig format. |
`config` | `MonitoringConfig` | The original monitoring configuration object loaded from Pulumi. |
`namespace` | string | Kubernetes namespace to be used, combining default namespace and environment. |
`domain` | string | The root domain name associated with the deployment. |
`additionalDomain` | string \ | undefined |
Functions
getConfig
async function getConfig(): Promise<LoopConfig>
Asynchronously loads and validates the monitoring and cluster configuration for the current Pulumi stack.
Functionality
Loads the monitoring configuration from Pulumi config under the project namespace
'unchained'.If the configuration is missing or invalid, throws a
pulumi.RunErrorwith guidance to create/update the Pulumi YAML configuration file.Creates a
StackReferenceto the specified Pulumi stack (fromconfig.stack) to access cluster outputs.Retrieves the following outputs from the referenced stack:
kubeconfig(string): Cluster credentials.namespaces(string[]): List of namespaces in the cluster.defaultNamespace(string): Default namespace name.rootDomainName(string): Root domain for the cluster.
Reads an optional environment variable
ADDITIONAL_ROOT_DOMAIN_NAME.Constructs the target namespace by appending the environment to the default namespace (if environment is specified).
Validates that the constructed namespace exists in the cluster's namespaces list.
Returns a
LoopConfigobject with collected and validated configuration data.
Parameters
None
Returns
Promise<LoopConfig>— a promise resolving to the complete configuration object required for deployment or monitoring tasks.
Errors
Throws
pulumi.RunErrorif the configuration file is missing or cannot be loaded.Throws generic
Errorif the target namespace (based on environment) does not exist in the cluster.
Usage Example
import { getConfig } from './config'
async function deployMonitoring() {
try {
const config = await getConfig()
console.log('Using kubeconfig:', config.kubeconfig)
console.log('Deploying to namespace:', config.namespace)
// Proceed with deployment logic, e.g., applying Kubernetes manifests
} catch (err) {
console.error('Configuration error:', err)
process.exit(1)
}
}
deployMonitoring()
Implementation Details
Pulumi Config Usage: Uses
pulumi.Config.requireObject<T>()to strongly type and require the presence of themonitoringconfig object under theunchainednamespace.StackReference: Dynamically references another Pulumi stack to retrieve shared cluster outputs, enabling environment and cluster decoupling.
Namespace Validation: Ensures that the environment-specific namespace exists in the cluster before continuing, preventing misconfiguration or deployment failures.
Environment Variable Support: Supports extending domain configurations via an optional environment variable
ADDITIONAL_ROOT_DOMAIN_NAME.Error Handling: Provides clear error messages guiding users to fix missing or incomplete configuration scenarios.
Interaction with Other System Components
Pulumi Stacks: Reads configuration and output values from other Pulumi stacks, linking infrastructure components such as Kubernetes clusters and namespaces.
Deployment Scripts or Pulumi Programs: Provides validated cluster access and environment settings, acting as a foundational module for deployment pipelines or monitoring setup scripts.
Environment Configuration: Relies on Pulumi YAML configuration files (
Pulumi.<stack>.yaml) and optional environment variables to customize behavior per deployment environment.Kubernetes Clusters: Supplies kubeconfig and namespace info for Kubernetes API interactions, ensuring deployments target the correct namespaces.
Mermaid Flowchart Diagram
The following flowchart illustrates the operational workflow of the `getConfig` function and its interaction with Pulumi configuration and stack references:
flowchart TD
A[Start: Call getConfig()] --> B[Load MonitoringConfig from Pulumi config ('unchained')]
B -- Success --> C[Create StackReference to config.stack]
B -- Failure --> E[Throw RunError: Missing config file]
C --> D[Get Outputs from StackReference]
D --> D1[kubeconfig]
D --> D2[namespaces]
D --> D3[defaultNamespace]
D --> D4[rootDomainName]
D --> F[Read ADDITIONAL_ROOT_DOMAIN_NAME env var (optional)]
F --> G[Construct namespace using defaultNamespace + environment]
G --> H{Is namespace in namespaces?}
H -- Yes --> I[Return LoopConfig object]
H -- No --> J[Throw Error: Environment not found]
style E fill:#f96,stroke:#333,stroke-width:2px
style J fill:#f96,stroke:#333,stroke-width:2px
style I fill:#bbf,stroke:#333,stroke-width:2px
Summary
The [config.ts](/projects/291/68809) module is a crucial part of the infrastructure provisioning and monitoring workflow in a Pulumi-managed Kubernetes environment. By centralizing the loading and validation of configuration data, it abstracts configuration complexity and enforces correctness before any deployment actions occur, reducing runtime errors and improving deployment reliability.