From 249a4e2ccd5918f002a473d0caccbbeb0bcc59a9 Mon Sep 17 00:00:00 2001 From: Matt Farina Date: Fri, 6 Oct 2017 17:22:57 -0400 Subject: [PATCH] Adding linting to Chart and values yaml files 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. --- .circleci/config.yml | 2 +- test/circle/install.sh | 8 ++++++-- test/circle/lint.sh | 40 ++++++++++++++++++++++++++++++++++--- test/circle/lintconf.yml | 43 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 87 insertions(+), 6 deletions(-) create mode 100644 test/circle/lintconf.yml diff --git a/.circleci/config.yml b/.circleci/config.yml index 4c5fd6dec6a6..e8bc5e1efbb9 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -10,5 +10,5 @@ jobs: name: install tools command: test/circle/install.sh - run: - name: helm lint + name: lint command: test/circle/lint.sh \ No newline at end of file diff --git a/test/circle/install.sh b/test/circle/install.sh index 44f098997364..fbafab88f390 100755 --- a/test/circle/install.sh +++ b/test/circle/install.sh @@ -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"); @@ -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 \ No newline at end of file +rm -rf linux-amd64 + +# Install A YAML Linter +# Pinning to a version for consistency +sudo pip install yamllint==1.8.1 \ No newline at end of file diff --git a/test/circle/lint.sh b/test/circle/lint.sh index 629cfe05f0f4..28e4f44ec990 100755 --- a/test/circle/lint.sh +++ b/test/circle/lint.sh @@ -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"); @@ -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` @@ -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 \ No newline at end of file +done + +exit $exitCode \ No newline at end of file diff --git a/test/circle/lintconf.yml b/test/circle/lintconf.yml new file mode 100644 index 000000000000..45e88541ab3f --- /dev/null +++ b/test/circle/lintconf.yml @@ -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