Replace remarshal with yq (#26)

Remarshal doesn’t seem to be maintained anymore.

It is also affected by upstream bugs like https://github.com/yaml/pyyaml/issues/89 which affects e.g. prometheus-operator CRDs.
This commit is contained in:
Felix Scheinost 2023-06-19 18:14:20 +02:00 committed by GitHub
parent 0456c92538
commit e77a3898de
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 36 deletions

View file

@ -1,10 +1,8 @@
{
stdenvNoCC,
runCommand,
lib,
kubernetes-helm,
gawk,
remarshal,
jq,
yq,
}:
with lib;
{
@ -24,37 +22,25 @@ with lib;
noHooks ? false,
}: let
valuesJsonFile = builtins.toFile "${name}-values.json" (builtins.toJSON values);
in
stdenvNoCC.mkDerivation {
name = "${name}.json";
buildCommand = ''
# template helm file and write resources to yaml
helm template "${name}" \
# The `helm template` and YAML -> JSON steps are separate `runCommand` derivations for easier debuggability
resourcesYaml = runCommand "${name}.yaml" {nativeBuildInputs = [kubernetes-helm];} ''
helm template "${name}" \
${optionalString (kubeVersion != null) "--kube-version ${kubeVersion}"} \
${optionalString (namespace != null) "--namespace ${namespace}"} \
${optionalString (values != {}) "-f ${valuesJsonFile}"} \
${optionalString includeCRDs "--include-crds"} \
${optionalString noHooks "--no-hooks"} \
${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];
}
${chart} >$out
'';
in
runCommand "${name}.json" {} ''
# Remove null values
${yq}/bin/yq -Scs 'walk(
if type == "object" then
with_entries(select(.value != null))
elif type == "array" then
map(select(. != null))
else
.
end)' ${resourcesYaml} >$out
''