Luca Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | # Copyright 2018-present Open Networking Foundation |
| 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 | |
| 52 | # update dependencies for profiles/workflows, as they include TOSCA from required charts |
Luca Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 53 | if [[ $chartdir =~ xos-profiles || $chartdir =~ workflows ]] && [ -f "${chartdir}/requirements.yaml" ] |
| 54 | then |
| 55 | helm dependency update "${chartdir}" |
| 56 | fi |
| 57 | |
Zack Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame] | 58 | # lint the chart (with values.yaml if it exists) |
Luca Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 59 | if [ -f "${chartdir}/values.yaml" ]; then |
| 60 | helm lint --strict --values "${chartdir}/values.yaml" "${chartdir}" |
| 61 | else |
| 62 | helm lint --strict "${chartdir}" |
| 63 | fi |
| 64 | |
| 65 | rc=$? |
| 66 | if [[ $rc != 0 ]]; then |
| 67 | fail_lint=1 |
Zack Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame] | 68 | failed_lint+="${chartdir} " |
Luca Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 69 | fi |
Zack Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame] | 70 | |
| 71 | # check that requirements are available if they're specified |
| 72 | if [ -f "${chartdir}/requirements.yaml" ] |
| 73 | then |
| 74 | echo "Chart has requirements.yaml, checking availability" |
| 75 | helm dependency update "${chartdir}" |
| 76 | rc=$? |
| 77 | if [[ $rc != 0 ]]; then |
| 78 | fail_lint=1 |
| 79 | failed_req+="${chartdir} " |
| 80 | fi |
| 81 | |
| 82 | # remove charts dir after checking for availability, as this chart might be |
| 83 | # required by other charts in the next loop |
| 84 | rm -rf "${chartdir}/charts" |
| 85 | fi |
| 86 | |
Luca Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 87 | done < <(find "${WORKSPACE}" -name Chart.yaml -print0) |
| 88 | |
| 89 | if [[ $fail_lint != 0 ]]; then |
Zack Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame] | 90 | echo "# helmlint.sh Failure! #" |
| 91 | echo "Charts that failed to lint: $failed_lint" |
| 92 | echo "Charts with failures in requirements.yaml: $failed_req" |
Luca Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 93 | exit 1 |
| 94 | fi |
| 95 | |
Zack Williams | 48542de | 2018-12-19 17:26:41 -0700 | [diff] [blame] | 96 | echo "# helmlint.sh Success! - all charts linted and have valid requirements.yaml #" |
| 97 | |
Luca Prete | 1b823d6 | 2018-12-13 17:33:47 -0800 | [diff] [blame] | 98 | exit 0 |