diff --git a/README.md b/README.md index bab0354..48cbd18 100644 --- a/README.md +++ b/README.md @@ -3,12 +3,12 @@ ![GitHub contributors](https://img.shields.io/github/contributors/JovianX/helm-release-plugin) [![GitHub stars](https://img.shields.io/github/stars/JovianX/helm-release-plugin)](https://github.com/JovianX/helm-release-plugin/stargazers) Add a star ⭐ if you like the project. -`helm-release` is a Helm 3 plugin that allows running operatins on Helm releases (deployed Helm charts). +`helm-release` is a Helm 3 plugin that allows running operatins on Helm releases (deployed Helm charts). Features: - * Pull (re-create) Helm charts from a deployed helm release. The helm chart is re-created from the release data stored in `helm.sh/release.v1` secret. + * Pull (re-create) Helm charts from a deployed helm release. The helm chart is re-created from the release data stored in `helm.sh/release.v1` secret. * Update values of a deployed release (without the chart package or path). @@ -20,11 +20,11 @@ $ helm plugin install https://github.com/JovianX/helm-release-plugin ``` > -> Dependencies: `helm-release` plugin depends on: ->> jq - a lightweight and flexible command-line JSON processor. ->> Install: https://stedolan.github.io/jq/download/ -> ->> yq - a lightweight and portable command-line YAML processor. +> Dependencies: `helm-release` plugin depends on: +>> jq - a lightweight and flexible command-line JSON processor. +>> Install: https://stedolan.github.io/jq/download/ +> +>> yq - a lightweight and portable command-line YAML processor. >> Install: https://github.com/mikefarah/yq/#install > @@ -35,9 +35,9 @@ $ helm plugin update release Verify it's been installed: ```shell $ helm plugin list -NAME VERSION DESCRIPTION +NAME VERSION DESCRIPTION ... -release 0.1.0 Update values of a releases, pull charts from releases +release 0.1.0 Update values of a releases, pull charts from releases ... ``` @@ -49,7 +49,7 @@ usage: ./release.sh [ pull ] Available Commands: pull Pulls (re-create) a Helm chart from a deployed Helm release -$ helm release pull +$ helm release pull usage: helm release pull Example: diff --git a/lib/api/helm-ops.sh b/lib/api/helm-ops.sh index 3cca1b2..7004014 100644 --- a/lib/api/helm-ops.sh +++ b/lib/api/helm-ops.sh @@ -1,55 +1,114 @@ #!/usr/bin/env bash function pull_chart_from_release() { - local msg="usage: helm release pull " - if [[ $# < 1 ]]; then exit_with_help "$msg"; fi - RELEASE=$1 + local msg="usage: helm release pull [-d | --destination Default(./)] [-o | --output [yaml | json | text]]" NAMESPACE=$HELM_NAMESPACE + K8S_CONTEXT=$HELM_KUBECONTEXT + DESTINATION_DIR="." + RELEASE="" + OUTPUT="text" - RELEASE_STATUS=$(helm -n $NAMESPACE status $RELEASE -o json) + while test $# -gt 0; do + case "$1" in + (-d|--destination) + shift + if test $# -gt 0; then + export DESTINATION_DIR=$1 + else + exit_with_help "$msg" + fi + shift + ;; + --destination*) + export DESTINATION_DIR=`echo $1 | sed -e 's/^[^=]*=//g'` + shift + ;; + (-o|--output) + shift + if test $# -gt 0; then + export OUTPUT=$1 + if [ "$OUTPUT" != "text" ] && [ "$OUTPUT" != "yaml" ] && [ "$OUTPUT" != "json" ]; then + exit_with_help "$msg" + fi + else + exit_with_help "$msg" + fi + shift + ;; + --output*) + export OUTPUT=`echo $1 | sed -e 's/^[^=]*=//g'` + if [ "$OUTPUT" != "text" ] && [ "$OUTPUT" != "yaml" ] && [ "$OUTPUT" != "json" ]; then + exit_with_help "$msg" + fi + shift + ;; + *) + if [[ -z $RELEASE ]]; then + RELEASE=$1 + else + exit_with_help "$msg" + fi + shift + ;; + esac + done + if [[ -z $RELEASE ]]; then + exit_with_help "$msg" + fi + + RELEASE_STATUS=$(helm -n $NAMESPACE --kube-context=$K8S_CONTEXT status $RELEASE -o json) if [ $? -eq 0 ]; then echo $RELEASE_STATUS | jq > $(dirname -- "$0")/data/release-$RELEASE-status.json; VERSION=$(cat $(dirname -- "$0")/data/release-$RELEASE-status.json | jq -r .version) RELEASE_CONTENT=$(kubectl \ - get secrets sh.helm.release.v1.$RELEASE.v$VERSION \ + get secrets sh.helm.release.v1.$RELEASE.v$VERSION \ -n $NAMESPACE \ + --context=$K8S_CONTEXT \ -o jsonpath='{.data.release}' \ | base64 -d | base64 -d | gzip -d | jq | tee $(dirname -- "$0")/data/release-$RELEASE-content.json) CHART_NAME=$(cat $(dirname -- "$0")/data/release-$RELEASE-content.json | jq -r .chart.metadata.name) CHART_VERSION=$(cat $(dirname -- "$0")/data/release-$RELEASE-content.json | jq -r .chart.metadata.version) - CHART_DIR=$CHART_NAME-$CHART_VERSION + CHART_DIR_NAME="$CHART_NAME-$CHART_VERSION" + CHART_DIR="$DESTINATION_DIR/$CHART_DIR_NAME" mkdir -p $CHART_DIR - # sudo wget https://github.com/mikefarah/yq/releases/download/v4.25.3/yq_linux_amd64 -O /usr/bin/yq - echo $RELEASE_CONTENT | jq -r '.chart.metadata' | yq e -P - > $CHART_DIR/Chart.yaml - echo $RELEASE_CONTENT | jq -r '.chart.values' | yq e -P - > $CHART_DIR/values.yaml + echo $RELEASE_CONTENT | jq -r '.chart.metadata' | yq e -P - > $CHART_DIR/Chart.yaml + echo $RELEASE_CONTENT | jq -r '.chart.values' | yq e -P - > $CHART_DIR/values.yaml - # Create Chart Templates + # Create Chart Templates for row in $(echo $RELEASE_CONTENT | jq -r '.chart.templates[] | @base64'); do - #echo $row - _jq() { - echo ${row} | base64 --decode | jq -r ${1} - } - filename=$(echo $(_jq '.name')) + #echo $row + _jq() { + echo ${row} | base64 --decode | jq -r ${1} + } + filename=$(echo $(_jq '.name')) data=$(echo $(_jq '.data') ) mkdir -p $(dirname $CHART_DIR/$filename) echo $data | base64 -d > $CHART_DIR/$filename done # Create Chart files for row in $(echo $RELEASE_CONTENT | jq -r '.chart.files[] | @base64'); do - #echo $row - _jq() { - echo ${row} | base64 --decode | jq -r ${1} - } - filename=$(echo $(_jq '.name')) + #echo $row + _jq() { + echo ${row} | base64 --decode | jq -r ${1} + } + filename=$(echo $(_jq '.name')) data=$(echo $(_jq '.data') ) mkdir -p $(dirname $CHART_DIR/$filename) echo $data | base64 -d > $CHART_DIR/$filename done fi rm_dir './data' + case $OUTPUT in + json) + echo "{\"chart_directory\": \"$CHART_DIR_NAME\"}" ;; + yaml) + echo "chart_directory: $CHART_DIR_NAME" ;; + *) + echo "Chart saved to $CHART_DIR_NAME" ;; + esac return 0; } \ No newline at end of file diff --git a/lib/main.sh b/lib/main.sh index b7a6a2f..27da73a 100644 --- a/lib/main.sh +++ b/lib/main.sh @@ -21,10 +21,10 @@ function main() { local fn_name=${command_table[$command]} if [[ $fn_name == '' ]]; then exit_with_help "$msg"; fi - if $fn_name $@; then + if $fn_name $@; then rm -Rf $(dirname -- "$0")/data; - return 0; - else - return 1; + return 0; + else + return 1; fi } \ No newline at end of file diff --git a/plugin.yaml b/plugin.yaml index 101e63c..ef9b892 100644 --- a/plugin.yaml +++ b/plugin.yaml @@ -1,11 +1,11 @@ # Copyright 2022 JovianX Ltd. -# +# # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at -# +# # http://www.apache.org/licenses/LICENSE-2.0 -# +# # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. diff --git a/release.sh b/release.sh index a3d1aeb..af26faf 100755 --- a/release.sh +++ b/release.sh @@ -1,12 +1,12 @@ #!/usr/bin/env bash # Copyright 2022 JovianX Ltd. -# +# # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at -# +# # http://www.apache.org/licenses/LICENSE-2.0 -# +# # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.