mirror of
https://github.com/TECHNOFAB11/kubenix.git
synced 2026-02-02 17:35:10 +01:00
switch formatting to nixpkgs-fmt
This commit is contained in:
parent
3598716c73
commit
2712e89716
65 changed files with 4839 additions and 5222 deletions
|
|
@ -1,7 +1,6 @@
|
|||
{
|
||||
stdenv,
|
||||
kubernetes,
|
||||
installShellFiles,
|
||||
{ stdenv
|
||||
, kubernetes
|
||||
, installShellFiles
|
||||
}:
|
||||
stdenv.mkDerivation {
|
||||
name = "kubectl-${kubernetes.version}";
|
||||
|
|
@ -10,9 +9,9 @@ stdenv.mkDerivation {
|
|||
# split out (see homepage)
|
||||
dontUnpack = true;
|
||||
|
||||
nativeBuildInputs = [installShellFiles];
|
||||
nativeBuildInputs = [ installShellFiles ];
|
||||
|
||||
outputs = ["out" "man"];
|
||||
outputs = [ "out" "man" ];
|
||||
|
||||
installPhase = ''
|
||||
install -D ${kubernetes}/bin/kubectl -t $out/bin
|
||||
|
|
|
|||
|
|
@ -1,21 +1,20 @@
|
|||
{
|
||||
stdenv,
|
||||
lib,
|
||||
fetchFromGitHub,
|
||||
removeReferencesTo,
|
||||
which,
|
||||
go,
|
||||
makeWrapper,
|
||||
rsync,
|
||||
installShellFiles,
|
||||
components ? [
|
||||
{ stdenv
|
||||
, lib
|
||||
, fetchFromGitHub
|
||||
, removeReferencesTo
|
||||
, which
|
||||
, go
|
||||
, makeWrapper
|
||||
, rsync
|
||||
, installShellFiles
|
||||
, components ? [
|
||||
"cmd/kubelet"
|
||||
"cmd/kube-apiserver"
|
||||
"cmd/kube-controller-manager"
|
||||
"cmd/kube-proxy"
|
||||
"cmd/kube-scheduler"
|
||||
"test/e2e/e2e.test"
|
||||
],
|
||||
]
|
||||
}:
|
||||
stdenv.mkDerivation rec {
|
||||
pname = "kubernetes";
|
||||
|
|
@ -28,11 +27,11 @@ stdenv.mkDerivation rec {
|
|||
hash = "sha256-r9Clwr+87Ns4VXUW9F6cgks+LknY39ngbQgZ5UMZ0Vo=";
|
||||
};
|
||||
|
||||
nativeBuildInputs = [removeReferencesTo makeWrapper which go rsync installShellFiles];
|
||||
nativeBuildInputs = [ removeReferencesTo makeWrapper which go rsync installShellFiles ];
|
||||
|
||||
outputs = ["out" "man" "pause"];
|
||||
outputs = [ "out" "man" "pause" ];
|
||||
|
||||
patches = [./fixup-addonmanager-lib-path.patch];
|
||||
patches = [ ./fixup-addonmanager-lib-path.patch ];
|
||||
|
||||
postPatch = ''
|
||||
# go env breaks the sandbox
|
||||
|
|
@ -48,10 +47,10 @@ stdenv.mkDerivation rec {
|
|||
'';
|
||||
|
||||
WHAT = lib.concatStringsSep " " ([
|
||||
"cmd/kubeadm"
|
||||
"cmd/kubectl"
|
||||
]
|
||||
++ components);
|
||||
"cmd/kubeadm"
|
||||
"cmd/kubectl"
|
||||
]
|
||||
++ components);
|
||||
|
||||
postBuild = ''
|
||||
./hack/update-generated-docs.sh
|
||||
|
|
@ -84,7 +83,7 @@ stdenv.mkDerivation rec {
|
|||
description = "Production-Grade Container Scheduling and Management";
|
||||
license = licenses.asl20;
|
||||
homepage = "https://kubernetes.io";
|
||||
maintainers = with maintainers; [johanot offline saschagrunert];
|
||||
maintainers = with maintainers; [ johanot offline saschagrunert ];
|
||||
platforms = platforms.unix;
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,45 +1,36 @@
|
|||
{
|
||||
pkgs,
|
||||
lib,
|
||||
}: let
|
||||
{ pkgs, lib }:
|
||||
let
|
||||
generateIstio = import ./istio {
|
||||
inherit
|
||||
pkgs
|
||||
lib
|
||||
;
|
||||
inherit pkgs lib;
|
||||
};
|
||||
|
||||
generateK8S = name: spec:
|
||||
import ./k8s {
|
||||
inherit
|
||||
name
|
||||
pkgs
|
||||
lib
|
||||
spec
|
||||
;
|
||||
inherit name pkgs lib spec;
|
||||
};
|
||||
in {
|
||||
istio = pkgs.linkFarm "istio-generated" [
|
||||
{
|
||||
name = "latest.nix";
|
||||
path = generateIstio;
|
||||
}
|
||||
];
|
||||
in
|
||||
{
|
||||
istio = pkgs.linkFarm "istio-generated" [{
|
||||
name = "latest.nix";
|
||||
path = generateIstio;
|
||||
}];
|
||||
|
||||
k8s = pkgs.linkFarm "k8s-generated" (
|
||||
builtins.attrValues (
|
||||
builtins.mapAttrs (
|
||||
version: sha: let
|
||||
short = builtins.concatStringsSep "." (lib.lists.sublist 0 2 (builtins.splitVersion version));
|
||||
in {
|
||||
name = "v${short}.nix";
|
||||
path = generateK8S "v${short}" (builtins.fetchurl {
|
||||
url = "https://github.com/kubernetes/kubernetes/raw/v${version}/api/openapi-spec/swagger.json";
|
||||
sha256 = sha;
|
||||
});
|
||||
}
|
||||
)
|
||||
(import ../../versions.nix).full
|
||||
builtins.mapAttrs
|
||||
(version: sha:
|
||||
let
|
||||
short = builtins.concatStringsSep "." (lib.lists.sublist 0 2 (builtins.splitVersion version));
|
||||
in
|
||||
{
|
||||
name = "v${short}.nix";
|
||||
path = generateK8S "v${short}" (builtins.fetchurl {
|
||||
url = "https://github.com/kubernetes/kubernetes/raw/v${version}/api/openapi-spec/swagger.json";
|
||||
sha256 = sha;
|
||||
});
|
||||
}
|
||||
)
|
||||
(import ../../versions.nix).full
|
||||
)
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
{
|
||||
pkgs ? import <nixpkgs> {},
|
||||
lib ? pkgs.lib,
|
||||
spec ? ./istio-schema.json,
|
||||
{ pkgs ? import <nixpkgs> { }
|
||||
, lib ? pkgs.lib
|
||||
, spec ? ./istio-schema.json
|
||||
}:
|
||||
with lib; let
|
||||
gen = rec {
|
||||
|
|
@ -20,14 +19,9 @@ with lib; let
|
|||
then "null"
|
||||
else builtins.toString value;
|
||||
|
||||
removeEmptyLines = str: concatStringsSep "\n" (filter (l: builtins.match "[[:space:]]*" l != []) (splitString "\n" str));
|
||||
removeEmptyLines = str: concatStringsSep "\n" (filter (l: builtins.match "[[:space:]]*" l != [ ]) (splitString "\n" str));
|
||||
|
||||
mkOption = {
|
||||
description ? null,
|
||||
type ? null,
|
||||
default ? null,
|
||||
apply ? null,
|
||||
}:
|
||||
mkOption = { description ? null, type ? null, default ? null, apply ? null }:
|
||||
removeEmptyLines '' mkOption {
|
||||
${optionalString (description != null) "description = ${builtins.toJSON description};"}
|
||||
${optionalString (type != null) ''type = ${type};''}
|
||||
|
|
@ -53,7 +47,7 @@ with lib; let
|
|||
|
||||
hasTypeMapping = def:
|
||||
hasAttr "type" def
|
||||
&& elem def.type ["string" "integer" "boolean"];
|
||||
&& elem def.type [ "string" "integer" "boolean" ];
|
||||
|
||||
mergeValuesByKey = mergeKey: ''(mergeValuesByKey "${mergeKey}")'';
|
||||
|
||||
|
|
@ -84,157 +78,152 @@ with lib; let
|
|||
refDefinition = attr: head (tail (tail (splitString "/" attr."$ref")));
|
||||
};
|
||||
|
||||
fixJSON = replaceStrings ["\\u"] ["u"];
|
||||
fixJSON = replaceStrings [ "\\u" ] [ "u" ];
|
||||
|
||||
fetchSpecs = path: builtins.fromJSON (fixJSON (builtins.readFile path));
|
||||
|
||||
genDefinitions = swagger:
|
||||
with gen; (mapAttrs
|
||||
(
|
||||
_name: definition:
|
||||
# if $ref is in definition it means it's an alias of other definition
|
||||
if hasAttr "$ref" definition
|
||||
then definitions."${refDefinition definition}"
|
||||
else if !(hasAttr "properties" definition)
|
||||
then {
|
||||
type = mapType definition;
|
||||
}
|
||||
else {
|
||||
options =
|
||||
mapAttrs
|
||||
(
|
||||
propName: property: let
|
||||
isRequired = elem propName (definition.required or []);
|
||||
requiredOrNot = type:
|
||||
if isRequired
|
||||
then type
|
||||
else types.nullOr type;
|
||||
optionProperties =
|
||||
# if $ref is in property it references other definition,
|
||||
# but if other definition does not have properties, then just take it's type
|
||||
if hasAttr "$ref" property
|
||||
then
|
||||
if hasTypeMapping swagger.definitions.${refDefinition property}
|
||||
then {
|
||||
type = requiredOrNot (mapType swagger.definitions.${refDefinition property});
|
||||
}
|
||||
else {
|
||||
type = requiredOrNot (submoduleOf definitions (refDefinition property));
|
||||
}
|
||||
else if !(hasAttr "type" property)
|
||||
genDefinitions = swagger: with gen; (mapAttrs
|
||||
(_name: definition:
|
||||
# if $ref is in definition it means it's an alias of other definition
|
||||
if hasAttr "$ref" definition
|
||||
then definitions."${refDefinition definition}"
|
||||
else if !(hasAttr "properties" definition)
|
||||
then {
|
||||
type = mapType definition;
|
||||
}
|
||||
else {
|
||||
options = mapAttrs
|
||||
(propName: property:
|
||||
let
|
||||
isRequired = elem propName (definition.required or [ ]);
|
||||
requiredOrNot = type:
|
||||
if isRequired
|
||||
then type
|
||||
else types.nullOr type;
|
||||
optionProperties =
|
||||
# if $ref is in property it references other definition,
|
||||
# but if other definition does not have properties, then just take it's type
|
||||
if hasAttr "$ref" property
|
||||
then
|
||||
if hasTypeMapping swagger.definitions.${refDefinition property}
|
||||
then {
|
||||
type = requiredOrNot (mapType swagger.definitions.${refDefinition property});
|
||||
}
|
||||
else {
|
||||
type = requiredOrNot (submoduleOf definitions (refDefinition property));
|
||||
}
|
||||
else if !(hasAttr "type" property)
|
||||
then {
|
||||
type = types.unspecified;
|
||||
}
|
||||
# if property has an array type
|
||||
else if property.type == "array"
|
||||
then
|
||||
# if reference is in items it can reference other type of another
|
||||
# definition
|
||||
if hasAttr "$ref" property.items
|
||||
then
|
||||
# if it is a reference to simple type
|
||||
if hasTypeMapping swagger.definitions.${refDefinition property.items}
|
||||
then {
|
||||
type = types.unspecified;
|
||||
type = requiredOrNot (types.listOf (mapType swagger.definitions.${refDefinition property.items}.type));
|
||||
}
|
||||
# if property has an array type
|
||||
else if property.type == "array"
|
||||
then
|
||||
# if reference is in items it can reference other type of another
|
||||
# definition
|
||||
if hasAttr "$ref" property.items
|
||||
# if a reference is to complex type
|
||||
else
|
||||
# if x-kubernetes-patch-merge-key is set then make it an
|
||||
# attribute set of submodules
|
||||
if hasAttr "x-kubernetes-patch-merge-key" property
|
||||
then
|
||||
# if it is a reference to simple type
|
||||
if hasTypeMapping swagger.definitions.${refDefinition property.items}
|
||||
then {
|
||||
type = requiredOrNot (types.listOf (mapType swagger.definitions.${refDefinition property.items}.type));
|
||||
let
|
||||
mergeKey = property."x-kubernetes-patch-merge-key";
|
||||
in
|
||||
{
|
||||
type = requiredOrNot (coerceAttrsOfSubmodulesToListByKey (refDefinition property.items) mergeKey);
|
||||
apply = attrsToList;
|
||||
}
|
||||
# if a reference is to complex type
|
||||
else
|
||||
# if x-kubernetes-patch-merge-key is set then make it an
|
||||
# attribute set of submodules
|
||||
if hasAttr "x-kubernetes-patch-merge-key" property
|
||||
then let
|
||||
mergeKey = property."x-kubernetes-patch-merge-key";
|
||||
in {
|
||||
type = requiredOrNot (coerceAttrsOfSubmodulesToListByKey (refDefinition property.items) mergeKey);
|
||||
apply = attrsToList;
|
||||
}
|
||||
# in other case it's a simple list
|
||||
else {
|
||||
type = requiredOrNot (types.listOf (submoduleOf definitions (refDefinition property.items)));
|
||||
}
|
||||
# in other case it only references a simple type
|
||||
# in other case it's a simple list
|
||||
else {
|
||||
type = requiredOrNot (types.listOf (mapType property.items));
|
||||
type = requiredOrNot (types.listOf (submoduleOf definitions (refDefinition property.items)));
|
||||
}
|
||||
else if property.type == "object" && hasAttr "additionalProperties" property
|
||||
then
|
||||
# if it is a reference to simple type
|
||||
if
|
||||
(
|
||||
hasAttr "$ref" property.additionalProperties
|
||||
&& hasTypeMapping swagger.definitions.${refDefinition property.additionalProperties}
|
||||
)
|
||||
then {
|
||||
type = requiredOrNot (types.attrsOf (mapType swagger.definitions.${refDefinition property.additionalProperties}));
|
||||
}
|
||||
else if hasAttr "$ref" property.additionalProperties
|
||||
then {
|
||||
type = requiredOrNot types.attrs;
|
||||
}
|
||||
# if is an array
|
||||
else if property.additionalProperties.type == "array"
|
||||
then {
|
||||
type = requiredOrNot (types.loaOf (mapType property.additionalProperties.items));
|
||||
}
|
||||
else {
|
||||
type = requiredOrNot (types.attrsOf (mapType property.additionalProperties));
|
||||
}
|
||||
# just a simple property
|
||||
else {
|
||||
type = requiredOrNot (mapType property);
|
||||
};
|
||||
in
|
||||
mkOption ({
|
||||
description = property.description or "";
|
||||
}
|
||||
// optionProperties)
|
||||
# in other case it only references a simple type
|
||||
else {
|
||||
type = requiredOrNot (types.listOf (mapType property.items));
|
||||
}
|
||||
else if property.type == "object" && hasAttr "additionalProperties" property
|
||||
then
|
||||
# if it is a reference to simple type
|
||||
if
|
||||
(
|
||||
hasAttr "$ref" property.additionalProperties
|
||||
&& hasTypeMapping swagger.definitions.${refDefinition property.additionalProperties}
|
||||
)
|
||||
then {
|
||||
type = requiredOrNot (types.attrsOf (mapType swagger.definitions.${refDefinition property.additionalProperties}));
|
||||
}
|
||||
else if hasAttr "$ref" property.additionalProperties
|
||||
then {
|
||||
type = requiredOrNot types.attrs;
|
||||
}
|
||||
# if is an array
|
||||
else if property.additionalProperties.type == "array"
|
||||
then {
|
||||
type = requiredOrNot (types.loaOf (mapType property.additionalProperties.items));
|
||||
}
|
||||
else {
|
||||
type = requiredOrNot (types.attrsOf (mapType property.additionalProperties));
|
||||
}
|
||||
# just a simple property
|
||||
else {
|
||||
type = requiredOrNot (mapType property);
|
||||
};
|
||||
in
|
||||
mkOption ({
|
||||
description = property.description or "";
|
||||
}
|
||||
// optionProperties)
|
||||
)
|
||||
definition.properties;
|
||||
config =
|
||||
let
|
||||
optionalProps = filterAttrs
|
||||
(propName: _property:
|
||||
!(elem propName (definition.required or [ ]))
|
||||
)
|
||||
definition.properties;
|
||||
config = let
|
||||
optionalProps =
|
||||
filterAttrs
|
||||
(
|
||||
propName: _property:
|
||||
!(elem propName (definition.required or []))
|
||||
)
|
||||
definition.properties;
|
||||
in
|
||||
mapAttrs (_name: _property: mkOverride 1002 null) optionalProps;
|
||||
}
|
||||
)
|
||||
swagger.definitions);
|
||||
in
|
||||
mapAttrs (_name: _property: mkOverride 1002 null) optionalProps;
|
||||
}
|
||||
)
|
||||
swagger.definitions);
|
||||
|
||||
genResources = swagger:
|
||||
(mapAttrsToList
|
||||
(_name: property: rec {
|
||||
splittedType = splitString "." (removePrefix "me.snowdrop.istio.api." property.javaType);
|
||||
group = (concatStringsSep "." (take ((length splittedType) - 2) splittedType)) + ".istio.io";
|
||||
kind = removeSuffix "Spec" (last splittedType);
|
||||
version = last (take ((length splittedType) - 1) splittedType);
|
||||
ref = removePrefix "#/definitions/" property."$ref";
|
||||
})
|
||||
(filterAttrs
|
||||
(
|
||||
_name: property:
|
||||
(hasPrefix "me.snowdrop.istio.api" property.javaType)
|
||||
&& hasSuffix "Spec" property.javaType
|
||||
)
|
||||
swagger.properties))
|
||||
++ (mapAttrsToList
|
||||
(_name: property: rec {
|
||||
splittedType = splitString "." (removePrefix "me.snowdrop.istio.mixer." property.javaType);
|
||||
group = "config.istio.io";
|
||||
version = "v1alpha2";
|
||||
kind = head (tail splittedType);
|
||||
ref = removePrefix "#/definitions/" property."$ref";
|
||||
})
|
||||
(filterAttrs
|
||||
(
|
||||
_name: property:
|
||||
(hasPrefix "me.snowdrop.istio.mixer" property.javaType)
|
||||
&& hasSuffix "Spec" property.javaType
|
||||
)
|
||||
swagger.properties));
|
||||
genResources = swagger: (mapAttrsToList
|
||||
(_name: property: rec {
|
||||
splittedType = splitString "." (removePrefix "me.snowdrop.istio.api." property.javaType);
|
||||
group = (concatStringsSep "." (take ((length splittedType) - 2) splittedType)) + ".istio.io";
|
||||
kind = removeSuffix "Spec" (last splittedType);
|
||||
version = last (take ((length splittedType) - 1) splittedType);
|
||||
ref = removePrefix "#/definitions/" property."$ref";
|
||||
})
|
||||
(filterAttrs
|
||||
(_name: property:
|
||||
(hasPrefix "me.snowdrop.istio.api" property.javaType)
|
||||
&& hasSuffix "Spec" property.javaType
|
||||
)
|
||||
swagger.properties))
|
||||
++ (mapAttrsToList
|
||||
(_name: property: rec {
|
||||
splittedType = splitString "." (removePrefix "me.snowdrop.istio.mixer." property.javaType);
|
||||
group = "config.istio.io";
|
||||
version = "v1alpha2";
|
||||
kind = head (tail splittedType);
|
||||
ref = removePrefix "#/definitions/" property."$ref";
|
||||
})
|
||||
(filterAttrs
|
||||
(_name: property:
|
||||
(hasPrefix "me.snowdrop.istio.mixer" property.javaType)
|
||||
&& hasSuffix "Spec" property.javaType
|
||||
)
|
||||
swagger.properties));
|
||||
|
||||
swagger = fetchSpecs spec;
|
||||
|
||||
|
|
@ -367,14 +356,14 @@ with lib; let
|
|||
}
|
||||
'';
|
||||
in
|
||||
pkgs.runCommand "istio-gen.nix"
|
||||
{
|
||||
buildInputs = [pkgs.nixpkgs-fmt];
|
||||
} ''
|
||||
cat << 'GENERATED' > ./raw
|
||||
"${generated}"
|
||||
GENERATED
|
||||
pkgs.runCommand "istio-gen.nix"
|
||||
{
|
||||
buildInputs = [ pkgs.nixpkgs-fmt ];
|
||||
} ''
|
||||
cat << 'GENERATED' > ./raw
|
||||
"${generated}"
|
||||
GENERATED
|
||||
|
||||
nixpkgs-fmt ./raw
|
||||
cp ./raw $out
|
||||
''
|
||||
nixpkgs-fmt ./raw
|
||||
cp ./raw $out
|
||||
''
|
||||
|
|
|
|||
|
|
@ -1,9 +1,4 @@
|
|||
{
|
||||
name,
|
||||
pkgs,
|
||||
lib,
|
||||
spec,
|
||||
}:
|
||||
{ name, pkgs, lib, spec }:
|
||||
with lib; let
|
||||
gen = rec {
|
||||
mkMerge = values: ''mkMerge [${concatMapStrings
|
||||
|
|
@ -21,14 +16,9 @@ with lib; let
|
|||
then "null"
|
||||
else builtins.toString value;
|
||||
|
||||
removeEmptyLines = str: concatStringsSep "\n" (filter (l: builtins.match "[[:space:]]*" l != []) (splitString "\n" str));
|
||||
removeEmptyLines = str: concatStringsSep "\n" (filter (l: builtins.match "[[:space:]]*" l != [ ]) (splitString "\n" str));
|
||||
|
||||
mkOption = {
|
||||
description ? null,
|
||||
type ? null,
|
||||
default ? null,
|
||||
apply ? null,
|
||||
}:
|
||||
mkOption = { description ? null, type ? null, default ? null, apply ? null }:
|
||||
removeEmptyLines '' mkOption {
|
||||
${optionalString (description != null) "description = ${builtins.toJSON description};"}
|
||||
${optionalString (type != null) ''type = ${type};''}
|
||||
|
|
@ -52,9 +42,7 @@ with lib; let
|
|||
loaOf = type: "(types.loaOf ${type})";
|
||||
};
|
||||
|
||||
hasTypeMapping = def:
|
||||
hasAttr "type" def
|
||||
&& elem def.type ["string" "integer" "boolean"];
|
||||
hasTypeMapping = def: hasAttr "type" def && elem def.type [ "string" "integer" "boolean" ];
|
||||
|
||||
mergeValuesByKey = mergeKey: ''(mergeValuesByKey "${mergeKey}")'';
|
||||
|
||||
|
|
@ -87,43 +75,42 @@ with lib; let
|
|||
|
||||
refType = attr: head (tail (tail (splitString "/" attr."$ref")));
|
||||
|
||||
compareVersions = ver1: ver2: let
|
||||
getVersion = substring 1 10;
|
||||
splitVersion = v: builtins.splitVersion (getVersion v);
|
||||
isAlpha = v: elem "alpha" (splitVersion v);
|
||||
patchVersion = v:
|
||||
if isAlpha v
|
||||
then ""
|
||||
else if length (splitVersion v) == 1
|
||||
then "${getVersion v}prod"
|
||||
else getVersion v;
|
||||
compareVersions = ver1: ver2:
|
||||
let
|
||||
getVersion = substring 1 10;
|
||||
splitVersion = v: builtins.splitVersion (getVersion v);
|
||||
isAlpha = v: elem "alpha" (splitVersion v);
|
||||
patchVersion = v:
|
||||
if isAlpha v
|
||||
then ""
|
||||
else if length (splitVersion v) == 1
|
||||
then "${getVersion v}prod"
|
||||
else getVersion v;
|
||||
|
||||
v1 = patchVersion ver1;
|
||||
v2 = patchVersion ver2;
|
||||
in
|
||||
v1 = patchVersion ver1;
|
||||
v2 = patchVersion ver2;
|
||||
in
|
||||
builtins.compareVersions v1 v2;
|
||||
|
||||
fixJSON = replaceStrings ["\\u"] ["u"];
|
||||
fixJSON = replaceStrings [ "\\u" ] [ "u" ];
|
||||
|
||||
fetchSpecs = path: builtins.fromJSON (fixJSON (builtins.readFile path));
|
||||
|
||||
genDefinitions = swagger:
|
||||
with gen;
|
||||
mapAttrs
|
||||
(
|
||||
_name: definition:
|
||||
# if $ref is in definition it means it's an alias of other definition
|
||||
if hasAttr "$ref" definition
|
||||
then definitions."${refDefinition definition}"
|
||||
else if !(hasAttr "properties" definition)
|
||||
then {}
|
||||
# in other case it's an actual definition
|
||||
else {
|
||||
options =
|
||||
mapAttrs
|
||||
(
|
||||
propName: property: let
|
||||
isRequired = elem propName (definition.required or []);
|
||||
genDefinitions = swagger: with gen; mapAttrs
|
||||
(_name: definition:
|
||||
# if $ref is in definition it means it's an alias of other definition
|
||||
if hasAttr "$ref" definition
|
||||
then definitions."${refDefinition definition}"
|
||||
else if !(hasAttr "properties" definition)
|
||||
then { }
|
||||
# in other case it's an actual definition
|
||||
else {
|
||||
options =
|
||||
mapAttrs
|
||||
(
|
||||
propName: property:
|
||||
let
|
||||
isRequired = elem propName (definition.required or [ ]);
|
||||
requiredOrNot = type:
|
||||
if isRequired
|
||||
then type
|
||||
|
|
@ -146,43 +133,47 @@ with lib; let
|
|||
# if property has an array type
|
||||
else if property.type == "array"
|
||||
then
|
||||
# if reference is in items it can reference other type of another
|
||||
# definition
|
||||
# if reference is in items it can reference other type of another
|
||||
# definition
|
||||
if hasAttr "$ref" property.items
|
||||
then
|
||||
# if it is a reference to simple type
|
||||
# if it is a reference to simple type
|
||||
if hasTypeMapping swagger.definitions.${refDefinition property.items}
|
||||
then {
|
||||
type = requiredOrNot (types.listOf (mapType swagger.definitions.${refDefinition property.items}.type));
|
||||
}
|
||||
# if a reference is to complex type
|
||||
else
|
||||
# make it an attribute set of submodules if only x-kubernetes-patch-merge-key is present, or
|
||||
# x-kubernetes-patch-merge-key == x-kubernetes-list-map-keys.
|
||||
if (hasAttr "x-kubernetes-patch-merge-key" property) && (!(hasAttr "x-kubernetes-list-map-keys" property) || (property."x-kubernetes-list-map-keys" == [property."x-kubernetes-patch-merge-key"]))
|
||||
then let
|
||||
mergeKey = property."x-kubernetes-patch-merge-key";
|
||||
in {
|
||||
type = requiredOrNot (coerceAttrsOfSubmodulesToListByKey (refDefinition property.items) mergeKey []);
|
||||
apply = attrsToList;
|
||||
}
|
||||
# make it an attribute set of submodules if only x-kubernetes-patch-merge-key is present, or
|
||||
# x-kubernetes-patch-merge-key == x-kubernetes-list-map-keys.
|
||||
if (hasAttr "x-kubernetes-patch-merge-key" property) && (!(hasAttr "x-kubernetes-list-map-keys" property) || (property."x-kubernetes-list-map-keys" == [ property."x-kubernetes-patch-merge-key" ]))
|
||||
then
|
||||
let
|
||||
mergeKey = property."x-kubernetes-patch-merge-key";
|
||||
in
|
||||
{
|
||||
type = requiredOrNot (coerceAttrsOfSubmodulesToListByKey (refDefinition property.items) mergeKey [ ]);
|
||||
apply = attrsToList;
|
||||
}
|
||||
# in other case it's a simple list
|
||||
else
|
||||
# make it an attribute set of submodules if only x-kubernetes-patch-merge-key is present, or
|
||||
# x-kubernetes-patch-merge-key == x-kubernetes-list-map-keys.
|
||||
# make it an attribute set of submodules if only x-kubernetes-patch-merge-key is present, or
|
||||
# x-kubernetes-patch-merge-key == x-kubernetes-list-map-keys.
|
||||
if
|
||||
hasAttr "properties" swagger.definitions.${refDefinition property.items}
|
||||
&& hasAttr "name" swagger.definitions.${refDefinition property.items}.properties
|
||||
then let
|
||||
mergeKey = "name";
|
||||
in {
|
||||
type = requiredOrNot (coerceAttrsOfSubmodulesToListByKey (refDefinition property.items) mergeKey (
|
||||
if hasAttr "x-kubernetes-list-map-keys" property
|
||||
then property."x-kubernetes-list-map-keys"
|
||||
else []
|
||||
));
|
||||
apply = attrsToList;
|
||||
}
|
||||
then
|
||||
let
|
||||
mergeKey = "name";
|
||||
in
|
||||
{
|
||||
type = requiredOrNot (coerceAttrsOfSubmodulesToListByKey (refDefinition property.items) mergeKey (
|
||||
if hasAttr "x-kubernetes-list-map-keys" property
|
||||
then property."x-kubernetes-list-map-keys"
|
||||
else [ ]
|
||||
));
|
||||
apply = attrsToList;
|
||||
}
|
||||
else {
|
||||
type =
|
||||
if (refDefinition property.items) == _name
|
||||
|
|
@ -195,7 +186,7 @@ with lib; let
|
|||
}
|
||||
else if property.type == "object" && hasAttr "additionalProperties" property
|
||||
then
|
||||
# if it is a reference to simple type
|
||||
# if it is a reference to simple type
|
||||
if
|
||||
(
|
||||
hasAttr "$ref" property.additionalProperties
|
||||
|
|
@ -221,63 +212,60 @@ with lib; let
|
|||
type = requiredOrNot (mapType property);
|
||||
};
|
||||
in
|
||||
mkOption ({
|
||||
description = property.description or "";
|
||||
}
|
||||
// optionProperties)
|
||||
)
|
||||
definition.properties;
|
||||
config = let
|
||||
optionalProps =
|
||||
filterAttrs
|
||||
mkOption ({
|
||||
description = property.description or "";
|
||||
}
|
||||
// optionProperties)
|
||||
)
|
||||
definition.properties;
|
||||
config =
|
||||
let
|
||||
optionalProps =
|
||||
filterAttrs
|
||||
(
|
||||
propName: _property:
|
||||
!(elem propName (definition.required or []))
|
||||
!(elem propName (definition.required or [ ]))
|
||||
)
|
||||
definition.properties;
|
||||
in
|
||||
mapAttrs (_name: _property: mkOverride 1002 null) optionalProps;
|
||||
}
|
||||
)
|
||||
swagger.definitions;
|
||||
in
|
||||
mapAttrs (_name: _property: mkOverride 1002 null) optionalProps;
|
||||
}
|
||||
)
|
||||
swagger.definitions;
|
||||
|
||||
mapCharPairs = f: s1: s2:
|
||||
concatStrings (imap0
|
||||
(
|
||||
i: c1:
|
||||
f i c1 (
|
||||
if i >= stringLength s2
|
||||
then ""
|
||||
else elemAt (stringToCharacters s2) i
|
||||
)
|
||||
mapCharPairs = f: s1: s2: concatStrings (imap0
|
||||
(i: c1:
|
||||
f i c1 (
|
||||
if i >= stringLength s2
|
||||
then ""
|
||||
else elemAt (stringToCharacters s2) i
|
||||
)
|
||||
(stringToCharacters s1));
|
||||
)
|
||||
(stringToCharacters s1));
|
||||
|
||||
getAttrName = resource: kind:
|
||||
mapCharPairs
|
||||
(
|
||||
i: c1: c2:
|
||||
if hasPrefix "API" kind && i == 0
|
||||
then "A"
|
||||
else if i == 0
|
||||
then c1
|
||||
else if c2 == "" || (toLower c2) != c1
|
||||
then c1
|
||||
else c2
|
||||
getAttrName = resource: kind: mapCharPairs
|
||||
(i: c1: c2:
|
||||
if hasPrefix "API" kind && i == 0
|
||||
then "A"
|
||||
else if i == 0
|
||||
then c1
|
||||
else if c2 == "" || (toLower c2) != c1
|
||||
then c1
|
||||
else c2
|
||||
)
|
||||
resource
|
||||
kind;
|
||||
|
||||
genResourceTypes = swagger:
|
||||
mapAttrs'
|
||||
(name: path: let
|
||||
ref = refType (head path.post.parameters).schema;
|
||||
group' = path.post."x-kubernetes-group-version-kind".group;
|
||||
version' = path.post."x-kubernetes-group-version-kind".version;
|
||||
kind' = path.post."x-kubernetes-group-version-kind".kind;
|
||||
name' = last (splitString "/" name);
|
||||
attrName = getAttrName name' kind';
|
||||
in
|
||||
genResourceTypes = swagger: mapAttrs'
|
||||
(name: path:
|
||||
let
|
||||
ref = refType (head path.post.parameters).schema;
|
||||
group' = path.post."x-kubernetes-group-version-kind".group;
|
||||
version' = path.post."x-kubernetes-group-version-kind".version;
|
||||
kind' = path.post."x-kubernetes-group-version-kind".kind;
|
||||
name' = last (splitString "/" name);
|
||||
attrName = getAttrName name' kind';
|
||||
in
|
||||
nameValuePair ref {
|
||||
inherit ref attrName;
|
||||
|
||||
|
|
@ -309,37 +297,34 @@ with lib; let
|
|||
})
|
||||
resourceTypes);
|
||||
|
||||
resourcesTypesByKindSortByVersion =
|
||||
mapAttrs
|
||||
(
|
||||
_kind: resourceTypes:
|
||||
reverseList (sort
|
||||
(
|
||||
r1: r2:
|
||||
compareVersions r1.version r2.version > 0
|
||||
)
|
||||
resourceTypes)
|
||||
resourcesTypesByKindSortByVersion = mapAttrs
|
||||
(_kind: resourceTypes:
|
||||
reverseList (sort
|
||||
(
|
||||
r1: r2:
|
||||
compareVersions r1.version r2.version > 0
|
||||
)
|
||||
resourceTypes)
|
||||
)
|
||||
resourceTypesByKind;
|
||||
|
||||
latestResourceTypesByKind =
|
||||
mapAttrs (_kind: last) resourcesTypesByKindSortByVersion;
|
||||
latestResourceTypesByKind = mapAttrs (_kind: last) resourcesTypesByKindSortByVersion;
|
||||
|
||||
genResourceOptions = resource:
|
||||
with gen; let
|
||||
submoduleForDefinition' = definition:
|
||||
submoduleForDefinition
|
||||
definition.ref
|
||||
definition.name
|
||||
definition.kind
|
||||
definition.group
|
||||
definition.version;
|
||||
definition.ref
|
||||
definition.name
|
||||
definition.kind
|
||||
definition.group
|
||||
definition.version;
|
||||
in
|
||||
mkOption {
|
||||
inherit (resource) description;
|
||||
type = types.attrsOf (submoduleForDefinition' resource);
|
||||
default = {};
|
||||
};
|
||||
mkOption {
|
||||
inherit (resource) description;
|
||||
type = types.attrsOf (submoduleForDefinition' resource);
|
||||
default = { };
|
||||
};
|
||||
|
||||
generated = ''
|
||||
# This file was generated with kubenix k8s generator, do not edit
|
||||
|
|
@ -530,14 +515,14 @@ with lib; let
|
|||
}
|
||||
'';
|
||||
in
|
||||
pkgs.runCommand "k8s-${name}-gen.nix"
|
||||
{
|
||||
buildInputs = [pkgs.nixpkgs-fmt];
|
||||
} ''
|
||||
cat << 'GENERATED' > ./raw
|
||||
${generated}
|
||||
GENERATED
|
||||
pkgs.runCommand "k8s-${name}-gen.nix"
|
||||
{
|
||||
buildInputs = [ pkgs.nixpkgs-fmt ];
|
||||
} ''
|
||||
cat << 'GENERATED' > ./raw
|
||||
${generated}
|
||||
GENERATED
|
||||
|
||||
nixpkgs-fmt ./raw
|
||||
cp ./raw $out
|
||||
''
|
||||
nixpkgs-fmt ./raw
|
||||
cp ./raw $out
|
||||
''
|
||||
|
|
|
|||
158
pkgs/kubenix.nix
158
pkgs/kubenix.nix
|
|
@ -1,99 +1,95 @@
|
|||
{
|
||||
kubectl,
|
||||
vals,
|
||||
colordiff,
|
||||
evalModules,
|
||||
runCommand,
|
||||
writeShellScript,
|
||||
module ? {},
|
||||
specialArgs ? {},
|
||||
}: let
|
||||
kubernetes =
|
||||
(evalModules {
|
||||
inherit module specialArgs;
|
||||
})
|
||||
.config
|
||||
.kubernetes
|
||||
or {};
|
||||
{ kubectl
|
||||
, vals
|
||||
, colordiff
|
||||
, evalModules
|
||||
, runCommand
|
||||
, writeShellScript
|
||||
, module ? { }
|
||||
, specialArgs ? { }
|
||||
}:
|
||||
let
|
||||
kubernetes = (evalModules {
|
||||
inherit module specialArgs;
|
||||
}).config.kubernetes or { };
|
||||
in
|
||||
runCommand "kubenix"
|
||||
{
|
||||
kubeconfig = kubernetes.kubeconfig or "";
|
||||
result = kubernetes.result or "";
|
||||
runCommand "kubenix"
|
||||
{
|
||||
kubeconfig = kubernetes.kubeconfig or "";
|
||||
result = kubernetes.result or "";
|
||||
|
||||
# kubectl does some parsing which removes the -I flag so
|
||||
# as workaround, we write to a script and call that
|
||||
# https://github.com/kubernetes/kubernetes/pull/108199#issuecomment-1058405404
|
||||
diff = writeShellScript "kubenix-diff" ''
|
||||
${colordiff}/bin/colordiff --nobanner -N -u -I ' kubenix/hash: ' -I ' generation: ' $@
|
||||
'';
|
||||
} ''
|
||||
set -euo pipefail
|
||||
mkdir -p $out/bin
|
||||
# kubectl does some parsing which removes the -I flag so
|
||||
# as workaround, we write to a script and call that
|
||||
# https://github.com/kubernetes/kubernetes/pull/108199#issuecomment-1058405404
|
||||
diff = writeShellScript "kubenix-diff" ''
|
||||
${colordiff}/bin/colordiff --nobanner -N -u -I ' kubenix/hash: ' -I ' generation: ' $@
|
||||
'';
|
||||
} ''
|
||||
set -euo pipefail
|
||||
mkdir -p $out/bin
|
||||
|
||||
# write the manifests for use with `nix build`
|
||||
ln -s $result $out/manifest.json
|
||||
# write the manifests for use with `nix build`
|
||||
ln -s $result $out/manifest.json
|
||||
|
||||
# create a script for `nix run`
|
||||
cat <<EOF> $out/bin/kubenix
|
||||
set -uo pipefail
|
||||
# create a script for `nix run`
|
||||
cat <<EOF> $out/bin/kubenix
|
||||
set -uo pipefail
|
||||
|
||||
export KUBECONFIG=$kubeconfig
|
||||
export KUBECTL_EXTERNAL_DIFF=$diff
|
||||
export KUBECONFIG=$kubeconfig
|
||||
export KUBECTL_EXTERNAL_DIFF=$diff
|
||||
|
||||
function _help() {
|
||||
echo "
|
||||
kubenix - Kubernetes management with Nix
|
||||
function _help() {
|
||||
echo "
|
||||
kubenix - Kubernetes management with Nix
|
||||
|
||||
commands:
|
||||
"" - run diff, prompt for confirmation, then apply
|
||||
apply - create resources in target cluster
|
||||
diff - show a diff between configured and live resources
|
||||
render - print resource manifests to stdout
|
||||
commands:
|
||||
"" - run diff, prompt for confirmation, then apply
|
||||
apply - create resources in target cluster
|
||||
diff - show a diff between configured and live resources
|
||||
render - print resource manifests to stdout
|
||||
|
||||
options:
|
||||
-h --help - show this menu
|
||||
"
|
||||
}
|
||||
options:
|
||||
-h --help - show this menu
|
||||
"
|
||||
}
|
||||
|
||||
function _kubectl() {
|
||||
${vals}/bin/vals eval -fail-on-missing-key-in-map < $result | ${kubectl}/bin/kubectl \$@
|
||||
}
|
||||
function _kubectl() {
|
||||
${vals}/bin/vals eval -fail-on-missing-key-in-map < $result | ${kubectl}/bin/kubectl \$@
|
||||
}
|
||||
|
||||
# if no args given, add empty string
|
||||
[ \$# -eq 0 ] && set -- ""
|
||||
# if no args given, add empty string
|
||||
[ \$# -eq 0 ] && set -- ""
|
||||
|
||||
# parse arguments
|
||||
while test \$# -gt 0; do
|
||||
case "\$1" in
|
||||
# parse arguments
|
||||
while test \$# -gt 0; do
|
||||
case "\$1" in
|
||||
|
||||
-h|--help)
|
||||
_help
|
||||
exit 0;;
|
||||
-h|--help)
|
||||
_help
|
||||
exit 0;;
|
||||
|
||||
"")
|
||||
_kubectl diff -f - --prune
|
||||
if [[ "\$?" -eq 1 ]]; then
|
||||
read -p 'apply? [y/N]: ' response
|
||||
[[ \$response == "y" ]] && _kubectl apply -f - --prune --all
|
||||
fi
|
||||
shift;;
|
||||
"")
|
||||
_kubectl diff -f - --prune
|
||||
if [[ "\$?" -eq 1 ]]; then
|
||||
read -p 'apply? [y/N]: ' response
|
||||
[[ \$response == "y" ]] && _kubectl apply -f - --prune --all
|
||||
fi
|
||||
shift;;
|
||||
|
||||
render)
|
||||
${vals}/bin/vals eval < $result
|
||||
shift;;
|
||||
render)
|
||||
${vals}/bin/vals eval < $result
|
||||
shift;;
|
||||
|
||||
apply|diff)
|
||||
_kubectl \$@ -f - --prune
|
||||
shift;;
|
||||
apply|diff)
|
||||
_kubectl \$@ -f - --prune
|
||||
shift;;
|
||||
|
||||
*)
|
||||
_kubectl \$@
|
||||
shift;;
|
||||
*)
|
||||
_kubectl \$@
|
||||
shift;;
|
||||
|
||||
esac
|
||||
done
|
||||
esac
|
||||
done
|
||||
|
||||
EOF
|
||||
chmod +x $out/bin/kubenix
|
||||
''
|
||||
EOF
|
||||
chmod +x $out/bin/kubenix
|
||||
''
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue