From a4e714a6e5b5aa86870ec4210c9cc07008d85b2c Mon Sep 17 00:00:00 2001 From: Armin Schlegel Date: Mon, 5 Sep 2022 14:27:49 +0200 Subject: [PATCH] feat: dependencies versions not pulled from upstream --- .gitignore | 1 + README.md | 21 +++++++++- updateChart | 110 ++++++++++++++++++++++++---------------------------- 3 files changed, 72 insertions(+), 60 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..722d5e7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.vscode diff --git a/README.md b/README.md index f8e6f3c..f1c59e8 100644 --- a/README.md +++ b/README.md @@ -5,5 +5,24 @@ Update a helm chart dependencies and own version ```sh - uses: edgefarm/setup-updatechart@v1 -- run: updateChart --help +- run: updateChart ``` + + +**Options** + +```sh +$ updateChart --help + -h, --help Display this help + -c, --chart Chart.yaml to update + -v, --new-version New version to set for the chart and its dependencies + -k, --keep-version Keep the old version of the chart. Only sets dependencies versions. + -l, --local-charts-dir Location where local managed dependency charts are located + -r, --repository Repository of the chart +``` + +**Example** + +```sh + updateChart --chart charts/core/Chart.yaml --new-version 2.3.4 --local-charts-dir charts --repository oci://ghcr.io/edgefarm/edgefarm.core +``` \ No newline at end of file diff --git a/updateChart b/updateChart index 41949f8..164056a 100755 --- a/updateChart +++ b/updateChart @@ -8,6 +8,8 @@ dependencies_names=() chart= new_version= keep_version="0" +repository= +local_charts_dir= # returns the index of the dependency in the chart # $1 - dependency name @@ -19,23 +21,25 @@ get_dependency_index() { echo ${index} } -# returns the version of the upstream dependencies helm chart -# $1 - repository of upstream helm chart -# returns the version of the upstream dependencies helm chart -# returns -1 if the dependency is not found -get_upstream_dependency_version() { - local repository=${1} - local chart=${2} - IFSBACKUP=${IFS} - IFS="" - ret=$(helm show chart ${repository}/${chart}) - if [ ${?} -ne 0 ]; then - exit 1 +# checks if the dependency helm chart is managed locally +# $1 - index of dependency in the chart +# $2 - directory where the helm charts are stored +# returns true if the dependency is managed locally +# returns false if the dependency is not managed locally +local_dependency() { + local dependency_index=${1} + local dir=${2} + dependency_repository=$(yq eval '.dependencies['${dependency_index}'].repository' ${chart}) + if [[ ${repository} == ${dependency_repository} ]]; then + dependency_name=$(yq eval '.dependencies['${dependency_index}'].name' ${chart}) + if [[ -d ${dir}/${dependency_name} ]]; then + echo "true" + else + echo "false" + fi else - version=$(echo "${ret}" | yq '.version' -) - echo ${version} + echo "false" fi - IFS=${IFSBACKUP} } get_dependencies_names() { @@ -47,8 +51,10 @@ cat << EOF Usage: $(basename "$0") -h, --help Display this help -c, --chart Chart.yaml to update - -v, --new-version New version to set for the chart - -k, --keep-version Keep the old version of the chart + -v, --new-version New version to set for the chart and its dependencies + -k, --keep-version Keep the old version of the chart. Only sets dependencies versions. + -l, --local-charts-dir Location where local managed dependency charts are located + -r, --repository Repository of the chart EOF } @@ -85,6 +91,26 @@ parse_command_line() { exit 1 fi ;; + -r | --repository) + if [[ -n "${2:-}" ]]; then + repository="$2" + shift + else + echo "ERROR: '--repository' cannot be empty." >&2 + show_help + exit 1 + fi + ;; + -l | --local-charts-dir) + if [[ -n "${2:-}" ]]; then + local_charts_dir="$2" + shift + else + echo "ERROR: '--local-charts-dir' cannot be empty." >&2 + show_help + exit 1 + fi + ;; -k | --keep-version) keep_version="1" ;; @@ -100,38 +126,7 @@ parse_command_line() { show_help exit 1 fi -} - -# Checks versions -# Taken from https://stackoverflow.com/a/48487783/18655480 -# Many thanks! -function V() # $1-a $2-op $3-$b -# Compare a and b as version strings. Rules: -# R1: a and b : dot-separated sequence of items. Items are numeric. The last item can optionally end with letters, i.e., 2.5 or 2.5a. -# R2: Zeros are automatically inserted to compare the same number of items, i.e., 1.0 < 1.0.1 means 1.0.0 < 1.0.1 => yes. -# R3: op can be '=' '==' '!=' '<' '<=' '>' '>=' (lexicographic). -# R4: Unrestricted number of digits of any item, i.e., 3.0003 > 3.0000004. -# R5: Unrestricted number of items. -# Example: 3.6 '>' 3.5b -{ - local a=$1 op=$2 b=$3 al=${1##*.} bl=${3##*.} - while [[ $al =~ ^[[:digit:]] ]]; do al=${al:1}; done - while [[ $bl =~ ^[[:digit:]] ]]; do bl=${bl:1}; done - local ai=${a%$al} bi=${b%$bl} - - local ap=${ai//[[:digit:]]} bp=${bi//[[:digit:]]} - ap=${ap//./.0} bp=${bp//./.0} - - local w=1 fmt=$a.$b x IFS=. - for x in $fmt; do [ ${#x} -gt $w ] && w=${#x}; done - fmt=${*//[^.]}; fmt=${fmt//./%${w}s} - printf -v a $fmt $ai$bp; printf -v a "%s-%${w}s" $a $al - printf -v b $fmt $bi$ap; printf -v b "%s-%${w}s" $b $bl - case $op in - '<='|'>=' ) [ "$a" ${op:0:1} "$b" ] || [ "$a" = "$b" ] ;; - * ) [ "$a" $op "$b" ] ;; - esac } main() { @@ -140,18 +135,15 @@ main() { get_dependencies_names for dependency in "${dependencies_names[@]}"; do index=$(get_dependency_index ${dependency}) - echo "Updating dependency ${dependency}" - current_version=$(yq eval ".dependencies[${index}].version" ${chart}) - repo=$(yq eval ".dependencies[${index}].repository" ${chart}) - upstream_version=$(get_upstream_dependency_version ${repo} ${dependency}) - if [ "${upstream_version}" != "not found" ]; then - if V ${upstream_version} '>' ${current_version}; then - echo "Updating ${dependency} to ${upstream_version}..." - yq e ".dependencies[${index}].version = \"$upstream_version\"" -i ${chart} - else - echo "No need to update ${dependency}..." - fi + loc_dep=$(local_dependency ${index} ${local_charts_dir}) + + if [[ ${loc_dep} == "true" ]]; then + echo "Dependency '${dependency}' updated to version ${new_version}" + yq eval '.dependencies['${index}'].version = "'${new_version}'"' -i ${chart} + else + echo "Dependency '${dependency}' managed externally. Skipping." fi + done if [ "${keep_version}" -eq "0" ]; then