2023-07-07 22:01:34 -04:00
|
|
|
{ lib }:
|
2022-04-02 12:40:35 -07:00
|
|
|
with lib; rec {
|
2020-01-15 10:42:29 +00:00
|
|
|
# TODO: refactor with mkOptionType
|
2023-07-07 22:01:34 -04:00
|
|
|
mkSecretOption = { description ? "", default ? { }, allowNull ? true }:
|
2022-04-02 12:40:35 -07:00
|
|
|
mkOption {
|
|
|
|
|
inherit description;
|
2023-07-07 22:01:34 -04:00
|
|
|
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
|
|
|
|
2023-07-07 22:01:34 -04: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 { };
|
2022-04-02 12:40:35 -07:00
|
|
|
};
|
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
|
2023-07-07 22:01:34 -04: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
|
2023-07-07 22:01:34 -04:00
|
|
|
mkHashedList = { items, labels ? { } }:
|
|
|
|
|
let
|
|
|
|
|
hash = builtins.hashString "sha1" (builtins.toJSON items);
|
2021-05-13 17:27:08 -04:00
|
|
|
|
2023-07-07 22:01:34 -04:00
|
|
|
labeledItems = map
|
|
|
|
|
(item:
|
|
|
|
|
recursiveUpdate item {
|
|
|
|
|
metadata.labels."kubenix/hash" = hash;
|
|
|
|
|
})
|
|
|
|
|
items;
|
|
|
|
|
in
|
2021-05-13 17:27:08 -04:00
|
|
|
mkList {
|
|
|
|
|
items = labeledItems;
|
2023-07-07 22:01:34 -04:00
|
|
|
labels = {
|
|
|
|
|
"kubenix/hash" = hash;
|
|
|
|
|
} // labels;
|
2021-05-13 17:27:08 -04:00
|
|
|
};
|
2020-01-14 18:35:01 +00:00
|
|
|
|
2023-09-26 05:04:32 +03:00
|
|
|
# Returns "<name>-<hash(data)>"
|
|
|
|
|
mkNameHash = { name, data, length ? 10 }:
|
|
|
|
|
"${name}-${builtins.substring 0 length (builtins.hashString "sha1" (builtins.toJSON data))}";
|
|
|
|
|
|
|
|
|
|
# Returns the same resources with addition of injected (or overwritten) metadata.name with hashed data
|
|
|
|
|
# name of the resource in Nix does not change for reference reasons
|
|
|
|
|
# useful for the ConfigMap and Secret resources
|
|
|
|
|
injectHashedNames = attrs:
|
|
|
|
|
lib.mapAttrs
|
|
|
|
|
(name: o:
|
|
|
|
|
recursiveUpdate o {
|
|
|
|
|
metadata.name = mkNameHash { inherit name; data = o.data; };
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
attrs;
|
|
|
|
|
|
|
|
|
|
|
2022-04-02 13:43:57 -07:00
|
|
|
inherit (lib) toBase64;
|
|
|
|
|
inherit (lib) octalToDecimal;
|
2019-03-07 18:02:26 +01:00
|
|
|
}
|