Skip to content

Commit 931bbff

Browse files
committed
Describe the user_kustomization in readme more
1 parent 3068a29 commit 931bbff

File tree

3 files changed

+108
-35
lines changed

3 files changed

+108
-35
lines changed

README.md

Lines changed: 105 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -318,12 +318,26 @@ See the [guide on adding robot servers](docs/add-robot-server.md)
318318

319319
If you need to install additional Helm charts or Kubernetes manifests that are not provided by default, you can easily do so by using [Kustomize](https://kustomize.io). This is done by creating one or more `extra-manifests/kustomization.yaml.tpl` files beside your `kube.tf`.
320320

321-
If you'd like to use a different folder name, you can configure it using the `extra_kustomize_folder` variable. By default, it is set to `extra-manifests`. This can be useful when working with multiple environments, allowing you to deploy different manifests for each one.
322-
323321
These files need to be valid `Kustomization` manifests, additionally supporting terraform templating! (The templating parameters can be passed via the `extra_kustomize_parameters` variable (via a map) to the module).
324322

325323
All files in the `extra-manifests` directory and its subdirectories including the rendered versions of the `*.yaml.tpl` will be applied to k3s with `kubectl apply -k` (which will be executed after and independently of the basic cluster configuration).
326324

325+
If you'd like to use a different folder name or apply the kustomizations in multiple steps due to CRD installation etc, you can override the values using `user_kustomizations`-variable. Note! This will override `extra_kustomize_deployment_commands` and `extra_kustomize_parameters`, so you will need to move them into the `user_kustomizations`-variable.
326+
327+
Example of using user_kustomizations in `kube.tf`:
328+
```
329+
user_kustomizations = {
330+
"1" = {
331+
source_folder = "extra-manifests-pre"
332+
},
333+
"2" = {
334+
source_folder = "extra-manifests"
335+
kustomize_parameters = {key: "LowSecretKey"}
336+
post_commands = var.extra_kustomize_deployment_commands
337+
}
338+
}
339+
```
340+
327341
See a working example in [examples/kustomization_user_deploy](https://github.com/kube-hetzner/terraform-hcloud-kube-hetzner/tree/master/examples/kustomization_user_deploy).
328342

329343
_You can use the above to pass all kinds of Kubernetes YAML configs, including HelmChart and/or HelmChartConfig definitions (see the previous section if you do not know what those are in the context of k3s)._
@@ -334,22 +348,22 @@ _That said, you can also use pure Terraform and import the kube-hetzner module a
334348

335349
<details>
336350

337-
<summary>Custom pre- and post-install actions</summary>
351+
<summary>Adding applications with Helm, custom pre- and post-install actions</summary>
338352

339353
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.
340354

341355
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.
342356

343-
To support these scenarios, this module provides a flexible mechanism using the `user_kustomizations` variable in your `kube.tf`. This allows you to define multiple "sets" of kustomizations that are applied sequentially, ordered by a numeric key. Each set can have its own `source_folder`, `kustomize_parameters`, and `pre_commands`/`post_commands` for running scripts before or after `kubectl apply`. This is the recommended way to handle complex, ordered deployments.
357+
To support these scenarios, you can use the `user_kustomizations` variable in your `kube.tf`. This allows you to define multiple "sets" of kustomizations that are applied sequentially. Each set can have its own `source_folder`, `kustomize_parameters`, and `pre_commands`/`post_commands` for running scripts before or after the `kubectl apply -k`.
344358

345-
For backward compatibility, if `user_kustomizations` is not defined, the module falls back to the previous behavior. The following sections provide examples of how this new system can be used.
359+
For backward compatibility, if `user_kustomizations` is not defined, the module uses the `extra_kustomize_parameters` and `extra_kustomize_deployment_commands` with `extra-manifests` being the source folder.
346360

347-
### Pre-install Actions, Example: external-secrets repo and Helm
348-
You can install Helm repos and CRDs before the main Kustomization scripts by adding the helm charts to the `extra-manifests-preinstall` folder and specifying the Helm chart in `extra-manifests-preinstall/kustomization.yaml.tpl`, just like with `extra-manifests`.
361+
### Example: external-secrets repo and Helm
362+
You can install Helm repos and CRDs before the main Kustomization scripts by adding the Helm charts a different folder, e.g. `extra-manifests-preinstall` and specifying the folder in `user_kustomizations`-settings.
349363

350-
For example, to add `external-secrets` so that it can be referenced later on, create files:
351-
1. extra-manifests-preinstall/eso.yaml.tpl
364+
For example, to add `external-secrets` you can first create the CRDs:
352365
```yaml
366+
# extra-manifests-1/eso-crd.yaml.tpl
353367
apiVersion: helm.cattle.io/v1
354368
kind: HelmChart
355369
metadata:
@@ -361,26 +375,97 @@ spec:
361375
targetNamespace: external-secrets
362376
createNamespace: true
363377
```
364-
2. extra-manifests-preinstall/kustomization.yaml.tpl
365378
```yaml
379+
# extra-manifests-1/kustomization.yaml.tpl
366380
apiVersion: kustomize.config.k8s.io/v1beta1
367381
kind: Kustomization
368382

369383
resources:
370-
- eso.yaml
384+
- eso-crd.yaml
371385
```
372386
373-
### Post-install actions, ArgoCD-example
374-
Specify `extra_kustomize_deployment_commands` in your `kube.tf` file containing a series of commands to be executed, after the `Kustomization` step has finished:
387+
Then create the objects that use the previously created CRDs:
375388
376-
```tf
377-
extra_kustomize_deployment_commands = <<-EOT
378-
kubectl -n argocd wait --for condition=established --timeout=120s crd/appprojects.argoproj.io
379-
kubectl -n argocd wait --for condition=established --timeout=120s crd/applications.argoproj.io
380-
kubectl apply -f /var/user_kustomize/argocd-projects.yaml
381-
kubectl apply -f /var/user_kustomize/argocd-application-argocd.yaml
389+
```yaml
390+
# extra-manifests-2/eso-secrets.yaml.tpl
391+
apiVersion: v1
392+
kind: Secret
393+
metadata:
394+
name: vault-creds
395+
namespace: external-secrets
396+
type: Opaque
397+
data:
398+
username: "${eso_access_username}"
399+
password: "${eso_access_password}"
400+
401+
---
402+
apiVersion: external-secrets.io/v1
403+
kind: SecretStore
404+
metadata:
405+
name: vault-sm-store
406+
namespace: external-secrets
407+
spec:
408+
provider:
409+
aws:
410+
service: SecretsManager
411+
region: us-east-1
412+
413+
auth:
414+
secretRef:
415+
accessKeyIDSecretRef:
416+
name: vault-creds
417+
key: username
418+
namespace: external-secrets
419+
secretAccessKeySecretRef:
420+
name: vault-creds
421+
key: password
422+
namespace: external-secrets
423+
424+
```
425+
```yaml
426+
# extra-manifests-2/kustomization.yaml.tpl
427+
apiVersion: kustomize.config.k8s.io/v1beta1
428+
kind: Kustomization
429+
430+
resources:
431+
- eso-secrets.yaml
432+
```
433+
434+
In `kube.tf`, specify the folders in `user_kustomizations`.
435+
436+
```
437+
# kube.tf
438+
...
439+
user_kustomizations = {
440+
"1" = {
441+
source_folder = "extra-manifests-1"
442+
post_commands = "kubectl -n external-secrets wait --for=condition=Available deployment --all --timeout=120s"
443+
},
444+
"2" = {
445+
source_folder = "extra-manifests-2"
446+
},
382447
...
383-
EOT
448+
}
449+
```
450+
451+
### Example: ArgoCD with Post-install actions
452+
453+
See examples from `examples/kustomization_user_deploy/helm-chart`, place the chart-files along with Kustomization.yaml.tpl into folder `argocd`.
454+
The specify additional project-helms in `argocd-projects`.
455+
456+
```
457+
user_kustomizations = {
458+
"1" = {
459+
source_folder = "argocd"
460+
post_commands = <<-EOT
461+
kubectl -n argocd wait --for condition=established --timeout=120s crd/appprojects.argoproj.io
462+
kubectl -n argocd wait --for condition=established --timeout=120s crd/applications.argoproj.io
463+
EOT
464+
},
465+
"2" = {
466+
source_folder = "argocd-projects"
467+
}
468+
}
384469
```
385470
386471
</details>

kube.tf.example

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -956,7 +956,7 @@ module "kube-hetzner" {
956956
# Extra commands to be executed after the `kubectl apply -k` (useful for post-install actions, e.g. wait for CRD, apply additional manifests, etc.).
957957
# extra_kustomize_deployment_commands=""
958958

959-
# Extra values that will be passed to the `extra-manifests/kustomization.yaml.tpl` and `extra-manifests-preinstall/kustomization.yaml.tpl` when present.
959+
# Extra values that will be passed to the `extra-manifests/kustomization.yaml.tpl`.
960960
# extra_kustomize_parameters={}
961961

962962
# You can add extra kustomizations to be deployed in sequence by setting the `user_kustomizations` variable.
@@ -975,16 +975,10 @@ module "kube-hetzner" {
975975
# An example from the default values:
976976
# user_kustomizations = {
977977
# "1" = {
978-
# source_folder = "extra-manifests-preinstall"
979-
# kustomize_parameters = {}
980-
# pre_commands = ""
981-
# post_commands = ""
982-
# },
983-
# "2" = {
984-
# source_folder = "extra-manifests" # Uses `var.extra_kustomize_folder` internally, defaults to "extra-manifests"
978+
# source_folder = "extra-manifests"
985979
# kustomize_parameters = var.extra_kustomize_parameters # Same as `extra_kustomize_parameters` in kube.tf, replace with actual values if uncommenting
986980
# pre_commands = ""
987-
# post_commands = var.extra_kustomize_deployment_commands # Same as `extra_kustomize_deployment_commands` in kube.tf, replace with actual values if uncommenting
981+
# post_commands = var.extra_kustomize_deployment_commands # Same as `extra_kustomize_deployment_commands` in kube.tf, replace with actual values if uncommenting
988982
# }
989983
# }
990984

kustomization_user.tf

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
locals {
22
default_user_kustomize = {
33
"1" = {
4-
source_folder = "extra-manifests-preinstall"
5-
kustomize_parameters = {}
6-
pre_commands = ""
7-
post_commands = ""
8-
},
9-
"2" = {
104
source_folder = var.extra_kustomize_folder
115
kustomize_parameters = var.extra_kustomize_parameters
126
pre_commands = ""

0 commit comments

Comments
 (0)