Kubestack Terraform Framework
repository·master·Indexed 20 days ago
https://github.com/kbst/terraform-kubestackAn 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.
What's inside terraform-kubestack
- Kubestack is an open source GitOps framework designed to automate infrastructure. It uses Terraform modules to define cluster infrastructure and platform features. A key feature is the Kubestack inheritance model, which is implemented within its modules to prevent configuration drift between different environments.
Overview of Kubestack
masterKubestack 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.
Understand the Kubestack repository structure
masterThe 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 theclustermodule.
- 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.
- Provider Directories: Modules are grouped by provider name (e.g.,
Understand Kubestack module divergences
masterKubestack 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.
Understand the difference between Divergences and Exceptions in Kubestack
masterKubestack distinguishes between two types of deviations from its standard module contract:
- Divergences: Temporary or planned departures from the standard contract that are intended to be resolved.
- 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.
Explore Kubestack ecosystem repositories
masterKubestack consists of several interconnected components:
- terraform-kubestack: The core Terraform framework for defining the cloud-native Kubernetes stack.
- kbst (CLI): The
kbstCLI tool helps scaffold Terraform code for clusters, node pools, or services. It operates on local files, allowing users to review changes viagit status. - terraform-provider-kustomization: A dedicated Terraform provider for Kustomize, maintained by Kubestack and available in the Terraform registry.
Perform manual operations using the Kubestack container
masterIf 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
.userdirectory (which is git-ignored). Use the standard CLI tools for your provider (aws,az,gcloud, orscw) to authenticate.3. Select Environment and Run Terraform
Select the desired Terraform workspace (
opsorapps) 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 planFollow the Kubestack GitOps process to make changes
masterTo modify Kubernetes clusters, supporting infrastructure, or Kubernetes services, follow this four-step GitOps workflow. This process ensures changes are validated against the
_opsenvironment before being promoted to the_appsenvironment.1. Change
Create a new branch from
main, apply your configuration changes, and push the branch. This triggers aterraform planagainst the_opsworkspace.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_opsenvironment. If successful, the pipeline automatically runs aterraform planagainst the_appsenvironment.4. Promote
Review the plan for the
_appsenvironment. To promote the changes to the_appsenvironment, create a Git tag on the merge commit using the prefixapps-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)-0Get started with Kubestack
masterThe recommended way to begin using Kubestack is by following the official Kubestack tutorial. The tutorial guides you through using the framework to build a Kubernetes platform application.
Tutorial Link: https://www.kubestack.com/framework/tutorial/
Identify provider-specific exceptions in Kubestack modules
masterSome modules have permanent exceptions to the standard contract. For example, the
aws/cluster/node-poolmodule requirescluster_default_node_pool_nameandcluster_default_node_pool_subnet_idsto 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.Google (google) node-pool implementation notes
masterThe
google/cluster/node-poolmodule has the following deviations:- Tagging: The merge order in
node_config.labelsismerge(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_poolresource setsinitial_node_countbut lacks alifecycle { ignore_changes = [initial_node_count] }block, meaning the node count is managed by Terraform state rather than the autoscaler.
- Tagging: The merge order in
AWS (aws) cluster implementation notes
masterThe
aws/clustermodule has several known deviations from standard Kubestack patterns:- Networking: Nodes are assigned public IPs by default (
map_public_ip_on_launch = truewhencluster_vpc_subnet_map_public_ipis unset). Standard practice requires private IPs and NAT gateway egress. - Configuration:
- There is no
regionconfiguration 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_tagsinsidedefault_node_pool. There is no top-leveltagsattribute, meaning VPC, subnets, and security groups cannot receive user-supplied tags.
- There is no
- Authentication & RBAC:
- The module allows arbitrary IAM-to-RBAC mappings via
cluster_aws_auth_map_rolesandcluster_aws_auth_map_usersusing an in-modulekubernetesprovider, which is considered an anti-pattern. - The module contains a
kubernetes_config_mapresource to manage theaws-authConfigMap, violating the rule that cluster modules must not containkubernetes_*resources.
- The module allows arbitrary IAM-to-RBAC mappings via
- Tagging: In
aws/cluster/node-pool, thelabelsattribute is applied to the cloud API but not to the Kubernetes node object's.metadata.labels.
- Networking: Nodes are assigned public IPs by default (