mirror of
https://github.com/TECHNOFAB11/kubenix.git
synced 2025-12-12 08:00:06 +01:00
feat: implement k8s-submodules
This commit is contained in:
parent
0abf88e0d5
commit
ecf531aeb4
3 changed files with 115 additions and 0 deletions
|
|
@ -26,6 +26,7 @@ let
|
||||||
lib = lib';
|
lib = lib';
|
||||||
submodules = ./submodules.nix;
|
submodules = ./submodules.nix;
|
||||||
k8s = ./k8s;
|
k8s = ./k8s;
|
||||||
|
k8s-submodules = ./k8s/submodule.nix;
|
||||||
istio = ./istio;
|
istio = ./istio;
|
||||||
testing = ./testing;
|
testing = ./testing;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
52
k8s/submodule.nix
Normal file
52
k8s/submodule.nix
Normal file
|
|
@ -0,0 +1,52 @@
|
||||||
|
{ config, lib, kubenix, ... }:
|
||||||
|
|
||||||
|
with lib;
|
||||||
|
|
||||||
|
let
|
||||||
|
globalConfig = config;
|
||||||
|
in {
|
||||||
|
imports = [ kubenix.submodules ];
|
||||||
|
|
||||||
|
options = {
|
||||||
|
kubernetes.propagateDefaults = mkOption {
|
||||||
|
description = "Whehter 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.all.metadata.namespace =
|
||||||
|
mkDefault config.namespace;
|
||||||
|
};
|
||||||
|
}));
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
config = {
|
||||||
|
submodules.defaults = mkMerge [{
|
||||||
|
imports = [ kubenix.k8s ];
|
||||||
|
kubernetes.version = mkDefault config.kubernetes.version;
|
||||||
|
kubernetes.api.defaults =
|
||||||
|
mkIf config.kubernetes.propagateDefaults config.kubernetes.api.defaults;
|
||||||
|
} ({config, ...}: {
|
||||||
|
kubernetes.api.defaults.all.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);
|
||||||
|
};
|
||||||
|
}
|
||||||
62
tests/k8s/submodule.nix
Normal file
62
tests/k8s/submodule.nix
Normal file
|
|
@ -0,0 +1,62 @@
|
||||||
|
{ name, config, lib, kubenix, images, ... }:
|
||||||
|
|
||||||
|
with lib;
|
||||||
|
|
||||||
|
let
|
||||||
|
cfg = config.submodules.instances.test.config;
|
||||||
|
deployment = cfg.kubernetes.api.deployments.nginx;
|
||||||
|
image = images.nginx;
|
||||||
|
in {
|
||||||
|
imports = [
|
||||||
|
kubenix.k8s-submodules
|
||||||
|
];
|
||||||
|
|
||||||
|
test = {
|
||||||
|
name = "k8s-submodules";
|
||||||
|
description = "Simple k8s submodule test";
|
||||||
|
assertions = [{
|
||||||
|
message = "Namespace not propagated";
|
||||||
|
assertion = deployment.metadata.namespace == "test";
|
||||||
|
} {
|
||||||
|
message = "Version not propagated";
|
||||||
|
assertion = cfg.kubernetes.version == config.kubernetes.version;
|
||||||
|
}];
|
||||||
|
testScript = ''
|
||||||
|
$kube->waitUntilSucceeds("docker load < ${image}");
|
||||||
|
$kube->waitUntilSucceeds("kubectl apply -f ${toYAML config.kubernetes.generated}");
|
||||||
|
|
||||||
|
$kube->succeed("kubectl get deployment -n test | grep -i test-nginx");
|
||||||
|
$kube->waitUntilSucceeds("kubectl get deployment -n test -o go-template test-nginx --template={{.status.readyReplicas}} | grep 1");
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
|
||||||
|
submodules.imports = [{
|
||||||
|
module = {name, config, ...}: {
|
||||||
|
submodule.name = "nginx";
|
||||||
|
kubernetes.api.deployments.nginx = {
|
||||||
|
metadata = {
|
||||||
|
name = "${name}-nginx";
|
||||||
|
labels.app = name;
|
||||||
|
};
|
||||||
|
spec = {
|
||||||
|
replicas = 1;
|
||||||
|
selector.matchLabels.app = "nginx";
|
||||||
|
template.metadata.labels.app = "nginx";
|
||||||
|
template.spec = {
|
||||||
|
containers.nginx = {
|
||||||
|
image = "${image.imageName}:${image.imageTag}";
|
||||||
|
imagePullPolicy = "Never";
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}];
|
||||||
|
|
||||||
|
kubernetes.api.namespaces.test = {};
|
||||||
|
|
||||||
|
submodules.instances.test = {
|
||||||
|
submodule = "nginx";
|
||||||
|
namespace = "test";
|
||||||
|
};
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue