From 80219a3d0a02f5e70d402a618e6d724fdfaeff24 Mon Sep 17 00:00:00 2001 From: Shahab Dogar Date: Sat, 22 Nov 2025 20:27:33 +0500 Subject: [PATCH] feat: Utils | add high level longhorn pvc construct --- utils/index.ts | 3 ++- utils/longhorn/index.ts | 59 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 utils/longhorn/index.ts diff --git a/utils/index.ts b/utils/index.ts index 30637ed..00c25ae 100644 --- a/utils/index.ts +++ b/utils/index.ts @@ -1,3 +1,4 @@ export { CloudflareCertificate } from "./cert-manager"; export { OnePasswordSecret } from "./1password-secret"; -export { IngressRoute } from "./traefik"; +export { IngressRoute, IngressRouteTcp } from "./traefik"; +export { LonghornPvc } from "./longhorn"; diff --git a/utils/longhorn/index.ts b/utils/longhorn/index.ts new file mode 100644 index 0000000..7a648ec --- /dev/null +++ b/utils/longhorn/index.ts @@ -0,0 +1,59 @@ +import { Construct } from "constructs"; +import { KubernetesProvider } from "@cdktf/provider-kubernetes/lib/provider"; +import { PersistentVolumeClaimV1 } from "@cdktf/provider-kubernetes/lib/persistent-volume-claim-v1"; + +type LonghornPvcOptions = { + provider: KubernetesProvider; + + /** Name of the PVC */ + name: string; + + /** Namespace of the PVC */ + namespace: string; + + /** Size, e.g. "10Gi" */ + size: string; + + /** Access modes (default: ["ReadWriteOnce"]) */ + accessModes?: string[]; + + /** Optional PVC labels */ + labels?: Record; + + /** Add backup annotations */ + backup?: boolean; +}; + +export class LonghornPvc extends Construct { + public readonly name: string; + + constructor(scope: Construct, id: string, opts: LonghornPvcOptions) { + super(scope, id); + + this.name = opts.name; + + new PersistentVolumeClaimV1(this, id, { + provider: opts.provider, + metadata: { + name: opts.name, + namespace: opts.namespace, + labels: opts.labels ?? {}, + annotations: opts.backup + ? { + "recurring-job.longhorn.io/daily-backup": "enabled", + "recurring-job.longhorn.io/source": "enabled", + } + : {}, + }, + spec: { + accessModes: opts.accessModes ?? ["ReadWriteOnce"], + storageClassName: "longhorn", // HARD-CODED + resources: { + requests: { + storage: opts.size, + }, + }, + }, + }); + } +}