Skip to content

Commit

Permalink
fix: allow to run codegen outside GOPATH (#16511)
Browse files Browse the repository at this point in the history
* fix: allow to run codegen outside GOPATH

Signed-off-by: Alexandre Gaudreault <[email protected]>

* clientgen

Signed-off-by: Alexandre Gaudreault <[email protected]>

* openapigen

Signed-off-by: Alexandre Gaudreault <[email protected]>

* remove ensure-gopath

Signed-off-by: Alexandre Gaudreault <[email protected]>

---------

Signed-off-by: Alexandre Gaudreault <[email protected]>
  • Loading branch information
agaudreault authored Jan 10, 2024
1 parent d6da9f2 commit 54f1572
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 40 deletions.
29 changes: 12 additions & 17 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -175,29 +175,21 @@ endif
.PHONY: all
all: cli image

# We have some legacy requirements for being checked out within $GOPATH.
# The ensure-gopath target can be used as dependency to ensure we are running
# within these boundaries.
.PHONY: ensure-gopath
ensure-gopath:
ifneq ("$(PWD)","$(LEGACY_PATH)")
@echo "Due to legacy requirements for codegen, repository needs to be checked out within \$$GOPATH"
@echo "Location of this repo should be '$(LEGACY_PATH)' but is '$(PWD)'"
@exit 1
endif

.PHONY: gogen
gogen: ensure-gopath
gogen:
export GO111MODULE=off
go generate ./util/argo/...

.PHONY: protogen
protogen: ensure-gopath mod-vendor-local
protogen: mod-vendor-local protogen-fast

.PHONY: protogen-fast
protogen-fast:
export GO111MODULE=off
./hack/generate-proto.sh

.PHONY: openapigen
openapigen: ensure-gopath
openapigen:
export GO111MODULE=off
./hack/update-openapi.sh

Expand All @@ -212,19 +204,22 @@ notification-docs:


.PHONY: clientgen
clientgen: ensure-gopath
clientgen:
export GO111MODULE=off
./hack/update-codegen.sh

.PHONY: clidocsgen
clidocsgen: ensure-gopath
clidocsgen:
go run tools/cmd-docs/main.go


.PHONY: codegen-local
codegen-local: ensure-gopath mod-vendor-local gogen protogen clientgen openapigen clidocsgen manifests-local notification-docs notification-catalog
codegen-local: mod-vendor-local gogen protogen clientgen openapigen clidocsgen manifests-local notification-docs notification-catalog
rm -rf vendor/

.PHONY: codegen-local-fast
codegen-local-fast: gogen protogen-fast clientgen openapigen clidocsgen manifests-local notification-docs notification-catalog

.PHONY: codegen
codegen: test-tools-image
$(call run-in-test-client,make codegen-local)
Expand Down
41 changes: 27 additions & 14 deletions hack/generate-proto.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ set -o nounset
set -o pipefail

# shellcheck disable=SC2128
PROJECT_ROOT=$(cd "$(dirname "${BASH_SOURCE}")"/..; pwd)
PROJECT_ROOT=$(
cd "$(dirname "${BASH_SOURCE}")"/..
pwd
)
PATH="${PROJECT_ROOT}/dist:${PATH}"
GOPATH=$(go env GOPATH)
GOPATH_PROJECT_ROOT="${GOPATH}/src/github.com/argoproj/argo-cd"

# output tool versions
go version
Expand Down Expand Up @@ -41,6 +45,7 @@ APIMACHINERY_PKGS=(

export GO111MODULE=on
[ -e ./v2 ] || ln -s . v2
[ -e "${GOPATH_PROJECT_ROOT}" ] || (mkdir -p "$(dirname "${GOPATH_PROJECT_ROOT}")" && ln -s "${PROJECT_ROOT}" "${GOPATH_PROJECT_ROOT}")

# protoc_include is the include directory containing the .proto files distributed with protoc binary
if [ -d /dist/protoc-include ]; then
Expand All @@ -53,10 +58,17 @@ fi

go-to-protobuf \
--go-header-file="${PROJECT_ROOT}"/hack/custom-boilerplate.go.txt \
--packages="$(IFS=, ; echo "${PACKAGES[*]}")" \
--apimachinery-packages="$(IFS=, ; echo "${APIMACHINERY_PKGS[*]}")" \
--proto-import=./vendor \
--proto-import="${protoc_include}"
--packages="$(
IFS=,
echo "${PACKAGES[*]}"
)" \
--apimachinery-packages="$(
IFS=,
echo "${APIMACHINERY_PKGS[*]}"
)" \
--proto-import="${PROJECT_ROOT}"/vendor \
--proto-import="${protoc_include}" \
--output-base="${GOPATH}/src/"

# Either protoc-gen-go, protoc-gen-gofast, or protoc-gen-gogofast can be used to build
# server/*/<service>.pb.go from .proto files. golang/protobuf and gogo/protobuf can be used
Expand Down Expand Up @@ -86,9 +98,11 @@ for i in ${PROTO_FILES}; do
--${GOPROTOBINARY}_out=plugins=grpc:"$GOPATH"/src \
--grpc-gateway_out=logtostderr=true:"$GOPATH"/src \
--swagger_out=logtostderr=true:. \
$i
"$i"
done
[ -e ./v2 ] && rm -rf v2

[ -L "${GOPATH_PROJECT_ROOT}" ] && rm -rf "${GOPATH_PROJECT_ROOT}"
[ -L ./v2 ] && rm -rf v2

# collect_swagger gathers swagger files into a subdirectory
collect_swagger() {
Expand All @@ -97,7 +111,7 @@ collect_swagger() {
PRIMARY_SWAGGER=$(mktemp)
COMBINED_SWAGGER=$(mktemp)

cat <<EOF > "${PRIMARY_SWAGGER}"
cat <<EOF >"${PRIMARY_SWAGGER}"
{
"swagger": "2.0",
"info": {
Expand All @@ -111,7 +125,7 @@ EOF

rm -f "${SWAGGER_OUT}"

find "${SWAGGER_ROOT}" -name '*.swagger.json' -exec swagger mixin --ignore-conflicts "${PRIMARY_SWAGGER}" '{}' \+ > "${COMBINED_SWAGGER}"
find "${SWAGGER_ROOT}" -name '*.swagger.json' -exec swagger mixin --ignore-conflicts "${PRIMARY_SWAGGER}" '{}' \+ >"${COMBINED_SWAGGER}"
jq -r 'del(.definitions[].properties[]? | select(."$ref"!=null and .description!=null).description) | del(.definitions[].properties[]? | select(."$ref"!=null and .title!=null).title) |
# The "array" and "map" fields have custom unmarshaling. Modify the swagger to reflect this.
.definitions.v1alpha1ApplicationSourcePluginParameter.properties.array = {"description":"Array is the value of an array type parameter.","type":"array","items":{"type":"string"}} |
Expand All @@ -120,10 +134,10 @@ EOF
del(.definitions.v1alpha1OptionalMap) |
# Output for int64 is incorrect, because it is based on proto definitions, where int64 is a string. In our JSON API, we expect int64 to be an integer. https://github.com/grpc-ecosystem/grpc-gateway/issues/219
(.definitions[]?.properties[]? | select(.type == "string" and .format == "int64")) |= (.type = "integer")
' "${COMBINED_SWAGGER}" | \
jq '.definitions.v1Time.type = "string" | .definitions.v1Time.format = "date-time" | del(.definitions.v1Time.properties)' | \
jq '.definitions.v1alpha1ResourceNode.allOf = [{"$ref": "#/definitions/v1alpha1ResourceRef"}] | del(.definitions.v1alpha1ResourceNode.properties.resourceRef) ' \
> "${SWAGGER_OUT}"
' "${COMBINED_SWAGGER}" |
jq '.definitions.v1Time.type = "string" | .definitions.v1Time.format = "date-time" | del(.definitions.v1Time.properties)' |
jq '.definitions.v1alpha1ResourceNode.allOf = [{"$ref": "#/definitions/v1alpha1ResourceRef"}] | del(.definitions.v1alpha1ResourceNode.properties.resourceRef) ' \
>"${SWAGGER_OUT}"

/bin/rm "${PRIMARY_SWAGGER}" "${COMBINED_SWAGGER}"
}
Expand All @@ -139,4 +153,3 @@ clean_swagger server
clean_swagger reposerver
clean_swagger controller
clean_swagger cmpserver

18 changes: 14 additions & 4 deletions hack/update-codegen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,31 @@ set -o errexit
set -o nounset
set -o pipefail

PROJECT_ROOT=$(cd $(dirname ${BASH_SOURCE})/..; pwd)
PROJECT_ROOT=$(
cd $(dirname ${BASH_SOURCE})/..
pwd
)
PATH="${PROJECT_ROOT}/dist:${PATH}"
GOPATH=$(go env GOPATH)
GOPATH_PROJECT_ROOT="${GOPATH}/src/github.com/argoproj/argo-cd"

TARGET_SCRIPT=/tmp/generate-groups.sh

# codegen utilities are installed outside of generate-groups.sh so remove the `go install` step in the script.
sed -e '/go install/d' ${PROJECT_ROOT}/vendor/k8s.io/code-generator/generate-groups.sh > ${TARGET_SCRIPT}
sed -e '/go install/d' ${PROJECT_ROOT}/vendor/k8s.io/code-generator/generate-groups.sh >${TARGET_SCRIPT}

# generate-groups.sh assumes codegen utilities are installed to GOBIN, but we just ensure the CLIs
# are in the path and invoke them without assumption of their location
sed -i.bak -e 's#${gobin}/##g' ${TARGET_SCRIPT}

[ -e ./v2 ] || ln -s . v2
[ -e "${GOPATH_PROJECT_ROOT}" ] || (mkdir -p "$(dirname "${GOPATH_PROJECT_ROOT}")" && ln -s "${PROJECT_ROOT}" "${GOPATH_PROJECT_ROOT}")

bash -x ${TARGET_SCRIPT} "deepcopy,client,informer,lister" \
github.com/argoproj/argo-cd/v2/pkg/client github.com/argoproj/argo-cd/v2/pkg/apis \
"application:v1alpha1" \
--go-header-file ${PROJECT_ROOT}/hack/custom-boilerplate.go.txt
[ -e ./v2 ] && rm -rf v2
--go-header-file "${PROJECT_ROOT}/hack/custom-boilerplate.go.txt" \
--output-base "${GOPATH}/src"

[ -L "${GOPATH_PROJECT_ROOT}" ] && rm -rf "${GOPATH_PROJECT_ROOT}"
[ -L ./v2 ] && rm -rf v2
20 changes: 15 additions & 5 deletions hack/update-openapi.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,30 @@ set -o errexit
set -o nounset
set -o pipefail

PROJECT_ROOT=$(cd $(dirname "$0")/.. ; pwd)
PROJECT_ROOT=$(
cd $(dirname "$0")/..
pwd
)
PATH="${PROJECT_ROOT}/dist:${PATH}"
GOPATH=$(go env GOPATH)
GOPATH_PROJECT_ROOT="${GOPATH}/src/github.com/argoproj/argo-cd"

VERSION="v1alpha1"

[ -e ./v2 ] || ln -s . v2
[ -e "${GOPATH_PROJECT_ROOT}" ] || (mkdir -p "$(dirname "${GOPATH_PROJECT_ROOT}")" && ln -s "${PROJECT_ROOT}" "${GOPATH_PROJECT_ROOT}")

openapi-gen \
--go-header-file ${PROJECT_ROOT}/hack/custom-boilerplate.go.txt \
--input-dirs github.com/argoproj/argo-cd/v2/pkg/apis/application/${VERSION} \
--output-package github.com/argoproj/argo-cd/v2/pkg/apis/application/${VERSION} \
--report-filename pkg/apis/api-rules/violation_exceptions.list \
--output-base "${GOPATH}/src" \
$@
[ -e ./v2 ] && rm -rf v2

[ -L "${GOPATH_PROJECT_ROOT}" ] && rm -rf "${GOPATH_PROJECT_ROOT}"
[ -L ./v2 ] && rm -rf v2

export GO111MODULE=on
go build -o ./dist/gen-crd-spec ${PROJECT_ROOT}/hack/gen-crd-spec
go build -o ./dist/gen-crd-spec "${PROJECT_ROOT}/hack/gen-crd-spec"
./dist/gen-crd-spec

0 comments on commit 54f1572

Please sign in to comment.