Skip to content

Commit

Permalink
adjusted build to go mod, fixed lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinWeindel committed Dec 3, 2019
1 parent ba03bc0 commit 63fd535
Show file tree
Hide file tree
Showing 67 changed files with 779 additions and 775 deletions.
24 changes: 19 additions & 5 deletions .ci/check
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,35 @@ ignore=(
": exported "
)

# Install Golint (linting tool).
go get -u golang.org/x/lint/golint
# Build Golint.
go install -mod=vendor ./vendor/golang.org/x/lint/golint

###############################################################################

join_by() { local IFS="$1"; shift; echo "$*"; }

PACKAGES="$(go list -e ./... | grep -vE '/tmp/|/vendor/|/local/')"
PACKAGES="$(go list -mod=vendor -e ./... | grep -vE '/tmp/|/vendor/|/local/|/test/')"
PACKAGES_DIRS="$(echo ${PACKAGES} | sed "s|github.com/gardener/cert-management|.|g")"

LINT_FOLDERS="$(echo ${PACKAGES} | sed "s|$GITPROVIDER/$PROJECT|.|g")"

# Execute static code checks.
#go vet ${PACKAGES}
echo "Running go vet..."
go vet -mod=vendor ${PACKAGES}

# go fmt ignores -mod=vendor and performs module lookup (https://github.com/golang/go/issues/27841).
# Also go fmt is just alias for gofmt -l -w and does not support flags that gofmt does.
# That is why gofmt is used.

# Execute automatic code formatting directive.
go fmt ${PACKAGES}
echo "Running gofmt..."
gofmt -l -w -s ${PACKAGES_DIRS}

# Execute lint checks.
echo "Running golint..."
for dir in ${PACKAGES_DIRS}; do
golint -set_exit_status $(find $dir -maxdepth 1 -name "*.go" | grep -vE 'zz_generated|_test.go')
done

# Execute lint checks.
hack/lint -vacs ${LINT_FOLDERS}
39 changes: 25 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,42 @@ PROJECT=github.com/gardener/cert-management
VERSION=$(shell cat VERSION)


.PHONY: build-local build release test alltests
.PHONY: revendor
revendor:
@GO111MODULE=on go mod vendor
@GO111MODULE=on go mod tidy

.PHONY: check
check:
@.ci/check

.PHONY: build
build:
GOOS=linux GOARCH=amd64 go build -o $(EXECUTABLE) \
-ldflags "-X main.Version=$(VERSION)-$(shell git rev-parse HEAD)"\
@CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -o $(EXECUTABLE) \
-mod=vendor \
-ldflags "-X main.version=$(VERSION)-$(shell git rev-parse HEAD)"\
./cmd/cert-controller-manager


.PHONY: build-local
build-local:
go build -o $(EXECUTABLE) \
-ldflags "-X main.Version=$(VERSION)-$(shell git rev-parse HEAD)"\
@CGO_ENABLED=0 GO111MODULE=on go build -o $(EXECUTABLE) \
-mod=vendor \
-ldflags "-X main.version=$(VERSION)-$(shell git rev-parse HEAD)"\
./cmd/cert-controller-manager


.PHONY: release
release:
GOOS=linux GOARCH=amd64 go build -o $(EXECUTABLE) \
-ldflags "-X main.Version=$(VERSION) \
@CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -o $(EXECUTABLE) \
-a \
-mod=vendor \
-ldflags "-w -X main.version=$(VERSION)" \
./cmd/cert-controller-manager

.PHONY: test
test:
go test ./pkg/...
# @echo ----- Skipping long running integration tests, use \'make alltests\' to run all tests -----
# test/integration/run.sh $(kindargs) -- -skip Many $(args)
GO111MODULE=on go test -mod=vendor ./pkg/...

alltests:
go test ./pkg/...
# test/integration/run.sh $(kindargs) -- $(args)
.PHONY: generate
generate:
@./hack/generate-code
26 changes: 3 additions & 23 deletions build/build
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,8 @@ echo WORKDIR="$(pwd)"
VERSION_FILE="$(readlink -f "${SOURCE_PATH}/VERSION")"
VERSION="$(cat "${VERSION_FILE}")"

# If no LOCAL_BUILD environment variable is set, we configure the `go build` command
# to build for linux OS, amd64 architectures and without CGO enablement.
echo "running code generator..."
"hack/update-codegen.sh"
#"hack/update-wrappers.sh"
echo "running build..."

if [[ -z "$LOCAL_BUILD" ]]; then
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \
-a \
-v \
-o ${BINARY_PATH}/rel/$EXE \
-ldflags "-w -X main.Version=${VERSION}" \
cmd/$CMD/*.go
make generate


# If the LOCAL_BUILD environment variable is set, we simply run `go build`.
else
go build \
-v \
-o ${BINARY_PATH}/$EXE \
-ldflags "-w -X main.Version=${VERSION}" \
cmd/$CMD/*.go

fi
echo "running build..."
make -e EXECUTABLE="${BINARY_PATH}/rel/$EXE" release
4 changes: 2 additions & 2 deletions cmd/cert-controller-manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
_ "github.com/gardener/cert-management/pkg/controller/source/service"
)

var Version string
var version string

func init() {
cluster.Configure(
Expand Down Expand Up @@ -60,7 +60,7 @@ func init() {

func main() {
if len(os.Args) == 2 && os.Args[1] == "version" {
fmt.Println(Version)
fmt.Println(version)
os.Exit(0)
}
controllermanager.Start("cert-controller-manager", "Certificate controller manager", "nothing")
Expand Down
92 changes: 92 additions & 0 deletions hack/code-generator/generate-groups.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#!/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

(
# To support running this script from anywhere, we have to first cd into this directory
# so we can install the tools.
cd "$(dirname "${0}")"
go install -mod=vendor ./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"
GO111MODULE="on" GOFLAGS="-mod=vendor" "${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}"
GO111MODULE="on" GOFLAGS="-mod=vendor" "${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"
GO111MODULE="on" GOFLAGS="-mod=vendor" "${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"
GO111MODULE="on" GOFLAGS="-mod=vendor" "${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
65 changes: 65 additions & 0 deletions hack/generate-code
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/bin/bash
#
# Copyright (c) 2019 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
#
# 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.
function headers() {
echo '''/*
Copyright (c) YEAR SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
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.
*/
'''
}

rm -f ${GOPATH}/bin/*-gen

CURRENT_DIR=$(dirname $0)
PROJECT_ROOT="${CURRENT_DIR}"/..

source "$PROJECT_ROOT/build/settings.src"

# Go modules are not able to properly vendor k8s.io/[email protected] (build with godep)
# to include also generate-groups.sh and generate-internal-groups.sh scripts under vendor/.
# However this is fixed with kubernetes-1.15.0 (k8s.io/[email protected] is opted in go modules).
# The workaround for now is to have a kubernetes-1.14.0 copy of the 2 scripts under hack/code-generator and
# to copy them to vendor/vendor/k8s.io/code-generator/ for the generation.
# And respectively clean up them after execution.
# The workaround should be removed adopting kubernetes-1.15.0.
# Similar thing is also done in https://github.com/heptio/contour/pull/1010.

cp "${CURRENT_DIR}"/code-generator/generate-*.sh "${PROJECT_ROOT}"/vendor/k8s.io/code-generator/

cleanup() {
rm -f "${PROJECT_ROOT}"/vendor/k8s.io/code-generator/generate-groups.sh
}
trap "cleanup" EXIT SIGINT

rm -rf "$SCRIPT_ROOT/pkg/client/cert"

bash "${PROJECT_ROOT}"/vendor/k8s.io/code-generator/generate-groups.sh \
"deepcopy,client,informer,lister" \
$PKGPATH/pkg/client/$APINAME \
$PKGPATH/pkg/apis \
$APINAME:$APIVERSION \
-h <(headers)
1 change: 1 addition & 0 deletions pkg/apis/cert/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
package cert

const (
// GroupName is the name of the certificate API group.
GroupName = "cert.gardener.cloud"
)
Loading

0 comments on commit 63fd535

Please sign in to comment.