|
1 | 1 | locals { |
2 | | - user_kustomization_templates = try(fileset(var.extra_kustomize_folder, "**/*.yaml.tpl"), toset([])) |
| 2 | + user_kustomization_templates = try(fileset(var.extra_kustomize_folder, "**/*.yaml.tpl"), toset([])) |
| 3 | + user_kustomization_pre_install_templates = try(fileset(var.extra_kustomize_pre_install_folder, "**/*.yaml.tpl"), toset([])) |
| 4 | +} |
| 5 | + |
| 6 | +resource "null_resource" "kustomization_user_pre_install_templates" { |
| 7 | + for_each = local.user_kustomization_pre_install_templates |
| 8 | + |
| 9 | + connection { |
| 10 | + user = "root" |
| 11 | + private_key = var.ssh_private_key |
| 12 | + agent_identity = local.ssh_agent_identity |
| 13 | + host = local.first_control_plane_ip |
| 14 | + port = var.ssh_port |
| 15 | + |
| 16 | + bastion_host = local.ssh_bastion.bastion_host |
| 17 | + bastion_port = local.ssh_bastion.bastion_port |
| 18 | + bastion_user = local.ssh_bastion.bastion_user |
| 19 | + bastion_private_key = local.ssh_bastion.bastion_private_key |
| 20 | + |
| 21 | + } |
| 22 | + |
| 23 | + provisioner "remote-exec" { |
| 24 | + inline = [ |
| 25 | + "mkdir -p $(dirname /var/user_kustomize_preinstall/${each.key})" |
| 26 | + ] |
| 27 | + } |
| 28 | + |
| 29 | + provisioner "file" { |
| 30 | + content = templatefile("${var.extra_kustomize_pre_install_folder}/${each.key}", var.extra_kustomize_parameters) |
| 31 | + destination = replace("/var/user_kustomize_preinstall/${each.key}", ".yaml.tpl", ".yaml") |
| 32 | + } |
| 33 | + |
| 34 | + triggers = { |
| 35 | + manifest_sha1 = "${sha1(templatefile("${var.extra_kustomize_pre_install_folder}/${each.key}", var.extra_kustomize_parameters))}" |
| 36 | + } |
| 37 | + |
| 38 | + depends_on = [ |
| 39 | + null_resource.kustomization |
| 40 | + ] |
| 41 | +} |
| 42 | + |
| 43 | +resource "null_resource" "kustomization_user_pre_install_deploy" { |
| 44 | + count = length(local.user_kustomization_pre_install_templates) > 0 ? 1 : 0 |
| 45 | + |
| 46 | + connection { |
| 47 | + user = "root" |
| 48 | + private_key = var.ssh_private_key |
| 49 | + agent_identity = local.ssh_agent_identity |
| 50 | + host = local.first_control_plane_ip |
| 51 | + port = var.ssh_port |
| 52 | + |
| 53 | + bastion_host = local.ssh_bastion.bastion_host |
| 54 | + bastion_port = local.ssh_bastion.bastion_port |
| 55 | + bastion_user = local.ssh_bastion.bastion_user |
| 56 | + bastion_private_key = local.ssh_bastion.bastion_private_key |
| 57 | + |
| 58 | + } |
| 59 | + |
| 60 | + # Remove templates after rendering, and apply changes. |
| 61 | + provisioner "remote-exec" { |
| 62 | + # Debugging: "sh -c 'for file in $(find /var/user_kustomize -type f -name \"*.yaml\" | sort -n); do echo \"\n### Template $${file}.tpl after rendering:\" && cat $${file}; done'", |
| 63 | + inline = compact([ |
| 64 | + "rm -f /var/user_kustomize_preinstall/**/*.yaml.tpl", |
| 65 | + "echo 'Applying user pre-install kustomization...'", |
| 66 | + "kubectl apply -k /var/user_kustomize_preinstall/", |
| 67 | + var.extra_kustomize_pre_install_post_deploy_commands |
| 68 | + ]) |
| 69 | + } |
| 70 | + |
| 71 | + lifecycle { |
| 72 | + replace_triggered_by = [ |
| 73 | + null_resource.kustomization_user_pre_install_templates |
| 74 | + ] |
| 75 | + } |
| 76 | + |
| 77 | + depends_on = [ |
| 78 | + null_resource.kustomization_user_pre_install_templates, |
| 79 | + ] |
3 | 80 | } |
4 | 81 |
|
5 | 82 | resource "null_resource" "kustomization_user" { |
@@ -35,7 +112,8 @@ resource "null_resource" "kustomization_user" { |
35 | 112 | } |
36 | 113 |
|
37 | 114 | depends_on = [ |
38 | | - null_resource.kustomization |
| 115 | + null_resource.kustomization, |
| 116 | + null_resource.kustomization_user_pre_install_deploy |
39 | 117 | ] |
40 | 118 | } |
41 | 119 |
|
@@ -74,6 +152,7 @@ resource "null_resource" "kustomization_user_deploy" { |
74 | 152 | } |
75 | 153 |
|
76 | 154 | depends_on = [ |
77 | | - null_resource.kustomization_user |
| 155 | + null_resource.kustomization_user, |
| 156 | + null_resource.kustomization_user_pre_install_deploy, |
78 | 157 | ] |
79 | 158 | } |
0 commit comments