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

Upgrade grpc-gateway from v1 to v2 #16595

Merged
merged 7 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Resolve review comments: add some comments to clarify some confusion …
…script or code

Signed-off-by: Benjamin Wang <wachao@vmware.com>
  • Loading branch information
ahrtr committed Sep 18, 2023
commit 8eba295bc5c5338b0495071bbdb8c445ce50a6df
22 changes: 22 additions & 0 deletions scripts/genproto.sh
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,35 @@ done
# We only upgraded grpc-gateway from v1 to v2, but keep gogo/protobuf as it's for now.
ahrtr marked this conversation as resolved.
Show resolved Hide resolved
# So we have to convert v1 message to v2 message. Once we get rid of gogo/protobuf, and
# start to depend on protobuf v2, then we can remove this patch.
#
# TODO(https://github.com/etcd-io/etcd/issues/14533): Remove the patch below after removal of gogo/protobuf
for pb in api/etcdserverpb/rpc server/etcdserver/api/v3lock/v3lockpb/v3lock server/etcdserver/api/v3election/v3electionpb/v3election; do
gwfile="$(dirname ${pb})/gw/$(basename ${pb}).pb.gw.go"

ahrtr marked this conversation as resolved.
Show resolved Hide resolved
# Changes something like below,
# import (
# + protov1 "github.com/golang/protobuf/proto"
# +
run sed -i -E "s|import \(|import \(\n\tprotov1 \"github.com/golang/protobuf/proto\"\n|g" "${gwfile}"

# Changes something like below,
# - return msg, metadata, err
# + return protov1.MessageV2(msg), metadata, err
run sed -i -E "s|return msg, metadata, err|return protov1.MessageV2\(msg\), metadata, err|g" "${gwfile}"

# Changes something like below,
# - if err := marshaler.NewDecoder(newReader()).Decode(&protoReq); err != nil && err != io.EOF {
# + if err := marshaler.NewDecoder(newReader()).Decode(protov1.MessageV2(&protoReq)); err != nil && err != io.EOF {
run sed -i -E "s|Decode\(\&protoReq\)|Decode\(protov1\.MessageV2\(\&protoReq\)\)|g" "${gwfile}"

# Changes something like below,
# - forward_Lease_LeaseKeepAlive_0(annotatedContext, mux, outboundMarshaler, w, req, func() (proto.Message, error) { return resp.Recv() }, mux.GetForwardResponseOptions()...)
# + forward_Lease_LeaseKeepAlive_0(annotatedContext, mux, outboundMarshaler, w, req, func() (proto.Message, error) {
# + m1, err := resp.Recv()
# + return protov1.MessageV2(m1), err
# + }, mux.GetForwardResponseOptions()...)
run sed -i -E "s|return resp.Recv\(\)|\n\t\t\tm1, err := resp.Recv\(\)\n\t\t\treturn protov1.MessageV2\(m1\), err\n\t\t|g" "${gwfile}"

run go fmt "${gwfile}"
done

Expand Down
1 change: 1 addition & 0 deletions server/embed/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ func (sctx *serveCtx) registerGateway(dial func(ctx context.Context) (*grpc.Clie
return nil, err
}

// Refer to https://grpc-ecosystem.github.io/grpc-gateway/docs/mapping/customizing_your_gateway/
gwmux := gw.NewServeMux(
gw.WithMarshalerOption(gw.MIMEWildcard,
&gw.HTTPBodyMarshaler{
Expand Down