blob: f26ea5b7fd1b925a7bf7b42047c771c5025abd99 [file] [log] [blame]
Luca Prete1b823d62018-12-13 17:33:47 -08001#!/usr/bin/env bash
Joey Armstrong419f7e12023-01-26 10:24:23 -05002# -----------------------------------------------------------------------
Joey Armstrong0476e912024-02-09 16:00:26 -05003# Copyright 2018-2024 Open Networking Foundation (ONF) and the ONF Contributors
Luca Prete1b823d62018-12-13 17:33:47 -08004#
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 Armstrong419f7e12023-01-26 10:24:23 -050016# -----------------------------------------------------------------------
Luca Prete1b823d62018-12-13 17:33:47 -080017# helmlint.sh
18# run `helm lint` on all helm charts that are found
Joey Armstrong419f7e12023-01-26 10:24:23 -050019# -----------------------------------------------------------------------
Luca Prete1b823d62018-12-13 17:33:47 -080020
Joey Armstrong419f7e12023-01-26 10:24:23 -050021# [TODO] use set -e else errors can fly under the radar
Luca Prete1b823d62018-12-13 17:33:47 -080022set +e -o pipefail
23
Joey Armstrong419f7e12023-01-26 10:24:23 -050024declare -g iam="${0##*/}"
25
Luca Prete1b823d62018-12-13 17:33:47 -080026# verify that we have helm installed
27command -v helm >/dev/null 2>&1 || { echo "helm not found, please install it" >&2; exit 1; }
28
Zack Williams48542de2018-12-19 17:26:41 -070029echo "# helmlint.sh, using helm version: $(helm version -c --short) #"
Luca Prete1b823d62018-12-13 17:33:47 -080030
Zack Williams48542de2018-12-19 17:26:41 -070031# Collect success/failure, and list/types of failures
Luca Prete1b823d62018-12-13 17:33:47 -080032fail_lint=0
Joey Armstrong419f7e12023-01-26 10:24:23 -050033declare -a failed_deps=()
34declare -a failed_lint=()
35declare -a failed_reqs=()
Luca Prete1b823d62018-12-13 17:33:47 -080036
37# when not running under Jenkins, use current dir as workspace
38WORKSPACE=${WORKSPACE:-.}
39
40# cleanup repos if `clean` option passed as parameter
Joey Armstrong419f7e12023-01-26 10:24:23 -050041# update then move set -u to set [+-]e -o pipefail above
42# if [[ $# -gt 0 ]] && [[ "$1" = 'clean' ]]; then <--- allow set -u
Luca Prete1b823d62018-12-13 17:33:47 -080043if [ "$1" = "clean" ]
44then
Joey Armstrong0476e912024-02-09 16:00:26 -050045 echo "Removing any downloaded charts"
46 find "${WORKSPACE}" -type d -name 'charts' -exec rm -rf {} \;
Luca Prete1b823d62018-12-13 17:33:47 -080047fi
48
Zack Williams48542de2018-12-19 17:26:41 -070049# now that $1 is checked, error on undefined vars
50set -u
51
52# loop on result of 'find -name Chart.yaml'
Luca Prete1b823d62018-12-13 17:33:47 -080053while IFS= read -r -d '' chart
54do
Joey Armstrong0476e912024-02-09 16:00:26 -050055 chartdir=$(dirname "${chart}")
Luca Prete1b823d62018-12-13 17:33:47 -080056
Joey Armstrong0476e912024-02-09 16:00:26 -050057 echo "Checking chart: $chartdir"
Zack Williams48542de2018-12-19 17:26:41 -070058
Joey Armstrong0476e912024-02-09 16:00:26 -050059 # update dependencies (if any)
60 if ! helm dependency update "${chartdir}";
61 then
62 fail_lint=1
63 failed_deps+=("${chartdir}")
Zack Williams48542de2018-12-19 17:26:41 -070064 fi
65
Joey Armstrong0476e912024-02-09 16:00:26 -050066 # 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 Williams48542de2018-12-19 17:26:41 -0700100
Luca Prete1b823d62018-12-13 17:33:47 -0800101done < <(find "${WORKSPACE}" -name Chart.yaml -print0)
102
103if [[ $fail_lint != 0 ]]; then
Joey Armstrong419f7e12023-01-26 10:24:23 -0500104 cat <<EOM
105
106** -----------------------------------------------------------------------
107** ${iam}: Errors Detected
108** -----------------------------------------------------------------------
109EOM
110
111 # echo "Charts that failed to lint: $failed_lint"
Joey Armstrong0476e912024-02-09 16:00:26 -0500112 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 Armstrong419f7e12023-01-26 10:24:23 -0500119
Joey Armstrong0476e912024-02-09 16:00:26 -0500120 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 Armstrong419f7e12023-01-26 10:24:23 -0500127
Joey Armstrong0476e912024-02-09 16:00:26 -0500128 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 Armstrong419f7e12023-01-26 10:24:23 -0500135
Joey Armstrong0476e912024-02-09 16:00:26 -0500136 echo
137 echo "See Also:"
138 echo " o https://wiki.opennetworking.org/display/VOLTHA/make+lint-helm"
139
140 echo
141 exit 1
Luca Prete1b823d62018-12-13 17:33:47 -0800142fi
143
Zack Williams48542de2018-12-19 17:26:41 -0700144echo "# helmlint.sh Success! - all charts linted and have valid requirements.yaml #"
145
Luca Prete1b823d62018-12-13 17:33:47 -0800146exit 0