TExcelTAD
Home › DevOps Capstone: Ship a Production Service › Local Track: kind + OpenTofu

Local Track: kind + OpenTofu

The local track gives you a real multi-node Kubernetes cluster on your laptop, with the same platform add-ons a cloud cluster would have: a Gateway API controller, a PostgreSQL operator, Argo CD and the Prometheus stack. It costs nothing and tears down in seconds. Everything is code in linkly-infra/local/, in two OpenTofu roots:

Root Creates Providers
local/cluster the kind cluster tehcyx/kind
local/platform namespaces, Redis per environment, platform add-ons as Helm releases hashicorp/kubernetes, hashicorp/helm

Two roots, because the platform's providers need the cluster's kubeconfig. In one root, the kubernetes and helm providers would be configured from a resource that doesn't exist yet during the first plan, the problem the EKS lesson warns about. Separate roots apply in order and can be destroyed independently.

Root 1: the cluster (sample)

The kind provider is a community provider, not part of the sandbox's offline mirror, so this root is a sample. Check the registry page for the current version before you pin it:

hcl
# local/cluster/main.tf
terraform {
  required_providers {
    kind = { source = "tehcyx/kind", version = "~> 0.9" }
  }
}

resource "kind_cluster" "linkly" {
  name           = "linkly"
  wait_for_ready = true
  # node_image = "kindest/node:<version>"   # pin the image listed in your kind release's notes

  kind_config {
    kind        = "Cluster"
    api_version = "kind.x-k8s.io/v1alpha4"
    node { role = "control-plane" }
    node { role = "worker" }
    node { role = "worker" }
  }
}

output "context" {
  value = "kind-${kind_cluster.linkly.name}"
}

Without OpenTofu, the kind CLI does the same from a config file, and k3d is a lighter alternative (k3s nodes; it ships Traefik, which you can disable to use the same Gateway controller as kind):

bash
kind create cluster --name linkly --config - <<'EOF'
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes: [{role: control-plane}, {role: worker}, {role: worker}]
EOF
# or
k3d cluster create linkly --agents 2 --k3s-arg "--disable=traefik@server:0"

Either way your kubeconfig gets a context (kind-linkly or k3d-linkly), which root 2 reads.

Root 2: the platform

The platform root installs four add-ons, each pinned to an exact chart version through a variable, and creates what linkly needs per environment:

Add-on Chart Why
Envoy Gateway oci://docker.io/envoyproxy/gateway-helm implements Gateway API: the Gateway that linkly's HTTPRoute attaches to
CloudNativePG cloudnative-pg from https://cloudnative-pg.github.io/charts an operator that runs PostgreSQL clusters declared as Cluster objects
Argo CD argo-cd from https://argoproj.github.io/argo-helm the GitOps agent (GitOps Delivery)
kube-prometheus-stack kube-prometheus-stack from https://prometheus-community.github.io/helm-charts Prometheus, Alertmanager, Grafana and the ServiceMonitor CRDs

All four are the projects' own charts, maintained upstream; none come from Bitnami, whose free charts moved to an unmaintained legacy repository in 2025. What OpenTofu does not create: the PostgreSQL Cluster objects, the Gateway and linkly itself. Those are Kubernetes objects that change with the application, so they live in the GitOps repository and Argo CD applies them. A kubernetes_manifest for a CRD-based object in this root would also fail to plan on a fresh cluster, because the CRD doesn't exist until the Helm release that brings it has been applied.

The session writes the root and a test file. tofu test with mock providers plans the configuration without a cluster, so the logic (the for_each over environments, the version validation, the outputs) is tested offline, exactly as it would be in CI:

bash · try it yourself
mkdir -p linkly-infra/local/platform/tests && cd linkly-infra/local/platform
cat > main.tf <<'X'
terraform {
  required_version = ">= 1.10"
  required_providers {
    helm       = { source = "hashicorp/helm", version = "~> 3.0" }
    kubernetes = { source = "hashicorp/kubernetes", version = "~> 3.0" }
  }
}

variable "kube_context" {
  type    = string
  default = "kind-linkly"
}

variable "environments" {
  type    = set(string)
  default = ["staging", "production"]
}

variable "addons" {
  description = "Platform add-ons, each pinned to an exact chart version."
  type = map(object({
    repository = string
    chart      = string
    version    = string
    namespace  = string
    values     = optional(any, {})
  }))
  validation {
    condition     = alltrue([for a in values(var.addons) : can(regex("^v?[0-9]+\\.[0-9]+\\.[0-9]+
quot;, a.version))]) error_message = "Pin every chart to an exact version (x.y.z), never a range or \"latest\"." } } provider "kubernetes" { config_path = "~/.kube/config" config_context = var.kube_context } provider "helm" { kubernetes = { config_path = "~/.kube/config" config_context = var.kube_context } } resource "helm_release" "addon" { for_each = var.addons name = each.key repository = each.value.repository chart = each.value.chart version = each.value.version namespace = each.value.namespace create_namespace = true values = [yamlencode(each.value.values)] wait = true timeout = 600 } resource "kubernetes_namespace_v1" "app" { for_each = var.environments metadata { name = "linkly-${each.key}" labels = { "app.kubernetes.io/part-of" = "linkly", env = each.key } } } # Redis per environment. It is a cache: one replica, no persistence, bounded memory. resource "kubernetes_deployment_v1" "redis" { for_each = var.environments metadata { name = "linkly-redis" namespace = kubernetes_namespace_v1.app[each.key].metadata[0].name } spec { replicas = 1 selector { match_labels = { app = "linkly-redis" } } template { metadata { labels = { app = "linkly-redis" } } spec { container { name = "redis" image = "redis:7.4" args = ["--save", "", "--appendonly", "no", "--maxmemory", "64mb", "--maxmemory-policy", "allkeys-lru"] port { container_port = 6379 } resources { requests = { cpu = "50m", memory = "80Mi" } limits = { memory = "96Mi" } } } } } } } resource "kubernetes_service_v1" "redis" { for_each = var.environments metadata { name = "linkly-redis" namespace = kubernetes_namespace_v1.app[each.key].metadata[0].name } spec { selector = { app = "linkly-redis" } port { port = 6379 target_port = 6379 } } } output "redis_addr" { value = { for env in var.environments : env => "linkly-redis.linkly-${env}.svc:6379" } } X cat > tests/platform.tftest.hcl <<'X' mock_provider "helm" {} mock_provider "kubernetes" {} variables { addons = { cnpg = { repository = "https://cloudnative-pg.github.io/charts", chart = "cloudnative-pg", version = "1.0.0", namespace = "cnpg-system" } monitoring = { repository = "https://prometheus-community.github.io/helm-charts", chart = "kube-prometheus-stack" version = "1.0.0", namespace = "monitoring" values = { prometheus = { prometheusSpec = { serviceMonitorSelectorNilUsesHelmValues = false } } } } } } run "plans_the_platform" { command = plan assert { condition = length(helm_release.addon) == 2 && helm_release.addon["cnpg"].namespace == "cnpg-system" error_message = "expected the two add-ons from the variables" } assert { condition = length(kubernetes_deployment_v1.redis) == 2 error_message = "expected one Redis per environment" } assert { condition = output.redis_addr["production"] == "linkly-redis.linkly-production.svc:6379" error_message = "unexpected Redis address" } } run "rejects_unpinned_charts" { command = plan variables { addons = { argocd = { repository = "https://argoproj.github.io/argo-helm", chart = "argo-cd", version = "latest", namespace = "argocd" } } } expect_failures = [var.addons] } X tofu fmt -check -recursive && echo "fmt: ok" tofu init -backend=false 2>&1 | grep -E 'Installed|initialized' | sort tofu validate tofu test
fmt: ok
- Installed hashicorp/helm v3.3.0 (unauthenticated)
- Installed hashicorp/kubernetes v3.4.0 (unauthenticated)
OpenTofu has been successfully initialized!
Success! The configuration is valid.
tests/platform.tftest.hcl... pass
  run "plans_the_platform"... pass
  run "rejects_unpinned_charts"... pass

Success! 2 passed, 0 failed.

Points worth copying into your own roots:

  • Exact versions, enforced. The validation block rejects latest or a range, and the second test run proves the rule works (expect_failures passes only if validation fails). A chart that moves under you is a change nobody reviewed.
  • values per add-on is optional(any, {}). The one setting in the test is the classic kube-prometheus-stack gotcha: by default its Prometheus selects only ServiceMonitors labelled with the Helm release; serviceMonitorSelectorNilUsesHelmValues = false makes it pick up linkly's ServiceMonitor from any namespace.
  • Redis is sized as a cache: no persistence (--save "", no AOF), a memory cap and LRU eviction, so it can never grow until the node kills it. A restart costs a few cache misses, which linkly is built to survive.
  • The namespaces carry app.kubernetes.io/part-of: linkly; the Gateway in the GitOps repository admits routes only from namespaces with that label.

Applying it (sample)

On your machine, with the real chart versions in a local.auto.tfvars (look them up with helm search repo <chart> or on Artifact Hub):

bash
$ tofu -chdir=local/cluster init && tofu -chdir=local/cluster apply
...
kind_cluster.linkly: Creation complete after 41s [id=linkly-]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

$ tofu -chdir=local/platform init && tofu -chdir=local/platform apply
...
Plan: 10 to add, 0 to change, 0 to destroy.
...
helm_release.addon["monitoring"]: Creation complete after 1m52s [id=monitoring]
Apply complete! Resources: 10 added, 0 changed, 0 destroyed.

Outputs:

redis_addr = {
  "production" = "linkly-redis.linkly-production.svc:6379"
  "staging" = "linkly-redis.linkly-staging.svc:6379"
}

$ kubectl get pods -A | grep -v kube-system | awk '{print $1, $2, $4}' | column -t
NAMESPACE             NAME                                              STATUS
argocd                argocd-application-controller-0                   Running
argocd                argocd-repo-server-6d9c8b7f5-x2mpl                Running
argocd                argocd-server-7c6b9d4f8-9kq4w                     Running
cnpg-system           cnpg-cloudnative-pg-5f7d8c9b6-lq2vt               Running
envoy-gateway-system  envoy-gateway-6c8f9d7b5-hn4kp                     Running
linkly-production     linkly-redis-7b9c6d5f4-m8x2q                      Running
linkly-staging        linkly-redis-7b9c6d5f4-p4n7z                      Running
monitoring            monitoring-grafana-5d6f7c8b9-t6wqz                Running
monitoring            prometheus-monitoring-kube-prometheus-prometheus-0 Running
...

Traffic into kind. The Gateway controller creates a Service of type LoadBalancer, which stays <pending> on kind because nothing hands out external IPs. Run the cloud-provider-kind helper on the host to get real load-balancer IPs, or use kubectl port-forward to the Gateway's Service for a quick test. k3d's built-in service load balancer handles it without extra tools.

Teardown

bash
tofu -chdir=local/platform destroy     # optional: deleting the cluster removes everything inside it
tofu -chdir=local/cluster destroy      # or: kind delete cluster --name linkly

On the local track the second command is enough: nothing outside the Docker containers was created. Still run the platform destroy now and then, to prove the root can be destroyed cleanly; you will want that property on the cloud track.

Test yourself

Why are the kind cluster and the platform add-ons in two separate OpenTofu roots?

  1. OpenTofu allows only one provider per root
  2. The kubernetes and helm providers need the cluster's kubeconfig, which doesn't exist during the first plan of a single root
  3. To make the state file smaller
  4. kind requires it

What does expect_failures = [var.addons] in the second test run check?

  1. That the plan has no failures
  2. That the variable validation rejects an unpinned chart version
  3. That the helm provider fails offline
  4. That the test is skipped

Why does the PostgreSQL Cluster object live in the GitOps repository rather than in this OpenTofu root?

  1. CloudNativePG can't be installed by Helm
  2. It is an application-level object applied by Argo CD, and a CRD-based manifest can't be planned before the operator's CRD exists
  3. OpenTofu can't create namespaces
  4. Clusters must be created by hand