kubenix/lib/k8s/default.nix

86 lines
1.8 KiB
Nix
Raw Normal View History

2022-04-02 12:40:35 -07:00
{lib}:
with lib; rec {
2020-01-15 10:42:29 +00:00
# TODO: refactor with mkOptionType
2022-04-02 12:40:35 -07:00
mkSecretOption = {
description ? "",
default ? {},
allowNull ? true,
}:
mkOption {
inherit description;
type =
(
if allowNull
then types.nullOr
else id
) (types.submodule {
options = {
name = mkOption ({
description = "Name of the secret where secret is stored";
type = types.str;
default = default.name;
}
// (optionalAttrs (default ? "name") {
default = default.name;
}));
2019-03-07 18:02:26 +01:00
2022-04-02 12:40:35 -07:00
key = mkOption ({
description = "Name of the key where secret is stored";
type = types.str;
}
// (optionalAttrs (default ? "key") {
default = default.key;
}));
};
});
default =
if default == null
then null
else {};
};
2019-03-07 18:02:26 +01:00
secretToEnv = value: {
valueFrom.secretKeyRef = {
inherit (value) name key;
};
};
# Creates kubernetes list from a list of kubernetes objects
2022-04-02 12:40:35 -07:00
mkList = {
items,
labels ? {},
}: {
2019-03-07 18:02:26 +01:00
kind = "List";
apiVersion = "v1";
inherit items labels;
};
# Creates hashed kubernetes list from a list of kubernetes objects
2022-04-02 12:40:35 -07:00
mkHashedList = {
items,
labels ? {},
}: let
hash = builtins.hashString "sha1" (builtins.toJSON items);
2021-05-13 17:27:08 -04:00
2022-04-02 12:40:35 -07:00
labeledItems =
map
(item:
recursiveUpdate item {
2021-05-13 17:27:08 -04:00
metadata.labels."kubenix/hash" = hash;
})
2022-04-02 12:40:35 -07:00
items;
in
2021-05-13 17:27:08 -04:00
mkList {
items = labeledItems;
2022-04-02 12:40:35 -07:00
labels =
{
"kubenix/hash" = hash;
}
// labels;
2021-05-13 17:27:08 -04:00
};
2022-04-02 13:43:57 -07:00
inherit (lib) toBase64;
inherit (lib) octalToDecimal;
2019-03-07 18:02:26 +01:00
}