mirror of
https://github.com/TECHNOFAB11/kubenix.git
synced 2025-12-12 16:10:05 +01:00
add helm cli functionality
This commit is contained in:
parent
9f0ae6e362
commit
d6ae84c7c7
2 changed files with 88 additions and 68 deletions
13
README.md
13
README.md
|
|
@ -59,14 +59,13 @@ Render all resources with
|
||||||
|
|
||||||
### Support
|
### Support
|
||||||
|
|
||||||
The following table gives a general overview of currently supported functionality.
|
The following table gives a general overview of currently supported/planned functionality.
|
||||||
|
|
||||||
| | kubectl | kustomize | helm | helmfile |
|
| | kubectl | kustomize | helm | helmfile |
|
||||||
| ------ | :-----: | :-------: | :---: | :------: |
|
| --------- | :-----: | :-------: | :---: | :------: |
|
||||||
| render | x | | x[^2] | |
|
| render | x | | x[^2] | |
|
||||||
| diff | | | | |
|
| diff | x | | x | |
|
||||||
| apply | x[^1] | | | |
|
| apply[^1] | x | | x | |
|
||||||
| hooks | - | - | | |
|
|
||||||
|
|
||||||
[^1]: currently create-only
|
[^1]: currently create-only
|
||||||
[^2]: piping rendered helm charts to kubectl is a lossy process (e.g., [hooks](https://helm.sh/docs/topics/charts_hooks/) will not work)
|
[^2]: piping rendered helm charts to kubectl is a lossy process (e.g., [hooks](https://helm.sh/docs/topics/charts_hooks/) will not work)
|
||||||
|
|
|
||||||
143
pkgs/kubenix.nix
143
pkgs/kubenix.nix
|
|
@ -1,71 +1,92 @@
|
||||||
{
|
{ lib
|
||||||
lib,
|
, writeShellScriptBin
|
||||||
writeShellScriptBin,
|
, nix
|
||||||
coreutils,
|
, jq
|
||||||
nix,
|
, kubectl
|
||||||
jq,
|
, kubernetes-helm
|
||||||
kubectl,
|
,
|
||||||
}: let
|
}:
|
||||||
name = "kubenix";
|
writeShellScriptBin "kubenix" ''
|
||||||
in
|
set -Eeuo pipefail
|
||||||
lib.recursiveUpdate (writeShellScriptBin name ''
|
|
||||||
set -Eeuo pipefail
|
|
||||||
|
|
||||||
NAME=${name}
|
function _help() {
|
||||||
function help() {
|
echo "
|
||||||
echo "
|
kubenix - Kubernetes resource management with Nix
|
||||||
kubenix - Kubernetes resource management with Nix
|
|
||||||
|
|
||||||
commands:
|
commands:
|
||||||
apply - create resources in target cluster
|
apply - create resources in target cluster
|
||||||
diff - show a diff between rendered and live resources
|
diff - show a diff between configured and live resources
|
||||||
render - print resource manifests to stdout
|
render - print resource manifests to stdout
|
||||||
"
|
"
|
||||||
}
|
}
|
||||||
|
|
||||||
MANIFEST="$(${nix}/bin/nix eval '.#k8s.config.kubernetes.result' --raw)"
|
function _helm() {
|
||||||
|
RELEASES="$(${nix}/bin/nix eval '.#k8s.config.kubernetes.helm' --json | jq -c '.releases[] | del(.objects)')"
|
||||||
|
[ -n "$RELEASES" ] || return 0
|
||||||
|
|
||||||
function apply() {
|
for release in $RELEASES; do
|
||||||
${kubectl}/bin/kubectl apply -f $MANIFEST
|
values=$(mktemp)
|
||||||
}
|
echo $release | jq -r '.values' > $values
|
||||||
|
|
||||||
function render() {
|
${kubernetes-helm}/bin/helm $@ \
|
||||||
cat $MANIFEST | ${jq}/bin/jq
|
-n $(echo $release | jq -r '.namespace // "default"') \
|
||||||
}
|
$(echo $release | jq -r '.name') \
|
||||||
|
$(echo $release | jq -r '.chart') \
|
||||||
|
-f $values
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
function diff() {
|
function _kubectl() {
|
||||||
${kubectl}/bin/kubectl diff -f $MANIFEST
|
MANIFESTS=$(mktemp)
|
||||||
}
|
# TODO: find a better filter, not just not-helm
|
||||||
|
cat $(${nix}/bin/nix build '.#k8s.config.kubernetes.result' --json | jq -r '.[0].outputs.out') \
|
||||||
|
| jq '.items[] | select(.metadata.labels."app.kubernetes.io/managed-by" != "Helm")' > $MANIFESTS
|
||||||
|
|
||||||
|
[ -n "$MANIFESTS" ] || return 0
|
||||||
|
|
||||||
|
case $1 in
|
||||||
|
render)
|
||||||
|
cat $MANIFESTS;;
|
||||||
|
*)
|
||||||
|
${kubectl}/bin/kubectl $@ -f $MANIFESTS;;
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
|
# if no args given, add empty string
|
||||||
|
[ $# -eq 0 ] && set -- ""
|
||||||
|
|
||||||
|
# parse arguments
|
||||||
|
while test $# -gt 0; do
|
||||||
|
case "$1" in
|
||||||
|
|
||||||
|
apply)
|
||||||
|
_kubectl apply
|
||||||
|
_helm upgrade --install
|
||||||
|
shift;;
|
||||||
|
|
||||||
|
diff)
|
||||||
|
_kubectl diff
|
||||||
|
_helm diff upgrade --allow-unreleased
|
||||||
|
shift;;
|
||||||
|
|
||||||
|
render)
|
||||||
|
_kubectl render
|
||||||
|
_helm template
|
||||||
|
shift;;
|
||||||
|
|
||||||
|
-h|--help|"")
|
||||||
|
_help
|
||||||
|
exit 0;;
|
||||||
|
|
||||||
|
-v|--verbose)
|
||||||
|
set -x
|
||||||
|
shift;;
|
||||||
|
|
||||||
|
*)
|
||||||
|
_help
|
||||||
|
exit 1;;
|
||||||
|
|
||||||
while test $# -gt 0; do
|
|
||||||
case "$1" in
|
|
||||||
apply|"")
|
|
||||||
shift
|
|
||||||
apply
|
|
||||||
;;
|
|
||||||
diff)
|
|
||||||
shift
|
|
||||||
diff
|
|
||||||
;;
|
|
||||||
render)
|
|
||||||
shift
|
|
||||||
render
|
|
||||||
;;
|
|
||||||
-h|--help)
|
|
||||||
help
|
|
||||||
exit 0
|
|
||||||
;;
|
|
||||||
-v|--verbose)
|
|
||||||
shift
|
|
||||||
set -x
|
|
||||||
;;
|
|
||||||
*)
|
|
||||||
help
|
|
||||||
exit 1
|
|
||||||
;;
|
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
''
|
||||||
|
|
||||||
|
|
||||||
'')
|
|
||||||
{meta.description = "";}
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue