Luca Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 1 | #!/usr/bin/env bash |
Joey Armstrong | 419f7e1 | 2023-01-26 10:24:23 -0500 | [diff] [blame] | 2 | # ----------------------------------------------------------------------- |
Joey Armstrong | 0476e91 | 2024-02-09 16:00:26 -0500 | [diff] [blame] | 3 | # Copyright 2018-2024 Open Networking Foundation (ONF) and the ONF Contributors |
Luca Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
Joey Armstrong | 419f7e1 | 2023-01-26 10:24:23 -0500 | [diff] [blame] | 16 | # ----------------------------------------------------------------------- |
Luca Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 17 | # helmlint.sh |
| 18 | # run `helm lint` on all helm charts that are found |
Joey Armstrong | 419f7e1 | 2023-01-26 10:24:23 -0500 | [diff] [blame] | 19 | # ----------------------------------------------------------------------- |
Luca Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 20 | |
Joey Armstrong | 419f7e1 | 2023-01-26 10:24:23 -0500 | [diff] [blame] | 21 | # [TODO] use set -e else errors can fly under the radar |
Luca Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 22 | set +e -o pipefail |
| 23 | |
Joey Armstrong | 419f7e1 | 2023-01-26 10:24:23 -0500 | [diff] [blame] | 24 | declare -g iam="${0##*/}" |
| 25 | |
Luca Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 26 | # verify that we have helm installed |
| 27 | command -v helm >/dev/null 2>&1 || { echo "helm not found, please install it" >&2; exit 1; } |
| 28 | |
Zack Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame] | 29 | echo "# helmlint.sh, using helm version: $(helm version -c --short) #" |
Luca Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 30 | |
Zack Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame] | 31 | # Collect success/failure, and list/types of failures |
Luca Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 32 | fail_lint=0 |
Joey Armstrong | 419f7e1 | 2023-01-26 10:24:23 -0500 | [diff] [blame] | 33 | declare -a failed_deps=() |
| 34 | declare -a failed_lint=() |
| 35 | declare -a failed_reqs=() |
Luca Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 36 | |
| 37 | # when not running under Jenkins, use current dir as workspace |
| 38 | WORKSPACE=${WORKSPACE:-.} |
| 39 | |
| 40 | # cleanup repos if `clean` option passed as parameter |
Joey Armstrong | 419f7e1 | 2023-01-26 10:24:23 -0500 | [diff] [blame] | 41 | # update then move set -u to set [+-]e -o pipefail above |
| 42 | # if [[ $# -gt 0 ]] && [[ "$1" = 'clean' ]]; then <--- allow set -u |
Luca Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 43 | if [ "$1" = "clean" ] |
| 44 | then |
Joey Armstrong | 0476e91 | 2024-02-09 16:00:26 -0500 | [diff] [blame] | 45 | echo "Removing any downloaded charts" |
| 46 | find "${WORKSPACE}" -type d -name 'charts' -exec rm -rf {} \; |
Luca Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 47 | fi |
| 48 | |
Zack Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame] | 49 | # now that $1 is checked, error on undefined vars |
| 50 | set -u |
| 51 | |
| 52 | # loop on result of 'find -name Chart.yaml' |
Luca Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 53 | while IFS= read -r -d '' chart |
| 54 | do |
Joey Armstrong | 0476e91 | 2024-02-09 16:00:26 -0500 | [diff] [blame] | 55 | chartdir=$(dirname "${chart}") |
Luca Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 56 | |
Joey Armstrong | 0476e91 | 2024-02-09 16:00:26 -0500 | [diff] [blame] | 57 | echo "Checking chart: $chartdir" |
Zack Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame] | 58 | |
Joey Armstrong | 0476e91 | 2024-02-09 16:00:26 -0500 | [diff] [blame] | 59 | # update dependencies (if any) |
| 60 | if ! helm dependency update "${chartdir}"; |
| 61 | then |
| 62 | fail_lint=1 |
| 63 | failed_deps+=("${chartdir}") |
Zack Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame] | 64 | fi |
| 65 | |
Joey Armstrong | 0476e91 | 2024-02-09 16:00:26 -0500 | [diff] [blame] | 66 | # lint the chart (with values.yaml if it exists) |
| 67 | if [ -f "${chartdir}/values.yaml" ]; then |
| 68 | helm lint --strict --values "${chartdir}/values.yaml" "${chartdir}" |
| 69 | else |
| 70 | helm lint --strict "${chartdir}" |
| 71 | fi |
| 72 | |
| 73 | rc=$? |
| 74 | if [[ $rc != 0 ]]; then |
| 75 | fail_lint=1 |
| 76 | failed_lint+=("${chartdir}") |
| 77 | fi |
| 78 | |
| 79 | # ----------------------------------------------------------------------- |
| 80 | # check that requirements are available if they're specified |
| 81 | # how is this check different than helm dep up above ? |
| 82 | # ----------------------------------------------------------------------- |
| 83 | # later helm versions allow requirements.yaml to be defined directly in |
| 84 | # Chart.yaml so an explicit check may no longer be needed. |
| 85 | # |
| 86 | # Should we err when requirements.yaml detected to cleanup old code ? |
| 87 | # ----------------------------------------------------------------------- |
| 88 | if [ -f "${chartdir}/requirements.yaml" ]; |
| 89 | then |
| 90 | echo "Chart has requirements.yaml, checking availability" |
| 91 | if ! helm dependency update "${chartdir}"; then |
| 92 | fail_lint=1 |
| 93 | failed_reqs+=("${chartdir}") |
| 94 | fi |
| 95 | |
| 96 | # remove charts dir after checking for availability, as this chart might be |
| 97 | # required by other charts in the next loop |
| 98 | rm -rf "${chartdir}/charts" |
| 99 | fi |
Zack Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame] | 100 | |
Luca Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 101 | done < <(find "${WORKSPACE}" -name Chart.yaml -print0) |
| 102 | |
| 103 | if [[ $fail_lint != 0 ]]; then |
Joey Armstrong | 419f7e1 | 2023-01-26 10:24:23 -0500 | [diff] [blame] | 104 | cat <<EOM |
| 105 | |
| 106 | ** ----------------------------------------------------------------------- |
| 107 | ** ${iam}: Errors Detected |
| 108 | ** ----------------------------------------------------------------------- |
| 109 | EOM |
| 110 | |
| 111 | # echo "Charts that failed to lint: $failed_lint" |
Joey Armstrong | 0476e91 | 2024-02-09 16:00:26 -0500 | [diff] [blame] | 112 | if [ ${#failed_lint[@]} -gt 0 ]; then |
| 113 | echo "Charts that failed to lint:" |
| 114 | for chart in "${failed_lint[@]}"; |
| 115 | do |
| 116 | echo " $chart" |
| 117 | done |
| 118 | fi |
Joey Armstrong | 419f7e1 | 2023-01-26 10:24:23 -0500 | [diff] [blame] | 119 | |
Joey Armstrong | 0476e91 | 2024-02-09 16:00:26 -0500 | [diff] [blame] | 120 | if [ ${#failed_deps[@]} -gt 0 ]; then |
| 121 | echo "Charts that failed helm dependency update:" |
| 122 | for chart in "${failed_deps[@]}"; |
| 123 | do |
| 124 | echo " $chart" |
| 125 | done |
| 126 | fi |
Joey Armstrong | 419f7e1 | 2023-01-26 10:24:23 -0500 | [diff] [blame] | 127 | |
Joey Armstrong | 0476e91 | 2024-02-09 16:00:26 -0500 | [diff] [blame] | 128 | if [ ${#failed_reqs[@]} -gt 0 ]; then |
| 129 | echo "Charts with failures in requirements.yaml:" |
| 130 | for chart in "${failed_reqs[@]}"; |
| 131 | do |
| 132 | echo " $chart" |
| 133 | done |
| 134 | fi |
Joey Armstrong | 419f7e1 | 2023-01-26 10:24:23 -0500 | [diff] [blame] | 135 | |
Joey Armstrong | 0476e91 | 2024-02-09 16:00:26 -0500 | [diff] [blame] | 136 | echo |
| 137 | echo "See Also:" |
| 138 | echo " o https://wiki.opennetworking.org/display/VOLTHA/make+lint-helm" |
| 139 | |
| 140 | echo |
| 141 | exit 1 |
Luca Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 142 | fi |
| 143 | |
Zack Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame] | 144 | echo "# helmlint.sh Success! - all charts linted and have valid requirements.yaml #" |
| 145 | |
Luca Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 146 | exit 0 |