feat(k8s): add function for injecting names from hashed data objects (#33)

This commit is contained in:
Matej Cotman 2023-09-26 05:04:32 +03:00 committed by Bryton Hall
parent 71cb0a2a47
commit 473fb3ae50
3 changed files with 62 additions and 1 deletions

View file

@ -62,6 +62,23 @@ with lib; rec {
} // labels;
};
# 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;
inherit (lib) toBase64;
inherit (lib) octalToDecimal;
}