2019-02-28 13:17:40 +01:00
|
|
|
{ stdenvNoCC, lib, kubernetes-helm, gawk, remarshal, jq }:
|
|
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
# chart to template
|
|
|
|
|
chart
|
|
|
|
|
|
|
|
|
|
# release name
|
|
|
|
|
, name
|
|
|
|
|
|
|
|
|
|
# namespace to install release into
|
|
|
|
|
, namespace ? null
|
|
|
|
|
|
|
|
|
|
# values to pass to chart
|
2021-05-13 17:27:08 -04:00
|
|
|
, values ? { }
|
2019-02-28 13:17:40 +01:00
|
|
|
|
|
|
|
|
# kubernetes version to template chart for
|
2021-05-13 17:27:08 -04:00
|
|
|
, kubeVersion ? null
|
|
|
|
|
}:
|
|
|
|
|
let
|
2019-02-28 13:17:40 +01:00
|
|
|
valuesJsonFile = builtins.toFile "${name}-values.json" (builtins.toJSON values);
|
2021-05-13 17:27:08 -04:00
|
|
|
in
|
|
|
|
|
stdenvNoCC.mkDerivation {
|
2019-02-28 13:17:40 +01:00
|
|
|
name = "${name}.json";
|
|
|
|
|
buildCommand = ''
|
|
|
|
|
# template helm file and write resources to yaml
|
2020-01-14 19:09:39 +00:00
|
|
|
helm template "${name}" \
|
|
|
|
|
${optionalString (kubeVersion != null) "--api-versions ${kubeVersion}"} \
|
2019-02-28 13:17:40 +01:00
|
|
|
${optionalString (namespace != null) "--namespace ${namespace}"} \
|
2021-05-13 17:27:08 -04:00
|
|
|
${optionalString (values != { }) "-f ${valuesJsonFile}"} \
|
2019-02-28 13:17:40 +01:00
|
|
|
${chart} >resources.yaml
|
|
|
|
|
|
|
|
|
|
# split multy yaml file into multiple files
|
|
|
|
|
awk 'BEGIN{i=1}{line[i++]=$0}END{j=1;n=0; while (j<i) {if (line[j] ~ /^---/) n++; else print line[j] >>"resource-"n".yaml"; j++}}' resources.yaml
|
|
|
|
|
|
|
|
|
|
# join multiple yaml files in jsonl file
|
|
|
|
|
for file in ./resource-*.yaml
|
|
|
|
|
do
|
|
|
|
|
remarshal -i $file -if yaml -of json >>resources.jsonl
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
# convert jsonl file to json array, remove null values and write to $out
|
|
|
|
|
cat resources.jsonl | jq -Scs 'walk(
|
|
|
|
|
if type == "object" then
|
|
|
|
|
with_entries(select(.value != null))
|
|
|
|
|
elif type == "array" then
|
|
|
|
|
map(select(. != null))
|
|
|
|
|
else
|
|
|
|
|
.
|
|
|
|
|
end)' > $out
|
|
|
|
|
'';
|
|
|
|
|
nativeBuildInputs = [ kubernetes-helm gawk remarshal jq ];
|
|
|
|
|
}
|