Skip to content

Commit 9d6c1e9

Browse files

File tree

4 files changed

+90
-1
lines changed

4 files changed

+90
-1
lines changed

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,37 @@ cluster_autoscaler_helm_values = {
349349
```
350350
</details>
351351

352+
<!-- CSI Driver Storage Class -->
353+
<details>
354+
<summary><b>CSI Driver Storage Class</b></summary>
355+
356+
The Hetzner Cloud Container Storage Interface (CSI) driver supports additional configuration options through the StorageClass resource:
357+
358+
- **Volume encryption**: Enables automatic LUKS2 encryption using a passphrase stored within the Kubernetes cluster.
359+
- **Volume filesystem**: Specify the desired filesystem (e.g., `ext4` or `xfs`) via the `csi.storage.k8s.io/fstype` parameter.
360+
- **Additional format options**: Provide custom formatting options that will be passed directly to `mkfs.<FSTYPE>` during volume provisioning using the `fsFormatOption` parameter.
361+
362+
For full documentation, refer to the [HCloud CSI Driver documentation](https://github.com/hetznercloud/csi-driver/tree/main/docs/kubernetes).
363+
364+
**Example `kubernetes.tf` snippet:**
365+
```hcl
366+
# Enable the HCloud CSI driver
367+
hcloud_csi_enabled = true
368+
369+
# Enable volume encryption; a 32-byte random passphrase is generated by default
370+
hcloud_csi_storage_class_encryption_enabled = true
371+
372+
# Optionally, specify your own encryption key
373+
hcloud_csi_storage_class_encryption_key = "passphrase"
374+
375+
# Define additional StorageClass parameters
376+
hcloud_csi_storage_class_extra_parameters = {
377+
"csi.storage.k8s.io/fstype" = "xfs"
378+
"fsFormatOption" = "-i nrext64=1"
379+
}
380+
```
381+
</details>
382+
352383
<!-- Egress Gateway -->
353384
<details>
354385
<summary><b>Egress Gateway</b></summary>

hcloud.tf

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@ locals {
1616
}
1717
})
1818
}
19+
hcloud_csi_storage_class_encryption_key_manifest = var.hcloud_csi_enabled ? var.hcloud_csi_storage_class_encryption_enabled ? {
20+
apiVersion = "v1"
21+
kind = "Secret"
22+
type = "Opaque"
23+
metadata = {
24+
name = "hcloud-csi-secret"
25+
namespace = "kube-system"
26+
}
27+
data = {
28+
encryption-passphrase = var.hcloud_csi_storage_class_encryption_key != null ? base64encode(var.hcloud_csi_storage_class_encryption_key) : base64encode(random_bytes.hcloud_csi_encryption_key[0].hex)
29+
}
30+
} : null : null
1931
}
2032

2133
# Hcloud CCM
@@ -54,6 +66,11 @@ locals {
5466
}
5567

5668
# Hcloud CSI
69+
resource "random_bytes" "hcloud_csi_encryption_key" {
70+
count = var.hcloud_csi_enabled ? var.hcloud_csi_storage_class_encryption_enabled ? var.hcloud_csi_storage_class_encryption_key == null ? 1 : 0 : 0 : 0
71+
length = 32
72+
}
73+
5774
data "helm_template" "hcloud_csi" {
5875
count = var.hcloud_csi_enabled ? 1 : 0
5976

@@ -92,6 +109,20 @@ data "helm_template" "hcloud_csi" {
92109
}
93110
]
94111
}
112+
storageClasses = [
113+
{
114+
name = "hcloud-volumes",
115+
defaultStorageClass = true,
116+
reclaimPolicy = "Delete",
117+
extraParameters = merge(
118+
var.hcloud_csi_storage_class_encryption_enabled ? {
119+
"csi.storage.k8s.io/node-publish-secret-name" = "hcloud-csi-secret",
120+
"csi.storage.k8s.io/node-publish-secret-namespace" = "kube-system"
121+
} : {},
122+
var.hcloud_csi_storage_class_extra_parameters
123+
)
124+
}
125+
]
95126
}),
96127
yamlencode(var.hcloud_csi_helm_values)
97128
]
@@ -100,6 +131,10 @@ data "helm_template" "hcloud_csi" {
100131
locals {
101132
hcloud_csi_manifest = var.hcloud_csi_enabled ? {
102133
name = "hcloud-csi"
103-
contents = data.helm_template.hcloud_csi[0].manifest
134+
contents = <<-EOF
135+
${data.helm_template.hcloud_csi[0].manifest}
136+
---
137+
${var.hcloud_csi_storage_class_encryption_enabled ? yamlencode(local.hcloud_csi_storage_class_encryption_key_manifest) : yamlencode({})}
138+
EOF
104139
} : null
105140
}

terraform.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ terraform {
2626
source = "hashicorp/tls"
2727
version = "~>4.0.0"
2828
}
29+
30+
random = {
31+
source = "hashicorp/random"
32+
version = "~>3.7.2"
33+
}
2934
}
3035
}
3136

variables.tf

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,24 @@ variable "hcloud_csi_enabled" {
888888
description = "Enables the Hetzner Container Storage Interface (CSI)."
889889
}
890890

891+
variable "hcloud_csi_storage_class_encryption_enabled" {
892+
type = bool
893+
default = false
894+
description = "Enable Hcloud CSI storage class LUKS encryption."
895+
}
896+
897+
variable "hcloud_csi_storage_class_encryption_key" {
898+
type = string
899+
default = null
900+
description = "User defined Hcloud CSI storage class LUKS encryption key."
901+
sensitive = true
902+
}
903+
904+
variable "hcloud_csi_storage_class_extra_parameters" {
905+
type = map(string)
906+
default = {}
907+
description = "Hcloud CSI storage class extra parameters."
908+
}
891909

892910
# Longhorn
893911
variable "longhorn_helm_repository" {

0 commit comments

Comments
 (0)