Skip to content

Commit 2d6a255

Browse files
committed
Add pre-install Kustomization folder
1 parent 2613b1f commit 2d6a255

File tree

4 files changed

+135
-7
lines changed

4 files changed

+135
-7
lines changed

README.md

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,11 +334,41 @@ _That said, you can also use pure Terraform and import the kube-hetzner module a
334334

335335
<details>
336336

337-
<summary>Custom post-install actions</summary>
337+
<summary>Custom pre- and post-install actions</summary>
338338

339339
After the initial bootstrapping of your Kubernetes cluster, you might want to deploy applications using the same terraform mechanism. For many scenarios it is sufficient to create a `kustomization.yaml.tpl` file (see [Adding Extras](#adding-extras)). All applied kustomizations will be applied at once by executing a single `kubectl apply -k` command.
340340

341-
However, some applications that e.g. provide custom CRDs (e.g. [ArgoCD](https://argoproj.github.io/cd/)) need a different deployment strategy: one has to deploy CRDs first, then wait for the deployment, before being able to install the actual application. In the ArgoCD case, not waiting for the CRD setup to finish will cause failures. Therefore, an additional mechanism is available to support these kind of deployments. Specify `extra_kustomize_deployment_commands` in your `kube.tf` file containing a series of commands to be executed, after the `Kustomization` step finished:
341+
However, some applications that e.g. provide custom CRDs (e.g. [ArgoCD](https://argoproj.github.io/cd/)) need a different deployment strategy: one has to deploy CRDs first, then wait for the deployment, before being able to install the actual application. In the ArgoCD case, not waiting for the CRD setup to finish will cause failures. Therefore, an additional mechanism is available to support these kind of deployments.
342+
343+
### Pre-install Actions, Example: external-secrets repo and Helm
344+
You can install Helm repos and CRDs before the Kustomization-scripts by adding the helm charts to `extra-manifests-preinstall`-folder and specifying them there in kustomization.yaml.tpl, just like with `extra-manifests`.
345+
Also, if you need to wait for the CRDs to be run, you can specify additional wait commands to `extra_kustomize_pre_install_post_deploy_commands`, which defaults to `sleep 10`.
346+
347+
For example, to add `external-secrets` so that it can be referenced later on, create files:
348+
1. extra-manifests-preinstall/eso.yaml.tpl
349+
```yaml
350+
apiVersion: helm.cattle.io/v1
351+
kind: HelmChart
352+
metadata:
353+
name: external-secrets
354+
namespace: kube-system
355+
spec:
356+
chart: external-secrets
357+
repo: https://charts.external-secrets.io
358+
targetNamespace: external-secrets
359+
createNamespace: true
360+
```
361+
2. extra-manifests-preinstall/kustomization.yaml.tpl
362+
```yaml
363+
apiVersion: kustomize.config.k8s.io/v1beta1
364+
kind: Kustomization
365+
366+
resources:
367+
- eso.yaml
368+
```
369+
370+
### Post-install actions, ArgoCD-example
371+
Specify `extra_kustomize_deployment_commands` in your `kube.tf` file containing a series of commands to be executed, after the `Kustomization` step finished:
342372

343373
```tf
344374
extra_kustomize_deployment_commands = <<-EOT

kube.tf.example

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -952,10 +952,18 @@ module "kube-hetzner" {
952952
# More information about the registration can be found here https://rancher.com/docs/rancher/v2.6/en/cluster-provisioning/registered-clusters/
953953
# rancher_registration_manifest_url = "https://rancher.xyz.dev/v3/import/xxxxxxxxxxxxxxxxxxYYYYYYYYYYYYYYYYYYYzzzzzzzzzzzzzzzzzzzzz.yaml"
954954

955+
956+
# Local folder for Kustomization *.yaml.tpl-files that are to be run before the `extra-manifests`. Defaults to `"extra-manifests-preinstall"`.
957+
# extra_kustomize_pre_install_folder = "extra-manifests-preinstall"
958+
959+
# Command to be executed after the `apply -k` of pre-install Kustomizations defined by `<extra_kustomize_pre_install_folder>/kustomization.yaml.tpl`. Defaults to "sleep 10".
960+
# Can be used with installation of CRDs with commands such as `kubectl wait --for=condition=Available deployment/mycrd`
961+
# extra_kustomize_pre_install_post_deploy_commands = "sleep 10"
962+
955963
# Extra commands to be executed after the `kubectl apply -k` (useful for post-install actions, e.g. wait for CRD, apply additional manifests, etc.).
956964
# extra_kustomize_deployment_commands=""
957965

958-
# Extra values that will be passed to the `extra-manifests/kustomization.yaml.tpl` if its present.
966+
# Extra values that will be passed to the `extra-manifests/kustomization.yaml.tpl` and `extra-manifests-preinstall/kustomization.yaml.tpl` when present.
959967
# extra_kustomize_parameters={}
960968

961969
# See working examples for extra manifests or a HelmChart in examples/kustomization_user_deploy/README.md

kustomization_user.tf

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,82 @@
11
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/${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/${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_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/**/*.yaml.tpl",
65+
"echo 'Applying user pre-install kustomization...'",
66+
"kubectl apply -k /var/user_kustomize/",
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+
]
380
}
481

582
resource "null_resource" "kustomization_user" {
@@ -35,7 +112,8 @@ resource "null_resource" "kustomization_user" {
35112
}
36113

37114
depends_on = [
38-
null_resource.kustomization
115+
null_resource.kustomization,
116+
null_resource.kustomization_user_pre_install_deploy
39117
]
40118
}
41119

@@ -74,6 +152,7 @@ resource "null_resource" "kustomization_user_deploy" {
74152
}
75153

76154
depends_on = [
77-
null_resource.kustomization_user
155+
null_resource.kustomization_user,
156+
null_resource.kustomization_user_pre_install_deploy,
78157
]
79158
}

variables.tf

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1068,7 +1068,6 @@ variable "postinstall_exec" {
10681068
description = "Additional to execute after the install calls, for example restoring a backup."
10691069
}
10701070

1071-
10721071
variable "extra_kustomize_deployment_commands" {
10731072
type = string
10741073
default = ""
@@ -1087,6 +1086,18 @@ variable "extra_kustomize_folder" {
10871086
description = "Folder from where to upload extra manifests"
10881087
}
10891088

1089+
variable "extra_kustomize_pre_install_folder" {
1090+
type = string
1091+
default = "extra-manifests-preinstall"
1092+
description = "Folder from where to upload extra manifests that are run before Kustomization, such as repo installation"
1093+
}
1094+
1095+
variable "extra_kustomize_pre_install_post_deploy_commands" {
1096+
type = string
1097+
default = "sleep 10"
1098+
description = "Commands to be executed after the Kustomization pre-install stage"
1099+
}
1100+
10901101
variable "create_kubeconfig" {
10911102
type = bool
10921103
default = true

0 commit comments

Comments
 (0)