index.ts
Overview
The `index.ts` file is an asynchronous Pulumi deployment script tailored for the **Thorchain** coinstack within the ShapeShift Unchained platform. It defines and configures the core blockchain services—namely the **daemon node**, **indexer**, and **TimescaleDB**—and orchestrates their deployment into a Kubernetes cluster using Pulumi infrastructure-as-code.
This file serves as the entrypoint for deploying the Thorchain coinstack's stateful services by:
Reading essential environment and configuration files.
Retrieving Kubernetes context and deployment configuration.
Mapping coinstack services to detailed deployment arguments including environment variables, ports, health probes, volume mounts, and configuration data.
Invoking a shared
deployCoinstackfunction that provisions the necessary Kubernetes resources.
The end goal is to reliably deploy and manage the lifecycle of Thorchain's blockchain node infrastructure in a cloud-native environment, ensuring health monitoring, configuration injection, and optimized resource mounting.
Detailed Explanation of the Main Exported Function
Function Signature
export = async (): Promise<Outputs>
Purpose: This default-exported asynchronous function is the Pulumi deployment entrypoint for the Thorchain coinstack.
Returns: A
Promiseresolving toOutputs, which encapsulates the result of the deployment process (e.g., Kubernetes resource metadata, endpoints, and status).
Parameters
The function takes no parameters directly but internally uses imported utilities and reads files relative to the project's directory structure.
Function Workflow and Steps
Define Constants:
appName: Set to'unchained'— a constant representing the application name.coinstack: Set to'thorchain'— identifies the blockchain coinstack being deployed.
Read Environment File:
Loads the
sample.envfile from the Thorchain command directory to provide sample environment variables or configuration placeholders.
Retrieve Kubernetes and Deployment Config:
Calls
getConfig()to asynchronously fetch:kubeconfig— Kubernetes cluster configuration.config— deployment configuration, including stateful services and network settings.namespace— target Kubernetes namespace for deployment.
Map Stateful Services to Deployment Arguments:
Iterates through
config.statefulService?.servicesto produce an array ofCoinServiceArgs.For each service (identified by
service.name), the function:Daemon Service:
Sets data directory to
/root.Defines environment variables such as
CHAIN_IDandNET(network).Configures two ports (
daemon-apianddaemon-rpc) with HTTP path prefixes for routing.Injects a shell script
tendermint.shas a ConfigMap and mounts it inside the container.Defines Kubernetes health probes (startup, liveness, readiness) with specific timing and thresholds.
Indexer Service:
Sets data directory to
/blockstore.Defines environment variables related to Midgard genesis configuration and blockstore URLs.
Opens port 8080 for the Midgard API.
Injects
indexer-config.jsonas a ConfigMap and mounts it.Enables a monitoring container sidecar.
Defines TCP socket-based health probes on port 8080.
TimescaleDB Service:
Sets data directory for PostgreSQL data.
Sets environment variables for PostgreSQL database credentials.
Opens port 5432 for PostgreSQL.
Mounts a shared memory volume (
dshm).Configures health probes using PostgreSQL readiness command
pg_isready.
Throws an error if an unsupported service name is encountered.
Define Additional Volumes:
Declares a memory-backed volume
dshmto be used by TimescaleDB for shared memory.
Invoke Deployment:
Calls
deployCoinstack()with:Application and coinstack names.
The constructed list of service deployment arguments.
Kubernetes configuration and namespace.
Loaded environment variables.
Volume definitions.
Type set as
'go'indicating the coinstack implementation language.
Return Deployment Outputs:
Resolves with the outputs returned by
deployCoinstack().
Key Types, Interfaces, and Utilities Used
CoinServiceArgs:
Interface describing the set of parameters required to deploy a blockchain service.
Includes properties such as
dataDir,env(environment variables),ports,configMapData,volumeMounts, and health probes (startupProbe,livenessProbe,readinessProbe).
Outputs:
Represents the resulting state and metadata of the deployed Kubernetes resources.
getConfig(): Promise<{ kubeconfig: any; config: any; namespace: string }>
Utility function that fetches Kubernetes cluster connection info and deployment configuration.
deployCoinstack(args: {...}): Promise
Core Pulumi function that translates the prepared deployment arguments into Kubernetes StatefulSets, Services, ConfigMaps, and other resources.
Usage Example
This file acts as a Pulumi program entrypoint and is typically invoked by the Pulumi CLI during deployment:
pulumi up -s thorchain
Or programmatically from a Node.js environment that supports ES modules:
import thorchainDeploy from './index.ts';
(async () => {
const outputs = await thorchainDeploy();
console.log('Deployment outputs:', outputs);
})();
Important Implementation Details and Algorithms
Service-Specific Configuration:
The file uses a
switchstatement to instantiate service-specific deployment details, tightly coupling configuration parameters to service roles (daemon,indexer,timescaledb).Environment variables and ports are explicitly set per service to match Thorchain's operational requirements.
Health Probes:
Kubernetes probes are finely tuned with specific
periodSeconds,failureThreshold, andtimeoutSecondsvalues to ensure robust monitoring.Probes use different mechanisms depending on the service:
HTTP path probing for daemon API endpoints.
TCP socket checks for the indexer.
Exec command checks for PostgreSQL readiness.
ConfigMap Injection:
Scripts and configuration files are read synchronously from the filesystem and injected into pods via ConfigMaps and volume mounts, allowing dynamic configuration without baking them into container images.
Memory Volume for TimescaleDB:
An ephemeral in-memory volume (
emptyDirwith mediumMemory) is mounted to improve PostgreSQL performance by providing shared memory with a 1Gi size limit.
Error Handling:
The function throws an error if any unsupported service name is encountered, preventing misconfiguration.
Interaction with Other System Components
Pulumi Core Library (
pulumi/src):Provides shared utilities and deployment primitives such as
deployCoinstackandgetConfig.
Pulumi Coinstack Module (
pulumi/src/coinstack):Contains the deployment logic to convert
CoinServiceArgsinto Kubernetes resources.
Filesystem:
Reads environment and configuration files (
sample.env,tendermint.sh,indexer-config.json) required for service configuration.
Kubernetes Cluster:
Deploys stateful services (pods, services, configmaps, volumes) into the cluster namespace defined in the configuration.
Thorchain-specific Services:
The deployed services correspond to Thorchain blockchain components: the full node daemon, data indexer, and TimescaleDB for historical data.
Mermaid Diagram: Flowchart of the Deployment Process in index.ts
flowchart TD
A[Start Deployment] --> B[Read sample.env file]
B --> C[Call getConfig() → kubeconfig, config, namespace]
C --> D[Map config.statefulService.services to CoinServiceArgs]
D --> E{Service.name}
E -->|daemon| F[Configure daemon service args (env, ports, probes, mounts)]
E -->|indexer| G[Configure indexer service args (env, ports, probes, mounts)]
E -->|timescaledb| H[Configure timescaledb service args (env, ports, probes, mounts)]
E -->|other| I[Throw unsupported service error]
F & G & H --> J[Define volumes (e.g., dshm)]
J --> K[Call deployCoinstack() with all args]
K --> L[Deploy Kubernetes StatefulSets, Services, ConfigMaps]
L --> M[Return deployment Outputs]
Summary
The
index.tsfile is the Pulumi deployment entrypoint for the Thorchain coinstack.It reads environment and configuration files and dynamically maps blockchain services to detailed Kubernetes deployment arguments.
Services include the daemon node, indexer, and TimescaleDB, each configured with specific environment variables, ports, health probes, and volume mounts.
Uses Pulumi's
deployCoinstackto provision Kubernetes resources.Ensures robust deployment with health checks and proper configuration injection.
Integrates closely with shared Pulumi utilities and Thorchain-specific service definitions.
This modular approach aligns with the overall Multi-Blockchain Coinstacks architecture, facilitating consistent and maintainable blockchain infrastructure deployment across various networks.