feat: refactor file structure module interfaces and other

This commit is contained in:
Jaka Hudoklin 2019-03-07 23:23:07 +01:00
parent 12a1c920c4
commit eac2d78667
No known key found for this signature in database
GPG key ID: 6A08896BFD32BD95
36 changed files with 58 additions and 139 deletions

View file

@ -1,9 +1,10 @@
{
k8s = ./k8s.nix;
istio = ./istio.nix;
submodules = ./submodules.nix;
k8s = ./k8s;
istio = ./istio;
testing = ./testing;
helm = ./helm;
docker = ./docker;
helm = ./helm.nix;
docker = ./docker.nix;
testing = ./testing.nix;
test = ./test.nix;
module = ./module.nix;
}

View file

@ -1,4 +1,7 @@
{ config, lib, pkgs, kubenix, ... }:
# helm defines kubenix module with options for using helm charts
# with kubenix
{ config, lib, pkgs, helm, ... }:
with lib;
@ -21,13 +24,11 @@ let
version = last splitted;
};
chart2json = pkgs.callPackage ./chart2json.nix { };
fetchhelm = pkgs.callPackage ./fetchhelm.nix { };
in {
imports = [
kubenix.k8s
];
imports = [ ./k8s.nix ];
# expose helm helper methods as module argument
config._module.args.helm = import ../lib/helm { inherit pkgs; };
options.kubernetes.helm = {
instances = mkOption {
@ -86,19 +87,13 @@ in {
metadata.namespace = mkDefault config.namespace;
}];
config.objects = importJSON (chart2json {
config.objects = importJSON (helm.chart2json {
inherit (config) chart name namespace values kubeVersion;
});
}));
};
};
# include helper helm methods as args
config._module.args.helm = {
fetch = fetchhelm;
chart2json = chart2json;
};
config.kubernetes.api = mkMerge (flatten (mapAttrsToList (_: instance:
map (object: let
apiVersion = parseApiVersion object.apiVersion;

View file

@ -1,51 +0,0 @@
{ 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 }: 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 "${name}" \
${optionalString (kubeVersion != null) "--kube-version ${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
# 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 ];
}

View file

@ -1,48 +0,0 @@
{ 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}";
buildCommand = ''
export HOME="$PWD"
helm init --client-only >/dev/null
${if repo == null then "" else "helm repo add repository ${repo}"}
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,43 +0,0 @@
{ 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";
sha256 = "1p3gfmaakxrqb4ncj6nclyfr5afv7xvcdw95c6qyazfg72h3zwjn";
};
istio-chart = fetchhelm {
chart = "istio";
version = "1.1.0";
repo = "https://storage.googleapis.com/istio-release/releases/1.1.0-rc.0/charts";
sha256 = "0ippv2914hwpsb3kkhk8d839dii5whgrhxjwhpb9vdwgji5s7yfl";
};
istio-official-chart = pkgs.fetchgit {
url = "https://github.com/fyery-chen/istio-helm";
rev = "47e235e775314daeb88a3a53689ed66c396ecd3f";
sha256 = "190sfyvhdskw6ijy8cprp6hxaazn7s7mg5ids4snshk1pfdg2q8h";
};
postgresql-json = chart2json {
name = "postgresql";
chart = postgresql-chart;
values = {
networkPolicy.enabled = true;
};
};
istio-json = chart2json {
name = "istio";
chart = istio-chart;
};
istio-official-json = chart2json {
name = "istio-official";
chart = "${istio-official-chart}/istio-official";
};
}

View file

@ -4697,7 +4697,7 @@ let
};
};
} // (import ./overrides.nix {inherit definitions lib;});
} // (import ./istio-overrides.nix {inherit definitions lib;});
in {
kubernetes.customResources = [
{

View file

@ -1,3 +1,5 @@
# K8S module defines kubernetes definitions for kubenix
{ config, lib, pkgs, k8s, ... }:
with lib;
@ -108,8 +110,8 @@ let
indexOf = lst: value:
head (filter (v: v != -1) (imap0 (i: v: if v == value then i else -1) lst));
in {
# expose k8s helper methods through arg in modules
config._module.args.k8s = import ../../lib/k8s.nix { inherit lib; };
# expose k8s helper methods as module argument
config._module.args.k8s = import ../lib/k8s.nix { inherit lib; };
options.kubernetes.version = mkOption {
description = "Kubernetes version to use";

View file

@ -1,59 +0,0 @@
{ config, lib, kubenix, ... }:
with lib;
let
globalConfig = config;
in {
imports = [ kubenix.submodules ];
options = {
kubernetes.propagateDefaults = mkOption {
description = "Whether to propagate child defaults to submodules";
type = types.bool;
default = true;
};
submodules.instances = mkOption {
type = types.attrsOf (types.submodule ({config, ...}: {
options = {
namespace = mkOption {
description = "Default kubernetes namespace";
type = types.str;
default = "default";
};
};
config.config = {
kubernetes.api.defaults = [{
default.metadata.namespace = mkDefault config.namespace;
}];
};
}));
};
};
config = {
submodules.defaults = [{
default = {
imports = [ kubenix.k8s ];
kubernetes.version = mkDefault config.kubernetes.version;
kubernetes.api.defaults =
mkIf config.kubernetes.propagateDefaults config.kubernetes.api.defaults;
};
} {
default = ({config, ...}: {
kubernetes.api.defaults = [{
default.metadata.labels = {
"kubenix/module-name" = config.submodule.name;
"kubenix/module-version" = config.submodule.version;
};
}];
});
}];
kubernetes.objects = mkMerge (mapAttrsToList (_: submodule:
submodule.config.kubernetes.objects
) config.submodules.instances);
};
}

View file

@ -1,11 +1,15 @@
{ config, lib, kubenix, ... }:
# module.nix defines default kubenix module with additional helper options
# and preincluded kubenix module definitions for kubernetes, docker and
# kubenix submodules
{ config, lib, ... }:
with lib;
let
parentConfig = config;
in {
imports = with kubenix; [ submodules k8s docker ];
imports = [ ./k8s.nix ./docker.nix ./submodules.nix ];
options = {
kubenix.release = mkOption {
@ -42,7 +46,7 @@ in {
config = {
submodules.defaults = [{
default = {
imports = [ kubenix.module ];
imports = [ ./module.nix ];
kubernetes.version = mkDefault config.kubernetes.version;
kubernetes.api.defaults =
mkIf config.kubernetes.propagateDefaults config.kubernetes.api.defaults;