From 42e73e0b581dc38fa492a2021527b5009f29f9bd Mon Sep 17 00:00:00 2001 From: ties Date: Tue, 27 Aug 2024 10:43:42 +0000 Subject: [PATCH] Update createHelmYaml.sh --- createHelmYaml.sh | 86 +++++++++++++++++++++++++++-------------------- 1 file changed, 49 insertions(+), 37 deletions(-) diff --git a/createHelmYaml.sh b/createHelmYaml.sh index df43e60..99b39ff 100644 --- a/createHelmYaml.sh +++ b/createHelmYaml.sh @@ -1,41 +1,53 @@ #!/bin/bash -# Load values from Chart.yaml -chart_version=$(yq e '.dependencies[0].version' Chart.yaml) -chart_name=$(yq e '.name' Chart.yaml) -chart_repo=$(yq e '.dependencies[0].repository' Chart.yaml) -chart_namespace=$(yq e '.annotations.namespace' Chart.yaml) +# Function to create HelmChart YAML +createHelmYaml() { + # Load values from Chart.yaml + chart_version=$(yq e '.version' Chart.yaml) + chart_name=$(yq e '.name' Chart.yaml) + chart_repo=$(yq e '.dependencies[0].repository' Chart.yaml) + chart_namespace=$(yq e '.annotations.namespace' Chart.yaml) -# Check if chart_namespace is empty and default to chart_name if it is -if [[ -z "$chart_namespace" ]]; then - chart_namespace="$chart_name" + # Check if chart_namespace is empty and default to chart_name if it is + if [[ -z "$chart_namespace" ]]; then + chart_namespace="$chart_name" + fi + + # Determine the correct chart and repo values based on chart_repo + if [[ $chart_repo == oci://* ]]; then + chart="${chart_repo}/${chart_name}" + else + repo="$chart_repo" + chart="$chart_name" + fi + + # Load the raw content of values.yaml + values_content=$(cat values.yaml) + + # Create HelmChart content in a variable + helm_chart_yaml=$(yq eval -n ".apiVersion = \"helm.cattle.io/v1\" | + .kind = \"HelmChart\" | + .metadata.name = \"$chart_name\" | + .metadata.namespace = \"$chart_namespace\" | + .spec.chart = \"$chart\" | + .spec.version = \"$chart_version\" | + .spec.targetNamespace = \"$chart_namespace\" | + .spec.valuesContent = \"$values_content\"") + + # If repo is set, add it to the spec + if [[ -n "$repo" ]]; then + helm_chart_yaml=$(echo "$helm_chart_yaml" | yq eval ".spec.repo = \"$repo\"") + elif + kubectl version + fi + + # Print the final YAML to stdout + echo "$helm_chart_yaml" +} + +# Check if the action environment variable is set to 'print' +if [[ "$action" == "print" ]]; then + createHelmYaml +else: + kubectl version --client=true fi - -# Determine the correct chart and repo values based on chart_repo -if [[ $chart_repo == oci://* ]]; then - chart="${chart_repo}/${chart_name}" -else - repo="$chart_repo" - chart="$chart_name" -fi - -# Load the raw content of values.yaml -values_content=$(cat values.yaml) - -# Create HelmChart content in a variable -helm_chart_yaml=$(yq eval -n ".apiVersion = \"helm.cattle.io/v1\" | - .kind = \"HelmChart\" | - .metadata.name = \"$chart_name\" | - .metadata.namespace = \"$chart_namespace\" | - .spec.chart = \"$chart\" | - .spec.version = \"$chart_version\" | - .spec.targetNamespace = \"$chart_namespace\" | - .spec.valuesContent = \"$values_content\"") - -# If repo is set, add it to the spec -if [[ -n "$repo" ]]; then - helm_chart_yaml=$(echo "$helm_chart_yaml" | yq eval ".spec.repo = \"$repo\"") -fi - -# Print the final YAML to stdout -echo "$helm_chart_yaml" \ No newline at end of file