mirror of
https://github.com/TECHNOFAB11/kubenix.git
synced 2026-02-02 09:25:10 +01:00
feat(k8s): add function for injecting names from hashed data objects (#33)
This commit is contained in:
parent
71cb0a2a47
commit
473fb3ae50
3 changed files with 62 additions and 1 deletions
|
|
@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||||
|
|
||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
|
|
||||||
|
### Added
|
||||||
|
|
||||||
|
- add `optionalHashedNames` to inject hashed names for referencing inside modules
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
|
||||||
- removed local `kubectl` and `kubernetes` packages in lieu of those from nixpkgs
|
- removed local `kubectl` and `kubernetes` packages in lieu of those from nixpkgs
|
||||||
|
|
|
||||||
|
|
@ -62,6 +62,23 @@ with lib; rec {
|
||||||
} // labels;
|
} // 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) toBase64;
|
||||||
inherit (lib) octalToDecimal;
|
inherit (lib) octalToDecimal;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -257,6 +257,39 @@ with lib; let
|
||||||
(types.listOf (types.submodule submodule))
|
(types.listOf (types.submodule submodule))
|
||||||
(mergeValuesByFn keyFn)
|
(mergeValuesByFn keyFn)
|
||||||
(types.attrsOf (types.submodule submodule));
|
(types.attrsOf (types.submodule submodule));
|
||||||
|
|
||||||
|
# inject hashed names for referencing inside modules, example:
|
||||||
|
# pod = {
|
||||||
|
# containers.nginx = {
|
||||||
|
# image = "nginx:1.25.1";
|
||||||
|
# volumeMounts = {
|
||||||
|
# "/etc/nginx".name = "config";
|
||||||
|
# "/var/lib/html".name = "static";
|
||||||
|
# };
|
||||||
|
# };
|
||||||
|
# volumes = {
|
||||||
|
# config.configMap.name = config.kubernetes.resources.configMaps.nginx-config.metadata.name;
|
||||||
|
# static.configMap.name = config.kubernetes.resources.configMaps.nginx-static.metadata.name;
|
||||||
|
# };
|
||||||
|
# };
|
||||||
|
optionalHashedNames = object:
|
||||||
|
if cfg.enableHashedNames then
|
||||||
|
recursiveUpdate object
|
||||||
|
(mapAttrs
|
||||||
|
(ks: v:
|
||||||
|
if builtins.elem ks [ "configMaps" "secrets" ] then
|
||||||
|
k8s.injectHashedNames v
|
||||||
|
else
|
||||||
|
v
|
||||||
|
)
|
||||||
|
object)
|
||||||
|
else object;
|
||||||
|
|
||||||
|
# inject hashed names in the output
|
||||||
|
optionalHashedNames' = object: kind:
|
||||||
|
if cfg.enableHashedNames && elem kind [ "ConfigMap" "Secret" ] then
|
||||||
|
k8s.injectHashedNames object
|
||||||
|
else object;
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
imports = [ ./base.nix ];
|
imports = [ ./base.nix ];
|
||||||
|
|
@ -319,6 +352,7 @@ in
|
||||||
description = "Alias for `config.kubernetes.api.resources` options";
|
description = "Alias for `config.kubernetes.api.resources` options";
|
||||||
default = { };
|
default = { };
|
||||||
type = types.attrsOf types.attrs;
|
type = types.attrsOf types.attrs;
|
||||||
|
apply = optionalHashedNames;
|
||||||
};
|
};
|
||||||
|
|
||||||
customTypes = mkOption {
|
customTypes = mkOption {
|
||||||
|
|
@ -411,6 +445,12 @@ in
|
||||||
description = "Genrated kubernetes YAML file";
|
description = "Genrated kubernetes YAML file";
|
||||||
type = types.package;
|
type = types.package;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
enableHashedNames = mkOption {
|
||||||
|
description = "Enable hashing of resource (ConfigMap,Secret) names";
|
||||||
|
type = types.bool;
|
||||||
|
default = false;
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
config = {
|
config = {
|
||||||
|
|
@ -497,7 +537,7 @@ in
|
||||||
kubernetes.objects = flatten (mapAttrsToList
|
kubernetes.objects = flatten (mapAttrsToList
|
||||||
(_: type:
|
(_: type:
|
||||||
mapAttrsToList (_name: moduleToAttrs)
|
mapAttrsToList (_name: moduleToAttrs)
|
||||||
cfg.api.resources.${type.group}.${type.version}.${type.kind}
|
(optionalHashedNames' cfg.api.resources.${type.group}.${type.version}.${type.kind} type.kind)
|
||||||
)
|
)
|
||||||
cfg.api.types);
|
cfg.api.types);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue