ipfs.ts


Overview

The `ipfs.ts` file is a Kubernetes infrastructure deployment script implemented using Pulumi and TypeScript. Its primary purpose is to automate the deployment of an IPFS (InterPlanetary File System) cluster within a Kubernetes namespace. This deployment provides a decentralized, fault-tolerant, and scalable file storage solution integrated into a Kubernetes environment.

The script handles the provisioning of all necessary Kubernetes resources, including Secrets, ConfigMaps, StatefulSets, Services, TLS certificates, and ingress routing to expose the IPFS gateway securely over HTTP(S). It configures both the IPFS daemon and the IPFS Cluster service, setting up storage volumes, network ports, health checks, and environment variables to ensure a robust IPFS cluster suitable for production usage.

This file is designed to be used as part of a broader infrastructure automation system, typically invoked during cluster setup or application deployment workflows that require decentralized file storage capabilities.


Detailed Documentation

Interface: IpfsClusterArgs

Defines the expected arguments for the IPFS deployment function.

Property

Type

Description

`namespace`

`string`

Kubernetes namespace in which to deploy the IPFS cluster.

`provider`

`k8s.Provider`

Pulumi Kubernetes provider instance for managing resource creation.

`domain`

`string`

The primary domain name used for TLS certificates and ingress routing.

`additionalDomain`

`string?`

Optional additional domain to be included in ingress routing rules for the IPFS gateway.


Function: deployIpfs

function deployIpfs({ namespace, provider, domain, additionalDomain }: IpfsClusterArgs): void

Deploys a fully featured IPFS cluster within a specified Kubernetes namespace using Pulumi.

Parameters

Returns

Usage Example

import * as k8s from '@pulumi/kubernetes'

const provider = new k8s.Provider('k8s-provider', { /* ...connection config... */ })

deployIpfs({
  namespace: 'unchained-infra',
  provider,
  domain: 'example.com',
  additionalDomain: 'example.org',
})

Implementation Details

The function `deployIpfs` orchestrates the following Kubernetes resource creations and configurations:

  1. Secret Creation

    • Stores the IPFS cluster secret, pulled from environment variables (IPFS_CLUSTER_SECRET).

    • Used for securing cluster communication.

  2. ConfigMap Creation

    • Contains:

      • A fixed bootstrap peer ID for cluster initialization.

      • Two shell scripts (entrypoint.sh and configure-ipfs.sh) read from the local filesystem; these scripts configure the IPFS environment inside the pods.

  3. Pod Specification

    • Defines a pod template with:

      • An init container (configure-ipfs) that runs the configuration script before the main containers start.

      • Two containers:

        • ipfs: Runs the IPFS daemon with appropriate ports exposed for swarm, API, HTTP gateway, and WebSocket communications.

        • ipfs-cluster: Runs the IPFS cluster management daemon coordinating pinning and replication.

      • Liveness probes on both containers to ensure health.

      • Volume mounts for persistent storage and configuration scripts.

      • Resource limits to control CPU and memory usage.

  4. Service Creation

    • A ClusterIP service exposing all relevant IPFS and cluster ports to enable intra-cluster communication and proxying.

  5. StatefulSet Creation

    • Deploys a 3-replica StatefulSet to manage the lifecycle of the IPFS cluster pods.

    • Uses Parallel pod management policy and RollingUpdate strategy for updates.

    • Defines PersistentVolumeClaims for:

      • cluster-storage (5Gi gp3 volume) for IPFS cluster state.

      • ipfs-storage (200Gi gp3 volume) for IPFS data.

  6. Certificate Creation (cert-manager)

    • Creates a Certificate resource for TLS termination using Let's Encrypt ClusterIssuer.

    • Configured for the IPFS gateway domain.

  7. IngressRoute Creation (Traefik)

    • Defines Traefik ingress routing rules for HTTP/HTTPS entry points.

    • Supports multiple domains if additionalDomain is provided.

    • Routes traffic to the IPFS HTTP gateway service port (8080).

    • Uses TLS with the created certificate.

  8. Legacy Ingress Resource (Kubernetes)

    • Creates a simple Kubernetes Ingress resource for the gateway domain.

    • This is likely for compatibility or fallback purposes.


Important Implementation Notes


Interaction with Other System Components


Mermaid Diagram

Class/Function Structure Diagram

flowchart TD
    A[deployIpfs(args: IpfsClusterArgs)] --> B[Create Secret (cluster-secret)]
    A --> C[Create ConfigMap (scripts + bootstrap-peer-id)]
    A --> D[Define podSpec (initContainers + containers + volumes)]
    D --> D1[Init Container: configure-ipfs]
    D --> D2[Container: ipfs daemon]
    D --> D3[Container: ipfs-cluster service]
    A --> E[Create Service (ClusterIP, exposes IPFS ports)]
    A --> F[Create StatefulSet (3 replicas, volume claims)]
    A --> G[Create Certificate (cert-manager)]
    A --> H[Create IngressRoute (Traefik)]
    A --> I[Create Kubernetes Ingress]

Summary

The `ipfs.ts` file implements a comprehensive deployment of an IPFS cluster on Kubernetes using Pulumi. It sets up all necessary Kubernetes resources—secrets, configmaps, services, StatefulSets, and ingress rules—along with persistent storage and TLS certificates to provide a secure and scalable decentralized file storage system. This deployment is designed to integrate tightly with cluster provisioning and infrastructure automation workflows, enabling blockchain and distributed applications to leverage IPFS storage seamlessly and securely within a Kubernetes environment.


Appendix: File Layout Snapshot

Resource Type

Name Pattern

Purpose

Secret

`ipfs`

Stores IPFS cluster secret

ConfigMap

`ipfs-cm`

Stores IPFS bootstrap ID and config scripts

Pod Spec

Inline in StatefulSet

Defines IPFS and IPFS Cluster containers

Service

`ipfs-svc`

Exposes IPFS and cluster ports internally

StatefulSet

`ipfs`

Manages IPFS cluster pods and persistent storage

CustomResource (Certificate)

`ipfs-cert`

TLS certificate for gateway domain

CustomResource (IngressRoute)

ipfs-ingressroute

Traefik ingress routing for IPFS gateway

Ingress

ipfs-ingress

Kubernetes ingress resource for gateway domain


If you require additional details on specific scripts (`entrypoint.sh` or `configure-ipfs.sh`), or integration examples, please let me know!