From 70d95e08f6c5af91d7b3fbcb6a91ca57a1ae5884 Mon Sep 17 00:00:00 2001 From: Hugo Bollon Date: Tue, 23 Jan 2024 14:05:35 +0100 Subject: [PATCH] feat: add automatic filesystem trim feature using recurringjob --- .../templates/backup-storageclass.yaml | 6 +++++ .../recurring-job-filesystem-trim.yaml | 24 +++++++++++++++++++ locals.tf | 14 +++++++++-- variables.tf | 23 ++++++++++++++++++ 4 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 charts/longhorn/templates/recurring-job-filesystem-trim.yaml diff --git a/charts/longhorn/templates/backup-storageclass.yaml b/charts/longhorn/templates/backup-storageclass.yaml index 9b45342..2f71cce 100644 --- a/charts/longhorn/templates/backup-storageclass.yaml +++ b/charts/longhorn/templates/backup-storageclass.yaml @@ -16,6 +16,12 @@ parameters: numberOfReplicas: {{ $.Values.numberOfReplicas | quote }} staleReplicaTimeout: "30" fromBackup: "" + {{- if $.Values.recurringJobSelector }} + recurringJobSelector: + {{- with $.Values.recurringJobSelector }} + {{- toYaml . | nindent 4 }} + {{- end }} + {{- end }} recurringJobs: '[ {{- if $.Values.backups.config.snapshot_enabled }} { diff --git a/charts/longhorn/templates/recurring-job-filesystem-trim.yaml b/charts/longhorn/templates/recurring-job-filesystem-trim.yaml new file mode 100644 index 0000000..8bc4996 --- /dev/null +++ b/charts/longhorn/templates/recurring-job-filesystem-trim.yaml @@ -0,0 +1,24 @@ +{{- if $.Values.automaticFilesystemTrim.enabled -}} + +--- +apiVersion: longhorn.io/v1beta2 +kind: RecurringJob +metadata: + name: {{ $.Release.Name }}-filesystem-trim + labels: + name: {{ $.Release.Name }}-filesystem-trim +spec: + concurrency: 1 + cron: {{ $.Values.automaticFilesystemTrim.cron | quote }} + {{- if and ($.Values.automaticFilesystemTrim.jobGroup) (ne $.Values.automaticFilesystemTrim.jobGroup "") }} + groups: + - {{ $.Values.automaticFilesystemTrim.jobGroup | quote }} + {{- else }} + groups: [] + {{- end }} + labels: {} + name: fs-trim + retain: 0 + task: filesystem-trim + +{{- end -}} diff --git a/locals.tf b/locals.tf index ad73817..03233e6 100644 --- a/locals.tf +++ b/locals.tf @@ -2,8 +2,8 @@ locals { domain = format("longhorn.apps.%s", var.base_domain) domain_full = format("longhorn.apps.%s.%s", var.cluster_name, var.base_domain) - # Generate a list of tolerations in a string format of `key=value:effect` for all the tolerations that have - # an `operator` equal to `Equal`. This list of strings will be joined to pass as the value of + # Generate a list of tolerations in a string format of `key=value:effect` for all the tolerations that have + # an `operator` equal to `Equal`. This list of strings will be joined to pass as the value of # `defaultSettings.taintToleration`. tolerations_list = [for toleration in var.tolerations : "${toleration.key}=${toleration.value}:${toleration.effect}" if toleration.operator == "Equal"] @@ -20,6 +20,10 @@ locals { persistence = { defaultClass = var.enable_pv_backups && var.set_default_storage_class ? "false" : "true" defaultClassReplicaCount = var.replica_count + recurringJobSelector = { + enable = tostring(var.automatic_filesystem_trim.enabled && !var.set_default_storage_class) + jobList = var.automatic_filesystem_trim.enabled && !var.set_default_storage_class && var.recurring_job_selectors != null ? jsonencode(var.recurring_job_selectors) : null + } } longhornManager = { tolerations = var.tolerations @@ -66,6 +70,12 @@ locals { servicemonitor = { enabled = var.enable_service_monitor } + automaticFilesystemTrim = { + enabled = var.automatic_filesystem_trim.enabled + cron = var.automatic_filesystem_trim.cron + job_group = var.automatic_filesystem_trim.job_group + } + recurringJobSelector = var.automatic_filesystem_trim.enabled && var.enable_pv_backups && var.set_default_storage_class && var.recurring_job_selectors != null ? jsonencode(var.recurring_job_selectors) : null }] } diff --git a/variables.tf b/variables.tf index 3ad3c35..1924dff 100644 --- a/variables.tf +++ b/variables.tf @@ -180,6 +180,29 @@ variable "oidc" { default = null } +variable "automatic_filesystem_trim" { + description = "Settings to enable and configure automatic filesystem trim of volumes managed by Longhorn." + type = object({ + enabled = bool + cron = string + job_group = string + }) + default = { + enabled = false + cron = "0 6 * * *" + job_group = "" + } +} + +variable "recurring_job_selectors" { + description = "Define a group list to add to recurring job selector for the default storage class (the custom backup one if `set_default_storage_class` is set or else the Longhorn default one)." + type = list(object({ + name = string + isGroup = bool + })) + default = null +} + variable "replica_count" { description = "Amount of replicas created by Longhorn for each volume." type = number