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

  1. Loads the monitoring configuration from Pulumi config under the project namespace 'unchained'.

  2. If the configuration is missing or invalid, throws a pulumi.RunError with guidance to create/update the Pulumi YAML configuration file.

  3. Creates a StackReference to the specified Pulumi stack (from config.stack) to access cluster outputs.

  4. 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.

  5. Reads an optional environment variable ADDITIONAL_ROOT_DOMAIN_NAME.

  6. Constructs the target namespace by appending the environment to the default namespace (if environment is specified).

  7. Validates that the constructed namespace exists in the cluster's namespaces list.

  8. Returns a LoopConfig object with collected and validated configuration data.

Parameters

Returns

Errors

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


Interaction with Other System Components


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.