Skip to content

Commit 59d18bf

Browse files
committed
Add preinstall commands to user_kustomization
1 parent 25995ad commit 59d18bf

File tree

9 files changed

+41
-11
lines changed

9 files changed

+41
-11
lines changed

kube.tf.example

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -965,21 +965,24 @@ module "kube-hetzner" {
965965
# The Kustomization "sets" are run in sequential order (by numeric key) so that you can for example install a CRD and wait for it to be deployed.
966966
#
967967
# source_folder: Sets the source folder for *.yaml.tpl and Kustomization.yaml.tpl
968-
# kustomize_parameters: Key-value map for passing variables into Kustomization. Applies only to the Kustomization-set in the object, but to all files defined in the source_folder of the "set".
969-
# post_commands: Commands to be executed after applying the Kustomization ("kubectl apply -k"). You can use it to wait for CRD deployment etc.
968+
# kustomize_parameters: Key-value map for passing variables into Kustomization. Applies only to the Kustomization-set in the object, but to all files defined in the source_folder of the "set". Defaults to {}.
969+
# pre_commands: Commands to be executed before applying the Kustomization ("kubectl apply -k"). Defaults to "".
970+
# post_commands: Commands to be executed after applying the Kustomization ("kubectl apply -k"). You can use it to wait for CRD deployment etc. Defaults to "".
970971
# - An example to wait for deployments in all namespaces: `kubectl wait --for=condition=Available deployment --all -A --timeout=120s || exit 0` (The `|| exit 0` is not "required", it just makes the deployment to continue even if a deployment hasn't started up properly)
971972
# - You can pass full bash-compatible scripts into the `post_commands`-variable with EOT
972973
#
973-
# An example from the default values: (uncomment or copy your own)
974+
# An example from the default values:
974975
# user_kustomizations = {
975976
# "1" = {
976977
# source_folder = "extra-manifests-preinstall"
977978
# kustomize_parameters = {}
979+
# pre_commands = ""
978980
# post_commands = ""
979981
# },
980982
# "2" = {
981983
# source_folder = "extra-manifests" # Uses `var.extra_kustomize_folder` internally, defaults to "extra-manifests"
982984
# kustomize_parameters = var.extra_kustomize_parameters # Same as `extra_kustomize_parameters` in kube.tf, replace with actual values if uncommenting
985+
# pre_commands = ""
983986
# post_commands = var.extra_kustomize_deployment_commands # Same as `extra_kustomize_deployment_commands` in kube.tf, replace with actual values if uncommenting
984987
# }
985988
# }

kustomization_user.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ locals {
33
"1" = {
44
source_folder = "extra-manifests-preinstall"
55
kustomize_parameters = {}
6+
pre_commands = ""
67
post_commands = ""
78
},
89
"2" = {
910
source_folder = var.extra_kustomize_folder
1011
kustomize_parameters = var.extra_kustomize_parameters
12+
pre_commands = ""
1113
post_commands = var.extra_kustomize_deployment_commands
1214
}
1315
}
@@ -19,6 +21,7 @@ locals {
1921
# kustomize_parameters may contain secrets
2022
kustomize_parameters = sensitive(config.kustomize_parameters)
2123
source_folder = config.source_folder
24+
pre_commands = config.pre_commands
2225
post_commands = config.post_commands
2326
}
2427
}

modules/user_kustomization_set/locals.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ locals {
55
filesha1("${var.source_folder}/${file_path}")
66
])
77
parameters_sha = sha1(jsonencode(var.template_parameters))
8+
pre_commands_string_sha = sha1(var.pre_commands_string)
89
post_commands_string_sha = sha1(var.post_commands_string)
910
}

modules/user_kustomization_set/main.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ resource "null_resource" "create_target_directory" {
77
triggers = {
88
source_files_sha = local.source_files_sha
99
parameters_sha = local.parameters_sha
10+
pre_commands_string_sha = local.pre_commands_string_sha
1011
post_commands_string_sha = local.post_commands_string_sha
1112
}
1213

@@ -29,6 +30,11 @@ resource "null_resource" "create_target_directory" {
2930
]
3031
}
3132

33+
provisioner "file" {
34+
content = templatefile("${path.module}/templates/bash.sh.tpl", { commands = var.pre_commands_string })
35+
destination = "${var.destination_folder}/preinstall.sh"
36+
}
37+
3238
provisioner "file" {
3339
content = templatefile("${path.module}/templates/bash.sh.tpl", { commands = var.post_commands_string })
3440
destination = "${var.destination_folder}/postinstall.sh"

modules/user_kustomization_set/out.tf

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ output "parameters_sha" {
1010
value = local.parameters_sha
1111
}
1212

13+
output "pre_commands_string_sha" {
14+
value = local.pre_commands_string_sha
15+
}
16+
1317
output "post_commands_string_sha" {
1418
value = local.post_commands_string_sha
1519
}
@@ -21,6 +25,6 @@ output "files_count" {
2125

2226
output "changes_sha" {
2327
value = sha1(join("", [
24-
local.source_files_sha, local.parameters_sha, local.post_commands_string_sha
28+
local.source_files_sha, local.parameters_sha, local.pre_commands_string_sha, local.post_commands_string_sha
2529
]))
2630
}

modules/user_kustomization_set/variables.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ variable "template_parameters" {
3434
sensitive = true
3535
}
3636

37+
variable "pre_commands_string" {
38+
type = string
39+
default = ""
40+
}
41+
3742
variable "post_commands_string" {
3843
type = string
3944
default = ""

modules/user_kustomizations/main.tf

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ module "user_kustomization_set" {
88

99
ssh_connection = var.ssh_connection
1010

11-
source_folder = each.value.source_folder
12-
destination_folder = "${local.base_destination_folder}/${each.key}"
13-
template_parameters = each.value.kustomize_parameters
11+
source_folder = each.value.source_folder
12+
destination_folder = "${local.base_destination_folder}/${each.key}"
13+
template_parameters = each.value.kustomize_parameters
14+
15+
pre_commands_string = each.value.pre_commands
1416
post_commands_string = each.value.post_commands
1517
}
1618

@@ -42,12 +44,16 @@ resource "null_resource" "kustomization_user_deploy" {
4244
#!/bin/bash
4345
for dest_folder in ${join(" ", local.sorted_kustomization_destination_folders)}; do
4446
if [ -d "$dest_folder" ]; then
47+
echo "Running pre-install script from $dest_folder"
48+
/bin/bash "$dest_folder/preinstall.sh"
49+
4550
if [ -f "$dest_folder/kustomization.yaml" ]; then
4651
echo "Applying kustomization from $dest_folder"
4752
kubectl apply -k "$dest_folder"
4853
else
4954
echo "No kustomization.yaml in $dest_folder, skipping apply."
5055
fi
56+
5157
echo "Running post-install script from $dest_folder"
5258
/bin/bash "$dest_folder/postinstall.sh"
5359
fi

modules/user_kustomizations/variables.tf

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ variable "ssh_connection" {
1717
variable "kustomizations_map" {
1818
type = map(object({
1919
source_folder = string
20-
kustomize_parameters = map(any)
21-
post_commands = string
20+
kustomize_parameters = optional(map(any), {})
21+
pre_commands = optional(string, "")
22+
post_commands = optional(string, "")
2223
}))
2324
default = {}
2425
description = "Map of kustomization entries, where key is the order number."

variables.tf

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,8 +1091,9 @@ variable "extra_kustomize_folder" {
10911091
variable "user_kustomizations" {
10921092
type = map(object({
10931093
source_folder = string
1094-
kustomize_parameters = map(any)
1095-
post_commands = string
1094+
kustomize_parameters = optional(map(any), {})
1095+
pre_commands = optional(string, "")
1096+
post_commands = optional(string, "")
10961097
}))
10971098
default = {}
10981099
description = "Map of Kustomization-set entries, where key is the order number."

0 commit comments

Comments
 (0)