kubernetes-deployments/kubenix.nix

101 lines
2.7 KiB
Nix
Raw Normal View History

2024-10-28 15:05:06 +00:00
inputs @ {
self,
flake-utils,
nixpkgs,
kubenix,
...
}:
flake-utils.lib.eachDefaultSystem
(system: let
pkgs = nixpkgs.legacyPackages.${system};
lib = pkgs.lib;
deployScript = (pkgs.writeScriptBin "applyset-deploy.sh" (builtins.readFile ./applyset-deploy.sh)).overrideAttrs (old: {
buildCommand = "${old.buildCommand}\npatchShebangs $out";
});
2024-09-07 19:59:41 +00:00
2024-10-28 15:05:06 +00:00
mkKubernetes = name: module: namespace:
(kubenix.evalModules.${system} {
2024-09-07 19:59:41 +00:00
specialArgs = {
inherit namespace system self;
inherit (inputs) nixhelm blog-pim dns nixpkgs nixng;
inherit (self) globals;
utils = import ./utils.nix {
inherit pkgs;
inherit (inputs) nixpkgs nixng;
inherit (self) globals;
};
2024-09-07 19:59:41 +00:00
};
2024-10-28 15:05:06 +00:00
module = {kubenix, ...}: {
imports = [
kubenix.modules.k8s
kubenix.modules.helm
./modules
module
];
2024-09-07 19:59:41 +00:00
2024-10-28 15:05:06 +00:00
config = {
kubenix.project = name;
kubernetes.namespace = namespace;
2024-09-07 19:59:41 +00:00
};
2024-10-28 15:05:06 +00:00
};
})
.config
.kubernetes;
2024-09-07 19:59:41 +00:00
2024-10-28 15:05:06 +00:00
mkManifest = name: {
module,
namespace,
}: {
name = "${name}-manifest";
value = (mkKubernetes name module namespace).result;
};
2024-09-07 19:59:41 +00:00
2024-10-28 15:05:06 +00:00
mkDeployApp = name: {
module,
namespace,
}: let
kubernetes = mkKubernetes name module namespace;
kubeconfig = kubernetes.kubeconfig or "";
result = kubernetes.result or "";
2024-09-07 19:59:41 +00:00
2024-10-28 15:05:06 +00:00
wrappedDeployScript =
pkgs.symlinkJoin
2024-09-07 19:59:41 +00:00
{
2024-10-28 15:05:06 +00:00
name = "applyset-deploy.sh";
paths = [deployScript pkgs.vals pkgs.kubectl];
buildInputs = [pkgs.makeWrapper];
passthru.manifest = result;
meta.mainProgram = "applyset-deploy.sh";
postBuild = let
# HACK: create normal way of checking if server runs k8s
k8sMachines = builtins.attrNames self.globals.nodeLabels;
k8sServerNames = builtins.concatStringsSep " " k8sMachines;
2024-10-28 15:05:06 +00:00
in
/*
bash
*/
''
wrapProgram $out/bin/applyset-deploy.sh \
--suffix PATH : "$out/bin" \
--run 'export KUBECONFIG=''${KUBECONFIG:-${toString kubeconfig}}' \
--set MANIFEST '${result}' \
--set NAME '${name}' \
--set NAMESPACE '${namespace}' \
--set SERVERS '${k8sServerNames}' \
--set DYFF '${lib.getExe pkgs.dyff}' \
--set GCROOTDIR '/nix/var/nix/gcroots/kubernetes-manifests'
'';
2024-09-07 19:59:41 +00:00
};
2024-10-28 15:05:06 +00:00
in {
name = "${name}-deploy";
value = wrappedDeployScript;
};
2024-09-07 19:59:41 +00:00
2024-10-28 15:05:06 +00:00
deployments = import ./deployments.nix;
in {
packages = pkgs.lib.mergeAttrs (pkgs.lib.mapAttrs' mkDeployApp deployments) (pkgs.lib.mapAttrs' mkManifest deployments);
})