#!/bin/bash function checkKubeConfig(){ echo "# Check if kubeconfig exists..." if [[ -n "$KUBECONFIG_DATA" ]]; then echo "Check if kubeconfig is valid..." kubectl version if [ $? -ne 0 ]; then echo "# Failed to connect to Kubernetes..." bash createServiceAccount.sh false exit 1 fi else echo "# No kubeconfig available..." createServiceAccount.sh false exit 1 fi } function printGlobal(){ if [ -e "Chart.yaml" ]; then printHelm else printRaw fi } function deployGlobal(){ if [ -e "Chart.yaml" ]; then deployHelm else deployRaw fi } function printRaw(){ cat ./*.yaml } function deployRaw(){ printRaw checkKubeConfig cat ./*.yaml | kubectl apply -f - } function printHelm(){ helm dependency build . helm dependency list helm template -g . --set-json="chart_template.subchartData=$(yq '.dependencies[] | select(.condition == "subchart")' Chart.yaml -ojson | jq -rc)" } function deployHelm(){ printHelm checkKubeConfig echo "Deploying in 5 seconds..." sleep 5 helm template -g --set-json="chart_template.subchartData=$(yq '.dependencies[] | select(.condition == "subchart")' Chart.yaml -ojson | jq -rc)" . | kubectl apply -f - } function packageHelm(){ if [[ -z "$REPO_SECRET" ]]; then echo "No credentials..." false exit 1 fi cd ${REPO_PATH:-.} helm dependency build . helm dependency list helm package . helmPackage="$(yq '.name' Chart.yaml)-$(yq '.version' Chart.yaml)" curl --user "$REPO_SECRET" -X POST --upload-file ./$helmPackage.tgz https://git.ties.one/api/packages/public/helm/api/charts } # if actions is not set if [[ -z "$action" ]]; then action="print" fi echo "# Action: $action" # if KUBECONFIG_DATA is set if [[ -n "$KUBECONFIG_DATA" ]]; then mkdir -p /root/.kube echo "$KUBECONFIG_DATA" > /root/.kube/config chmod 600 /root/.kube/config fi # Check if the action environment variable is set to 'print' if [[ "$action" == "print" ]]; then printGlobal elif [[ "$action" == "deploy" ]]; then deployGlobal elif [[ "$action" == "helm-deploy" ]]; then deployHelm elif [[ "$action" == "package" ]]; then packageHelm else echo "# invalid action" fi