2019-03-07 23:23:07 +01:00
|
|
|
# helm defines kubenix module with options for using helm charts
|
|
|
|
|
# with kubenix
|
2023-07-07 22:01:34 -04:00
|
|
|
{ config, lib, pkgs, helm, ... }:
|
2022-04-02 12:40:35 -07:00
|
|
|
with lib; let
|
2019-02-28 13:17:40 +01:00
|
|
|
cfg = config.kubernetes.helm;
|
|
|
|
|
|
|
|
|
|
globalConfig = config;
|
|
|
|
|
|
|
|
|
|
recursiveAttrs = mkOptionType {
|
|
|
|
|
name = "recursive-attrs";
|
|
|
|
|
description = "recursive attribute set";
|
|
|
|
|
check = isAttrs;
|
2023-07-07 22:01:34 -04:00
|
|
|
merge = _loc: foldl' (res: def: recursiveUpdate res def.value) { };
|
2019-02-28 13:17:40 +01:00
|
|
|
};
|
|
|
|
|
|
2023-07-07 22:01:34 -04:00
|
|
|
parseApiVersion = apiVersion:
|
|
|
|
|
let
|
|
|
|
|
splitted = splitString "/" apiVersion;
|
|
|
|
|
in
|
|
|
|
|
{
|
|
|
|
|
group =
|
|
|
|
|
if length splitted == 1
|
|
|
|
|
then "core"
|
|
|
|
|
else head splitted;
|
|
|
|
|
version = last splitted;
|
|
|
|
|
};
|
|
|
|
|
in
|
|
|
|
|
{
|
|
|
|
|
imports = [ ./k8s.nix ];
|
2019-03-07 23:23:07 +01:00
|
|
|
|
2019-02-28 13:17:40 +01:00
|
|
|
options.kubernetes.helm = {
|
2022-08-15 09:46:23 -04:00
|
|
|
releases = mkOption {
|
|
|
|
|
description = "Attribute set of helm releases";
|
2023-07-07 22:01:34 -04:00
|
|
|
type = types.attrsOf (types.submodule ({ config, name, ... }: {
|
2019-02-28 13:17:40 +01:00
|
|
|
options = {
|
|
|
|
|
name = mkOption {
|
|
|
|
|
description = "Helm release name";
|
|
|
|
|
type = types.str;
|
|
|
|
|
default = name;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
chart = mkOption {
|
|
|
|
|
description = "Helm chart to use";
|
|
|
|
|
type = types.package;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
namespace = mkOption {
|
|
|
|
|
description = "Namespace to install helm chart to";
|
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
|
default = null;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
values = mkOption {
|
|
|
|
|
description = "Values to pass to chart";
|
|
|
|
|
type = recursiveAttrs;
|
2023-07-07 22:01:34 -04:00
|
|
|
default = { };
|
2019-02-28 13:17:40 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
kubeVersion = mkOption {
|
|
|
|
|
description = "Kubernetes version to build chart for";
|
|
|
|
|
type = types.str;
|
|
|
|
|
default = globalConfig.kubernetes.version;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
overrides = mkOption {
|
|
|
|
|
description = "Overrides to apply to all chart resources";
|
|
|
|
|
type = types.listOf types.unspecified;
|
2023-07-07 22:01:34 -04:00
|
|
|
default = [ ];
|
2019-02-28 13:17:40 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
overrideNamespace = mkOption {
|
|
|
|
|
description = "Whether to apply namespace override";
|
|
|
|
|
type = types.bool;
|
|
|
|
|
default = true;
|
|
|
|
|
};
|
|
|
|
|
|
2023-02-28 02:22:59 +01:00
|
|
|
includeCRDs = mkOption {
|
2023-06-03 03:11:07 -04:00
|
|
|
description = ''
|
2023-02-28 02:22:59 +01:00
|
|
|
Whether to include CRDs.
|
|
|
|
|
|
|
|
|
|
Warning: Always including CRDs here is dangerous and can break CRs in your cluster as CRDs may be updated unintentionally.
|
|
|
|
|
An interactive `helm install` NEVER updates CRDs, only installs them when they are not existing.
|
|
|
|
|
See https://github.com/helm/community/blob/aa8e13054d91ee69857b13149a9652be09133a61/hips/hip-0011.md
|
2023-06-03 03:11:07 -04:00
|
|
|
|
2023-02-28 02:22:59 +01:00
|
|
|
Only set this to true if you know what you are doing and are manually checking the included CRDs for breaking changes whenever updating the Helm chart.
|
|
|
|
|
'';
|
|
|
|
|
type = types.bool;
|
|
|
|
|
default = false;
|
|
|
|
|
};
|
|
|
|
|
|
2023-06-12 02:41:00 -04:00
|
|
|
noHooks = mkOption {
|
|
|
|
|
description = ''
|
|
|
|
|
Wether to include Helm hooks.
|
|
|
|
|
|
|
|
|
|
Without this all hooks run immediately on apply since we are bypassing the Helm CLI.
|
|
|
|
|
However, some charts only have minor validation hooks (e.g., upgrade version skew validation) and are safe to ignore.
|
|
|
|
|
'';
|
|
|
|
|
type = types.bool;
|
|
|
|
|
default = false;
|
|
|
|
|
};
|
|
|
|
|
|
2023-10-18 17:45:20 +02:00
|
|
|
apiVersions = mkOption {
|
|
|
|
|
description = ''
|
|
|
|
|
Inform Helm about which CRDs are available in the cluster (`--api-versions` option).
|
|
|
|
|
This is useful for charts which contain `.Capabilities.APIVersions.Has` checks.
|
|
|
|
|
If you use `kubernetes.customTypes` to make kubenix aware of CRDs, it will include those as well by default.
|
|
|
|
|
'';
|
|
|
|
|
type = types.listOf types.str;
|
2023-11-10 03:02:55 +01:00
|
|
|
default = builtins.concatMap
|
|
|
|
|
(customType:
|
|
|
|
|
[
|
|
|
|
|
"${customType.group}/${customType.version}"
|
|
|
|
|
"${customType.group}/${customType.version}/${customType.kind}"
|
|
|
|
|
])
|
2023-10-18 17:45:20 +02:00
|
|
|
(builtins.attrValues globalConfig.kubernetes.customTypes);
|
|
|
|
|
};
|
|
|
|
|
|
2019-02-28 13:17:40 +01:00
|
|
|
objects = mkOption {
|
|
|
|
|
description = "Generated kubernetes objects";
|
|
|
|
|
type = types.listOf types.attrs;
|
2023-07-07 22:01:34 -04:00
|
|
|
default = [ ];
|
2019-02-28 13:17:40 +01:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2023-07-07 22:01:34 -04:00
|
|
|
config.overrides = mkIf (config.overrideNamespace && config.namespace != null) [{
|
|
|
|
|
metadata.namespace = config.namespace;
|
|
|
|
|
}];
|
2019-02-28 13:17:40 +01:00
|
|
|
|
2019-03-07 23:23:07 +01:00
|
|
|
config.objects = importJSON (helm.chart2json {
|
2023-10-18 17:45:20 +02:00
|
|
|
inherit (config) chart name namespace values kubeVersion includeCRDs noHooks apiVersions;
|
2019-02-28 13:17:40 +01:00
|
|
|
});
|
|
|
|
|
}));
|
2023-07-07 22:01:34 -04:00
|
|
|
default = { };
|
2019-02-28 13:17:40 +01:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2019-03-12 20:33:56 +01:00
|
|
|
config = {
|
|
|
|
|
# expose helm helper methods as module argument
|
2023-07-07 22:01:34 -04:00
|
|
|
_module.args.helm = import ../lib/helm { inherit pkgs; };
|
2019-03-12 20:33:56 +01:00
|
|
|
|
2021-05-13 17:27:08 -04:00
|
|
|
kubernetes.api.resources = mkMerge (flatten (mapAttrsToList
|
2023-07-07 22:01:34 -04:00
|
|
|
(_: release: map
|
|
|
|
|
(object:
|
|
|
|
|
let
|
2022-04-02 12:40:35 -07:00
|
|
|
apiVersion = parseApiVersion object.apiVersion;
|
2022-04-02 13:43:57 -07:00
|
|
|
inherit (object.metadata) name;
|
2023-07-07 22:01:34 -04:00
|
|
|
in
|
|
|
|
|
{
|
2022-04-02 12:40:35 -07:00
|
|
|
"${apiVersion.group}"."${apiVersion.version}".${object.kind}."${name}" = mkMerge ([
|
2023-07-07 22:01:34 -04:00
|
|
|
object
|
|
|
|
|
]
|
|
|
|
|
++ release.overrides);
|
2022-04-02 12:40:35 -07:00
|
|
|
})
|
2023-07-07 22:01:34 -04:00
|
|
|
release.objects
|
2021-05-13 17:27:08 -04:00
|
|
|
)
|
2022-08-15 09:46:23 -04:00
|
|
|
cfg.releases));
|
2019-03-12 20:33:56 +01:00
|
|
|
};
|
2019-02-28 13:17:40 +01:00
|
|
|
}
|