1
0
Fork 0
mirror of https://git.sr.ht/~goorzhel/turboprop synced 2024-12-15 17:50:52 +00:00
turboprop/lib/default.nix
2024-02-17 01:22:29 -08:00

94 lines
2.7 KiB
Nix

{
kubelib,
pkgs,
}: let
parseYAMLsFile = p: kubelib.fromYAML (builtins.readFile p);
# This avoids making the output attrset recursive, which
# I avoid in large files.
builders = import ./builders.nix {inherit kubelib pkgs;};
fetchers = import ./fetchers.nix {inherit kubelib pkgs;};
in {
app-template = import ./app-template {inherit builders fetchers pkgs;};
inherit builders fetchers;
parseYAMLFile = p: builtins.head (parseYAMLsFile p);
inherit parseYAMLsFile;
liftDefault = _: mod:
# haumea.lib.transformers.liftDefault uses pkgs.lib.attrsets.unionOfDisjoint,
# which fails in mkServices because mod.default is a function, not a set.
if (mod ? default && builtins.isFunction mod.default)
then mod.default
else mod;
# Helm cannot see my cluster from within the sandbox, so it cannot
# infer capabilities from it. Therefore, API versions must be gathered
# from charts that provide them.
# https://helm.sh/docs/chart_template_guide/builtin_objects/
# Output looks like ["cert-manager.io/v1/Certificate" ...]
gatherApis = yamlPath: let
crds =
builtins.filter
(obj: obj.kind == "CustomResourceDefinition")
(parseYAMLsFile yamlPath);
filterActive =
builtins.filter
(crdVersion: (! crdVersion ? "deprecated") || crdVersion.deprecated != true);
in
pkgs.lib.lists.flatten
(map
(
crd:
map
(
crdVersion: let
group = crd.spec.group;
version = crdVersion.name;
kind = crd.spec.names.kind;
in "${group}/${version}/${kind}"
)
(filterActive crd.spec.versions)
)
crds);
gatherNamespaces = yamlPath:
builtins.filter
builtins.isString
(
builtins.foldl'
(acc: obj: acc ++ [(obj.metadata.namespace or null)])
[]
(parseYAMLsFile yamlPath)
);
# https://github.com/divnix/digga/blob/baa54f8/src/importers.nix#L2-L59
# Borrowed from divnix/digga, with minor modifications:
# - Slash separators instead of periods, because `output.sh`
# will use these values as paths to built the derivation.
# - Look for functions (half-built service modules), not paths.
flattenTree = tree: let
op = sum: path: val: let
pathStr = builtins.concatStringsSep "/" path;
in
if builtins.isFunction val
then
(sum
// {
"${pathStr}" = val;
})
else if builtins.isAttrs val
then
(recurse sum path val)
else
# builtins.trace "${toString path} is something else"
sum; # ignore val
recurse = sum: path: val:
builtins.foldl'
(sum: key: op sum (path ++ [key]) val.${key})
sum
(builtins.attrNames val);
in
recurse {} [] tree;
}