Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: make ci/local codegen consistent #1772

Merged
merged 1 commit into from
Jan 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 2 additions & 33 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ jobs:
runs-on: ubuntu-latest
env:
GOPATH: /home/runner/go
PROTOC_ZIP: protoc-3.12.3-linux-x86_64.zip
steps:
- name: Checkout code
uses: actions/checkout@v2
Expand All @@ -84,53 +83,23 @@ jobs:
with:
path: /home/runner/go/bin
key: go-bin-v1-${{ hashFiles('**/go.mod') }}
- uses: actions/cache@v2
with:
path: protoc-3.12.3-linux-x86_64.zip
key: protoc-3.12.3-linux-x86_64.zip
- name: Install protoc
run: |
set -eux -o pipefail
curl -OL https://github.com/protocolbuffers/protobuf/releases/download/v3.12.3/$PROTOC_ZIP
sudo unzip -o $PROTOC_ZIP -d /usr/local bin/protoc
sudo unzip -o $PROTOC_ZIP -d /usr/local 'include/*'
sudo chmod +x /usr/local/bin/protoc
sudo find /usr/local/include -type f | xargs sudo chmod a+r
sudo find /usr/local/include -type d | xargs sudo chmod a+rx
ls /usr/local/include/google/protobuf/

make install-toolchain
- name: Add ~/go/bin to PATH
run: |
echo "/home/runner/go/bin" >> $GITHUB_PATH
- name: Add /usr/local/bin to PATH
run: |
echo "/usr/local/bin" >> $GITHUB_PATH

- name: Create links
run: |
mkdir -p ~/go/src/github.com/argoproj
cp -a ../argo-rollouts ~/go/src/github.com/argoproj

- name: Vendor and Download
run: |
go mod vendor -v
go mod download

- name: Install UI code generator
run: |
wget https://repo1.maven.org/maven2/io/swagger/codegen/v3/swagger-codegen-cli/3.0.25/swagger-codegen-cli-3.0.25.jar -O swagger-codegen-cli.jar
echo "#!/usr/bin/java -jar" > swagger-codegen
cat swagger-codegen-cli.jar >> swagger-codegen
chmod +x swagger-codegen
sudo mv swagger-codegen /usr/local/bin/swagger-codegen
rm swagger-codegen-cli.jar

- uses: actions/setup-java@v1
with:
java-version: "9.0.4"

- name: Run codegen
run: |
make go-mod-vendor
make codegen
make manifests
make docs
Expand Down
66 changes: 16 additions & 50 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ define protoc
# protoc $(1)
PATH=${DIST_DIR}:$$PATH protoc \
-I /usr/local/include \
-I ${DIST_DIR}/protoc-include \
-I . \
-I ./vendor \
-I ${GOPATH}/src \
Expand All @@ -69,81 +70,46 @@ go-mod-vendor:
go mod tidy
go mod vendor

# go_get,path
# use go_get to install a toolchain binary for a package which is *not* vendored in go.mod
define go_get
cd /tmp && GOBIN=${DIST_DIR} go get $(1)
endef

# go_install,path
# use go_install to install a toolchain binary for a package which *is* vendored in go.mod
define go_install
GOBIN=${DIST_DIR} go install -mod=vendor ./vendor/$(1)
endef

.PHONY: $(DIST_DIR)/controller-gen
$(DIST_DIR)/controller-gen:
$(call go_get,sigs.k8s.io/controller-tools/cmd/[email protected])

.PHONY: $(DIST_DIR)/bin/goimports
$(DIST_DIR)/bin/goimports:
$(call go_get,golang.org/x/tools/cmd/goimports)

.PHONY: $(DIST_DIR)/go-to-protobuf
$(DIST_DIR)/go-to-protobuf: go-mod-vendor
$(call go_install,k8s.io/code-generator/cmd/go-to-protobuf)

.PHONY: $(DIST_DIR)/protoc-gen-gogo
$(DIST_DIR)/protoc-gen-gogo: go-mod-vendor
$(call go_install,github.com/gogo/protobuf/protoc-gen-gogo)

.PHONY: $(DIST_DIR)/protoc-gen-gogofast
$(DIST_DIR)/protoc-gen-gogofast:
$(call go_install,github.com/gogo/protobuf/protoc-gen-gogofast)

.PHONY: $(DIST_DIR)/protoc-gen-grpc-gateway
$(DIST_DIR)/protoc-gen-grpc-gateway: go-mod-vendor
$(call go_install,github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway)

.PHONY: $(DIST_DIR)/protoc-gen-swagger
$(DIST_DIR)/protoc-gen-swagger: go-mod-vendor
$(call go_install,github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger)
.PHONY: install-go-tools-local
install-go-tools-local: go-mod-vendor
./hack/installers/install-codegen-go-tools.sh

.PHONY: $(DIST_DIR)/openapi-gen
$(DIST_DIR)/openapi-gen: go-mod-vendor
$(call go_install,k8s.io/kube-openapi/cmd/openapi-gen)
.PHONY: install-protoc-local
install-protoc-local:
./hack/installers/install-protoc.sh

.PHONY: $(DIST_DIR)/mockery
$(DIST_DIR)/mockery:
$(call go_get,github.com/vektra/mockery/[email protected])
# Installs all tools required to build and test locally
.PHONY: install-tools-local
install-tools-local: install-go-tools-local install-protoc-local

TYPES := $(shell find pkg/apis/rollouts/v1alpha1 -type f -name '*.go' -not -name openapi_generated.go -not -name '*generated*' -not -name '*test.go')
APIMACHINERY_PKGS=k8s.io/apimachinery/pkg/util/intstr,+k8s.io/apimachinery/pkg/api/resource,+k8s.io/apimachinery/pkg/runtime/schema,+k8s.io/apimachinery/pkg/runtime,k8s.io/apimachinery/pkg/apis/meta/v1,k8s.io/api/core/v1,k8s.io/api/batch/v1

.PHONY: install-toolchain
install-toolchain: go-mod-vendor $(DIST_DIR)/controller-gen $(DIST_DIR)/bin/goimports $(DIST_DIR)/go-to-protobuf $(DIST_DIR)/protoc-gen-gogo $(DIST_DIR)/protoc-gen-gogofast $(DIST_DIR)/protoc-gen-grpc-gateway $(DIST_DIR)/protoc-gen-swagger $(DIST_DIR)/openapi-gen $(DIST_DIR)/mockery
install-toolchain: install-go-tools-local install-protoc-local

# generates all auto-generated code
.PHONY: codegen
codegen: gen-proto gen-k8scodegen gen-openapi gen-mocks gen-crd manifests
codegen: go-mod-vendor gen-proto gen-k8scodegen gen-openapi gen-mocks gen-crd manifests

# generates all files related to proto files
.PHONY: gen-proto
gen-proto: k8s-proto api-proto ui-proto

# generates the .proto files affected by changes to types.go
.PHONY: k8s-proto
k8s-proto: go-mod-vendor install-toolchain $(TYPES)
k8s-proto: go-mod-vendor $(TYPES)
PATH=${DIST_DIR}:$$PATH go-to-protobuf \
--go-header-file=./hack/custom-boilerplate.go.txt \
--packages=github.com/argoproj/argo-rollouts/pkg/apis/rollouts/v1alpha1 \
--apimachinery-packages=${APIMACHINERY_PKGS} \
--proto-import $(CURDIR)/vendor
--proto-import $(CURDIR)/vendor \
--proto-import=${DIST_DIR}/protoc-include
touch pkg/apis/rollouts/v1alpha1/generated.proto

# generates *.pb.go, *.pb.gw.go, swagger from .proto files
.PHONY: api-proto
api-proto: go-mod-vendor install-toolchain k8s-proto
api-proto: go-mod-vendor k8s-proto
$(call protoc,pkg/apiclient/rollout/rollout.proto)

# generates ui related proto files
Expand Down
2 changes: 1 addition & 1 deletion analysis/analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ func (c *Controller) garbageCollectMeasurements(run *v1alpha1.AnalysisRun, measu
measurementRetentionObject := measurementRetentionMetricNamesMap[result.Name]
measurementsLimit := limit
if measurementRetentionObject != nil && measurementRetentionObject.Limit > 0 {
measurementsLimit = measurementRetentionObject.Limit
measurementsLimit = int(measurementRetentionObject.Limit)
}
if length > measurementsLimit {
metric, ok := metricsByName[result.Name]
Expand Down
48 changes: 48 additions & 0 deletions docs/features/kustomize/rollout_cr_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@
"type": "array",
"x-kubernetes-patch-merge-key": "name",
"x-kubernetes-patch-strategy": "merge"
},
"dryRun": {
"items": {
"properties": {
"metricName": {
"type": "string"
}
},
"required": [
"metricName"
],
"type": "object"
},
"type": "array",
"x-kubernetes-patch-merge-key": "metricName",
"x-kubernetes-patch-strategy": "merge"
}
},
"type": "object"
Expand Down Expand Up @@ -90,6 +106,22 @@
"type": "array",
"x-kubernetes-patch-merge-key": "name",
"x-kubernetes-patch-strategy": "merge"
},
"dryRun": {
"items": {
"properties": {
"metricName": {
"type": "string"
}
},
"required": [
"metricName"
],
"type": "object"
},
"type": "array",
"x-kubernetes-patch-merge-key": "metricName",
"x-kubernetes-patch-strategy": "merge"
}
},
"type": "object"
Expand Down Expand Up @@ -141,6 +173,22 @@
"type": "array",
"x-kubernetes-patch-merge-key": "name",
"x-kubernetes-patch-strategy": "merge"
},
"dryRun": {
"items": {
"properties": {
"metricName": {
"type": "string"
}
},
"required": [
"metricName"
],
"type": "object"
},
"type": "array",
"x-kubernetes-patch-merge-key": "metricName",
"x-kubernetes-patch-strategy": "merge"
}
},
"type": "object"
Expand Down
57 changes: 57 additions & 0 deletions hack/installers/install-codegen-go-tools.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/bin/bash
set -eux -o pipefail

SRCROOT="$( CDPATH='' cd -- "$(dirname "$0")/../.." && pwd -P )"

# This script installs all our golang-based codegen utility CLIs necessary for codegen.
# Some dependencies are vendored in go.mod (ones which are actually imported in our codebase).
# Other dependencies are only used as a CLI and do not need vendoring in go.mod (doing so adds
# unecessary dependencies to go.mod). We want to maintain a single source of truth for versioning
# our binaries (either go.mod or go install <pkg>@<version>), so we use two techniques to install
# our CLIs:
# 1. For CLIs which are NOT vendored in go.mod, we can run `go install <pkg>@<version>` with an explicit version
# 2. For packages which we *do* vendor in go.mod, we determine version from go.mod followed by `go install` with that version
go_mod_install() {
module=$(go list -f '{{.Module}}' $1 | awk '{print $1}')
module_version=$(go list -m $module | awk '{print $NF}' | head -1)
go install $1@$module_version
}

# All binaries are compiled into the argo-cd/dist directory, which is added to the PATH during codegen
export GOBIN="${SRCROOT}/dist"
mkdir -p $GOBIN

# protoc-gen-go* is used to generate <service>.pb.go from .proto files
#go_mod_install github.com/golang/protobuf/protoc-gen-go
#go_mod_install github.com/gogo/protobuf/protoc-gen-gogo
go_mod_install github.com/gogo/protobuf/protoc-gen-gogofast

# protoc-gen-grpc-gateway is used to build <service>.pb.gw.go files from from .proto files
go_mod_install github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway

# # protoc-gen-swagger is used to build swagger.json
go_mod_install github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger

# k8s tools to codegen .proto files, client libraries, and helpers from types.go
go_mod_install k8s.io/code-generator/cmd/go-to-protobuf
go_mod_install k8s.io/code-generator/cmd/go-to-protobuf/protoc-gen-gogo
go_mod_install k8s.io/code-generator/cmd/client-gen
go_mod_install k8s.io/code-generator/cmd/deepcopy-gen
go_mod_install k8s.io/code-generator/cmd/defaulter-gen
go_mod_install k8s.io/code-generator/cmd/informer-gen
go_mod_install k8s.io/code-generator/cmd/lister-gen

# We still install openapi-gen from go.mod since upstream does not utilize release tags
go_mod_install k8s.io/kube-openapi/cmd/openapi-gen

# controller-gen is run by ./hack/gen-crd-spec to generate the CRDs
go install sigs.k8s.io/controller-tools/cmd/[email protected]

# swagger cli is used to generate swagger docs
go install github.com/go-swagger/go-swagger/cmd/[email protected]

# goimports is used to auto-format generated code
go install golang.org/x/tools/cmd/[email protected]

# mockery is used for generating mock
go install github.com/vektra/mockery/[email protected]
44 changes: 44 additions & 0 deletions hack/installers/install-protoc.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/bin/bash
set -eux -o pipefail

PROJECT_ROOT=$(cd $(dirname ${BASH_SOURCE})/../..; pwd)
DIST_PATH="${PROJECT_ROOT}/dist"
PATH="${DIST_PATH}:${PATH}"

protoc_version=3.17.3

OS=$(go env GOOS)
ARCH=$(go env GOARCH)
case $OS in
darwin)
# For macOS, the x86_64 binary is used even on Apple Silicon (it is run through rosetta), so
# we download and install the x86_64 version. See: https://github.com/protocolbuffers/protobuf/pull/8557
protoc_os=osx
protoc_arch=x86_64
;;
*)
protoc_os=linux
case $ARCH in
arm64|arm)
protoc_arch=aarch_64
;;
*)
protoc_arch=x86_64
;;
esac
;;
esac

export TARGET_FILE=protoc_${protoc_version}_${OS}_${ARCH}.zip
temp_path="/tmp/${TARGET_FILE}"
url=https://github.com/protocolbuffers/protobuf/releases/download/v${protoc_version}/protoc-${protoc_version}-${protoc_os}-${protoc_arch}.zip
[ -e ${temp_path} ] || curl -sLf --retry 3 -o ${temp_path} ${url}

mkdir -p /tmp/protoc-${protoc_version}
unzip -o ${temp_path} -d /tmp/protoc-${protoc_version}
mkdir -p ${DIST_PATH}/protoc-include
cp /tmp/protoc-${protoc_version}/bin/protoc ${DIST_PATH}/protoc
chmod +x ${DIST_PATH}/protoc
cp -a /tmp/protoc-${protoc_version}/include/* ${DIST_PATH}/protoc-include
chmod -R +rx ${DIST_PATH}/protoc-include
protoc --version
1 change: 1 addition & 0 deletions hack/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
_ "k8s.io/code-generator/cmd/deepcopy-gen"
_ "k8s.io/code-generator/cmd/defaulter-gen"
_ "k8s.io/code-generator/cmd/go-to-protobuf"
_ "k8s.io/code-generator/cmd/go-to-protobuf/protoc-gen-gogo"
_ "k8s.io/code-generator/cmd/informer-gen"
_ "k8s.io/code-generator/cmd/lister-gen"
_ "k8s.io/code-generator/pkg/util"
Expand Down
1 change: 1 addition & 0 deletions manifests/crds/analysis-run-crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ spec:
items:
properties:
limit:
format: int32
type: integer
metricName:
type: string
Expand Down
1 change: 1 addition & 0 deletions manifests/crds/analysis-template-crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ spec:
items:
properties:
limit:
format: int32
type: integer
metricName:
type: string
Expand Down
1 change: 1 addition & 0 deletions manifests/crds/cluster-analysis-template-crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ spec:
items:
properties:
limit:
format: int32
type: integer
metricName:
type: string
Expand Down
Loading