1
0
Fork 0
mirror of https://git.sr.ht/~goorzhel/turboprop synced 2024-12-14 11:37:37 +00:00

Refactor modules

By declaring builders at the module level, only to call them in
flake.nix, I give myself the opportunity to inject `{name, namespace}`
there and need no longer pass these args into every module myself.
This commit is contained in:
Antonio Gurgel 2023-11-18 17:11:49 -08:00
parent 293fdfe41b
commit 11a3fcb002
12 changed files with 210 additions and 189 deletions

View file

@ -38,7 +38,7 @@
overlays = [devshell.overlays.default];
};
kubelib = nix-kube-generators.lib {inherit pkgs;};
lib = import ./lib {inherit kubelib pkgs;};
lib = import ./lib {inherit charts kubelib pkgs;};
downloadCharts = reponame: charts:
builtins.mapAttrs
@ -48,7 +48,13 @@
buildReleases = namespace: releases:
builtins.mapAttrs
(name: module: (module {inherit name namespace lib charts kubelib;}))
(name: module: (
let
release = module {inherit namespace kubelib lib;};
in
release.builder
(release.args // {inherit name namespace;})
))
releases;
releases = builtins.mapAttrs buildReleases self.releaseData;
in {

View file

@ -1,17 +1,20 @@
{vars}: {
mainImage = repository: leaf: tag: {
controllers.main.containers.main.image = {
repository = "${repository}/${leaf}";
inherit tag;
};
{
charts,
kubelib,
vars,
}: rec {
mkMainImage = repository: tag: {
# I get "tag" and "version" confused a lot, so
# it's better not to mind the attribute names.
inherit repository tag;
};
existingClaim = pvc: subPath: {
mkExistingClaim = pvc: subPath: {
existingClaim = pvc;
inherit subPath;
};
simpleHTTPRoute = namespace: name: port: {
mkSimpleHTTPRoute = namespace: name: port: {
service.main.ports.http.port = port;
route.main = {
enabled = true;
@ -28,4 +31,38 @@
];
};
};
appTemplate = {
namespace,
name,
mainImage,
nfsConfigVolume ? false,
httpPort ? false,
extraValues ? {},
}: let
configVolume = {
persistence.config =
if nfsConfigVolume
then mkExistingClaim "svc" name
else
# TODO: There' room here to chain other types, like
# if longhornVolume then {...} else ...
{};
};
httpRoute =
if httpPort != false
then mkSimpleHTTPRoute namespace name httpPort
else {};
in
kubelib.buildHelmChart {
inherit name namespace;
chart = charts.bjw-s.app-template;
values =
configVolume
// httpRoute
// extraValues
// {
controllers.main.containers.main.image = mainImage;
};
};
}

View file

@ -20,26 +20,6 @@ in {
installPhase = "cp -v $src $out";
});
gitChart = {
chartPath,
vPrefixInRef ? false,
...
} @ attrs:
genericBuilder (attrs
// {
src = pkgs.fetchgit {
inherit (attrs) url hash;
rev =
if vPrefixInRef
then "v${attrs.version}"
else attrs.version;
sparseCheckout = [chartPath];
};
installPhase = ''
cp -rv $src/${chartPath} $out
'';
});
buildYAMLStream = {
name,
namespace,

View file

@ -1,9 +1,38 @@
{
charts,
kubelib,
pkgs,
}: let
vars = import ./vars.nix;
in
(import ./app-template.nix {inherit vars;})
(import ./app-template.nix {inherit charts kubelib vars;})
// (import ./builders.nix {inherit pkgs;})
// vars
// {
gitChart = {
name,
version,
url,
chartPath,
vPrefixInRef ? false,
hash,
}:
pkgs.stdenv.mkDerivation {
pname = "${name}-${chartPath}";
inherit version;
src = pkgs.fetchgit {
inherit url hash;
rev =
if vPrefixInRef
then "v${version}"
else version;
sparseCheckout = [chartPath];
};
phases = ["installPhase"];
installPhase = ''
cp -rv $src/${chartPath} $out
'';
};
}

View file

@ -4,6 +4,8 @@ let
# e.g.: `dirToAttrs ./src "svc"` renders
# {name = "svc"; value = {"sota-slack-spotter" = <lambda>, ...};}
# which, combined with `attrNames`, becomes input to listToAttrs.
# TODO: nixpkgs has lib.attrsets.nameValuePair
dirToAttrs = root: dirName: {
name = dirName;
value = let

View file

@ -1,14 +1,9 @@
{
namespace,
name,
lib,
...
}: let
version = "0.8.1";
in
lib.remoteYAMLFile {
inherit namespace name version;
{lib, ...}: {
builder = lib.remoteYAMLFile;
args = rec {
version = "0.8.1";
url = "https://github.com/kubernetes-sigs/gateway-api/releases/download/v${version}/experimental-install.yaml";
hash = "sha256-rLLBaLkNKOqTy2sg6XcAzX122lgPRXYESq0kAeqsKa4=";
# hash = "sha256-bGAdzteHKpQNdvpmeuEmunGMtMbblw0Lq0kSjswRkqM="; # v1.0.0
}
};
}

View file

@ -1,21 +1,18 @@
{
namespace,
name,
kubelib,
lib,
...
}: let
version = "0.0.24";
in
kubelib.buildHelmChart {
inherit name namespace;
chart = lib.gitChart {
}: {
builder = kubelib.buildHelmChart;
args = {
chart = lib.gitChart rec {
# TODO: send PR to github:rancher/charts similar to #856
inherit namespace name version;
url = "https://github.com/rancher/local-path-provisioner";
chartPath = "deploy/chart/local-path-provisioner";
name = "local-path-provisioner";
url = "https://github.com/rancher/${name}";
chartPath = "deploy/chart/${name}";
vPrefixInRef = true;
version = "0.0.24";
hash = "sha256-TFATLWnpZ/mfe6qwBBpe4iDZSMzmhCbKmDmcKB/j640=";
};
@ -28,4 +25,5 @@ in
];
storageClass.defaultClass = false;
};
}
};
}

View file

@ -1,20 +1,7 @@
{
name,
namespace,
charts,
lib,
kubelib,
...
}: let
repoDomain = "quay.io/pussthecatorg";
version = "latest";
port = 10416;
in
kubelib.buildHelmChart {
inherit name namespace;
chart = charts.bjw-s.app-template;
values =
lib.mainImage repoDomain name version
// lib.simpleHTTPRoute namespace name port;
}
{lib, ...}: {
builder = lib.appTemplate;
args = {
mainImage = lib.mkMainImage "quay.io/pussthecatorg" "latest";
httpPort = 10416;
};
}

View file

@ -1,33 +1,21 @@
{
name,
namespace,
charts,
lib,
kubelib,
...
}: let
repoDomain = "linuxserver";
version = "0.6.19";
port = 8083;
in
kubelib.buildHelmChart {
inherit name namespace;
chart = charts.bjw-s.app-template;
{lib, ...}: {
builder = lib.appTemplate;
args = {
mainImage = lib.mkMainImage "linuxserver/calibre-web" "0.6.19";
nfsConfigVolume = true;
httpPort = 8083;
values =
{
controllers.main.type = "statefulset";
controllers.main.containers.main.env = {
PGID = "65534"; # nobody
PUID = lib.nfsUser;
TZ = lib.timeZone;
};
extraValues = {
controllers.main.type = "statefulset";
controllers.main.containers.main.env = {
PGID = "65534"; # nobody
PUID = lib.nfsUser;
TZ = lib.timeZone;
};
persistence = {
books = lib.existingClaim "media" "book/calibre";
config = lib.existingClaim "svc" name;
};
}
// lib.mainImage repoDomain "calibre-web" version
// lib.simpleHTTPRoute namespace name port;
}
persistence = {
books = lib.mkExistingClaim "media" "book/calibre";
};
};
};
}

View file

@ -1,7 +1,5 @@
{
name,
namespace,
kubelib,
lib,
...
}: let
@ -12,6 +10,12 @@
listener = name: protocol: port: hostname: {
inherit name protocol port hostname;
};
in rec {
builder = lib.buildYAMLStream;
args = {
inherit namespace version;
objs = [gateway httpToHttps];
};
gateway = {
inherit apiVersion;
@ -74,8 +78,4 @@
];
};
};
in
lib.buildYAMLStream {
inherit namespace name version;
objs = [gateway httpToHttps];
}
}

View file

@ -1,49 +1,55 @@
{
name,
namespace,
charts,
lib,
kubelib,
...
}: let
version = "10.8.11";
port = 8096;
mediaPath = dir: {
mediaPath = dirname: {
type = "hostPath";
hostPath = "/zpool/media/" + dir;
mountPath = "/mnt/" + dir;
readOnly = true;
};
in
kubelib.buildHelmChart {
inherit name namespace;
chart = charts.bjw-s.app-template;
values =
hostPath = "/zpool/media/" + dirname;
globalMounts = [
{
controllers.main.type = "statefulset";
controllers.main.containers.main = {
env.TZ = lib.timeZone;
probes = {
readiness.spec.initialDelaySeconds = 15;
startup.enabled = false;
};
};
defaultPodOptions = {
nodeSelector."kubernetes.io/hostname" = "losangeles";
securityContext = {
runAsUser = lib.nfsUser;
runAsGroup = lib.usersGroup;
};
};
service.main = {
type = "NodePort";
ports.http.nodePort = 30096;
};
path = "/mnt/" + dirname;
readOnly = true;
}
// lib.mainImage "ghcr.io/onedr0p" name version
// lib.simpleHTTPRoute namespace name port;
}
];
};
in {
builder = lib.appTemplate;
args = {
mainImage = lib.mkMainImage "ghcr.io/onedr0p/jellyfin" "10.8.11";
httpPort = 8096;
extraValues = {
controllers.main.type = "statefulset";
controllers.main.containers.main = {
env.TZ = lib.timeZone;
probes = {
readiness.spec.initialDelaySeconds = 15;
startup.enabled = false;
};
};
defaultPodOptions = {
nodeSelector."kubernetes.io/hostname" = "losangeles";
securityContext = {
runAsUser = lib.nfsUser;
runAsGroup = lib.usersGroup;
};
};
persistence = {
music = mediaPath "music";
images = mediaPath "img";
video = mediaPath "video";
};
service.main = {
type = "NodePort";
ports.http = {
port = 8096;
nodePort = 30096;
};
};
};
};
}

View file

@ -1,39 +1,32 @@
{
name,
namespace,
charts,
lib,
kubelib,
...
}: let
repoDomain = "registry.svc.eureka.lan";
version = "latest";
in
kubelib.buildHelmChart {
inherit name namespace;
chart = charts.bjw-s.app-template;
{lib, ...}: {
builder = lib.appTemplate;
args = {
mainImage =
lib.mkMainImage
"registry.svc.eureka.lan/sota-slack-spotter"
"latest";
values =
{
controllers.main.type = "statefulset";
controllers.main.containers.main = {
env.RUST_LOG = "debug,rustls=info,ureq=info,hyper=info";
envFrom = [{secretRef = {inherit name;};}];
probes = {
liveness.enabled = false;
readiness.enabled = false;
startup.enabled = false;
};
extraValues = {
controllers.main.type = "statefulset";
controllers.main.containers.main = {
env.RUST_LOG = "debug,rustls=info,ureq=info,hyper=info";
envFrom = [{secretRef.name = "sota-slack-spotter";}];
probes = {
liveness.enabled = false;
readiness.enabled = false;
startup.enabled = false;
};
};
service.main.enabled = false;
service.main.enabled = false;
persistence.cache = {
type = "persistentVolumeClaim";
storageClass = "longhorn";
size = "50Mi";
accessMode = "ReadWriteOnce";
};
}
// lib.mainImage repoDomain name version;
}
persistence.cache = {
type = "persistentVolumeClaim";
storageClass = "longhorn";
size = "50Mi";
accessMode = "ReadWriteOnce";
};
};
};
}