refactor(flake): Improve flake outputs for k8s scripts and manifests

docs(readme): Update k8s deployment instructions
This commit is contained in:
Pim Kunis 2024-07-17 18:20:49 +02:00
parent 7a40738989
commit 835aea667c
3 changed files with 48 additions and 60 deletions

View file

@ -43,36 +43,18 @@ To deploy to the Kubernetes cluster, first make sure you have an admin account o
You can generate this using `nix run '.#gen-k3s-cert' <username> <servername> ~/.kube`, assuming you have SSH access to the master node.
This puts a private key, signed certificate and a kubeconfig in the kubeconfig directory
If the cluster has not been initialized yet, next run `nix run '.#kubenix.x86_64-linux.bootstrap.deploy'`.
We are now ready to deploy to the Kubernetes cluster.
Deployments are done through an experimental Kubernetes feature called [ApplySets](https://kubernetes.io/docs/tasks/manage-kubernetes-objects/declarative-config/#how-to-delete-objects).
Each applyset is responsible for a set number of resources within a namespace.
Applications are currently deployed in two method:
- A single big deployment of many applications (which I am trying to move away from)
- A separate deployment for each application using [ApplySets](https://kubernetes.io/docs/tasks/manage-kubernetes-objects/declarative-config/#how-to-delete-objects)
If the cluster has not been initialized yet, we must bootstrap it first.
Run these deployments:
- `nix run '.#bootstrap-default.deploy'`
- `nix run '.#bootstrap-kube-system.deploy'`
The first method: `nix run '.#kubenix.x86_64-linux.all.deploy'`
The second method: `nix run '.#kubenix.x86_64-linux.<application>.deploy'`
Currently, the applications being deployed like this are:
- `cyberchef`
- `freshrss`
- `radicale`
- `kms`
- `atuin`
- `blog`
- `nextcloud`
- `hedgedoc`
- `kitchenowl`
- `forgejo`
- `paperless-ngx`
- `syncthing`
- `pihole`
- `immich`
- `attic`
- `inbucket`
- `dnsmasq`
- `bind9`
- `media`
- `traefik`
- `minecraft`
Now the cluster has been initialized and we can deploy applications.
To explore which applications we can deploy, run `nix flake show`.
Then, for each application, run `nix run '.#<application>.deploy'`.
## Known bugs

View file

@ -1,37 +1,11 @@
{ self, pkgs, machines, dns, myLib, flake-utils, kubenix, nixhelm, blog-pim, ... }: flake-utils.lib.eachDefaultSystem
(system:
let
deployScript = (pkgs.writeScriptBin "kubenix" (builtins.readFile ./kubenix-deploy.sh)).overrideAttrs (old: {
deployScript = (pkgs.writeScriptBin "applyset-deploy.sh" (builtins.readFile ./applyset-deploy.sh)).overrideAttrs (old: {
buildCommand = "${old.buildCommand}\npatchShebangs $out";
});
mkDeployScript = kubernetes: applysetName: namespace:
let
kubeconfig = kubernetes.kubeconfig or "";
result = kubernetes.result or "";
wrappedDeployScript = pkgs.symlinkJoin
{
name = "kubenix";
paths = [ deployScript pkgs.vals pkgs.kubectl ];
buildInputs = [ pkgs.makeWrapper ];
passthru.manifest = result;
postBuild = ''
wrapProgram $out/bin/kubenix \
--suffix PATH : "$out/bin" \
--run 'export KUBECONFIG=''${KUBECONFIG:-${toString kubeconfig}}' \
--set MANIFEST '${result}' \
--set APPLYSET 'applyset-${applysetName}' \
--set NAMESPACE '${namespace}'
'';
};
in
wrappedDeployScript;
mkDeployScriptAndManifest = name: { module, namespace }:
let
kubernetes = (kubenix.evalModules.${system} {
mkKubernetes = name: module: namespace: (kubenix.evalModules.${system} {
specialArgs = { inherit namespace myLib blog-pim dns nixhelm system machines; };
module = { kubenix, ... }:
@ -50,10 +24,41 @@
};
};
}).config.kubernetes;
mkManifest = name: { module, namespace }:
{
manifest = (mkKubernetes name module namespace).result;
};
mkDeployApp = name: { module, namespace }:
let
kubernetes = mkKubernetes name module namespace;
kubeconfig = kubernetes.kubeconfig or "";
result = kubernetes.result or "";
wrappedDeployScript = pkgs.symlinkJoin
{
name = "applyset-deploy.sh";
paths = [ deployScript pkgs.vals pkgs.kubectl ];
buildInputs = [ pkgs.makeWrapper ];
passthru.manifest = result;
meta.mainProgram = "applyset-deploy.sh";
postBuild = ''
wrapProgram $out/bin/applyset-deploy.sh \
--suffix PATH : "$out/bin" \
--run 'export KUBECONFIG=''${KUBECONFIG:-${toString kubeconfig}}' \
--set MANIFEST '${result}' \
--set APPLYSET 'applyset-${name}' \
--set NAMESPACE '${namespace}'
'';
};
in
{
manifest = kubernetes.result;
deploy = mkDeployScript kubernetes name namespace;
deploy = {
type = "app";
program = "${pkgs.lib.getExe wrappedDeployScript}";
};
};
deployers = {
@ -174,5 +179,6 @@
};
in
{
kubenix = builtins.mapAttrs mkDeployScriptAndManifest deployers;
apps = builtins.mapAttrs mkDeployApp deployers;
packages = builtins.mapAttrs mkManifest deployers;
})