Skip to content

Instantly share code, notes, and snippets.

@curx
Last active July 28, 2025 13:13
Show Gist options
  • Select an option

  • Save curx/ae1fb5619ae226b3807fcf8e6e1f5bdc to your computer and use it in GitHub Desktop.

Select an option

Save curx/ae1fb5619ae226b3807fcf8e6e1f5bdc to your computer and use it in GitHub Desktop.
create CRD rancher helm chart the easy
#!/usr/bin/env bash
## desc: create k3s/rke2 crd helmChart from a default helmChart
## author: Thorsten Schifferdecker https://github.com/curx
## license: Apache 2.0
## vars
helmrepo="${1:?error no helm repository given, abort.}"
helmchart_name="${2:?error no helm name given, abort.}"
helmchart_namespace="${3:-${helmchart_name}-system}"
helmchart_crd_export="${4:-${PWD}}/helmchartcrd-$helmchart_name.yaml"
helmchart_version="${5:-""}"
helmchart_included="${6:-no}"
## main
# fetch helmchart url
helmchart_url="$(curl -sL ${helmrepo}/index.yaml | awk '/tgz/ { print $2 }' | grep -m1 -e $helmchart_name".*"$helmchart_version - )"
# if no url is included add helmrepo url
if [ "$(echo $helmchart_url | grep -c http )" -eq "0" ]; then
helmchart_url="${helmrepo}/${helmchart_url}"
fi
# create kubernetes namespace
echo "[-] Create CRD k3s/rke2 helmChart \"$helmchart_name\" from \"$helmrepo\" $helmchart_version"
echo " templated to file \"$helmchart_crd_export\""
# template CustomResourceDefinintion helmChart
cat << EOL > $helmchart_crd_export
---
# task: create namespace
#
apiVersion: v1
kind: Namespace
metadata:
creationTimestamp: null
name: ${helmchart_namespace}
---
# task: create helmchart for rke2/k3s
#
apiVersion: helm.cattle.io/v1
kind: HelmChart
metadata:
name: ${helmchart_name}
namespace: kube-system
spec:
helmVersion: v3
bootstrap: false
targetNamespace: ${helmchart_namespace}
EOL
if [ "$helmchart_included" = "yes" ]; then
cat << EOL >> $helmchart_crd_export
chartContent: $(curl --silent --location ${helmchart_url} | base64 -w 0 -)
EOL
else
cat << EOL >> $helmchart_crd_export
chart: ${helmchart_url}
EOL
fi
# add default values from helmchart
cat << EOL >> $helmchart_crd_export
valuesContent: |-
$(curl --silent --location ${helmchart_url} | tar xzf - ${helmchart_name}/values.yaml --strip-components=1 --to-stdout | paste /dev/null - | expand -4)
EOL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment