diff --git a/createHelmYaml.sh b/createHelmYaml.sh index fdbcab0..c456524 100644 --- a/createHelmYaml.sh +++ b/createHelmYaml.sh @@ -44,6 +44,25 @@ createHelmYaml() { echo "$helm_chart_yaml" } +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..." + bash createServiceAccount.sh + false + exit 1 + fi +} + # if actions is not set if [[ -z "$action" ]]; then action="print" @@ -54,33 +73,25 @@ echo "# action: $action" if [[ -n "$KUBECONFIG_DATA" ]]; then mkdir -p /root/.kube echo "$KUBECONFIG_DATA" > /root/.kube/config - echo "# .kube/config is set" fi # Check if the action environment variable is set to 'print' if [[ "$action" == "print" ]]; then createHelmYaml -elif [[ "$action" == "deploy" ]] && [[ -n "$KUBECONFIG_DATA" ]]; then - kubectl version - createHelmYaml | kubectl apply -f - -elif [[ "$action" == "deploy-dry-run" ]] && [[ -n "$KUBECONFIG_DATA" ]]; then - kubectl version - createHelmYaml | kubectl apply --dry-run=client -o yaml -f - elif [[ "$action" == "deploy" ]]; then - echo "# no .kube/config set" - kubectl version --client=true + checkKubeConfig + createHelmYaml | kubectl apply -f - +elif [[ "$action" == "helm-deploy" ]]; then + checkKubeConfig + createHelmYaml | kubectl apply -f - elif [[ "$action" == "helm-print" ]]; then helm dependency build . helm template -g . --set-json="subchartData=$(yq '.dependencies[0]' Chart.yaml -ojson | jq -rc)" elif [[ "$action" == "helm-deploy" ]]; then + checkKubeConfig helm dependency build . helm template -g . --set-json="subchartData=$(yq '.dependencies[0]' Chart.yaml -ojson | jq -rc)" helm template -g . --set-json="subchartData=$(yq '.dependencies[0]' Chart.yaml -ojson | jq -rc)" | kubectl apply -f - -elif [[ "$action" == "debug" ]]; then - echo "# dumping files" - find . -type f -print -exec cat {} \; - echo "# Computed file" - createHelmYaml else echo "# invalid action" fi