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 codegen more consistent across environments #1095

Merged
merged 1 commit into from
Apr 20, 2021
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
1 change: 1 addition & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -133,5 +133,6 @@ jobs:
run: |
make codegen
make manifests

- name: Ensure nothing changed
run: git diff --exit-code
142 changes: 79 additions & 63 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
PACKAGE=github.com/argoproj/argo-rollouts
CURRENT_DIR=$(shell pwd)
DIST_DIR=${CURRENT_DIR}/dist
PATH := $(DIST_DIR):$(PATH)
PLUGIN_CLI_NAME?=kubectl-argo-rollouts
TEST_TARGET ?= ./...

Expand All @@ -20,8 +21,7 @@ E2E_PARALLEL ?= 1

# go_install,path
define go_install
[ -e ./vendor ] || go mod vendor
go install -mod=vendor ./vendor/$(1)
cd /tmp && GOBIN=${DIST_DIR} go get $(1)
endef

override LDFLAGS += \
Expand Down Expand Up @@ -57,8 +57,7 @@ endif
# protoc,my.proto
define protoc
# protoc $(1)
[ -e vendor ] || go mod vendor
protoc \
PATH=${DIST_DIR}:$$PATH protoc \
-I /usr/local/include \
-I . \
-I ./vendor \
Expand All @@ -71,81 +70,106 @@ define protoc
$(1)
endef

PROTO_BINARIES := $(GOPATH)/bin/protoc-gen-gogo $(GOPATH)/bin/protoc-gen-gogofast $(GOPATH)/bin/goimports $(GOPATH)/bin/protoc-gen-grpc-gateway $(GOPATH)/bin/protoc-gen-swagger
TYPES := $(shell find pkg/apis/rollouts/v1alpha1 -type f -name '*.go' -not -name openapi_generated.go -not -name '*generated*' -not -name '*test.go')

.PHONY: all
all: controller image

.PHONY: codegen
codegen: protogen mocks
./hack/update-codegen.sh
./hack/update-openapigen.sh
PATH=${DIST_DIR}:$$PATH go run ./hack/gen-crd-spec/main.go

LEGACY_PATH=$(GOPATH)/src/github.com/argoproj/argo-rollouts

install-codegen-tools:
sudo ./hack/install-codegen-go-tools.sh

.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

UI_PROTOGEN_CMD=yarn --cwd ui run protogen
.PHONY: protogen
protogen: pkg/apis/rollouts/v1alpha1/generated.proto pkg/apiclient/rollout/rollout.swagger.json
rm -Rf vendor
# downloads vendor files needed by tools.go (i.e. gen-k8scodegen)
.PHONY: go-mod-vendor
go-mod-vendor:
go mod tidy
${UI_PROTOGEN_CMD}
go mod vendor

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

$(GOPATH)/bin/go-to-protobuf:
$(call go_install,k8s.io/code-generator/cmd/go-to-protobuf)
.PHONY: $(DIST_DIR)/go-to-protobuf
$(DIST_DIR)/go-to-protobuf:
$(call go_install,k8s.io/code-generator/cmd/[email protected])

$(GOPATH)/bin/protoc-gen-gogo:
$(call go_install,github.com/gogo/protobuf/protoc-gen-gogo)
.PHONY: $(DIST_DIR)/protoc-gen-gogo
$(DIST_DIR)/protoc-gen-gogo:
$(call go_install,github.com/gogo/protobuf/[email protected])

$(GOPATH)/bin/protoc-gen-gogofast:
$(call go_install,github.com/gogo/protobuf/protoc-gen-gogofast)
.PHONY: $(DIST_DIR)/protoc-gen-gogofast
$(DIST_DIR)/protoc-gen-gogofast:
$(call go_install,github.com/gogo/protobuf/[email protected])

$(GOPATH)/bin/protoc-gen-grpc-gateway:
$(call go_install,github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway)
.PHONY: $(DIST_DIR)/protoc-gen-grpc-gateway
$(DIST_DIR)/protoc-gen-grpc-gateway:
$(call go_install,github.com/grpc-ecosystem/grpc-gateway/[email protected])

$(GOPATH)/bin/protoc-gen-swagger:
$(call go_install,github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger)
.PHONY: $(DIST_DIR)/protoc-gen-swagger
$(DIST_DIR)/protoc-gen-swagger:
$(call go_install,github.com/grpc-ecosystem/grpc-gateway/[email protected])

$(GOPATH)/bin/openapi-gen:
.PHONY: $(DIST_DIR)/openapi-gen
$(DIST_DIR)/openapi-gen:
$(call go_install,k8s.io/kube-openapi/cmd/openapi-gen)

$(GOPATH)/bin/swagger:
$(call go_install,github.com/go-swagger/go-swagger/cmd/swagger)

$(GOPATH)/bin/goimports:
$(call go_install,golang.org/x/tools/cmd/goimports)
.PHONY: $(DIST_DIR)/mockery
$(DIST_DIR)/mockery:
$(call go_install,github.com/vektra/mockery/[email protected])

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

pkg/apis/rollouts/v1alpha1/generated.proto: $(GOPATH)/bin/go-to-protobuf $(PROTO_BINARIES) $(TYPES)
[ -e vendor ] || go mod vendor
${GOPATH}/bin/go-to-protobuf \
.PHONY: install-toolchain
install-toolchain: $(DIST_DIR)/controller-gen $(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

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

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

# generates the .proto files affected by changes to types.go
.PHONY: k8s-proto
k8s-proto: go-mod-vendor install-toolchain $(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
touch pkg/apis/rollouts/v1alpha1/generated.proto

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

# generates ui related proto files
.PHONY: ui-proto
yarn --cwd ui run protogen

# generates k8s client, informer, lister, deepcopy from types.go
.PHONY: gen-k8scodegen
gen-k8scodegen: go-mod-vendor
./hack/update-codegen.sh

# generates ./manifests/crds/
.PHONY: gen-crd
gen-crd: $(DIST_DIR)/controller-gen
go run ./hack/gen-crd-spec/main.go

# generates mock files from interfaces
.PHONY: gen-mocks
gen-mocks: $(DIST_DIR)/mockery
./hack/update-mocks.sh

# generates openapi_generated.go
.PHONY: gen-openapi
gen-openapi: $(DIST_DIR)/openapi-gen
PATH=${DIST_DIR}:$$PATH openapi-gen \
--go-header-file ${CURRENT_DIR}/hack/custom-boilerplate.go.txt \
--input-dirs github.com/argoproj/argo-rollouts/pkg/apis/rollouts/v1alpha1 \
--output-package github.com/argoproj/argo-rollouts/pkg/apis/rollouts/v1alpha1 \
--report-filename pkg/apis/api-rules/violation_exceptions.list

.PHONY: controller
controller: clean-debug
controller:
CGO_ENABLED=0 go build -v -i -ldflags '${LDFLAGS}' -o ${DIST_DIR}/rollouts-controller ./cmd/rollouts-controller

.PHONY: plugin
Expand Down Expand Up @@ -211,22 +235,14 @@ coverage: test
go tool cover -html=coverage.out -o coverage.html
open coverage.html

.PHONY: mocks
mocks:
./hack/update-mocks.sh

.PHONY: manifests
manifests:
./hack/update-manifests.sh

# Cleans VSCode debug.test files from sub-dirs to prevent them from being included in packr boxes
.PHONY: clean-debug
clean-debug:
-find ${CURRENT_DIR} -name debug.test | xargs rm -f

.PHONY: clean
clean: clean-debug
-rm -rf ${CURRENT_DIR}/dist
-rm -rf ${CURRENT_DIR}/ui/dist

.PHONY: precheckin
precheckin: test lint
Expand Down
38 changes: 16 additions & 22 deletions docs/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ Install:
* [kustomize](https://github.com/kubernetes-sigs/kustomize/releases)
* [minikube](https://kubernetes.io/docs/setup/minikube/) or Docker for Desktop

Argo Rollout additionally uses `golangci-lint` to lint the project.
Argo Rollout additionally uses the following tools
* `golangci-lint` to lint the project.
* `protoc` and `swagger-codegen` to generate proto related files
* `yarn` to build the UI

Run the following commands to install them:

Expand All @@ -26,7 +29,7 @@ go get -u github.com/golangci/golangci-lint/cmd/golangci-lint
Brew users can quickly install the lot:

```bash
brew install go kubectl kustomize
brew install go kubectl kustomize golangci-lint protobuf swagger-codegen
```

Set up environment variables (e.g. is `~/.bashrc`):
Expand All @@ -43,25 +46,24 @@ go get -u github.com/argoproj/argo-rollouts
cd ~/go/src/github.com/argoproj/argo-rollouts
```

Run the following command to download all the dependencies:

```
go mod download
```


## Building

`go.mod` is used, so the `go build/test` commands automatically install the needed dependencies


The `make controller` command will build the controller.

* `make codegen` - Runs the code generator that creates the informers, client, lister, and deepcopies from the types.go
and modifies the open-api spec. This command fails if the user has not run `go mod download` to download all the
dependencies of the project.
* `make codegen` - Runs the code generator that creates the informers, client, lister, and deepcopies from the types.go and modifies the open-api spec.


## Running Controller Locally

It is much easier to run and debug if you run Argo Rollout in your local machine than in the Kubernetes cluster.

```bash
cd ~/go/src/github.com/argoproj/argo-rollouts
go run ./cmd/rollouts-controller/main.go
```

## Running Unit Tests

To run unit tests:
Expand Down Expand Up @@ -104,7 +106,7 @@ make test-e2e E2E_TEST_OPTIONS="-testify.m ^TestRolloutRestart$"
3. The e2e tests are designed to run as quickly as possible, eliminating readiness and termination
delays. However, it is often desired to artificially slow down the tests for debugging purposes,
as well as to understand what the test is doing. To delay startup and termination of pods, set the
`E2E_POD_DELAY` to a integer value in seconds. This environment variable is often coupled with
`E2E_POD_DELAY` to an integer value in seconds. This environment variable is often coupled with
`E2E_TEST_OPTIONS` to debug and slow down a specific test.

```shell
Expand Down Expand Up @@ -137,14 +139,6 @@ Alternatively, the e2e tests can be run against the system controller (i.e. with
make start-e2e E2E_INSTANCE_ID=''
```

## Running Locally

It is much easier to run and debug if you run Argo Rollout in your local machine than in the Kubernetes cluster.

```bash
cd ~/go/src/github.com/argoproj/argo-rollouts
go run ./cmd/rollouts-controller/main.go
```

## Running Local Containers

Expand Down
5 changes: 1 addition & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ require (
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 // indirect
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/jstemmer/go-junit-report v0.9.1
github.com/juju/ansiterm v0.0.0-20180109212912-720a0952cc2a
github.com/lunixbochs/vtclean v1.0.0 // indirect
github.com/newrelic/newrelic-client-go v0.49.0
Expand All @@ -32,8 +31,7 @@ require (
github.com/tj/assert v0.0.3
github.com/undefinedlabs/go-mpatch v1.0.6
github.com/valyala/fasttemplate v1.2.1
github.com/vektra/mockery/v2 v2.6.0
golang.org/x/tools v0.1.0
golang.org/x/tools v0.1.0 // indirect
google.golang.org/genproto v0.0.0-20201110150050-8816d57aaa9a
google.golang.org/grpc v1.33.1
google.golang.org/grpc/examples v0.0.0-20210331235824-f6bb3972ed15 // indirect
Expand All @@ -53,7 +51,6 @@ require (
k8s.io/kubectl v0.19.4
k8s.io/kubernetes v1.20.4
k8s.io/utils v0.0.0-20201110183641-67b214c5f920
sigs.k8s.io/controller-tools v0.4.1
)

replace (
Expand Down
Loading