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

View file

@ -6,19 +6,19 @@ with lib; let
self = { self = {
importYAML = path: importYAML = path:
importJSON (pkgs.runCommand "yaml-to-json" {} '' importJSON (pkgs.runCommand "yaml-to-json" {} ''
${pkgs.remarshal}/bin/remarshal -i ${path} -if yaml -of json > $out ${pkgs.yq}/bin/yq -c . ${path} > $out
''); '');
toYAML = config: toYAML = config:
builtins.readFile (pkgs.runCommand "to-yaml" {} '' builtins.readFile (pkgs.runCommand "to-yaml" {} ''
${pkgs.remarshal}/bin/remarshal -i ${pkgs.writeText "to-json" (builtins.toJSON config)} -if json -of yaml > $out ${pkgs.yq}/bin/yq -y . ${pkgs.writeText "to-json" (builtins.toJSON config)} > $out
''); '');
toMultiDocumentYaml = name: documents: toMultiDocumentYaml = name: documents:
pkgs.runCommand name {} pkgs.runCommand name {}
(concatMapStringsSep "\necho --- >> $out\n" (concatMapStringsSep "\necho --- >> $out\n"
( (
d: "${pkgs.remarshal}/bin/remarshal -i ${builtins.toFile "doc" (builtins.toJSON d)} -if json -of yaml >> $out" d: "${pkgs.yq}/bin/yq -y . ${pkgs.writeText "to-json" (builtins.toJSON config)} >> $out"
) )
documents); documents);