Add kubeval and yaml-lint hooks

This commit is contained in:
Javier J. Salmeron Garcia
2019-10-01 16:26:03 +03:00
parent 79a3a10421
commit 03f78ff49a
2 changed files with 112 additions and 0 deletions

33
githooks/pre-push/kubeval Executable file
View File

@@ -0,0 +1,33 @@
#!/bin/bash
currentBranch="$(git rev-parse --abbrev-ref HEAD)"
originCommit="$(git rev-parse --short "$(git merge-base master "$currentBranch")")"
filesToBePushed="$(git diff --name-only "$originCommit")"
failed=0
check-kubeval() {
if ! command -v kubeval > /dev/null 2>&1; then
printf '\033[0;31m\U1F6ABkubeval is not installed\033[0m'
printf ' Install it from https://github.com/instrumenta/kubeval/releases'
exit 1
fi
}
for chartName in $( cut -d'/' -f1,2 <<< "$filesToBePushed" | uniq ); do
check-kubeval
# Avoid running 'kubeval' when modified dirs are not charts
if [[ $chartName = bitnami/* ]]; then
printf '\033[01;33mValidating %s:\n\033[0m' "$chartName"
chartPath="$(git rev-parse --show-toplevel)"/"$chartName"
printf '\033[0;34m- Running helm template %s | kubeval\n\033[0m' "$chartPath"
if helm template "$chartPath" | kubeval; then
printf '\033[0;32m\U2705 helm template %s | kubeval\n\n\033[0m' "$chartPath"
else
printf '\033[0;31m\U1F6AB helm template %s | kubeval failed. Push cancelled.\n\n\033[0m' "$chartPath"
failed=1
fi
fi
done
exit $failed

79
githooks/pre-push/yaml-lint Executable file
View File

@@ -0,0 +1,79 @@
#!/bin/bash
currentBranch="$(git rev-parse --abbrev-ref HEAD)"
originCommit="$(git rev-parse --short "$(git merge-base master "$currentBranch")")"
filesToBePushed="$(git diff --name-only "$originCommit")"
repoPath="$(git rev-parse --show-toplevel)"
failed=0
yaml-lint-file-render() {
local -r chart_path="${1:?missing_chart}"
local -r path="${2:?missing_file}"
local -r display_path=${1#"$repoPath/"}
local -r lint_rules="{extends: default, rules: {line-length: disable, trailing-spaces: disable, truthy: enable, document-start: disable, empty-lines: {max-end: 2} }}"
printf '\033[0;34m- Running yamllint on %s/%s\n\033[0m' "$display_path" "$path"
if ! helm template "$chart_path" -x "$path" | yamllint -d "$lint_rules" -; then
printf '\033[0;31m\U1F6AB (helm template %s -x %s | yamllint -d "%s" -) failed\n\n\033[0m' "$chart_path" "$path" "$lint_rules"
false
else
printf '\033[0;32m\U2705 %s/%s\n\n\033[0m' "$display_path" "$path"
true
fi
}
yaml-lint-file() {
local -r path="${1:?missing_file}"
local -r lint_rules="{extends: default, rules: {line-length: disable, trailing-spaces: disable, truthy: enable, document-start: disable, empty-lines: {max-end: 2}}}"
local -r display_path=${1#"$repoPath/"}
printf '\033[0;34m- Running yamllint on %s\n' "$display_path"
if ! yamllint -d "$lint_rules" "$path"; then
printf '\033[0;31m\U1F6AB yamllint -d "%s" %s failed\n\n\033[0m' "$lint_rules" "$path"
false
else
printf '\033[0;32m\U2705 %s\n\n\033[0m' "$path"
true
fi
}
check-yaml-lint() {
if ! command -v yamllint > /dev/null 2>&1; then
printf '\033[0;31m\U1F6AByamllint is not installed\033[0m'
printf ' Installation for Linux'
printf ' pip install --user yamllint'
printf ' Installation for Mac OS'
printf ' brew install yamllint'
exit 1
fi
}
for chartName in $( cut -d'/' -f1,2 <<< "$filesToBePushed" | uniq ); do
check-yaml-lint
# Avoid running 'yamllint' when modified dirs are not charts
if [[ $chartName = bitnami/* ]]; then
printf '\033[01;33mLinting yaml of %s:\n\033[0m' "$chartName"
chartPath="$repoPath"/"$chartName"
for yaml_file in values.yaml values-production.yaml requirements.yaml Chart.yaml; do
if [[ -f "$chartPath"/"$yaml_file" ]] && ! yaml-lint-file "$chartPath"/"$yaml_file"; then
failed=1
fi
done
for yaml_file in "$chartPath"/templates/*.yaml; do
path_basename=templates/$(basename "$yaml_file")
if ! yaml-lint-file-render "$chartPath" "$path_basename"; then
failed=1
fi
done
fi
done
if [[ "$failed" = "1" ]]; then
printf '\033[0;31m\U1F6ABYAML lint failed. Not pushing\n\n\033[0m'
else
printf '\033[01;33mYAML lint succeeded\n\n\033[0m'
fi
exit $failed