Skip to content

Commit

Permalink
Use standalone-strict version of schemas
Browse files Browse the repository at this point in the history
The kubernetes-json-schema provides nice variants of the JSON schema
set, one being the `standalone` variant, which avoid external references
(at least to remote URIs). We use this source instead, which allows to
get rid of the previously sed-based URL rewriting step, which was
fragile.
  • Loading branch information
yannham committed Apr 18, 2024
1 parent d5a18ea commit 3fe964e
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
hash = "sha256-Vd3iHeB7LvFDNu+m/RE2P6zgRhNJemiE+j1AAuc6swo=";
};

k8sSchemas = { version ? latestK8sVersion }: k8sSchemasRepo + "/${version}";
k8sSchemas = { version ? latestK8sVersion }: k8sSchemasRepo + "/${version}-standalone-strict";

# K8s JSON schemas of a specific K8s version taken from
# `kubernetes-json-schema` (as a derivation)
Expand All @@ -35,7 +35,10 @@

# Generate Nickel contracts from K8s JSON schemas using
# `json-schema-to-nickel`.
k8sContracts = { version ? latestK8sVersion }: pkgs.stdenv.mkDerivation {
#
# Pass `keepJsonArtifacts = true` to keep the JSON artifacts (original
# and processed JSON schemas) for debugging purposes.
k8sContracts = { version ? latestK8sVersion, keepJsonArtifacts ? false }: pkgs.stdenv.mkDerivation {
name = "k8s-contracts-${version}";
src = k8sSchemas { inherit version; };
phases = [ "buildPhase" "installPhase" ];
Expand All @@ -47,26 +50,21 @@

buildPhase = ''
mkdir -p contracts
for filename in "${k8sSchemas {inherit version;}}"/*.json; do
basename="$(basename "$filename" .json)"
urlRewritten="$basename-url-rewritten.json"
bundled="$basename-bundled.json"
# Rewrite ref URLs to local file accesses, which we can do because
# all references are actually local and pointing to the
# kubernetes-json-schema repository, which is already in the store
sed 's%"\$ref": "https://raw.githubusercontent.com/yannh/kubernetes-json-schema/master%"$ref": "${k8sSchemasRepo}%g' "$filename" > "$urlRewritten"
json-schema-bundler "$urlRewritten" > "$bundled"
json-schema-bundler "$filename" > "$bundled"
json-schema-to-nickel "$bundled" > ./contracts/"$basename".ncl
done
'';

installPhase = ''
mkdir -p $out
mv ./*.json $out/
mv ./contracts $out
'';
'' ++
nixpkgs.lib.optionalString keepJsonArtifacts "mv ./*.json $out/";
};

json-schema-bundler = (pkgs.callPackage ./json-schema-bundler { }).package;
Expand Down

0 comments on commit 3fe964e

Please sign in to comment.