2019-02-10 21:03:47 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
|
|
let
|
|
|
|
|
removeKubenixOptions = filterAttrs (name: attr: name != "kubenix");
|
|
|
|
|
|
2019-02-26 21:22:03 +01:00
|
|
|
getDefaults = resource: group: version: kind:
|
|
|
|
|
catAttrs "default" (filter (default:
|
2019-02-27 14:18:38 +01:00
|
|
|
(resource == null || default.resource == null || default.resource == resource) &&
|
2019-02-26 21:22:03 +01:00
|
|
|
(default.group == null || default.group == group) &&
|
|
|
|
|
(default.version == null || default.version == version) &&
|
|
|
|
|
(default.kind == null || default.kind == kind)
|
|
|
|
|
) config.kubernetes.api.defaults);
|
|
|
|
|
|
2019-02-10 21:03:47 +01:00
|
|
|
moduleToAttrs = value:
|
|
|
|
|
if isAttrs value
|
|
|
|
|
then mapAttrs (n: v: moduleToAttrs v) (filterAttrs (n: v: !(hasPrefix "_" n) && v != null) value)
|
|
|
|
|
|
|
|
|
|
else if isList value
|
|
|
|
|
then map (v: moduleToAttrs v) value
|
|
|
|
|
|
|
|
|
|
else value;
|
|
|
|
|
|
|
|
|
|
flattenResources = resources: flatten (
|
|
|
|
|
mapAttrsToList (groupName: versions:
|
|
|
|
|
mapAttrsToList (versionName: kinds:
|
|
|
|
|
builtins.trace versionName kinds
|
|
|
|
|
) versions
|
|
|
|
|
) resources
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
toKubernetesList = resources: {
|
|
|
|
|
kind = "List";
|
|
|
|
|
apiVersion = "v1";
|
|
|
|
|
items = resources;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
apiOptions = { config, ... }: {
|
|
|
|
|
options = {
|
|
|
|
|
definitions = mkOption {
|
|
|
|
|
description = "Attribute set of kubernetes definitions";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
defaults = mkOption {
|
2019-02-26 21:22:03 +01:00
|
|
|
description = "Kubernetes defaults to apply to resources";
|
|
|
|
|
type = types.listOf (types.submodule ({config, ...}: {
|
|
|
|
|
options = {
|
|
|
|
|
resource = mkOption {
|
|
|
|
|
description = "Resource to apply default to (all by default)";
|
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
|
default = null;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
group = mkOption {
|
|
|
|
|
description = "Group to apply default to (all by default)";
|
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
|
default = null;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
version = mkOption {
|
|
|
|
|
description = "Version to apply default to (all by default)";
|
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
|
default = null;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
kind = mkOption {
|
|
|
|
|
description = "Kind to apply default to (all by default)";
|
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
|
default = null;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
default = mkOption {
|
|
|
|
|
description = "Default to apply";
|
|
|
|
|
type = types.unspecified;
|
|
|
|
|
default = {};
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
}));
|
|
|
|
|
default = [];
|
2019-02-10 21:03:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
resources = mkOption {
|
|
|
|
|
type = types.listOf (types.submodule {
|
|
|
|
|
options = {
|
|
|
|
|
group = mkOption {
|
2019-02-26 21:22:03 +01:00
|
|
|
description = "Resoruce group";
|
2019-02-10 21:03:47 +01:00
|
|
|
type = types.str;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
version = mkOption {
|
2019-02-26 21:22:03 +01:00
|
|
|
description = "Resoruce version";
|
2019-02-10 21:03:47 +01:00
|
|
|
type = types.str;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
kind = mkOption {
|
2019-02-26 21:22:03 +01:00
|
|
|
description = "Resource kind";
|
2019-02-10 21:03:47 +01:00
|
|
|
type = types.str;
|
|
|
|
|
};
|
|
|
|
|
|
2019-02-26 21:22:03 +01:00
|
|
|
resource = mkOption {
|
|
|
|
|
description = "Resource name";
|
|
|
|
|
type = type.str;
|
2019-02-10 21:03:47 +01:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
default = [];
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
in {
|
|
|
|
|
imports = [./lib.nix];
|
|
|
|
|
|
|
|
|
|
options.kubernetes.version = mkOption {
|
|
|
|
|
description = "Kubernetes version to use";
|
2019-02-13 17:04:13 +01:00
|
|
|
type = types.enum ["1.7" "1.8" "1.9" "1.10" "1.11" "1.12" "1.13"];
|
|
|
|
|
default = "1.13";
|
2019-02-10 21:03:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
options.kubernetes.api = mkOption {
|
|
|
|
|
type = types.submodule {
|
|
|
|
|
imports = [
|
|
|
|
|
(./generated + ''/v'' + config.kubernetes.version + ".nix")
|
|
|
|
|
apiOptions
|
|
|
|
|
] ++ (map (cr: {config, ...}: {
|
|
|
|
|
options.${cr.group}.${cr.version}.${cr.kind} = mkOption {
|
|
|
|
|
description = cr.description;
|
|
|
|
|
type = types.attrsOf (types.submodule ({name, ...}: {
|
2019-02-27 12:06:18 +01:00
|
|
|
imports = getDefaults cr.resource cr.group cr.version cr.kind;
|
2019-02-10 21:03:47 +01:00
|
|
|
options = {
|
|
|
|
|
apiVersion = mkOption {
|
|
|
|
|
description = "APIVersion defines the versioned schema of this representation of an object. Servers should convert recognized schemas to the latest internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#resources";
|
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
kind = mkOption {
|
|
|
|
|
description = "Kind is a string value representing the REST resource this object represents. Servers may infer this from the endpoint the client submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds";
|
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
metadata = mkOption {
|
|
|
|
|
description = "Standard object metadata; More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata.";
|
|
|
|
|
type = types.nullOr (types.submodule config.definitions."io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta");
|
|
|
|
|
};
|
2019-02-11 21:20:45 +01:00
|
|
|
|
|
|
|
|
spec = mkOption {
|
|
|
|
|
description = "Module spec";
|
|
|
|
|
type = types.submodule cr.module;
|
|
|
|
|
default = {};
|
|
|
|
|
};
|
2019-02-10 21:03:47 +01:00
|
|
|
};
|
|
|
|
|
|
2019-02-27 12:06:18 +01:00
|
|
|
config = {
|
2019-02-10 21:03:47 +01:00
|
|
|
apiVersion = mkOptionDefault "${cr.group}/${cr.version}";
|
|
|
|
|
kind = mkOptionDefault cr.kind;
|
|
|
|
|
metadata.name = mkOptionDefault name;
|
2019-02-27 12:06:18 +01:00
|
|
|
};
|
2019-02-10 21:03:47 +01:00
|
|
|
}));
|
|
|
|
|
default = {};
|
|
|
|
|
};
|
|
|
|
|
}) config.kubernetes.customResources);
|
|
|
|
|
};
|
|
|
|
|
default = {};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
options.kubernetes.customResources = mkOption {
|
|
|
|
|
default = [];
|
2019-02-26 21:22:03 +01:00
|
|
|
description = "List of custom resource definitions to make API for";
|
2019-02-10 21:03:47 +01:00
|
|
|
type = types.listOf (types.submodule ({config, ...}: {
|
|
|
|
|
options = {
|
|
|
|
|
group = mkOption {
|
2019-02-26 21:22:03 +01:00
|
|
|
description = "Custom resource definition group";
|
2019-02-10 21:03:47 +01:00
|
|
|
type = types.str;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
version = mkOption {
|
2019-02-26 21:22:03 +01:00
|
|
|
description = "Custom resource definition version";
|
2019-02-10 21:03:47 +01:00
|
|
|
type = types.str;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
kind = mkOption {
|
2019-02-26 21:22:03 +01:00
|
|
|
description = "Custom resource definition kind";
|
2019-02-10 21:03:47 +01:00
|
|
|
type = types.str;
|
|
|
|
|
};
|
|
|
|
|
|
2019-02-26 21:22:03 +01:00
|
|
|
resource = mkOption {
|
|
|
|
|
description = "Custom resource definition resource name";
|
2019-02-27 14:18:38 +01:00
|
|
|
type = types.nullOr types.str;
|
|
|
|
|
default = null;
|
2019-02-10 21:03:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
description = mkOption {
|
2019-02-26 21:22:03 +01:00
|
|
|
description = "Custom resource definition description";
|
2019-02-10 21:03:47 +01:00
|
|
|
type = types.str;
|
2019-02-13 17:05:18 +01:00
|
|
|
default = "";
|
2019-02-10 21:03:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module = mkOption {
|
2019-02-26 21:22:03 +01:00
|
|
|
description = "Custom resource definition module";
|
2019-02-13 17:05:18 +01:00
|
|
|
default = types.unspecified;
|
2019-02-10 21:03:47 +01:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
}));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
config.kubernetes.api.resources = map (cr: {
|
2019-02-26 21:22:03 +01:00
|
|
|
inherit (cr) group version kind resource;
|
2019-02-10 21:03:47 +01:00
|
|
|
}) config.kubernetes.customResources;
|
|
|
|
|
|
|
|
|
|
options.kubernetes.objects = mkOption {
|
|
|
|
|
description = "Attribute set of kubernetes objects";
|
|
|
|
|
type = types.listOf types.attrs;
|
2019-02-20 09:28:12 +01:00
|
|
|
apply = items: sort (r1: r2:
|
|
|
|
|
if r1.kind == "CustomResourceDefinition" || r2.kind == "CustomResourceDefinition" then true else false
|
|
|
|
|
) (moduleToAttrs (unique items));
|
2019-02-10 21:03:47 +01:00
|
|
|
default = [];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
config.kubernetes.objects = flatten (map (gvk:
|
|
|
|
|
mapAttrsToList (name: resource:
|
|
|
|
|
removeKubenixOptions (moduleToAttrs resource)
|
|
|
|
|
) config.kubernetes.api.${gvk.group}.${gvk.version}.${gvk.kind}
|
|
|
|
|
) config.kubernetes.api.resources);
|
|
|
|
|
|
|
|
|
|
options.kubernetes.generated = mkOption {
|
2019-02-20 09:30:43 +01:00
|
|
|
type = types.attrs;
|
2019-02-10 21:03:47 +01:00
|
|
|
description = "Generated json file";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
config.kubernetes.generated = let
|
|
|
|
|
kubernetesList = toKubernetesList config.kubernetes.objects;
|
|
|
|
|
|
|
|
|
|
hashedList = kubernetesList // {
|
2019-02-20 09:33:33 +01:00
|
|
|
labels."kubenix/build" = config.kubernetes.hash;
|
2019-02-10 21:03:47 +01:00
|
|
|
items = map (resource: recursiveUpdate resource {
|
2019-02-20 09:33:33 +01:00
|
|
|
metadata.labels."kubenix/build" = config.kubernetes.hash;
|
2019-02-10 21:03:47 +01:00
|
|
|
}) kubernetesList.items;
|
|
|
|
|
};
|
2019-02-20 09:30:43 +01:00
|
|
|
in hashedList;
|
2019-02-20 09:33:33 +01:00
|
|
|
|
|
|
|
|
options.kubernetes.hash = mkOption {
|
|
|
|
|
type = types.str;
|
|
|
|
|
description = "Output hash";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
config.kubernetes.hash = builtins.hashString "sha1" (builtins.toJSON config.kubernetes.objects);
|
2019-02-10 21:03:47 +01:00
|
|
|
}
|