Skip to content

Commit ec50a2c

Browse files
feat: terragrunt example with documentation (#165)
* feat: `terragrunt` example + docs * fix: wrong ref, wrong format * fix: you can't use "stable"; needs major.minor.patch * test: tested https link; works as expected; switching link prior to merge * chore: forgot to remove todo * chore: remove todo for comments I think I would rather force people to go read the terragrunt documentation to understand the example. * fix: forgot to change branch name to `main` --------- Co-authored-by: Scott Murray <scott@murray.kiwi>
1 parent c64b36f commit ec50a2c

File tree

11 files changed

+206
-0
lines changed

11 files changed

+206
-0
lines changed

docs/guides/terragrunt.md

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
---
2+
page_title: "terragrunt Guide - terraform-provider-minikube"
3+
subcategory: ""
4+
description: |-
5+
Guide for creating minikube cluster with terragrunt
6+
---
7+
8+
# Guide: Launching `minikube` clusters with `terragrunt`
9+
10+
`terragrunt` is a tool that wraps `terraform` or `tofu` for reusing modules and generating sources.
11+
This is primarily useful if you're working with a large number of `minikube` configurations, or you're wanting to keep them DRY.
12+
13+
## Prerequisites
14+
15+
* Install [terraform](https://github.com/hashicorp/terraform) or [tofu](https://github.com/opentofu/opentofu)
16+
* Install [terragrunt](https://github.com/gruntwork-io/terragrunt)
17+
18+
## Terragrunt Project with Common Environment
19+
20+
While it's possible to create a working `terragrunt` project with fewer files, this example is the most generally applicable.
21+
The [`examples/terragrunt`](https://github.com/scott-the-programmer/terraform-provider-minikube/tree/main/examples) folder contains two projects: `terraform_project` and `terragrunt_project`.
22+
23+
### terraform_project
24+
25+
The `terraform_project` folder is a valid project and can be initialized using the standard `terraform init`, `terraform plan`, and `terraform apply`.
26+
27+
```shell
28+
cat <<EOF > terraform.tfvars
29+
cluster_name = "minikube"
30+
driver = "docker"
31+
nodes = 1
32+
cpus = 2
33+
memory = 2048
34+
EOF
35+
terraform init
36+
terraform plan
37+
terraform apply
38+
```
39+
40+
### terragrunt_project
41+
42+
Within `terragrunt_project/dev`, there are two runnable configurations named `cluster-a` and `cluster-b` that reuse the `terraform_project` with shared inputs; they're run from their working directories using `terragrunt init`, `terragrunt plan`, and `terragrunt apply`.
43+
44+
```shell
45+
cat <<EOF > terraform.tfvars
46+
nodes = 1
47+
cpus = 2
48+
memory = 2048
49+
EOF
50+
terragrunt init
51+
terragrunt plan
52+
terragrunt apply
53+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
terraform {
2+
backend "local" {
3+
path = "terraform.tfstate"
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
terraform {
2+
required_providers {
3+
minikube = {
4+
source = "scott-the-programmer/minikube"
5+
version = "0.3.10"
6+
}
7+
}
8+
}
9+
10+
resource "minikube_cluster" "default" {
11+
cluster_name = var.cluster_name
12+
driver = var.driver
13+
nodes = var.nodes
14+
cpus = var.cpus
15+
preload = true
16+
cache_images = true
17+
auto_update_drivers = true
18+
install_addons = true
19+
addons = [
20+
"default-storageclass",
21+
"storage-provisioner"
22+
]
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
output "client_certificate" {
2+
value = minikube_cluster.default.client_certificate
3+
description = "The cluster client certificate."
4+
sensitive = true
5+
}
6+
7+
output "client_key" {
8+
value = minikube_cluster.default.client_key
9+
description = "The cluster client key."
10+
sensitive = true
11+
}
12+
13+
output "cluster_ca_certificate" {
14+
value = minikube_cluster.default.cluster_ca_certificate
15+
description = "The cluster CA certificate."
16+
sensitive = true
17+
}
18+
19+
output "host" {
20+
value = minikube_cluster.default.host
21+
description = "The cluster host."
22+
}
23+
24+
output "id" {
25+
value = minikube_cluster.default.id
26+
description = "The cluster id."
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
provider "minikube" {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
variable "cluster_name" {
2+
type = string
3+
description = "The minikube name."
4+
}
5+
6+
variable "driver" {
7+
type = string
8+
description = "The minikube driver."
9+
}
10+
11+
variable "nodes" {
12+
type = number
13+
description = "The number of nodes."
14+
}
15+
16+
variable "cpus" {
17+
type = number
18+
description = "The number of cpus."
19+
}
20+
21+
variable "memory" {
22+
type = number
23+
description = "The amount of memory."
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
locals {
2+
base_source_url = "git::https://github.com/scott-the-programmer/terraform-provider-minikube.git//examples/guides/terragrunt/terraform_project"
3+
ref = "main"
4+
kubernetes_version = "v1.28.3"
5+
}
6+
7+
generate "provider" {
8+
path = "provider.tf"
9+
if_exists = "overwrite"
10+
contents = <<EOF
11+
provider "minikube" {
12+
kubernetes_version = "${local.kubernetes_version}"
13+
}
14+
EOF
15+
}
16+
17+
remote_state {
18+
backend = "local"
19+
generate = {
20+
path = "backend.tf"
21+
if_exists = "overwrite"
22+
}
23+
config = {
24+
path = "${get_parent_terragrunt_dir()}/${path_relative_to_include()}/terraform.tfstate"
25+
}
26+
}
27+
28+
inputs = {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
include "root" {
2+
path = find_in_parent_folders()
3+
}
4+
5+
include "envcommon" {
6+
path = "${dirname(find_in_parent_folders())}/_envcommon/minikube.hcl"
7+
expose = true
8+
}
9+
10+
terraform {
11+
source = "${include.envcommon.locals.base_source_url}?ref=${include.envcommon.locals.ref}"
12+
}
13+
14+
inputs = {
15+
cluster_name = "dev-cluster-a"
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
include "root" {
2+
path = find_in_parent_folders()
3+
}
4+
5+
include "envcommon" {
6+
path = "${dirname(find_in_parent_folders())}/_envcommon/minikube.hcl"
7+
expose = true
8+
}
9+
10+
terraform {
11+
source = "${include.envcommon.locals.base_source_url}?ref=${include.envcommon.locals.ref}"
12+
}
13+
14+
inputs = {
15+
cluster_name = "dev-cluster-b"
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
locals {
2+
environment = "dev"
3+
driver = "docker"
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
locals {
2+
# load variables
3+
environment_vars = read_terragrunt_config(find_in_parent_folders("environment.hcl"))
4+
}
5+
6+
# merge variables
7+
inputs = merge(
8+
local.environment_vars.locals,
9+
)

0 commit comments

Comments
 (0)