{ lib, config, ... }: let longhornVolumeOpts = {name, ...}: { options = { storage = lib.mkOption { type = lib.types.str; }; namespace = lib.mkOption { type = lib.types.str; default = "default"; }; }; }; longhornPVOpts = {name, ...}: { options = { storage = lib.mkOption { type = lib.types.str; }; }; }; longhornPVCOpts = {name, ...}: { options = { volumeName = lib.mkOption { type = lib.types.str; default = name; }; # TODO: ideally we take this from the longhornPV so we don't duplicate this information. storage = lib.mkOption { type = lib.types.str; }; }; }; in { options = { lab.longhornVolumes = lib.mkOption { type = with lib.types; attrsOf (submodule longhornVolumeOpts); default = {}; }; lab.longhorn = { persistentVolume = lib.mkOption { type = with lib.types; attrsOf (submodule longhornPVOpts); default = {}; }; persistentVolumeClaim = lib.mkOption { type = with lib.types; attrsOf (submodule longhornPVCOpts); default = {}; }; }; }; config = { kubernetes.resources = { persistentVolumes = lib.mergeAttrs (builtins.mapAttrs (name: longhornVolume: { spec = { accessModes = ["ReadWriteOnce"]; capacity.storage = longhornVolume.storage; persistentVolumeReclaimPolicy = "Delete"; volumeMode = "Filesystem"; claimRef = { inherit name; namespace = longhornVolume.namespace; }; csi = { driver = "driver.longhorn.io"; fsType = "ext4"; volumeHandle = name; volumeAttributes = { dataLocality = "disabled"; fromBackup = ""; fsType = "ext4"; numberOfReplicas = "2"; staleReplicaTimeout = "30"; unmapMarkSnapChainRemoved = "ignored"; recurringJobSelector = lib.generators.toYAML {} [ { name = "backup-nfs"; isGroup = false; } ]; }; }; }; }) config.lab.longhornVolumes) (builtins.mapAttrs (name: longhornPV: { spec = { accessModes = ["ReadWriteOnce"]; capacity.storage = longhornPV.storage; persistentVolumeReclaimPolicy = "Delete"; volumeMode = "Filesystem"; csi = { driver = "driver.longhorn.io"; fsType = "ext4"; volumeHandle = name; volumeAttributes = { dataLocality = "disabled"; fromBackup = ""; fsType = "ext4"; numberOfReplicas = "2"; staleReplicaTimeout = "30"; unmapMarkSnapChainRemoved = "ignored"; recurringJobSelector = lib.generators.toYAML {} [ { name = "backup-nfs"; isGroup = false; } ]; }; }; }; }) config.lab.longhorn.persistentVolume); persistentVolumeClaims = lib.mergeAttrs (builtins.mapAttrs (name: longhornVolume: { spec = { accessModes = ["ReadWriteOnce"]; resources.requests.storage = longhornVolume.storage; storageClassName = ""; }; }) config.lab.longhornVolumes) (builtins.mapAttrs (name: longhornPVC: { spec = { accessModes = ["ReadWriteOnce"]; resources.requests.storage = longhornPVC.storage; storageClassName = ""; volumeName = longhornPVC.volumeName; }; }) config.lab.longhorn.persistentVolumeClaim); }; }; }