Skip to content

Commit

Permalink
add gen-proto and init-proto target and regen protobufv2 clients (dap…
Browse files Browse the repository at this point in the history
…r#2684)

* add init-proto and gen-proto targets to makefile

* regen

* skip proto

* fix test
  • Loading branch information
youngbupark authored Jan 20, 2021
1 parent b4b4c19 commit f69345a
Show file tree
Hide file tree
Showing 35 changed files with 15,557 additions and 4,218 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ run:
skip-dirs:
- ^pkg.*client.*clientset.*versioned.*
- ^pkg.*client.*informers.*externalversions.*
- ^pkg.*proto.*

# which files to skip: they will be analyzed, but issues from them
# won't be reported. Default value is empty list, but there is
Expand Down
30 changes: 30 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ FORCE_INMEM ?= true
# Add latest tag if LATEST_RELEASE is true
LATEST_RELEASE ?=

PROTOC ?=protoc

ifdef REL_VERSION
DAPR_VERSION := $(REL_VERSION)
else
Expand Down Expand Up @@ -234,6 +236,34 @@ lint:
modtidy:
go mod tidy

################################################################################
# Target: init-proto #
################################################################################
.PHONY: init-proto
init-proto:
go get google.golang.org/protobuf/cmd/[email protected] google.golang.org/grpc/cmd/[email protected]

################################################################################
# Target: gen-proto #
################################################################################
GRPC_PROTOS:=common internals operator placement runtime sentry
PROTO_PREFIX:=github.com/dapr/dapr

# Generate archive files for each binary
# $(1): the binary name to be archived
define genProtoc
.PHONY: gen-proto-$(1)
gen-proto-$(1):
$(PROTOC) --go_out=. --go_opt=module=$(PROTO_PREFIX) --go-grpc_out=. --go-grpc_opt=require_unimplemented_servers=false,module=$(PROTO_PREFIX) ./dapr/proto/$(1)/v1/*.proto
endef

$(foreach ITEM,$(GRPC_PROTOS),$(eval $(call genProtoc,$(ITEM))))

GEN_PROTOS:=$(foreach ITEM,$(GRPC_PROTOS),gen-proto-$(ITEM))

.PHONY: gen-proto
gen-proto: $(GEN_PROTOS) modtidy

################################################################################
# Target: get-components-contrib #
################################################################################
Expand Down
83 changes: 7 additions & 76 deletions dapr/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,91 +11,22 @@

## Proto client generation

### Prerequsites
1. Install protoc version: [v3.11.0](https://github.com/protocolbuffers/protobuf/releases/tag/v3.11.0)

Because of [etcd dependency issue](https://github.com/etcd-io/etcd/issues/11563), contributor needs to use the below verisons of tools to generate gRPC protobuf clients.
2. Install protoc-gen-go and protoc-gen-go-grpc

* protoc version: [v3.11.0](https://github.com/protocolbuffers/protobuf/releases/tag/v3.11.0)
* protobuf protoc-gen-go: [v1.3.2](https://github.com/golang/protobuf/releases/tag/v1.3.2)
* gRPC version: [v1.26.0](https://github.com/grpc/grpc-go/releases/tag/v1.26.0)

### Getting the correct versions of tools

If you have the versions above, you can skip this section.

#### protoc

Click the link above, download the appropriate file for your OS and unzip it in a location of your choice. You can add this to your path if you choose. If you don't, you'll have to use the full path later.


#### protoc-gen-go/grpc-go

To get the version listed above (1.3.2):

```
cd ~/go/src
mkdir temp
cd temp
go mod init
go get -d -v github.com/golang/[email protected]
```

Open go.mod and add this line to the end to add the specific version grpc required, including the comment:

```
require google.golang.org/grpc v1.26.0 // indirect
```

go.mod should now look like this:

```
module temp
go 1.14
require github.com/golang/protobuf v1.3.2 // indirect
require google.golang.org/grpc v1.26.0 // indirect
```

Now build:

```
go build github.com/golang/protobuf/protoc-gen-go
```

The binary will be put in current directory.

Copy the binary to your go bin (e.g. ~/go/bin) or some preferred location. Add that location to your path.

Now generate the protobufs. For this example assume you have a change in `dapr/dapr/proto/runtime/v1/dapr.proto` and want to generate from that:

```
protoc -I . ./dapr/proto/runtime/v1/*.proto --go_out=plugins=grpc:../../../
```bash
make init-proto
```

> Note if you didn't add protoc to your path above, you'll have to use the full path.
The output will be `*pb.go` files in a file hierarchy starting 3 dirs above the current directory. Find the `*pb.go` files and diff them with the current `dapr/dapr/pkg/proto/runtime/v1/dapr.pb.go`. Assuming you have a small change (e.g. adding a field to a struct), the diff should be relatively small, less than 10 lines, other than to the file descriptor which will look like an array of bytes. If the size of the diff is much larger than that, the version of tools you're using likely does not match the ones above.

Finally, copy the generated pb.go files over the corresponding ones in the dapr/dapr repo. In this case, that is `dapr/dapr/pkg/proto/runtime/v1/dapr.pb.go`.
3. Generate gRPC proto clients

Repeat for each modified `.proto`.

### Generate go clients

> TODO: move the commands to makefile
To generate all protobufs:

```bash
protoc -I . ./dapr/proto/operator/v1/*.proto --go_out=plugins=grpc:../../../
protoc -I . ./dapr/proto/placement/v1/*.proto --go_out=plugins=grpc:../../../
protoc -I . ./dapr/proto/sentry/v1/*.proto --go_out=plugins=grpc:../../../
protoc -I . ./dapr/proto/common/v1/*.proto --go_out=plugins=grpc:../../../
protoc -I . ./dapr/proto/runtime/v1/*.proto --go_out=plugins=grpc:../../../
protoc -I . ./dapr/proto/internals/v1/*.proto --go_out=plugins=grpc:../../../
make gen-proto
```


## Update e2e test apps
Whenever there are breaking changes in the proto files, we need to update the e2e test apps to use the correct version of dapr dependencies. This can be done by navigating to the tests folder and running the commands:-

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ require (
go.uber.org/atomic v1.6.0
google.golang.org/genproto v0.0.0-20201110150050-8816d57aaa9a
google.golang.org/grpc v1.33.1
google.golang.org/protobuf v1.25.0
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
gopkg.in/square/go-jose.v2 v2.5.0 // indirect
gopkg.in/yaml.v2 v2.3.0
Expand Down
4 changes: 2 additions & 2 deletions pkg/actors/internal/placement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ func newTestServer() (string, *testServer, func()) {

type testServer struct {
isLeader bool
lastHost placementv1pb.Host
lastHost *placementv1pb.Host
recvCount int
lastTimestamp time.Time
recvError error
Expand All @@ -298,7 +298,7 @@ func (s *testServer) ReportDaprStatus(srv placementv1pb.Placement_ReportDaprStat
return nil
}
s.recvCount++
s.lastHost = *req
s.lastHost = req
s.lastTimestamp = time.Now()
}
}
Expand Down
Loading

0 comments on commit f69345a

Please sign in to comment.