Skip to main content

Reusable CDKTF Infrastructure Library for Google Cloud Platform

Project description

๐Ÿ—๏ธ x-infra-kit

Reusable CDKTF Infrastructure Library for Google Cloud Platform

Production-ready infrastructure constructs that enable you to deploy GCP resources with a single line of code.

Python 3.9+ CDKTF License: MIT


๐Ÿ“‹ Table of Contents


โœจ Features

Feature Description
Single-Line Deployment Deploy complete GCP infrastructure with StandardPlatform
Golden Path Profiles Pre-configured Dev, Staging, and Prod environments
Override Support Customize any parameter while keeping sensible defaults
Type Safety Full type hints and dataclass validation
Best Practices Private clusters, Workload Identity, VPC-native networking
Modular Design Use composite or individual building blocks

๐Ÿ›๏ธ Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                     StandardPlatform                        โ”‚
โ”‚  (Composite Construct - Recommended for most users)        โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚                                                             โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
โ”‚  โ”‚   Profile   โ”‚  โ”‚   Profile   โ”‚  โ”‚      Profile        โ”‚ โ”‚
โ”‚  โ”‚  DevProfile โ”‚  โ”‚StagingProfileโ”‚ โ”‚    ProdProfile      โ”‚ โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
โ”‚         โ”‚                โ”‚                     โ”‚            โ”‚
โ”‚         โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜            โ”‚
โ”‚                          โ”‚                                  โ”‚
โ”‚                          โ–ผ                                  โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”   โ”‚
โ”‚  โ”‚              NetworkConfig / ClusterConfig           โ”‚   โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜   โ”‚
โ”‚                          โ”‚                                  โ”‚
โ”‚         โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                โ”‚
โ”‚         โ–ผ                โ–ผ                โ–ผ                โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”        โ”‚
โ”‚  โ”‚ StandardVPC โ”‚  โ”‚StandardClusterโ”‚ โ”‚StandardSecretsโ”‚      โ”‚
โ”‚  โ”‚             โ”‚  โ”‚             โ”‚  โ”‚(optional)    โ”‚        โ”‚
โ”‚  โ”‚ โ€ข VPC       โ”‚  โ”‚ โ€ข GKE       โ”‚  โ”‚             โ”‚        โ”‚
โ”‚  โ”‚ โ€ข Subnet    โ”‚  โ”‚ โ€ข Node Pool โ”‚  โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค        โ”‚
โ”‚  โ”‚ โ€ข Router    โ”‚  โ”‚             โ”‚  โ”‚StandardIdentityโ”‚      โ”‚
โ”‚  โ”‚ โ€ข NAT       โ”‚  โ”‚             โ”‚  โ”‚(optional)    โ”‚        โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜        โ”‚
โ”‚                                                             โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

GCP Resources Created

Resource Description
google_compute_network Custom VPC
google_compute_subnetwork Subnet with secondary ranges
google_compute_router Cloud Router for NAT
google_compute_router_nat Cloud NAT for private egress
google_container_cluster Private GKE cluster
google_container_node_pool Managed node pool with autoscaling
google_secret_manager_secret Secret Manager secrets (optional)
google_service_account Workload Identity SA (optional)
google_project_iam_member IAM role bindings (optional)
google_service_account_iam_binding Workload Identity binding (optional)

๐Ÿš€ Quick Start

1. Install the library

pip install -e .
# or
pip install x-infra-kit  # when published to PyPI

2. Create your stack

from cdktf import App, TerraformStack
from cdktf_cdktf_provider_google.provider import GoogleProvider
from infrastructure_lib import StandardPlatform

class MyStack(TerraformStack):
    def __init__(self, scope, id):
        super().__init__(scope, id)
        
        GoogleProvider(self, "Google", 
            project="my-gcp-project", 
            region="europe-west1"
        )
        
        # ๐Ÿš€ One line to create complete infrastructure!
        platform = StandardPlatform(self, "platform",
            project_id="my-gcp-project",
            region="europe-west1",
            env="prod",
            prefix="myapp"
        )

app = App()
MyStack(app, "my-stack")
app.synth()

3. Deploy

cdktf synth    # Generate Terraform JSON
cdktf deploy   # Apply to GCP

๐Ÿ“ฆ Installation

Prerequisites

  • Python 3.9+
  • Node.js 16+ (for cdktf-cli)
  • Google Cloud SDK (gcloud)
  • Terraform 1.0+ (installed automatically by cdktf)

Install from source

git clone https://github.com/your-org/x-infra-kit.git
cd x-infra-kit

# Create virtual environment
python -m venv venv
source venv/bin/activate  # Linux/Mac
# or
.\venv\Scripts\activate   # Windows

# Install dependencies
pip install -r requirements.txt

Install as package

pip install -e .

๐Ÿ“– Usage

StandardPlatform (Recommended)

The easiest way to use the library. Creates VPC, GKE, and optionally Secrets and Workload Identity.

from infrastructure_lib import StandardPlatform

# Minimal usage (just 4 required parameters)
platform = StandardPlatform(stack, "platform",
    project_id="my-gcp-project",
    region="europe-west1",
    env="dev",
    prefix="myapp"
)

# Access created resources
print(platform.cluster_name)      # myapp-dev-cluster
print(platform.cluster_endpoint)  # https://...
print(platform.network_id)        # projects/.../networks/myapp-dev-vpc
print(platform.subnet_id)         # projects/.../subnetworks/myapp-dev-subnet

With Secrets and Workload Identity

platform = StandardPlatform(stack, "platform",
    project_id="my-gcp-project",
    region="europe-west1",
    env="prod",
    prefix="myapp",
    
    # Optional: Create secrets
    secret_ids=["db-password", "api-key", "jwt-secret"],
    
    # Optional: Setup Workload Identity
    workload_identity={
        "sa_id": "external-secrets-sa",
        "k8s_namespace": "external-secrets",
        "k8s_sa_name": "external-secrets",
        "roles": ["roles/secretmanager.secretAccessor"]
    }
)

# Access optional resources (check first!)
if platform.has_secrets:
    print(platform.secrets.secret_ids)

if platform.has_identity:
    print(platform.identity_email)

With Cluster Overrides

platform = StandardPlatform(stack, "platform",
    project_id="my-gcp-project",
    region="europe-west1",
    env="prod",
    prefix="myapp",
    
    # Override any cluster setting
    machine_type="n2-standard-8",
    max_nodes=20,
    disk_size=200
)

Building Blocks (Advanced)

For more control, use individual constructs:

from infrastructure_lib import (
    DevProfile, ProdProfile,
    StandardVPC, StandardCluster, 
    StandardSecrets, StandardIdentity,
    NetworkConfig, ClusterConfig
)

# 1. Choose a profile
profile = ProdProfile(
    project_id="my-project",
    region="us-central1",
    env="prod",
    prefix="myapp"
)

# 2. Create VPC with custom CIDR
vpc = StandardVPC(stack, "vpc",
    config=profile.get_network_config(cidr="10.1.0.0/16")
)

# 3. Create GKE with custom settings
cluster = StandardCluster(stack, "gke",
    config=profile.get_cluster_config(
        machine_type="n2-standard-8",
        max_nodes=15
    ),
    network_id=vpc.network_id,
    subnet_id=vpc.subnet_id
)

# 4. Create secrets separately
secrets = StandardSecrets(stack, "secrets",
    secret_ids=["db-password", "redis-password"]
)

# 5. Setup Workload Identity
identity = StandardIdentity(stack, "identity",
    project_id="my-project",
    sa_id="my-service-account",
    k8s_namespace="default",
    k8s_sa_name="my-k8s-sa",
    roles=["roles/secretmanager.secretAccessor"]
)

โš™๏ธ Configuration

Required Parameters

These must always be provided:

Parameter Type Description Example
project_id str GCP Project ID "my-gcp-project-123"
region str GCP Region "europe-west1"
env str Environment name "dev", "staging", "prod"
prefix str Resource naming prefix "myapp"

Optional Parameters

Network Configuration

Parameter Default Description
cidr 10.0.0.0/16 Primary subnet CIDR
pod_cidr 10.11.0.0/21 Secondary range for pods (~2000 pods)
service_cidr 10.12.0.0/21 Secondary range for services
pod_range_name pod-ranges Name of pod secondary range
service_range_name service-ranges Name of service secondary range

Cluster Configuration

Parameter Dev Default Prod Default Description
zone {region}-a {region}-a GCP zone
machine_type e2-medium n2-standard-4 Node machine type
node_count 1 3 Initial node count
min_nodes 1 3 Autoscaler minimum
max_nodes 3 10 Autoscaler maximum
disk_size 50 100 Node disk size (GB)
spot_instances true false Use spot/preemptible VMs
master_cidr 172.16.0.0/28 172.16.0.0/28 Private cluster master CIDR

Environment Profiles

Profile Use Case Key Characteristics
DevProfile Development Cost-optimized, spot instances, small nodes
StagingProfile Pre-production Production-like but with spot instances
ProdProfile Production HA, no spot, larger nodes, deletion protection

๐Ÿ“š API Reference

StandardPlatform

StandardPlatform(
    scope: Construct,
    id: str,
    project_id: str,           # Required
    region: str,               # Required
    env: str,                  # Required
    prefix: str,               # Required
    secret_ids: list[str],     # Optional
    workload_identity: dict,   # Optional
    **cluster_overrides        # Optional
)

Properties:

  • vpc โ†’ StandardVPC
  • cluster โ†’ StandardCluster
  • secrets โ†’ StandardSecrets (raises ValueError if not configured)
  • identity โ†’ StandardIdentity (raises ValueError if not configured)
  • has_secrets โ†’ bool
  • has_identity โ†’ bool
  • cluster_name โ†’ str
  • cluster_endpoint โ†’ str
  • network_id โ†’ str
  • subnet_id โ†’ str
  • identity_email โ†’ str

StandardVPC

StandardVPC(
    scope: Construct,
    id: str,
    config: NetworkConfig,
    labels: dict[str, str] = None  # Optional
)

Properties:

  • network โ†’ ComputeNetwork
  • subnet โ†’ ComputeSubnetwork
  • router โ†’ ComputeRouter
  • nat โ†’ ComputeRouterNat
  • network_id โ†’ str
  • subnet_id โ†’ str

StandardCluster

StandardCluster(
    scope: Construct,
    id: str,
    config: ClusterConfig,
    network_id: str,
    subnet_id: str
)

Properties:

  • cluster โ†’ ContainerCluster
  • node_pool โ†’ ContainerNodePool

StandardSecrets

StandardSecrets(
    scope: Construct,
    id: str,
    secret_ids: list[str]
)

Properties:

  • secret_ids โ†’ list[str]

Methods:

  • get_secret(secret_id: str) โ†’ SecretManagerSecret

StandardIdentity

StandardIdentity(
    scope: Construct,
    id: str,
    project_id: str,
    sa_id: str,              # 6-30 characters
    k8s_namespace: str,
    k8s_sa_name: str,
    roles: list[str] = None  # Optional
)

Properties:

  • gsa โ†’ ServiceAccount
  • email โ†’ str

๐Ÿ’ก Examples

Example 1: Basic Dev Environment

platform = StandardPlatform(stack, "dev",
    project_id="my-project",
    region="us-central1",
    env="dev",
    prefix="myapp"
)

Creates:

  • VPC: myapp-dev-vpc
  • Subnet: myapp-dev-subnet
  • Cluster: myapp-dev-cluster (e2-medium, 1-3 nodes, spot)

Example 2: Production with HA

platform = StandardPlatform(stack, "prod",
    project_id="my-project",
    region="europe-west1",
    env="prod",
    prefix="myapp",
    max_nodes=20
)

Creates:

  • Cluster with n2-standard-4 nodes
  • 3-20 nodes autoscaling
  • Deletion protection enabled
  • No spot instances

Example 3: Multiple Environments

import os

env = os.getenv("ENV", "dev")

platform = StandardPlatform(stack, f"{env}-platform",
    project_id=os.getenv("GCP_PROJECT_ID"),
    region=os.getenv("GCP_REGION", "us-central1"),
    env=env,
    prefix="myapp"
)

๐Ÿงช Testing

Run the test suite:

# Run all tests
pytest tests/ -v

# Run with coverage
pytest tests/ --cov=infrastructure_lib

# Run specific test
pytest tests/test_config.py::TestClusterConfig -v

Test Coverage

  • โœ… Configuration validation
  • โœ… Auto-generated properties
  • โœ… Profile configuration
  • โœ… Override mechanism

๐Ÿ“ Project Structure

x-infra-kit/
โ”œโ”€โ”€ infrastructure_lib/          # Main library
โ”‚   โ”œโ”€โ”€ __init__.py             # Public exports
โ”‚   โ”œโ”€โ”€ config.py               # Configuration dataclasses
โ”‚   โ”œโ”€โ”€ profiles.py             # Environment profiles
โ”‚   โ”œโ”€โ”€ networking.py           # VPC, Subnet, NAT
โ”‚   โ”œโ”€โ”€ gke.py                  # GKE Cluster
โ”‚   โ”œโ”€โ”€ security.py             # Secrets, Workload Identity
โ”‚   โ””โ”€โ”€ composites.py           # StandardPlatform
โ”œโ”€โ”€ tests/                       # Unit tests
โ”‚   โ””โ”€โ”€ test_config.py
โ”œโ”€โ”€ main.py                      # Example usage
โ”œโ”€โ”€ setup.py                     # Package definition
โ”œโ”€โ”€ requirements.txt             # Dependencies
โ”œโ”€โ”€ cdktf.json                   # CDKTF configuration
โ””โ”€โ”€ README.md                    # This file

๐Ÿค Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Follow the existing code style:
    • Type hints on all functions
    • Docstrings on all classes and public methods
    • Validation in constructors
    • Tests for new features
  4. Run tests (pytest tests/ -v)
  5. Commit your changes (git commit -m 'Add amazing feature')
  6. Push to the branch (git push origin feature/amazing-feature)
  7. Open a Pull Request

๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.


๐Ÿ™ Acknowledgments


Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

x_infra_kit-1.0.0.tar.gz (19.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

x_infra_kit-1.0.0-py3-none-any.whl (17.2 kB view details)

Uploaded Python 3

File details

Details for the file x_infra_kit-1.0.0.tar.gz.

File metadata

  • Download URL: x_infra_kit-1.0.0.tar.gz
  • Upload date:
  • Size: 19.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for x_infra_kit-1.0.0.tar.gz
Algorithm Hash digest
SHA256 b892a9bd4ca52928287e1c78be562f6692b1b67005cdf0c6bde6acc1e8d05739
MD5 82ef9086662e530e471873d971abd508
BLAKE2b-256 4f147821990d85741e6b51d9657c176d3e25cb8cc6e48dee92a8edc916cb2fb5

See more details on using hashes here.

Provenance

The following attestation bundles were made for x_infra_kit-1.0.0.tar.gz:

Publisher: ci.yml on xofyy/x-infra-kit

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file x_infra_kit-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: x_infra_kit-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 17.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for x_infra_kit-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bf2db72608920c0fa8ca415ead8e12510dd95bc3841e942e8c3c27b848985327
MD5 f1df0f3372081be1eeb786210b104dc9
BLAKE2b-256 f30efa7566e3ead52819f3b5af1dc1dbce89a4013180029c0b6b65fefa2e722a

See more details on using hashes here.

Provenance

The following attestation bundles were made for x_infra_kit-1.0.0-py3-none-any.whl:

Publisher: ci.yml on xofyy/x-infra-kit

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page