diff --git a/.travis.yml b/.travis.yml index bf40ab59c..dcc2bfbf7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -32,7 +32,7 @@ jobs: - stage: Static Check script: - go get github.com/golang/lint/golint - - make fmt lint vet + - make verify-all - stage: Coverage before_script: # start minikube diff --git a/Makefile b/Makefile index dea74e2b1..9e160a67e 100644 --- a/Makefile +++ b/Makefile @@ -143,18 +143,12 @@ build: clean -ldflags "-s -w -X ${PKG}/version.RELEASE=${TAG} -X ${PKG}/version.COMMIT=${COMMIT} -X ${PKG}/version.REPO=${REPO_INFO}" \ -o ${TEMP_DIR}/rootfs/nginx-ingress-controller ${PKG}/cmd/nginx -.PHONY: fmt -fmt: - @echo "+ $@" - @go list -f '{{if len .TestGoFiles}}"gofmt -s -l {{.Dir}}"{{end}}' $(shell go list ${PKG}/... | grep -v vendor) | xargs -L 1 sh -c - -.PHONY: lint -lint: - @echo "+ $@" - @go list -f '{{if len .TestGoFiles}}"golint {{.Dir}}/..."{{end}}' $(shell go list ${PKG}/... | grep -v vendor | grep -v '/test/e2e') | xargs -L 1 sh -c +.PHONY: verify-all +verify-all: + @./hack/verify-all.sh .PHONY: test -test: fmt lint vet +test: @echo "+ $@" @go test -v -race -tags "$(BUILDTAGS) cgo" $(shell go list ${PKG}/... | grep -v vendor | grep -v '/test/e2e') diff --git a/hack/boilerplate/boilerplate.py b/hack/boilerplate/boilerplate.py index 7b7f0c868..bc57d9b4f 100755 --- a/hack/boilerplate/boilerplate.py +++ b/hack/boilerplate/boilerplate.py @@ -130,8 +130,7 @@ def file_passes(filename, refs, regexs): def file_extension(filename): return os.path.splitext(filename)[1].split(".")[-1].lower() -skipped_dirs = ['Godeps', 'third_party', '_gopath', '_output', '.git', 'cluster/env.sh', - "vendor", "test/e2e/generated/bindata.go", "hack/boilerplate/test"] +skipped_dirs = ['.git', "vendor", "test/e2e/framework/framework.go", "test/e2e/generated/bindata.go", "hack/boilerplate/test", "internal/file/bindata.go"] def normalize_files(files): newfiles = [] diff --git a/hack/kube-env.sh b/hack/kube-env.sh new file mode 100644 index 000000000..4415df155 --- /dev/null +++ b/hack/kube-env.sh @@ -0,0 +1,44 @@ +#!/bin/bash + +# Copyright 2014 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Some useful colors. +if [[ -z "${color_start-}" ]]; then + declare -r color_start="\033[" + declare -r color_red="${color_start}0;31m" + declare -r color_yellow="${color_start}0;33m" + declare -r color_green="${color_start}0;32m" + declare -r color_norm="${color_start}0m" +fi + +# Returns the server version as MMmmpp, with MM as the major +# component, mm the minor component, and pp as the patch +# revision. e.g. 0.7.1 is echoed as 701, and 1.0.11 would be +# 10011. (This makes for easy integer comparison in bash.) +function kube_server_version() { + local server_version + local major + local minor + local patch + + # This sed expression is the POSIX BRE to match strings like: + # Server Version: &version.Info{Major:"0", Minor:"7+", GitVersion:"v0.7.0-dirty", GitCommit:"ad44234f7152e9c66bc2853575445c7071335e57", GitTreeState:"dirty"} + # and capture the GitVersion portion (which has the patch level) + server_version=$(${KUBECTL} --match-server-version=false version | grep "Server Version:") + read major minor patch < <( + echo ${server_version} | \ + sed "s/.*GitVersion:\"v\([0-9]\{1,\}\)\.\([0-9]\{1,\}\)\.\([0-9]\{1,\}\).*/\1 \2 \3/") + printf "%02d%02d%02d" ${major} ${minor} ${patch} | sed 's/^0*//' +} diff --git a/hack/verify-all.sh b/hack/verify-all.sh new file mode 100755 index 000000000..764d7c3c1 --- /dev/null +++ b/hack/verify-all.sh @@ -0,0 +1,78 @@ +#!/bin/bash + +# Copyright 2014 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -o errexit +set -o nounset +set -o pipefail + +KUBE_ROOT=$(dirname "${BASH_SOURCE}")/.. +source "${KUBE_ROOT}/hack/kube-env.sh" + +SILENT=true + +function is-excluded { + for e in $EXCLUDE; do + if [[ $1 -ef ${BASH_SOURCE} ]]; then + return + fi + if [[ $1 -ef "$KUBE_ROOT/hack/$e" ]]; then + return + fi + done + return 1 +} + +while getopts ":v" opt; do + case $opt in + v) + SILENT=false + ;; + \?) + echo "Invalid flag: -$OPTARG" >&2 + exit 1 + ;; + esac +done + +if $SILENT ; then + echo "Running in the silent mode, run with -v if you want to see script logs." +fi + +EXCLUDE="verify-all.sh verify-codegen.sh" + +ret=0 +for t in `ls $KUBE_ROOT/hack/verify-*.sh` +do + if is-excluded $t ; then + echo "Skipping $t" + continue + fi + if $SILENT ; then + echo -e "Verifying $t" + if bash "$t" &> /dev/null; then + echo -e "${color_green}SUCCESS${color_norm}" + else + echo -e "${color_red}FAILED${color_norm}" + ret=1 + fi + else + bash "$t" || ret=1 + fi +done + +exit $ret + +# ex: ts=2 sw=2 et filetype=sh diff --git a/hack/verify-codegen.sh b/hack/verify-codegen.sh index f5835c425..13f830f1e 100755 --- a/hack/verify-codegen.sh +++ b/hack/verify-codegen.sh @@ -21,8 +21,8 @@ set -o pipefail SCRIPT_ROOT=$(dirname "${BASH_SOURCE}")/.. SCRIPT_BASE=${SCRIPT_ROOT}/../.. -DIFFROOT="${SCRIPT_ROOT}/pkg" -TMP_DIFFROOT="${SCRIPT_ROOT}/_tmp/pkg" +DIFFROOT="${SCRIPT_ROOT}/internal" +TMP_DIFFROOT="${SCRIPT_ROOT}/_tmp/internal" _tmp="${SCRIPT_ROOT}/_tmp" cleanup() { diff --git a/hack/verify-gofmt.sh b/hack/verify-gofmt.sh new file mode 100755 index 000000000..db31c0469 --- /dev/null +++ b/hack/verify-gofmt.sh @@ -0,0 +1,43 @@ +#!/bin/bash + +# Copyright 2014 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# GoFmt apparently is changing @ head... + +set -o errexit +set -o nounset +set -o pipefail + +KUBE_ROOT=$(dirname "${BASH_SOURCE}")/.. + +cd "${KUBE_ROOT}" + +find_files() { + find . -not \( \ + \( \ + -wholename './.git' \ + -o -wholename '*/vendor/*' \ + -o -wholename '*bindata.go' \ + \) -prune \ + \) -name '*.go' +} + +GOFMT="gofmt -s" +bad_files=$(find_files | xargs $GOFMT -l) +if [[ -n "${bad_files}" ]]; then + echo "!!! '$GOFMT' needs to be run on the following files: " + echo "${bad_files}" + exit 1 +fi diff --git a/hack/verify-golint.sh b/hack/verify-golint.sh new file mode 100755 index 000000000..2ea50ea49 --- /dev/null +++ b/hack/verify-golint.sh @@ -0,0 +1,40 @@ +#!/bin/bash + +# Copyright 2014 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -o errexit +set -o nounset +set -o pipefail + +KUBE_ROOT=$(dirname "${BASH_SOURCE}")/.. + +cd "${KUBE_ROOT}" + +GOLINT=${GOLINT:-"golint"} +PACKAGES=($(go list ./... | grep -v /vendor/ | grep -v /test\/e2e/)) +bad_files=() +for package in "${PACKAGES[@]}"; do + out=$("${GOLINT}" -min_confidence=0.9 "${package}") + if [[ -n "${out}" ]]; then + bad_files+=("${out}") + fi +done +if [[ "${#bad_files[@]}" -ne 0 ]]; then + echo "!!! '$GOLINT' problems: " + echo "${bad_files[@]}" + exit 1 +fi + +# ex: ts=2 sw=2 et filetype=sh diff --git a/images/custom-error-pages/main.go b/images/custom-error-pages/main.go index 88b709b6c..d67dd9123 100644 --- a/images/custom-error-pages/main.go +++ b/images/custom-error-pages/main.go @@ -27,10 +27,13 @@ import ( ) const ( + // FormatHeader name of the header used to extract the format FormatHeader = "X-Format" + // CodeHeader name of the header used as source of the HTTP statu code to return CodeHeader = "X-Code" + // ContentType name of the header that defines the format of the reply ContentType = "Content-Type" ) diff --git a/internal/file/filesystem.go b/internal/file/filesystem.go index 1adaba8ec..1d03b4b14 100644 --- a/internal/file/filesystem.go +++ b/internal/file/filesystem.go @@ -1,3 +1,19 @@ +/* +Copyright 2017 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + package file import ( diff --git a/internal/file/structure.go b/internal/file/structure.go index aa4cd74dd..2cdbea070 100644 --- a/internal/file/structure.go +++ b/internal/file/structure.go @@ -1,3 +1,19 @@ +/* +Copyright 2017 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + package file const ( diff --git a/internal/ingress/controller/certificate.go b/internal/ingress/controller/certificate.go index cec420659..e8707c716 100644 --- a/internal/ingress/controller/certificate.go +++ b/internal/ingress/controller/certificate.go @@ -1,3 +1,19 @@ +/* +Copyright 2017 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + package controller import ( diff --git a/internal/ingress/controller/process/nginx.go b/internal/ingress/controller/process/nginx.go index 4a925a3e1..4ebaee4fb 100644 --- a/internal/ingress/controller/process/nginx.go +++ b/internal/ingress/controller/process/nginx.go @@ -45,6 +45,8 @@ NGINX master process died (%v): %v return true } +// WaitUntilPortIsAvailable waits until there is no NGINX master or worker +// process/es listentning in a particular port. func WaitUntilPortIsAvailable(port int) { // we wait until the workers are killed for { diff --git a/internal/ingress/controller/tcp.go b/internal/ingress/controller/tcp.go index efcc83077..4b3f1a36d 100644 --- a/internal/ingress/controller/tcp.go +++ b/internal/ingress/controller/tcp.go @@ -26,6 +26,7 @@ import ( "github.com/paultag/sniff/parser" ) +// TCPServer describes a server that works in passthrough mode type TCPServer struct { Hostname string IP string @@ -33,11 +34,13 @@ type TCPServer struct { ProxyProtocol bool } +// TCPProxy describes the passthrough servers and a default as catch all type TCPProxy struct { ServerList []*TCPServer Default *TCPServer } +// Get returns the TCPServer to use func (p *TCPProxy) Get(host string) *TCPServer { if p.ServerList == nil { return p.Default @@ -52,6 +55,8 @@ func (p *TCPProxy) Get(host string) *TCPServer { return p.Default } +// Handle reads enough information from the connection to extract the hostname +// and open a connection to the passthrough server. func (p *TCPProxy) Handle(conn net.Conn) { defer conn.Close() data := make([]byte, 4096) diff --git a/internal/ingress/errors/errors.go b/internal/ingress/errors/errors.go index 1a9db9ba4..03e19db6f 100644 --- a/internal/ingress/errors/errors.go +++ b/internal/ingress/errors/errors.go @@ -84,10 +84,13 @@ func IsInvalidContent(e error) bool { return ok } +// New returns a new error func New(m string) error { return errors.New(m) } +// Errorf formats according to a format specifier and returns the string +// as a value that satisfies error. func Errorf(format string, args ...interface{}) error { return errors.Errorf(format, args) } diff --git a/internal/watch/dummy.go b/internal/watch/dummy.go index 16a607fc2..eb9874a32 100644 --- a/internal/watch/dummy.go +++ b/internal/watch/dummy.go @@ -19,6 +19,7 @@ package watch // DummyFileWatcher noop implementation of a file watcher type DummyFileWatcher struct{} +// NewDummyFileWatcher creates a FileWatcher using the DummyFileWatcher func NewDummyFileWatcher(file string, onEvent func()) FileWatcher { return DummyFileWatcher{} } diff --git a/internal/watch/file_watcher.go b/internal/watch/file_watcher.go index 91daf0620..0713b245b 100644 --- a/internal/watch/file_watcher.go +++ b/internal/watch/file_watcher.go @@ -24,6 +24,7 @@ import ( "gopkg.in/fsnotify.v1" ) +// FileWatcher is an interface we use to watch changes in files type FileWatcher interface { Close() error } diff --git a/test/e2e/annotations/affinity.go b/test/e2e/annotations/affinity.go index 0f80dddde..a14edc190 100644 --- a/test/e2e/annotations/affinity.go +++ b/test/e2e/annotations/affinity.go @@ -51,7 +51,7 @@ var _ = framework.IngressNginxDescribe("Annotations - Affinity", func() { Name: host, Namespace: f.Namespace.Name, Annotations: map[string]string{ - "nginx.ingress.kubernetes.io/affinity": "cookie", + "nginx.ingress.kubernetes.io/affinity": "cookie", "nginx.ingress.kubernetes.io/session-cookie-name": "SERVERID", }, }, @@ -103,9 +103,9 @@ var _ = framework.IngressNginxDescribe("Annotations - Affinity", func() { Name: host, Namespace: f.Namespace.Name, Annotations: map[string]string{ - "nginx.ingress.kubernetes.io/affinity": "cookie", + "nginx.ingress.kubernetes.io/affinity": "cookie", "nginx.ingress.kubernetes.io/session-cookie-name": "SERVERID", - "nginx.ingress.kubernetes.io/rewrite-target": "/something", + "nginx.ingress.kubernetes.io/rewrite-target": "/something", }, }, Spec: v1beta1.IngressSpec{ diff --git a/test/e2e/e2e.go b/test/e2e/e2e.go index e448df03c..4639b7a3d 100644 --- a/test/e2e/e2e.go +++ b/test/e2e/e2e.go @@ -24,10 +24,12 @@ import ( "github.com/onsi/ginkgo/config" "github.com/onsi/gomega" "k8s.io/apiserver/pkg/util/logs" + // required _ "k8s.io/client-go/plugin/pkg/client/auth" "k8s.io/ingress-nginx/test/e2e/framework" + // tests to run _ "k8s.io/ingress-nginx/test/e2e/annotations" _ "k8s.io/ingress-nginx/test/e2e/defaultbackend" _ "k8s.io/ingress-nginx/test/e2e/settings" diff --git a/test/e2e/framework/ssl.go b/test/e2e/framework/ssl.go index 41f0dd0eb..db3d9660e 100644 --- a/test/e2e/framework/ssl.go +++ b/test/e2e/framework/ssl.go @@ -1,3 +1,19 @@ +/* +Copyright 2017 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + package framework import (