Kubestack Terraform Framework

repository·master·Indexed 20 days ago

https://github.com/kbst/terraform-kubestack

An open-source Terraform framework for Kubernetes Platform Engineering. Kubestack enables teams to define their cloud-native stack in a single codebase and manage platform evolution via a GitOps workflow. It features an inheritance model to prevent configuration drift and provides modules for multiple cloud providers including AWS, Azure, Google, and Scaleway.

Tokens
3.2K
Snippets
3
Records
15
Agent score
72%

What's inside terraform-kubestack

  1. Overview of Kubestack

    master

    Kubestack is an open-source Terraform framework designed for Kubernetes Platform Engineering teams. It allows teams to define their entire cloud-native stack within a single Terraform codebase and evolve the platform safely using GitOps workflows.

    Key features include:

    • Convention over configuration: Simplifies platform engineering by providing sensible defaults.
    • Platform architecture & GitOps: Enables safe iteration across team members while protecting application environments.
    • Extendable & Low-maintenance: Designed to be a future-proof Terraform codebase that supports complex Kubernetes platforms through robust automation.
  2. Understand the Kubestack repository structure

    master

    The repository is organized by cloud provider and contains specific module patterns:

    • Provider Directories: Modules are grouped by provider name (e.g., aws/, azurerm/, google/).
    • Common Modules: The common/ directory contains modules used across all providers. Notable modules include:
      • common/metadata: Ensures a consistent naming scheme.
      • kustomization/overlay: Integrates Kustomize into the Terraform apply process for platform features.
    • Module Pattern: Each provider directory contains:
      • cluster/: The user-facing module. This interface is intended to be stable and is the primary entry point for users.
      • _modules/: Contains the actual implementation details used internally by the cluster module.
    • Quickstart Source: The quickstart/ directory contains the source for zip files used to bootstrap user repositories during the tutorial.
    • Tests: The tests/ directory contains happy path tests.
  3. Understand Kubestack module divergences

    master

    Kubestack modules may contain 'divergences'—intentional deviations from the project's standard architectural rules (defined in AGENTS.md). These divergences exist because the current implementation cannot yet meet a specific rule due to provider limitations, historical reasons, or planned refactors.

    Important for Developers:

    • If you are performing a task that requires modifying code covered by a divergence, you must note this in your Pull Request description.
    • Do not attempt to work around a divergence silently.
    • Divergences are tracked with a planned resolution path to ensure the module eventually reaches full compliance.
  4. Understand the difference between Divergences and Exceptions in Kubestack

    master

    Kubestack distinguishes between two types of deviations from its standard module contract:

    1. Divergences: Temporary or planned departures from the standard contract that are intended to be resolved.
    2. Exceptions: Permanent, justified departures. These occur when the standard rule cannot be applied to a specific module without causing fundamental issues, such as circular dependencies. Exceptions are not planned for resolution and exist because the standard contract is technically incompatible with the module's requirements.
  5. Explore Kubestack ecosystem repositories

    master

    Kubestack consists of several interconnected components:

    • terraform-kubestack: The core Terraform framework for defining the cloud-native Kubernetes stack.
    • kbst (CLI): The kbst CLI tool helps scaffold Terraform code for clusters, node pools, or services. It operates on local files, allowing users to review changes via git status.
    • terraform-provider-kustomization: A dedicated Terraform provider for Kustomize, maintained by Kubestack and available in the Terraform registry.
  6. Perform manual operations using the Kubestack container

    master

    If automation is unavailable or you are in a disaster recovery scenario, you can run Terraform and cloud CLIs locally using the Kubestack container image. This image bundles all necessary dependencies.

    1. Build and Exec into the container

    Build the image and run it interactively, mounting your current directory to /infra.

    2. Authenticate Providers

    Credentials are cached in the .user directory (which is git-ignored). Use the standard CLI tools for your provider (aws, az, gcloud, or scw) to authenticate.

    3. Select Environment and Run Terraform

    Select the desired Terraform workspace (ops or apps) and run your commands.

    # Build and run the container
    docker build -t kubestack .
    docker run --rm -ti \
       -v `pwd`:/infra \
       kubestack
    
    # Authenticate (examples)
    aws configure
    az login
    gcloud init
    
    # Select environment
    terraform workspace select ops
    
    # Run commands
    terraform init
    terraform plan
  7. Follow the Kubestack GitOps process to make changes

    master

    To modify Kubernetes clusters, supporting infrastructure, or Kubernetes services, follow this four-step GitOps workflow. This process ensures changes are validated against the _ops environment before being promoted to the _apps environment.

    1. Change

    Create a new branch from main, apply your configuration changes, and push the branch. This triggers a terraform plan against the _ops workspace.

    2. Review

    Request a peer review. Reviewers will examine the proposed changes and the Terraform plan. If changes are requested, commit and push them to the same branch.

    3. Merge

    Once approved, merge the branch into main. This applies the changes to the _ops environment. If successful, the pipeline automatically runs a terraform plan against the _apps environment.

    4. Promote

    Review the plan for the _apps environment. To promote the changes to the _apps environment, create a Git tag on the merge commit using the prefix apps-deploy- followed by the date and an optional counter.

    # 1. Change
    git checkout -b examplechange main
    git commit
    git push origin examplechange
    
    # 2. Review (if changes are needed)
    git checkout examplechange
    git commit
    git push origin examplechange
    
    # 3. Merge
    git checkout main
    git merge examplechange
    git push origin main
    
    # 4. Promote
    git checkout main
    git pull
    git log -1
    # Tagging triggers the apps environment pipeline
    git tag apps-deploy-$(date -I)-0
  8. Identify provider-specific exceptions in Kubestack modules

    master

    Some modules have permanent exceptions to the standard contract. For example, the aws/cluster/node-pool module requires cluster_default_node_pool_name and cluster_default_node_pool_subnet_ids to be explicitly provided when provisioning the default node pool itself. This is necessary to break a circular dependency where the node-pool module would otherwise attempt to determine subnets from the default node pool's own subnet IDs.

    > **Exception — `aws/cluster/node-pool`:** `cluster_default_node_pool_name` and `cluster_default_node_pool_subnet_ids` — the node-pool module for extra node pools determines subnets from the default node pool's subnet IDs by default; when provisioning the default node pool itself these variables are required to break the circular dependency.
  9. Google (google) node-pool implementation notes

    master

    The google/cluster/node-pool module has the following deviations:

    • Tagging: The merge order in node_config.labels is merge(cfg.labels, cluster_metadata.labels), which allows metadata labels to override user-supplied labels (the inverse of the required precedence).
    • Lifecycle: The google_container_node_pool resource sets initial_node_count but lacks a lifecycle { ignore_changes = [initial_node_count] } block, meaning the node count is managed by Terraform state rather than the autoscaler.
  10. AWS (aws) cluster implementation notes

    master

    The aws/cluster module has several known deviations from standard Kubestack patterns:

    • Networking: Nodes are assigned public IPs by default (map_public_ip_on_launch = true when cluster_vpc_subnet_map_public_ip is unset). Standard practice requires private IPs and NAT gateway egress.
    • Configuration:
      • There is no region configuration attribute; the AWS region is sourced from the provider configuration. The EKS quickstart demonstrates how to alias the provider per cluster.
      • User-supplied cloud resource tags are only configurable via additional_node_tags inside default_node_pool. There is no top-level tags attribute, meaning VPC, subnets, and security groups cannot receive user-supplied tags.
    • Authentication & RBAC:
      • The module allows arbitrary IAM-to-RBAC mappings via cluster_aws_auth_map_roles and cluster_aws_auth_map_users using an in-module kubernetes provider, which is considered an anti-pattern.
      • The module contains a kubernetes_config_map resource to manage the aws-auth ConfigMap, violating the rule that cluster modules must not contain kubernetes_* resources.
    • Tagging: In aws/cluster/node-pool, the labels attribute is applied to the cloud API but not to the Kubernetes node object's .metadata.labels.