Add Makefile and generator automation

This commit is contained in:
James Munnelly 2017-07-19 17:03:53 +01:00
parent 1626b6ea2e
commit c642807b8c
7 changed files with 391 additions and 0 deletions

2
.gitignore vendored
View File

@ -1 +1,3 @@
.DS_Store
.get_deps
.generate_exes

157
Makefile
View File

@ -0,0 +1,157 @@
ACCOUNT=jetstack
APP_NAME=cert-manager
PACKAGE_NAME=github.com/${ACCOUNT}/${APP_NAME}
GO_VERSION=1.8
GOOS := linux
GOARCH := amd64
DOCKER_IMAGE=${ACCOUNT}/${APP_NAME}
BUILD_DIR=_build
TEST_DIR=_test
CONTAINER_DIR=/go/src/${PACKAGE_NAME}
BUILD_TAG := build
IMAGE_TAGS := canary
PACKAGES=$(shell find . -name "*_test.go" | xargs -n1 dirname | grep -v 'vendor/' | sort -u | xargs -n1 printf "%s.test_pkg ")
BINDIR ?= bin
HACK_DIR ?= hack
TYPES_FILES = $(shell find pkg/apis -name types.go)
.PHONY: version
all: test build
.get_deps:
@echo "Grabbing dependencies..."
@go get -d k8s.io/kubernetes/cmd/libs/go2idl/... || true
@go get -d github.com/kubernetes/repo-infra || true
@touch $@
.hack_verify: .generate_exes
@echo Running repo-infra verify scripts
@echo Running href checker:
@${HACK_DIR}/verify-links.sh
@echo Running errexit checker:
@${HACK_DIR}/verify-errexit.sh
@echo Running generated client checker:
@${HACK_DIR}/verify-client-gen.sh
depend:
rm -rf $(TEST_DIR)/
rm -rf ${BUILD_DIR}/
mkdir $(TEST_DIR)/
mkdir $(BUILD_DIR)/
verify: .hack_verify
test:
build: depend version
CGO_ENABLED=0 GOOS=$(GOOS) GOARCH=$(GOARCH) go build \
-a -tags netgo \
-o ${BUILD_DIR}/${APP_NAME}-$(GOOS)-$(GOARCH) \
-ldflags "-X main.AppGitState=${GIT_STATE} -X main.AppGitCommit=${GIT_COMMIT} -X main.AppVersion=${APP_VERSION}" \
./cmd/controller
docker: docker_all
docker_%:
# create a container
$(eval CONTAINER_ID := $(shell docker create \
-i \
-w $(CONTAINER_DIR) \
golang:${GO_VERSION} \
/bin/bash -c "tar xf - && make $*" \
))
# run build inside container
tar cf - . | docker start -a -i $(CONTAINER_ID)
# copy artifacts over
rm -rf $(BUILD_DIR)/ $(TEST_DIR)/
docker cp $(CONTAINER_ID):$(CONTAINER_DIR)/$(BUILD_DIR)/ .
docker cp $(CONTAINER_ID):$(CONTAINER_DIR)/$(TEST_DIR)/ .
# remove container
docker rm $(CONTAINER_ID)
image: docker_all version
docker build --build-arg VCS_REF=$(GIT_COMMIT) -t $(DOCKER_IMAGE):$(BUILD_TAG) .
push: image
set -e; \
for tag in $(IMAGE_TAGS); do \
docker tag $(DOCKER_IMAGE):$(BUILD_TAG) $(DOCKER_IMAGE):$${tag} ; \
docker push $(DOCKER_IMAGE):$${tag}; \
done
release:
ifndef VERSION
$(error VERSION is not set)
endif
@echo "Preparing release of version $(VERSION)"
echo $(VERSION) > VERSION
find examples -name '*.yaml' -type f -exec sed -i 's/kube-lego:[0-9\.]*$$/kube-lego:$(VERSION)/g' {} \;
# This section contains the code generation stuff
#################################################
.generate_exes: .get_deps \
$(BINDIR)/defaulter-gen \
$(BINDIR)/deepcopy-gen \
$(BINDIR)/conversion-gen \
$(BINDIR)/client-gen \
$(BINDIR)/lister-gen \
$(BINDIR)/informer-gen
touch $@
$(BINDIR)/defaulter-gen:
go build -o $@ k8s.io/kubernetes/cmd/libs/go2idl/defaulter-gen
$(BINDIR)/deepcopy-gen:
go build -o $@ k8s.io/kubernetes/cmd/libs/go2idl/deepcopy-gen
$(BINDIR)/conversion-gen:
go build -o $@ k8s.io/kubernetes/cmd/libs/go2idl/conversion-gen
$(BINDIR)/client-gen:
go build -o $@ k8s.io/kubernetes/cmd/libs/go2idl/client-gen
$(BINDIR)/lister-gen:
go build -o $@ k8s.io/kubernetes/cmd/libs/go2idl/lister-gen
$(BINDIR)/informer-gen:
go build -o $@ k8s.io/kubernetes/cmd/libs/go2idl/informer-gen
# Regenerate all files if the gen exes changed or any "types.go" files changed
.generate_files: .generate_exes $(TYPES_FILES)
# Generate defaults
$(BINDIR)/defaulter-gen \
--v 1 --logtostderr \
--go-header-file "$${GOPATH}/src/github.com/kubernetes/repo-infra/verify/boilerplate/boilerplate.go.txt" \
--input-dirs "$(PACKAGE_NAME)/pkg/apis/certmanager" \
--input-dirs "$(PACKAGE_NAME)/pkg/apis/certmanager/v1alpha1" \
--extra-peer-dirs "$(PACKAGE_NAME)/pkg/apis/certmanager" \
--extra-peer-dirs "$(PACKAGE_NAME)/pkg/apis/certmanager/v1alpha1" \
--output-file-base "zz_generated.defaults"
# Generate deep copies
$(BINDIR)/deepcopy-gen \
--v 1 --logtostderr \
--go-header-file "$${GOPATH}/src/github.com/kubernetes/repo-infra/verify/boilerplate/boilerplate.go.txt" \
--input-dirs "$(PACKAGE_NAME)/pkg/apis/certmanager" \
--input-dirs "$(PACKAGE_NAME)/pkg/apis/certmanager/v1alpha1" \
--bounding-dirs "github.com/openshift/open-service-broker-sdk" \
--output-file-base zz_generated.deepcopy
# Generate conversions
$(BINDIR)/conversion-gen \
--v 1 --logtostderr \
--go-header-file "$${GOPATH}/src/github.com/kubernetes/repo-infra/verify/boilerplate/boilerplate.go.txt" \
--input-dirs "$(PACKAGE_NAME)/pkg/apis/certmanager" \
--input-dirs "$(PACKAGE_NAME)/pkg/apis/certmanager/v1alpha1" \
--output-file-base zz_generated.conversion
# generate all pkg/client contents
$(HACK_DIR)/update-client-gen.sh

8
hack/builder/Dockerfile Normal file
View File

@ -0,0 +1,8 @@
FROM golang:1.8-alpine
RUN apk add --no-cache \
make \
git \
bash \
openssl \
wget

40
hack/update-client-gen.sh Executable file
View File

@ -0,0 +1,40 @@
#!/bin/bash
# The only argument this script should ever be called with is '--verify-only'
set -o errexit
set -o nounset
set -o pipefail
REPO_ROOT=$(dirname "${BASH_SOURCE}")/..
BINDIR=${REPO_ROOT}/bin
# Generate the internal clientset (pkg/client/clientset_generated/internalclientset)
${BINDIR}/client-gen "$@" \
--input-base "github.com/jetstack/cert-manager/pkg/apis/" \
--input "certmanager/" \
--clientset-path "github.com/jetstack/cert-manager/pkg/client/" \
--clientset-name internalclientset \
--go-header-file "${GOPATH}/src/github.com/kubernetes/repo-infra/verify/boilerplate/boilerplate.go.txt"
# Generate the versioned clientset (pkg/client/clientset_generated/clientset)
${BINDIR}/client-gen "$@" \
--input-base "github.com/jetstack/cert-manager/pkg/apis/" \
--input "certmanager/v1alpha1" \
--clientset-path "github.com/jetstack/cert-manager/pkg/" \
--clientset-name "client" \
--go-header-file "${GOPATH}/src/github.com/kubernetes/repo-infra/verify/boilerplate/boilerplate.go.txt"
# generate lister
${BINDIR}/lister-gen "$@" \
--input-dirs="github.com/jetstack/cert-manager/pkg/apis/certmanager" \
--input-dirs="github.com/jetstack/cert-manager/pkg/apis/certmanager/v1alpha1" \
--output-package "github.com/jetstack/cert-manager/pkg/listers" \
--go-header-file "${GOPATH}/src/github.com/kubernetes/repo-infra/verify/boilerplate/boilerplate.go.txt"
# generate informer
${BINDIR}/informer-gen "$@" \
--go-header-file "${GOPATH}/src/github.com/kubernetes/repo-infra/verify/boilerplate/boilerplate.go.txt" \
--input-dirs "github.com/jetstack/cert-manager/pkg/apis/certmanager" \
--input-dirs "github.com/jetstack/cert-manager/pkg/apis/certmanager/v1alpha1" \
--internal-clientset-package "github.com/jetstack/cert-manager/pkg/client/internalclientset" \
--versioned-clientset-package "github.com/jetstack/cert-manager/pkg/client" \
--listers-package "github.com/jetstack/cert-manager/pkg/listers" \
--output-package "github.com/jetstack/cert-manager/pkg/informers"

27
hack/verify-client-gen.sh Executable file
View File

@ -0,0 +1,27 @@
#!/bin/bash
# Copyright 2015 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.
# This script verifies that the generated code for the client matches
# what would be generated currently. It will error out and print the
# first difference found.
set -o errexit
set -o nounset
set -o pipefail
REPO_ROOT=$(dirname "${BASH_SOURCE}")/..
"${REPO_ROOT}/hack/update-client-gen.sh" --verify-only

51
hack/verify-errexit.sh Executable file
View File

@ -0,0 +1,51 @@
#!/bin/bash
# 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.
# This script will verify that the specified script files are valid, meaing:
# - they have "set -o errexit" turned on at some point
#
# Usage: verify-errexit.sh [ dir | file ... ]
# default args is the root of our source tree
set -o errexit
set -o nounset
set -o pipefail
REPO_ROOT=$(dirname "${BASH_SOURCE}")/..
if [ "$*" != "" ]; then
args="$*"
else
args=$(ls "$REPO_ROOT" | grep -v vendor | grep -v glide)
fi
# Gather the list of files that appear to be shell scripts.
# Meaning they have some form of "#!...sh" as a line in them.
shFiles=$(grep -rl '^#!.*sh$' $args)
tmp=/tmp/out$RANDOM
for file in ${shFiles}; do
grep "set -o errexit" $file > /dev/null 2>&1 && continue
grep "set -[a-z]*e" $file > /dev/null 2>&1 && continue
echo $file: appears to be missing \"set -o errexit\" | tee -a $tmp
done
rc="0"
if [ -e "$tmp" ]; then
rc="1"
fi
rm -f $tmp
exit $rc

106
hack/verify-links.sh Executable file
View File

@ -0,0 +1,106 @@
#!/bin/bash
# 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.
# This script will scan all md (markdown) files for bad references.
# It will look for strings of the form [...](...) and make sure that
# the (...) points to either a valid file in the source tree or, in the
# case of it being an http url, it'll make sure we don't get a 404.
#
# Usage: verify-links.sh [ dir | file ... ]
# default arg is root of our source tree
set -o errexit
set -o nounset
set -o pipefail
REPO_ROOT=$(dirname "${BASH_SOURCE}")/..
if [ "$*" != "" ]; then
args="$*"
else
args="${REPO_ROOT}"
fi
mdFiles=$(find "${args}" -name "*.md" | grep -v vendor | grep -v glide)
tmp=/tmp/out${RANDOM}
rm -f /tmp/$tmp*
for file in ${mdFiles}; do
# echo scanning $file
dir=$(dirname $file)
# Replace ) with )\n so that each possible href is on its own line.
# Then only grab lines that have [..](..) in them - put results in tmp file.
# If the file doesn't have any lines with [..](..) then skip this file
sed "s/)/)\n/g" < $file | grep "\[.*\](.*)" > ${tmp}1 || continue
# This sed will extract the href portion of the [..](..) - meaning
# the stuff in the parens.
sed "s/.*\(\[[^\[\]*\]([^()]*)\)/\1/" < ${tmp}1 > ${tmp}2 || continue
# Extract all headings/anchors.
# And strip off the leading #'s and leading/trailing blanks
grep "^ *#" < $file | sed "s/ *#* *\(.*\) *$/\1/" > ${tmp}anchors
# Now convert the header to what the anchor will look like.
# - lower case stuff
# - convert spaces to -'s
# - remove punctuation marks (only accept 0-9, a-z
cat ${tmp}anchors | \
tr '[:upper:]' '[:lower:]' | \
sed "s/ /-/g" | \
sed "s/[^-a-zA-Z0-9]//g" > ${tmp}anchors1
cat ${tmp}2 | while read line ; do
# Strip off the leading and trailing parens
ref=${line#*(}
ref=${ref%)*}
# An external href (ie. starts with http)
if [ "${ref:0:4}" == "http" ]; then
if ! curl --connect-timeout 10 -o /dev/null ${ref} > /dev/null 2>&1 ; then
echo $file: Can\'t load: url ${ref} | tee -a ${tmp}3
fi
continue
fi
# Local file href - skip for now.
# TODO add support for checking these
if [ "${ref:0:1}" == "#" ]; then
ref=${ref:1}
if ! grep "^$ref$" ${tmp}anchors1 > /dev/null 2>&1 ; then
echo $file: Can\'t find anchor \'\#${ref}\' | tee -a ${tmp}3
fi
continue
fi
newPath=${dir}/${ref}
# And finally make sure the file is there
# debug line: echo ref: $ref "->" $newPath
if ! ls "${newPath}" > /dev/null 2>&1 ; then
echo $file: Can\'t find: ${newPath} | tee -a ${tmp}3
failed=true
fi
done
done
rc=0
if [ -a ${tmp}3 ]; then
rc=1
fi
rm -f ${tmp}*
exit $rc