This commit is contained in:
GTrunSec 2022-04-02 12:40:35 -07:00
parent a0ce293db8
commit 60592d3096
No known key found for this signature in database
GPG key ID: 2368FAFA4ABDD2A0
55 changed files with 23668 additions and 30925 deletions

View file

@ -1,7 +1,8 @@
{ lib, pkgs }:
{
k8s = import ./k8s { inherit lib; };
docker = import ./docker { inherit lib pkgs; };
helm = import ./helm { inherit pkgs; };
lib,
pkgs,
}: {
k8s = import ./k8s {inherit lib;};
docker = import ./docker {inherit lib pkgs;};
helm = import ./helm {inherit pkgs;};
}

View file

@ -1,9 +1,13 @@
{ lib, pkgs }:
with lib;
{
copyDockerImages = { images, dest, args ? "" }:
lib,
pkgs,
}:
with lib; {
copyDockerImages = {
images,
dest,
args ? "",
}:
pkgs.writeScript "copy-docker-images.sh" (concatMapStrings
(image: ''
#!${pkgs.runtimeShell}

View file

@ -1,54 +1,54 @@
{ 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
, values ? { }
# kubernetes version to template chart for
, kubeVersion ? null
stdenvNoCC,
lib,
kubernetes-helm,
gawk,
remarshal,
jq,
}:
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}" \
${optionalString (kubeVersion != null) "--api-versions ${kubeVersion}"} \
${optionalString (namespace != null) "--namespace ${namespace}"} \
${optionalString (values != { }) "-f ${valuesJsonFile}"} \
${chart} >resources.yaml
with lib;
{
# chart to template
chart,
# release name
name,
# namespace to install release into
namespace ? null,
# values to pass to chart
values ? {},
# kubernetes version to template chart for
kubeVersion ? null,
}: 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}" \
${optionalString (kubeVersion != null) "--api-versions ${kubeVersion}"} \
${optionalString (namespace != null) "--namespace ${namespace}"} \
${optionalString (values != {}) "-f ${valuesJsonFile}"} \
${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
# 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
# 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 ];
}
# 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];
}

View file

@ -1,6 +1,4 @@
{ pkgs }:
{
chart2json = pkgs.callPackage ./chart2json.nix { };
fetch = pkgs.callPackage ./fetchhelm.nix { };
{pkgs}: {
chart2json = pkgs.callPackage ./chart2json.nix {};
fetch = pkgs.callPackage ./fetchhelm.nix {};
}

View file

@ -1,50 +1,80 @@
{ stdenvNoCC, lib, kubernetes-helm, cacert }:
let
cleanName = name: lib.replaceStrings [ "/" ] [ "-" ] name;
in
{
# name of the chart
chart
stdenvNoCC,
lib,
kubernetes-helm,
cacert,
}: let
cleanName = name: lib.replaceStrings ["/"] ["-"] name;
in
{
# name of the chart
chart,
# chart url to fetch from custom location
chartUrl ? null,
# version of the chart
version ? null,
# chart hash
sha256,
# whether to extract chart
untar ? true,
# use custom charts repo
repo ? null,
# pass --verify to helm chart
verify ? false,
# pass --devel to helm chart
devel ? false,
}:
stdenvNoCC.mkDerivation {
name = "${cleanName chart}-${
if version == null
then "dev"
else version
}";
# chart url to fetch from custom location
, chartUrl ? null
# version of the chart
, version ? null
# chart hash
, sha256
# whether to extract chart
, untar ? true
# use custom charts repo
, repo ? null
# pass --verify to helm chart
, verify ? false
# pass --devel to helm chart
, devel ? false
}: stdenvNoCC.mkDerivation {
name = "${cleanName chart}-${if version == null then "dev" else version}";
buildCommand = ''
export HOME="$PWD"
echo "adding helm repo"
${if repo == null then "" else "helm repo add repository ${repo}"}
echo "fetching helm chart"
helm fetch -d ./chart \
${if untar then "--untar" else ""} \
${if version == null then "" else "--version ${version}"} \
${if devel then "--devel" else ""} \
${if verify then "--verify" else ""} \
${if chartUrl == null then (if repo == null then chart else "repository/${chart}") else chartUrl}
cp -r chart/*/ $out
'';
outputHashMode = "recursive";
outputHashAlgo = "sha256";
outputHash = sha256;
nativeBuildInputs = [ kubernetes-helm cacert ];
}
buildCommand = ''
export HOME="$PWD"
echo "adding helm repo"
${
if repo == null
then ""
else "helm repo add repository ${repo}"
}
echo "fetching helm chart"
helm fetch -d ./chart \
${
if untar
then "--untar"
else ""
} \
${
if version == null
then ""
else "--version ${version}"
} \
${
if devel
then "--devel"
else ""
} \
${
if verify
then "--verify"
else ""
} \
${
if chartUrl == null
then
(
if repo == null
then chart
else "repository/${chart}"
)
else chartUrl
}
cp -r chart/*/ $out
'';
outputHashMode = "recursive";
outputHashAlgo = "sha256";
outputHash = sha256;
nativeBuildInputs = [kubernetes-helm cacert];
}

View file

@ -1,9 +1,7 @@
{ pkgs ? import <nixpkgs> { } }:
let
fetchhelm = pkgs.callPackage ./fetchhelm.nix { };
chart2json = pkgs.callPackage ./chart2json.nix { };
in
rec {
{pkgs ? import <nixpkgs> {}}: let
fetchhelm = pkgs.callPackage ./fetchhelm.nix {};
chart2json = pkgs.callPackage ./chart2json.nix {};
in rec {
postgresql-chart = fetchhelm {
chart = "stable/postgresql";
version = "0.18.1";

View file

@ -1,31 +1,43 @@
{ lib }:
with lib;
rec {
{lib}:
with lib; rec {
# TODO: refactor with mkOptionType
mkSecretOption = { description ? "", default ? { }, allowNull ? true }: mkOption {
inherit description;
type = (if allowNull then types.nullOr else id) (types.submodule {
options = {
name = mkOption ({
description = "Name of the secret where secret is stored";
type = types.str;
default = default.name;
} // (optionalAttrs (default ? "name") {
default = default.name;
}));
mkSecretOption = {
description ? "",
default ? {},
allowNull ? true,
}:
mkOption {
inherit description;
type =
(
if allowNull
then types.nullOr
else id
) (types.submodule {
options = {
name = mkOption ({
description = "Name of the secret where secret is stored";
type = types.str;
default = default.name;
}
// (optionalAttrs (default ? "name") {
default = default.name;
}));
key = mkOption ({
description = "Name of the key where secret is stored";
type = types.str;
} // (optionalAttrs (default ? "key") {
default = default.key;
}));
};
});
default = if default == null then null else { };
};
key = mkOption ({
description = "Name of the key where secret is stored";
type = types.str;
}
// (optionalAttrs (default ? "key") {
default = default.key;
}));
};
});
default =
if default == null
then null
else {};
};
secretToEnv = value: {
valueFrom.secretKeyRef = {
@ -34,7 +46,10 @@ rec {
};
# Creates kubernetes list from a list of kubernetes objects
mkList = { items, labels ? { } }: {
mkList = {
items,
labels ? {},
}: {
kind = "List";
apiVersion = "v1";
@ -42,22 +57,27 @@ rec {
};
# Creates hashed kubernetes list from a list of kubernetes objects
mkHashedList = { items, labels ? { } }:
let
hash = builtins.hashString "sha1" (builtins.toJSON items);
mkHashedList = {
items,
labels ? {},
}: let
hash = builtins.hashString "sha1" (builtins.toJSON items);
labeledItems = map
(item: recursiveUpdate item {
labeledItems =
map
(item:
recursiveUpdate item {
metadata.labels."kubenix/hash" = hash;
})
items;
in
items;
in
mkList {
items = labeledItems;
labels = {
"kubenix/hash" = hash;
} // labels;
labels =
{
"kubenix/hash" = hash;
}
// labels;
};
toBase64 = lib.toBase64;

View file

@ -1,36 +1,45 @@
{ lib, pkgs }:
{
lib,
pkgs,
}:
with lib; let
self = {
importYAML = path:
importJSON (pkgs.runCommand "yaml-to-json" {} ''
${pkgs.remarshal}/bin/remarshal -i ${path} -if yaml -of json > $out
'');
with lib;
toYAML = config:
builtins.readFile (pkgs.runCommand "to-yaml" {} ''
${pkgs.remarshal}/bin/remarshal -i ${pkgs.writeText "to-json" (builtins.toJSON config)} -if json -of yaml > $out
'');
let self = {
toMultiDocumentYaml = name: documents:
pkgs.runCommand name {}
(concatMapStringsSep "\necho --- >> $out\n"
(
d: "${pkgs.remarshal}/bin/remarshal -i ${builtins.toFile "doc" (builtins.toJSON d)} -if json -of yaml >> $out"
)
documents);
importYAML = path: importJSON (pkgs.runCommand "yaml-to-json" { } ''
${pkgs.remarshal}/bin/remarshal -i ${path} -if yaml -of json > $out
'');
toBase64 = value:
builtins.readFile
(pkgs.runCommand "value-to-b64" {} "echo -n '${value}' | ${pkgs.coreutils}/bin/base64 -w0 > $out");
toYAML = config: builtins.readFile (pkgs.runCommand "to-yaml" { } ''
${pkgs.remarshal}/bin/remarshal -i ${pkgs.writeText "to-json" (builtins.toJSON config)} -if json -of yaml > $out
'');
exp = base: exp: foldr (value: acc: acc * base) 1 (range 1 exp);
toMultiDocumentYaml = name: documents: pkgs.runCommand name { }
(concatMapStringsSep "\necho --- >> $out\n"
(d:
"${pkgs.remarshal}/bin/remarshal -i ${builtins.toFile "doc" (builtins.toJSON d)} -if json -of yaml >> $out"
)
documents);
toBase64 = value:
builtins.readFile
(pkgs.runCommand "value-to-b64" { } "echo -n '${value}' | ${pkgs.coreutils}/bin/base64 -w0 > $out");
exp = base: exp: foldr (value: acc: acc * base) 1 (range 1 exp);
octalToDecimal = value: (foldr
(char: acc: {
i = acc.i + 1;
value = acc.value + (toInt char) * (self.exp 8 acc.i);
})
{ i = 0; value = 0; }
(stringToCharacters value)).value;
};
in self
octalToDecimal = value:
(foldr
(char: acc: {
i = acc.i + 1;
value = acc.value + (toInt char) * (self.exp 8 acc.i);
})
{
i = 0;
value = 0;
}
(stringToCharacters value))
.value;
};
in
self