Skip to content
This repository has been archived by the owner on Feb 22, 2022. It is now read-only.

Commit

Permalink
Adding linting to Chart and values yaml files (#2429)
Browse files Browse the repository at this point in the history
This does not lint yaml templates as they are templates rather
than valid yaml files. They cannot be linted with a normal linter

yamllint is used for linting. This is an existing Python project
https://github.com/adrienverge/yamllint

The rules are not the default rules and are stored in their
entirity so they can be controlled over time

The existance of a Chart.yaml file and values.yaml file is checked
and an error is thrown if one is missing. Helm lint will not
detect a chart if Chart.yaml is missing and if a values.yaml file
is missing it is noted as info.

The run function is introduced to enable running all the linters,
capturing non-zero exit codes, and exiting with a non-zdero code
if any of them fail. This is used instead of exiting when the
first failure happens to provide more feedback to chart developers.
  • Loading branch information
mattfarina authored and unguiculus committed Oct 9, 2017
1 parent f8d3a11 commit 2f12841
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ jobs:
name: install tools
command: test/circle/install.sh
- run:
name: helm lint
name: lint
command: test/circle/lint.sh
8 changes: 6 additions & 2 deletions test/circle/install.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash -xe
#!/bin/bash -e
# Copyright 2017 The Kubernetes Authors All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -20,4 +20,8 @@ wget http://storage.googleapis.com/kubernetes-helm/helm-${HELM_LATEST_VERSION}-l
tar -xvf helm-${HELM_LATEST_VERSION}-linux-amd64.tar.gz
sudo mv linux-amd64/helm /usr/local/bin
rm -f helm-${HELM_LATEST_VERSION}-linux-amd64.tar.gz
rm -rf linux-amd64
rm -rf linux-amd64

# Install A YAML Linter
# Pinning to a version for consistency
sudo pip install yamllint==1.8.1
40 changes: 37 additions & 3 deletions test/circle/lint.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash -xe
#!/bin/bash
# Copyright 2017 The Kubernetes Authors All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -16,6 +16,20 @@
# Compare to the merge-base rather than master to limit scanning to changes
# this PR/changeset is introducing. Making sure the comparison is to the
# upstream charts repo rather than a fork.

exitCode=0

# Run is a wrapper around the execution of functions. It captures non-zero exit
# codes and remembers an error happened. This enables running all the linters
# and capturing if any of them failed.
run() {
$@
local ret=$?
if [ $ret -ne 0 ]; then
exitCode=1
fi
}

git remote add k8s https://github.com/kubernetes/charts
git fetch k8s master
CHANGED_FOLDERS=`git diff --find-renames --name-only $(git merge-base k8s/master HEAD) stable/ incubator/ | awk -F/ '{print $1"/"$2}' | uniq`
Expand All @@ -30,6 +44,26 @@ for directory in ${CHANGED_FOLDERS}; do
if [ "$directory" == "incubator/common" ]; then
continue
elif [ -d $directory ]; then
helm lint ${directory}
run helm lint ${directory}

# If a Chart.yaml file is present lint it. Otherwise report an error
# because one should exist
if [ -e ${directory}/Chart.yaml ]; then
run yamllint -c test/circle/lintconf.yml ${directory}/Chart.yaml
else
echo "Error ${directory}/Chart.yaml file is missing"
exitCode=1
fi

# If a values.yaml file is present lint it. Otherwise report an error
# because one should exist
if [ -e ${directory}/values.yaml ]; then
run yamllint -c test/circle/lintconf.yml ${directory}/values.yaml
else
echo "Error ${directory}/values.yaml file is missing"
exitCode=1
fi
fi
done
done

exit $exitCode
43 changes: 43 additions & 0 deletions test/circle/lintconf.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
rules:
braces:
min-spaces-inside: 0
max-spaces-inside: 0
min-spaces-inside-empty: -1
max-spaces-inside-empty: -1
brackets:
min-spaces-inside: 0
max-spaces-inside: 0
min-spaces-inside-empty: -1
max-spaces-inside-empty: -1
colons:
max-spaces-before: 0
max-spaces-after: 1
commas:
max-spaces-before: 0
min-spaces-after: 1
max-spaces-after: 1
comments:
require-starting-space: true
min-spaces-from-content: 2
comments-indentation: enable
document-end: disable
document-start: disable # No --- to start a file
empty-lines:
max: 2
max-start: 0
max-end: 0
hyphens:
max-spaces-after: 1
indentation:
spaces: consistent
indent-sequences: whatever # - list indentation will handle both indentation and without
check-multi-line-strings: false
key-duplicates: enable
line-length: disable # Lines can be any length
new-line-at-end-of-file: enable
new-lines:
type: unix
trailing-spaces: enable
truthy:
level: warning

0 comments on commit 2f12841

Please sign in to comment.