photoncloud-monorepo/nix/modules/fiberlb.nix

141 lines
4.2 KiB
Nix

{ config, lib, pkgs, ... }:
let
cfg = config.services.fiberlb;
tomlFormat = pkgs.formats.toml { };
fiberlbConfigFile = tomlFormat.generate "fiberlb.toml" {
grpc_addr = "0.0.0.0:${toString cfg.port}";
log_level = "info";
auth = {
iam_server_addr =
if cfg.iamAddr != null
then cfg.iamAddr
else "127.0.0.1:50080";
};
};
in
{
options.services.fiberlb = {
enable = lib.mkEnableOption "fiberlb service";
port = lib.mkOption {
type = lib.types.port;
default = 50085;
description = "Port for fiberlb gRPC management API";
};
iamAddr = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
description = "IAM service endpoint address (host:port)";
example = "10.0.0.1:50080";
};
chainfireAddr = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
description = "ChainFire endpoint address (host:port) for cluster coordination only";
example = "10.0.0.1:2379";
};
flaredbAddr = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
description = "FlareDB endpoint address (host:port) for metadata/user data";
example = "10.0.0.1:2479";
};
metadataBackend = lib.mkOption {
type = lib.types.enum [ "flaredb" "postgres" "sqlite" ];
default = "flaredb";
description = "Metadata backend for FiberLB.";
};
databaseUrl = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default = null;
description = "SQL database URL for metadata (required when metadataBackend is postgres/sqlite).";
example = "postgres://fiberlb:secret@10.0.0.10:5432/fiberlb";
};
singleNode = lib.mkOption {
type = lib.types.bool;
default = false;
description = "Enable single-node mode (required when metadata backend is SQLite)";
};
dataDir = lib.mkOption {
type = lib.types.path;
default = "/var/lib/fiberlb";
description = "Data directory for fiberlb";
};
settings = lib.mkOption {
type = lib.types.attrs;
default = {};
description = "Additional configuration settings";
};
package = lib.mkOption {
type = lib.types.package;
default = pkgs.fiberlb-server or (throw "fiberlb-server package not found");
description = "Package to use for fiberlb";
};
};
config = lib.mkIf cfg.enable {
# Create system user
users.users.fiberlb = {
isSystemUser = true;
group = "fiberlb";
description = "FiberLB service user";
home = cfg.dataDir;
};
users.groups.fiberlb = {};
# Create systemd service
systemd.services.fiberlb = {
description = "FiberLB Load Balancing Service";
wantedBy = [ "multi-user.target" ];
after = [ "network.target" "iam.service" "flaredb.service" ];
requires = [ "iam.service" "flaredb.service" ];
serviceConfig = {
Type = "simple";
User = "fiberlb";
Group = "fiberlb";
Restart = "on-failure";
RestartSec = "10s";
# State directory management
StateDirectory = "fiberlb";
StateDirectoryMode = "0750";
# Security hardening
NoNewPrivileges = true;
PrivateTmp = true;
ProtectSystem = "strict";
ProtectHome = true;
ReadWritePaths = [ cfg.dataDir ];
# Environment variables for service endpoints
Environment = [
"RUST_LOG=info"
"FIBERLB_FLAREDB_ENDPOINT=${if cfg.flaredbAddr != null then cfg.flaredbAddr else "127.0.0.1:2479"}"
"FIBERLB_METADATA_BACKEND=${cfg.metadataBackend}"
] ++ lib.optional (cfg.databaseUrl != null) "FIBERLB_METADATA_DATABASE_URL=${cfg.databaseUrl}"
++ lib.optional cfg.singleNode "FIBERLB_SINGLE_NODE=1"
++ lib.optional (cfg.chainfireAddr != null) "FIBERLB_CHAINFIRE_ENDPOINT=http://${cfg.chainfireAddr}";
# Start command
ExecStart = lib.concatStringsSep " " ([
"${cfg.package}/bin/fiberlb"
"--config ${fiberlbConfigFile}"
"--grpc-addr 0.0.0.0:${toString cfg.port}"
"--flaredb-endpoint ${if cfg.flaredbAddr != null then cfg.flaredbAddr else "127.0.0.1:2479"}"
]);
};
};
};
}