kubenix/lib/k8s.nix

62 lines
1.5 KiB
Nix
Raw Normal View History

2019-03-07 18:02:26 +01:00
{ lib }:
with lib;
rec {
# TODO: refactor into mkOptionType
mkSecretOption = {description ? "", default ? {}, allowNull ? true}: mkOption {
2019-03-07 18:02:26 +01:00
inherit description;
type = (if allowNull then types.nullOr else id) (types.submodule {
2019-03-07 18:02:26 +01:00
options = {
name = mkOption ({
2019-03-07 18:02:26 +01:00
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
key = mkOption ({
2019-03-07 18:02:26 +01:00
description = "Name of the key where secret is stored";
type = types.str;
} // (optionalAttrs (default ? "key") {
default = default.key;
}));
2019-03-07 18:02:26 +01:00
};
});
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
mkList = { items, labels ? {} }: {
kind = "List";
apiVersion = "v1";
inherit items labels;
};
# Creates hashed kubernetes list from a list of kubernetes objects
mkHashedList = { items, labels ? {} }: let
hash = builtins.hashString "sha1" (builtins.toJSON items);
labeledItems = map (item: recursiveUpdate item {
metadata.labels."kubenix/hash" = hash;
}) items;
in mkList {
items = labeledItems;
labels = {
"kubenix/hash" = hash;
} // labels;
};
toBase64 = lib.toBase64;
octalToDecimal = lib.octalToDecimal;
2019-03-07 18:02:26 +01:00
}