config.ts
Overview
The [config.ts](/projects/291/68824) file provides a robust mechanism to load, validate, and expose configuration settings required for deploying and running an application stack using Pulumi—a modern infrastructure as code tool. This file’s primary purpose is to retrieve configuration values from Pulumi’s state and ensure all required parameters are present and valid before deployment proceeds.
It defines a `Config` interface representing the shape of the configuration object returned, and an asynchronous `getConfig()` function that:
Reads the base configuration (typed as
BaseConfig) from Pulumi’s config store.References another Pulumi stack to fetch Kubernetes cluster information (
kubeconfig, namespaces).Validates environment namespaces and essential configuration fields including nested objects such as API autoscaling and stateful services.
Throws detailed errors if required configuration fields are missing or invalid.
Returns a consolidated configuration object with Kubernetes context, base config, and namespace information.
This file is critical for ensuring consistency, correctness, and readiness of deployment configurations before any infrastructure or application resources are provisioned or updated.
Detailed Documentation
Constants
SUPPORTED_NETWORKS: string[]
Description:
An array of strings defining the supported network environments for the application. Currently, it only includes'mainnet'.Usage:
Used to validate the network setting in the configuration.
Interface: Config
export interface Config {
kubeconfig: string
config: BaseConfig
namespace: string
}
Purpose:
Represents the unified configuration object returned bygetConfig().Properties:
kubeconfig(string): The Kubernetes cluster configuration in string format, used to authenticate and interact with the cluster.config(BaseConfig): The base configuration object loaded from Pulumi config, containing application-specific settings.namespace(string): The Kubernetes namespace derived from the configuration and environment.
Function: getConfig
export const getConfig = async (): Promise<Config>
Description:
Asynchronously loads and validates the entire configuration required for deploying the application stack. It aggregates configuration data from Pulumi’s config store and stack outputs, performs comprehensive validation of required fields, and returns a typed configuration object.Parameters:
NoneReturns:
A Promise resolving to aConfigobject containing:kubeconfig: Kubernetes cluster configuration string.config: Base configuration loaded from Pulumi config.namespace: Kubernetes namespace string derived from environment and defaults.
Throws:
pulumi.RunErrorif the base configuration object (coinstack) cannot be found.Errorif:The environment namespace is invalid or missing.
Any required configuration value is missing.
The network is unsupported.
Implementation Details:
Attempts to load the base configuration object from Pulumi config under the key
coinstack.Creates a
StackReferenceto the stack specified in the base config to fetch outputs:kubeconfig,namespaces,defaultNamespace,dockerhub, androotDomainName.Constructs the namespace string by appending the environment suffix if provided.
Validates that the namespace exists in the cluster's namespaces plus an additional
'unchained-infra'namespace.Performs deep validation of critical configuration values, including nested API autoscaling, CPU/memory limits, replicas, and stateful service requirements.
Aggregates missing configuration keys and throws an error listing all missing items to aid troubleshooting.
Usage Example:
import { getConfig } from './config'
async function deploy() {
try {
const { kubeconfig, config, namespace } = await getConfig()
console.log(`Deploying to namespace ${namespace} on cluster with config:`, kubeconfig)
// Proceed with deployment logic using config and kubeconfig
} catch (error) {
console.error('Configuration error:', error)
process.exit(1)
}
}
deploy()
Important Implementation Details and Algorithms
Pulumi Config Object Loading:
Usespulumi.Config.requireObject<T>()to load a typed configuration object. This method throws if the object is missing, which is caught and rethrown as aRunErrorwith a clear message.StackReference Usage:
The function usespulumi.StackReferenceto fetch outputs from a referenced stack dynamically. This enables separation of concerns where cluster infrastructure and application deployment stacks are managed independently.Namespace Validation:
The namespace is dynamically derived based on the environment. It ensures that the namespace exists in the cluster by checking against the namespaces output plus a hardcoded'unchained-infra'namespace, preventing deployment to non-existent environments.Comprehensive Configuration Validation:
The function iteratively checks for required configuration properties including nested objects like API autoscaling settings and stateful service definitions. It collects all missing keys before throwing, providing a consolidated error message.Supported Networks Constraint:
Enforces that the configured network matches one of the predefined supported networks (mainnet), preventing misconfiguration.
Interactions with Other Parts of the System
Pulumi Configuration and Stack Outputs:
This file interacts heavily with Pulumi’s configuration management and stack outputs, acting as a bridge between infrastructure provisioning and application deployment.Base Config and Dockerhub Types:
Imports types such asBaseConfigandDockerhubfrom the local module ('.'), indicating this file depends on shared type definitions and possibly configuration schemas from other parts of the application.Kubernetes Cluster and Namespaces:
Thekubeconfigandnamespacevalues returned are essential for other components that deploy resources to Kubernetes clusters.Error Handling for Deployment Workflow:
By throwing detailed errors on configuration issues, this file helps early detection of deployment problems, improving reliability of the overall deployment pipeline.
Mermaid Diagram: Flowchart of getConfig() Function Workflow
flowchart TD
A[Start: Call getConfig()] --> B[Load base config from Pulumi config ('coinstack')]
B -- success --> C[Create StackReference using config.stack]
B -- failure --> Z[Throw RunError: Missing configuration]
C --> D[Fetch kubeconfig, namespaces, defaultNamespace from StackReference]
D --> E[Derive namespace based on environment]
E --> F{Is namespace valid?}
F -- No --> Z2[Throw Error: Invalid environment namespace]
F -- Yes --> G[Fetch dockerhub and rootDomainName outputs]
G --> H[Validate required config fields]
H --> I{Any missing required fields?}
I -- Yes --> Z3[Throw Error: Missing configuration keys]
I -- No --> J[Return { kubeconfig, config, namespace }]
Z --> End[End]
Z2 --> End
Z3 --> End
Summary
The [config.ts](/projects/291/68824) file is a critical utility module that:
Loads deployment configuration from Pulumi.
Validates all critical configuration parameters.
Provides a clean, typed configuration object for use in deployment scripts and other infrastructure code.
Ensures deployment consistency and prevents runtime errors due to misconfiguration by proactively checking environment and required parameters.
Leverages Pulumi’s stack references to integrate multiple stacks and externalize configuration data.
This design promotes modularity, reusability, and robustness in the infrastructure deployment process.