Skip to content
Permalink
Browse files

code-generator: integrate scripts from vendor

We no longer depend on the vendorized versions of the scripts for code
generation. This is because it's impossible to make them work with the
dependencies already in vendor/, so it would try to download and install
its own dependecies every single time it ran in CI. That's slow, prone
to breakage, and might generate different output if the dependencies get
updated.

For convenience, we now also have make tasks for both generation and
verification: `make update-codegen` and `make verify-codegen`.

Last but not least, we no longer depend on generate-internal-groups.sh,
as it only generates empty stubs (remove by this commit as well) that we
do not need.
  • Loading branch information...
juliogreff committed Aug 29, 2019
1 parent 6bef85a commit be2cb1bd415961bf920b505084a2e83bc7b3c1f2
@@ -69,7 +69,7 @@ export CGO_ENABLED := 0
# working on shipper, or via CI scripts.

KUBECTL ?= kubectl -n $(SHIPPER_NAMESPACE)
.PHONY: setup install install-shipper install-shipper-state-metrics install-helm e2e restart logs lint test vendor clean
.PHONY: setup install install-shipper install-shipper-state-metrics install-helm e2e restart logs lint test vendor verify-codegen update-codegen clean

# Set up shipper clusters with `shipperctl`. This is probably the first thing
# you should do when starting to work on shipper, as most of everything else
@@ -128,6 +128,15 @@ vendor:
go mod vendor -v
go mod verify

# Verifies if the auto-generated code in pkg/apis/shipper/v1alpha1 and
# pkg/client is up to date.
verify-codegen:
GOPATH=$(shell go env GOPATH) ./hack/verify-codegen.sh

# Generates code for the types in pkg/apis/shipper/v1alpha1 and pkg/client.
update-codegen:
GOPATH=$(shell go env GOPATH) ./hack/update-codegen.sh

# Remove all build artifacts from the filesystem, and all objects installed in
# kubernetes.
.NOTPARALLEL: clean
@@ -1,7 +1,3 @@
#!/bin/bash -ex

go mod verify

./hack/verify-codegen.sh

make lint test
make verify-codegen lint test
@@ -0,0 +1,87 @@
#!/usr/bin/env 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.

set -o errexit
set -o nounset
set -o pipefail

# generate-groups generates everything for a project with external types only, e.g. a project based
# on CustomResourceDefinitions.

if [ "$#" -lt 4 ] || [ "${1}" == "--help" ]; then
cat <<EOF
Usage: $(basename "$0") <generators> <output-package> <apis-package> <groups-versions> ...
<generators> the generators comma separated to run (deepcopy,defaulter,client,lister,informer) or "all".
<output-package> the output package name (e.g. github.com/example/project/pkg/generated).
<apis-package> the external types dir (e.g. github.com/example/api or github.com/example/project/pkg/apis).
<groups-versions> the groups and their versions in the format "groupA:v1,v2 groupB:v1 groupC:v2", relative
to <api-package>.
... arbitrary flags passed to all generator binaries.
Examples:
$(basename "$0") all github.com/example/project/pkg/client github.com/example/project/pkg/apis "foo:v1 bar:v1alpha1,v1beta1"
$(basename "$0") deepcopy,client github.com/example/project/pkg/client github.com/example/project/pkg/apis "foo:v1 bar:v1alpha1,v1beta1"
EOF
exit 0
fi

GENS="$1"
OUTPUT_PKG="$2"
APIS_PKG="$3"
GROUPS_WITH_VERSIONS="$4"
shift 4

go install k8s.io/code-generator/cmd/{defaulter-gen,client-gen,lister-gen,informer-gen,deepcopy-gen}

function codegen::join() { local IFS="$1"; shift; echo "$*"; }

# enumerate group versions
FQ_APIS=() # e.g. k8s.io/api/apps/v1
for GVs in ${GROUPS_WITH_VERSIONS}; do
IFS=: read -r G Vs <<<"${GVs}"

# enumerate versions
for V in ${Vs//,/ }; do
FQ_APIS+=("${APIS_PKG}/${G}/${V}")
done
done

if [ "${GENS}" = "all" ] || grep -qw "deepcopy" <<<"${GENS}"; then
echo "Generating deepcopy funcs"
"${GOPATH}/bin/deepcopy-gen" --input-dirs "$(codegen::join , "${FQ_APIS[@]}")" -O zz_generated.deepcopy --bounding-dirs "${APIS_PKG}" "$@"
fi

if [ "${GENS}" = "all" ] || grep -qw "client" <<<"${GENS}"; then
echo "Generating clientset for ${GROUPS_WITH_VERSIONS} at ${OUTPUT_PKG}/${CLIENTSET_PKG_NAME:-clientset}"
"${GOPATH}/bin/client-gen" --clientset-name "${CLIENTSET_NAME_VERSIONED:-versioned}" --input-base "" --input "$(codegen::join , "${FQ_APIS[@]}")" --output-package "${OUTPUT_PKG}/${CLIENTSET_PKG_NAME:-clientset}" "$@"
fi

if [ "${GENS}" = "all" ] || grep -qw "lister" <<<"${GENS}"; then
echo "Generating listers for ${GROUPS_WITH_VERSIONS} at ${OUTPUT_PKG}/listers"
"${GOPATH}/bin/lister-gen" --input-dirs "$(codegen::join , "${FQ_APIS[@]}")" --output-package "${OUTPUT_PKG}/listers" "$@"
fi

if [ "${GENS}" = "all" ] || grep -qw "informer" <<<"${GENS}"; then
echo "Generating informers for ${GROUPS_WITH_VERSIONS} at ${OUTPUT_PKG}/informers"
"${GOPATH}/bin/informer-gen" \
--input-dirs "$(codegen::join , "${FQ_APIS[@]}")" \
--versioned-clientset-package "${OUTPUT_PKG}/${CLIENTSET_PKG_NAME:-clientset}/${CLIENTSET_NAME_VERSIONED:-versioned}" \
--listers-package "${OUTPUT_PKG}/listers" \
--output-package "${OUTPUT_PKG}/informers" \
"$@"
fi
@@ -4,24 +4,8 @@ set -o errexit
set -o nounset
set -o pipefail

SCRIPT_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
CODEGEN_PKG=${CODEGEN_PKG:-$(cd "${SCRIPT_ROOT}"; ls -d -1 ./vendor/k8s.io/code-generator 2>/dev/null || echo ../code-generator)}
# generate the code with:
# --output-base because this script should also be able to run inside the vendor dir of
# k8s.io/kubernetes. The output-base is needed for the generators to output into the vendor dir
# instead of the $GOPATH directly. For normal projects this can be dropped.
bash "${CODEGEN_PKG}/generate-groups.sh" all \
bash "hack/generate-groups.sh" all \
github.com/bookingcom/shipper/pkg/client \
github.com/bookingcom/shipper/pkg/apis \
shipper:v1alpha1 \
--go-header-file "${SCRIPT_ROOT}/hack/boilerplate.go.txt"

bash "${CODEGEN_PKG}/generate-internal-groups.sh" all \
github.com/bookingcom/shipper/pkg/client \
github.com/bookingcom/shipper/pkg/apis \
github.com/bookingcom/shipper/pkg/apis \
shipper:v1alpha1 \
--go-header-file "${SCRIPT_ROOT}/hack/boilerplate.go.txt"

# To use your own boilerplate text append:
# --go-header-file "${SCRIPT_ROOT}/hack/custom-boilerplate.go.txt"
--go-header-file "hack/boilerplate.go.txt"

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit be2cb1b

Please sign in to comment.
You can’t perform that action at this time.