1
0
Fork 0
mirror of https://git.sr.ht/~goorzhel/turboprop synced 2024-12-14 11:37:37 +00:00
turboprop/lib/default.nix
Antonio Gurgel 27b06c5798 Implement lib.gatherNamespaces
The documentation for pkgs.lib.lists.unique warned me that the function
is O(n^2). So I conducted some rather unscientific tests using my own
Kubernetes flake and found no noticeable time penalty:
`rm result && nix-collect-garbage -d && nix build` always took
between 37 and 40 seconds, with or without gatherNamespaces.
But, well, n=10.

Also, make more obvious the flaw in mk.namespaces.
If I prefix something with "N.B." it shouldn't be hidden in the code.
2023-12-07 00:03:08 -08:00

95 lines
2.8 KiB
Nix

# TODO: I don't know yet how much of this I want to commit to the public API.
# So far, all of this is hidden from view.
{
kubelib,
pkgs,
}: let
parseYAMLsFile = p: kubelib.fromYAML (builtins.readFile p);
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/
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
# builtins.trace "${toString val} is a function"
(sum
// {
"${pathStr}" = val;
})
else if builtins.isAttrs val
then
# builtins.trace "${builtins.toJSON val} is an attrset"
(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;
}