kubernetes-deployments/modules/longhorn-volume.nix

158 lines
4.1 KiB
Nix
Raw Permalink Normal View History

2024-10-28 15:05:06 +00:00
{
lib,
config,
...
}: let
longhornVolumeOpts = {name, ...}: {
2024-09-07 10:35:02 +00:00
options = {
storage = lib.mkOption {
type = lib.types.str;
};
namespace = lib.mkOption {
type = lib.types.str;
default = "default";
};
};
};
2024-10-28 15:05:06 +00:00
longhornPVOpts = {name, ...}: {
2024-09-07 10:35:02 +00:00
options = {
storage = lib.mkOption {
type = lib.types.str;
};
};
};
2024-10-28 15:05:06 +00:00
longhornPVCOpts = {name, ...}: {
2024-09-07 10:35:02 +00:00
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;
};
};
};
2024-10-28 15:05:06 +00:00
in {
2024-09-07 10:35:02 +00:00
options = {
lab.longhornVolumes = lib.mkOption {
type = with lib.types; attrsOf (submodule longhornVolumeOpts);
2024-10-28 15:05:06 +00:00
default = {};
2024-09-07 10:35:02 +00:00
};
lab.longhorn = {
persistentVolume = lib.mkOption {
type = with lib.types; attrsOf (submodule longhornPVOpts);
2024-10-28 15:05:06 +00:00
default = {};
2024-09-07 10:35:02 +00:00
};
persistentVolumeClaim = lib.mkOption {
type = with lib.types; attrsOf (submodule longhornPVCOpts);
2024-10-28 15:05:06 +00:00
default = {};
2024-09-07 10:35:02 +00:00
};
};
};
config = {
kubernetes.resources = {
2024-10-28 15:05:06 +00:00
persistentVolumes =
lib.mergeAttrs
2024-09-07 10:35:02 +00:00
(builtins.mapAttrs
(name: longhornVolume: {
spec = {
2024-10-28 15:05:06 +00:00
accessModes = ["ReadWriteOnce"];
2024-09-07 10:35:02 +00:00
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";
2024-10-28 15:05:06 +00:00
recurringJobSelector = lib.generators.toYAML {} [
{
name = "backup-nfs";
isGroup = false;
}
];
2024-09-07 10:35:02 +00:00
};
};
};
})
config.lab.longhornVolumes)
(builtins.mapAttrs
(name: longhornPV: {
spec = {
2024-10-28 15:05:06 +00:00
accessModes = ["ReadWriteOnce"];
2024-09-07 10:35:02 +00:00
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";
2024-10-28 15:05:06 +00:00
recurringJobSelector = lib.generators.toYAML {} [
{
name = "backup-nfs";
isGroup = false;
}
];
2024-09-07 10:35:02 +00:00
};
};
};
})
config.lab.longhorn.persistentVolume);
2024-10-28 15:05:06 +00:00
persistentVolumeClaims =
lib.mergeAttrs
2024-09-07 10:35:02 +00:00
(builtins.mapAttrs
(name: longhornVolume: {
spec = {
2024-10-28 15:05:06 +00:00
accessModes = ["ReadWriteOnce"];
2024-09-07 10:35:02 +00:00
resources.requests.storage = longhornVolume.storage;
storageClassName = "";
};
})
config.lab.longhornVolumes)
(builtins.mapAttrs
(name: longhornPVC: {
spec = {
2024-10-28 15:05:06 +00:00
accessModes = ["ReadWriteOnce"];
2024-09-07 10:35:02 +00:00
resources.requests.storage = longhornPVC.storage;
storageClassName = "";
volumeName = longhornPVC.volumeName;
};
})
config.lab.longhorn.persistentVolumeClaim);
};
};
}