Skip to content

Commit

Permalink
feat: dependencies versions not pulled from upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
siredmar committed Sep 5, 2022
1 parent ef31279 commit a4e714a
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 60 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vscode
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,24 @@ Update a helm chart dependencies and own version

```sh
- uses: edgefarm/setup-updatechart@v1
- run: updateChart --help
- run: updateChart <args>
```


**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
```
110 changes: 51 additions & 59 deletions updateChart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() {
Expand All @@ -47,8 +51,10 @@ cat << EOF
Usage: $(basename "$0") <options>
-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
}

Expand Down Expand Up @@ -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"
;;
Expand All @@ -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() {
Expand All @@ -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
Expand Down

0 comments on commit a4e714a

Please sign in to comment.