Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pull extended parameters #2

Merged
merged 2 commits into from
Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).


Expand All @@ -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
>

Expand All @@ -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
...
```

Expand All @@ -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 <RELEASE NAME>

Example:
Expand Down
99 changes: 79 additions & 20 deletions lib/api/helm-ops.sh
Original file line number Diff line number Diff line change
@@ -1,55 +1,114 @@
#!/usr/bin/env bash
function pull_chart_from_release() {
local msg="usage: helm release pull <RELEASE NAME>"
if [[ $# < 1 ]]; then exit_with_help "$msg"; fi

RELEASE=$1
local msg="usage: helm release pull <RELEASE NAME> [-d | --destination <TARGET CHART DIRECTORY> 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;
}
8 changes: 4 additions & 4 deletions lib/main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
6 changes: 3 additions & 3 deletions plugin.yaml
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
6 changes: 3 additions & 3 deletions release.sh
Original file line number Diff line number Diff line change
@@ -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.
Expand Down