Luca Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | |
Joey Armstrong | aebf185 | 2022-12-09 08:34:13 -0500 | [diff] [blame] | 3 | # Copyright 2018-2023 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. |
| 16 | |
| 17 | # helmlint.sh |
| 18 | # run `helm lint` on all helm charts that are found |
| 19 | |
| 20 | set +e -o pipefail |
| 21 | |
| 22 | # verify that we have helm installed |
| 23 | command -v helm >/dev/null 2>&1 || { echo "helm not found, please install it" >&2; exit 1; } |
| 24 | |
Zack Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame] | 25 | echo "# helmlint.sh, using helm version: $(helm version -c --short) #" |
Luca Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 26 | |
Zack Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame] | 27 | # Collect success/failure, and list/types of failures |
Luca Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 28 | fail_lint=0 |
Zack Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame] | 29 | failed_lint="" |
| 30 | failed_req="" |
Luca Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 31 | |
| 32 | # when not running under Jenkins, use current dir as workspace |
| 33 | WORKSPACE=${WORKSPACE:-.} |
| 34 | |
| 35 | # cleanup repos if `clean` option passed as parameter |
| 36 | if [ "$1" = "clean" ] |
| 37 | then |
Zack Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame] | 38 | echo "Removing any downloaded charts" |
| 39 | find "${WORKSPACE}" -type d -name 'charts' -exec rm -rf {} \; |
Luca Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 40 | fi |
| 41 | |
Zack Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame] | 42 | # now that $1 is checked, error on undefined vars |
| 43 | set -u |
| 44 | |
| 45 | # loop on result of 'find -name Chart.yaml' |
Luca Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 46 | while IFS= read -r -d '' chart |
| 47 | do |
| 48 | chartdir=$(dirname "${chart}") |
| 49 | |
Zack Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame] | 50 | echo "Checking chart: $chartdir" |
| 51 | |
Matteo Scandolo | 5073411 | 2021-01-11 10:18:18 -0800 | [diff] [blame] | 52 | # update dependencies (if any) |
| 53 | helm dependency update "${chartdir}" |
Luca Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 54 | |
Zack Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame] | 55 | # lint the chart (with values.yaml if it exists) |
Luca Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 56 | if [ -f "${chartdir}/values.yaml" ]; then |
| 57 | helm lint --strict --values "${chartdir}/values.yaml" "${chartdir}" |
| 58 | else |
| 59 | helm lint --strict "${chartdir}" |
| 60 | fi |
| 61 | |
| 62 | rc=$? |
| 63 | if [[ $rc != 0 ]]; then |
| 64 | fail_lint=1 |
Zack Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame] | 65 | failed_lint+="${chartdir} " |
Luca Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 66 | fi |
Zack Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame] | 67 | |
| 68 | # check that requirements are available if they're specified |
| 69 | if [ -f "${chartdir}/requirements.yaml" ] |
| 70 | then |
| 71 | echo "Chart has requirements.yaml, checking availability" |
| 72 | helm dependency update "${chartdir}" |
| 73 | rc=$? |
| 74 | if [[ $rc != 0 ]]; then |
| 75 | fail_lint=1 |
| 76 | failed_req+="${chartdir} " |
| 77 | fi |
| 78 | |
| 79 | # remove charts dir after checking for availability, as this chart might be |
| 80 | # required by other charts in the next loop |
| 81 | rm -rf "${chartdir}/charts" |
| 82 | fi |
| 83 | |
Luca Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 84 | done < <(find "${WORKSPACE}" -name Chart.yaml -print0) |
| 85 | |
| 86 | if [[ $fail_lint != 0 ]]; then |
Zack Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame] | 87 | echo "# helmlint.sh Failure! #" |
| 88 | echo "Charts that failed to lint: $failed_lint" |
| 89 | echo "Charts with failures in requirements.yaml: $failed_req" |
Luca Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 90 | exit 1 |
| 91 | fi |
| 92 | |
Zack Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame] | 93 | echo "# helmlint.sh Success! - all charts linted and have valid requirements.yaml #" |
| 94 | |
Luca Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 95 | exit 0 |