Use protoc-gen-go instead of protoc-gen-gogoctrd

Signed-off-by: Kazuyoshi Kato <katokazu@amazon.com>
This commit is contained in:
Kazuyoshi Kato 2022-04-20 05:31:48 +00:00
parent e044759ebd
commit fd37cc75be
9 changed files with 24 additions and 352 deletions

View File

@ -159,7 +159,7 @@ generate: protos
@echo "$(WHALE) $@" @echo "$(WHALE) $@"
@PATH="${ROOTDIR}/bin:${PATH}" $(GO) generate -x ${PACKAGES} @PATH="${ROOTDIR}/bin:${PATH}" $(GO) generate -x ${PACKAGES}
protos: bin/protoc-gen-gogoctrd ## generate protobuf protos: bin/protoc-gen-go-fieldpath
@echo "$(WHALE) $@" @echo "$(WHALE) $@"
@find . -path ./vendor -prune -false -o -name '*.pb.go' | xargs rm @find . -path ./vendor -prune -false -o -name '*.pb.go' | xargs rm
$(eval TMPDIR := $(shell mktemp -d)) $(eval TMPDIR := $(shell mktemp -d))
@ -168,7 +168,7 @@ protos: bin/protoc-gen-gogoctrd ## generate protobuf
@(PATH="${ROOTDIR}/bin:${PATH}" protobuild --quiet ${NON_API_PACKAGES}) @(PATH="${ROOTDIR}/bin:${PATH}" protobuild --quiet ${NON_API_PACKAGES})
@mv ${TMPDIR}/vendor ${ROOTDIR} @mv ${TMPDIR}/vendor ${ROOTDIR}
@rm -rf ${TMPDIR} @rm -rf ${TMPDIR}
go-fix-acronym -w -a '(Io|Uuid|Os)$$' $(shell find api -name '*.pb.go') go-fix-acronym -w -a '(Id|Io|Uuid|Os)$$' $(shell find api/ runtime/ -name '*.pb.go')
check-protos: protos ## check if protobufs needs to be generated again check-protos: protos ## check if protobufs needs to be generated again
@echo "$(WHALE) $@" @echo "$(WHALE) $@"

View File

@ -1,6 +1,5 @@
version = "1" version = "2"
generator = "gogoctrd" generators = ["go"]
plugins = ["grpc", "fieldpath"]
# Control protoc include paths. Below are usually some good defaults, but feel # Control protoc include paths. Below are usually some good defaults, but feel
# free to try it without them if it works for your project. # free to try it without them if it works for your project.
@ -12,25 +11,13 @@ plugins = ["grpc", "fieldpath"]
# Paths that should be treated as include roots in relation to the vendor # Paths that should be treated as include roots in relation to the vendor
# directory. These will be calculated with the vendor directory nearest the # directory. These will be calculated with the vendor directory nearest the
# target package. # target package.
packages = ["github.com/gogo/protobuf", "github.com/gogo/googleapis"] packages = ["github.com/gogo/protobuf"]
# Paths that will be added untouched to the end of the includes. We use # Paths that will be added untouched to the end of the includes. We use
# `/usr/local/include` to pickup the common install location of protobuf. # `/usr/local/include` to pickup the common install location of protobuf.
# This is the default. # This is the default.
after = ["/usr/local/include", "/usr/include"] after = ["/usr/local/include", "/usr/include"]
# This section maps protobuf imports to Go packages. These will become
# `-M` directives in the call to the go protobuf generator.
[packages]
"gogoproto/gogo.proto" = "github.com/gogo/protobuf/gogoproto"
"google/protobuf/any.proto" = "github.com/gogo/protobuf/types"
"google/protobuf/empty.proto" = "github.com/gogo/protobuf/types"
"google/protobuf/descriptor.proto" = "github.com/gogo/protobuf/protoc-gen-gogo/descriptor"
"google/protobuf/field_mask.proto" = "github.com/gogo/protobuf/types"
"google/protobuf/timestamp.proto" = "github.com/gogo/protobuf/types"
"google/protobuf/duration.proto" = "github.com/gogo/protobuf/types"
"google/rpc/status.proto" = "github.com/gogo/googleapis/google/rpc"
[[overrides]] [[overrides]]
# enable ttrpc and disable fieldpath and grpc for the shim # enable ttrpc and disable fieldpath and grpc for the shim
prefixes = [ prefixes = [
@ -38,7 +25,7 @@ prefixes = [
"github.com/containerd/containerd/api/runtime/task/v2", "github.com/containerd/containerd/api/runtime/task/v2",
"github.com/containerd/containerd/api/runtime/sandbox/v1", "github.com/containerd/containerd/api/runtime/sandbox/v1",
] ]
plugins = ["ttrpc"] generators = ["go", "go-ttrpc"]
# Lock down runc config # Lock down runc config
[[descriptors]] [[descriptors]]

View File

@ -1,6 +1,5 @@
version = "1" version = "2"
generator = "gogoctrd" generators = ["go", "go-grpc"]
plugins = ["grpc", "fieldpath"]
# Control protoc include paths. Below are usually some good defaults, but feel # Control protoc include paths. Below are usually some good defaults, but feel
# free to try it without them if it works for your project. # free to try it without them if it works for your project.
@ -12,7 +11,7 @@ plugins = ["grpc", "fieldpath"]
# Paths that should be treated as include roots in relation to the vendor # Paths that should be treated as include roots in relation to the vendor
# directory. These will be calculated with the vendor directory nearest the # directory. These will be calculated with the vendor directory nearest the
# target package. # target package.
packages = ["github.com/gogo/protobuf", "github.com/gogo/googleapis"] packages = ["github.com/gogo/protobuf"]
# Paths that will be added untouched to the end of the includes. We use # Paths that will be added untouched to the end of the includes. We use
# `/usr/local/include` to pickup the common install location of protobuf. # `/usr/local/include` to pickup the common install location of protobuf.
@ -22,22 +21,15 @@ plugins = ["grpc", "fieldpath"]
# This section maps protobuf imports to Go packages. These will become # This section maps protobuf imports to Go packages. These will become
# `-M` directives in the call to the go protobuf generator. # `-M` directives in the call to the go protobuf generator.
[packages] [packages]
"gogoproto/gogo.proto" = "github.com/gogo/protobuf/gogoproto" "google/rpc/status.proto" = "google.golang.org/genproto/googleapis/rpc/status"
"google/protobuf/any.proto" = "github.com/gogo/protobuf/types"
"google/protobuf/empty.proto" = "github.com/gogo/protobuf/types"
"google/protobuf/descriptor.proto" = "github.com/gogo/protobuf/protoc-gen-gogo/descriptor"
"google/protobuf/field_mask.proto" = "github.com/gogo/protobuf/types"
"google/protobuf/timestamp.proto" = "github.com/gogo/protobuf/types"
"google/protobuf/duration.proto" = "github.com/gogo/protobuf/types"
"google/rpc/status.proto" = "github.com/gogo/googleapis/google/rpc"
[[overrides]] [[overrides]]
prefixes = ["github.com/containerd/containerd/api/events"] prefixes = ["github.com/containerd/containerd/api/events"]
plugins = ["fieldpath"] # disable grpc for this package generators = ["go", "go-ttrpc", "go-fieldpath"]
[[overrides]] [[overrides]]
prefixes = ["github.com/containerd/containerd/api/services/ttrpc/events/v1"] prefixes = ["github.com/containerd/containerd/api/services/ttrpc/events/v1"]
plugins = ["ttrpc", "fieldpath"] generators = ["go", "go-ttrpc", "go-fieldpath"]
[[overrides]] [[overrides]]
# enable ttrpc and disable fieldpath and grpc for the shim # enable ttrpc and disable fieldpath and grpc for the shim
@ -46,7 +38,7 @@ prefixes = [
"github.com/containerd/containerd/api/runtime/task/v2", "github.com/containerd/containerd/api/runtime/task/v2",
"github.com/containerd/containerd/api/runtime/sandbox/v1", "github.com/containerd/containerd/api/runtime/sandbox/v1",
] ]
plugins = ["ttrpc"] generators = ["go", "go-ttrpc"]
# Aggregrate the API descriptors to lock down API changes. # Aggregrate the API descriptors to lock down API changes.
[[descriptors]] [[descriptors]]

View File

@ -1,73 +0,0 @@
/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"strings"
"github.com/gogo/protobuf/gogoproto"
"github.com/gogo/protobuf/proto"
"github.com/gogo/protobuf/protoc-gen-gogo/descriptor"
"github.com/gogo/protobuf/protoc-gen-gogo/generator"
"github.com/gogo/protobuf/vanity"
)
// CustomNameID preprocess the field, and set the [(gogoproto.customname) = "..."]
// if necessary, in order to avoid setting `gogoproto.customname` manually.
// The automatically assigned name should conform to Golang convention.
func CustomNameID(file *descriptor.FileDescriptorProto) {
f := func(field *descriptor.FieldDescriptorProto) {
// Skip if [(gogoproto.customname) = "..."] has already been set.
if gogoproto.IsCustomName(field) {
return
}
// Skip if embedded
if gogoproto.IsEmbed(field) {
return
}
if field.OneofIndex != nil {
return
}
fieldName := generator.CamelCase(*field.Name)
switch {
case *field.Name == "id":
// id -> ID
fieldName = "ID"
case strings.HasPrefix(*field.Name, "id_"):
// id_some -> IDSome
fieldName = "ID" + fieldName[2:]
case strings.HasSuffix(*field.Name, "_id"):
// some_id -> SomeID
fieldName = fieldName[:len(fieldName)-2] + "ID"
case strings.HasSuffix(*field.Name, "_ids"):
// some_ids -> SomeIDs
fieldName = fieldName[:len(fieldName)-3] + "IDs"
default:
return
}
if field.Options == nil {
field.Options = &descriptor.FieldOptions{}
}
if err := proto.SetExtension(field.Options, gogoproto.E_Customname, &fieldName); err != nil {
panic(err)
}
}
// Iterate through all fields in file
vanity.ForEachFieldExcludingExtensions(file.MessageType, f)
}

View File

@ -1,45 +0,0 @@
/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
_ "github.com/containerd/containerd/protobuf/plugin/fieldpath"
_ "github.com/containerd/ttrpc/plugin"
"github.com/gogo/protobuf/protoc-gen-gogo/descriptor"
"github.com/gogo/protobuf/vanity"
"github.com/gogo/protobuf/vanity/command"
)
func main() {
req := command.Read()
files := req.GetProtoFile()
files = vanity.FilterFiles(files, vanity.NotGoogleProtobufDescriptorProto)
for _, opt := range []func(*descriptor.FileDescriptorProto){
vanity.TurnOffGoGettersAll,
vanity.TurnOffGoStringerAll,
vanity.TurnOnMarshalerAll,
vanity.TurnOnStringerAll,
vanity.TurnOnUnmarshalerAll,
vanity.TurnOnSizerAll,
CustomNameID,
} {
vanity.ForEachFile(files, opt)
}
resp := command.Generate(req)
command.Write(resp)
}

View File

@ -1,172 +0,0 @@
/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package fieldpath
import (
"strings"
"github.com/gogo/protobuf/gogoproto"
"github.com/gogo/protobuf/protoc-gen-gogo/descriptor"
"github.com/gogo/protobuf/protoc-gen-gogo/generator"
)
type fieldpathGenerator struct {
*generator.Generator
generator.PluginImports
typeurlPkg generator.Single
}
func init() {
generator.RegisterPlugin(new(fieldpathGenerator))
}
func (p *fieldpathGenerator) Name() string {
return "fieldpath"
}
func (p *fieldpathGenerator) Init(g *generator.Generator) {
p.Generator = g
}
func (p *fieldpathGenerator) Generate(file *generator.FileDescriptor) {
p.PluginImports = generator.NewPluginImports(p.Generator)
p.typeurlPkg = p.NewImport("github.com/containerd/typeurl")
for _, m := range file.Messages() {
if m.DescriptorProto.GetOptions().GetMapEntry() {
continue
}
if Enabled(file.FileDescriptorProto, m.DescriptorProto) {
p.generateMessage(m)
}
}
}
func (p *fieldpathGenerator) generateMessage(m *generator.Descriptor) {
ccTypeName := generator.CamelCaseSlice(m.TypeName())
p.P()
p.P(`// Field returns the value for the given fieldpath as a string, if defined.`)
p.P(`// If the value is not defined, the second value will be false.`)
p.P("func(m *", ccTypeName, ") Field(fieldpath []string) (string, bool) {")
p.In()
var (
fields []*descriptor.FieldDescriptorProto
unhandled []*descriptor.FieldDescriptorProto
)
for _, f := range m.Field {
if f.IsBool() || f.IsString() || isLabelsField(f) || isAnyField(f) || isMessageField(f) {
fields = append(fields, f)
} else {
unhandled = append(unhandled, f)
}
}
if len(fields) > 0 {
p.P(`if len(fieldpath) == 0 {`)
p.In()
p.P(`return "", false`)
p.Out()
p.P(`}`)
p.P()
p.P("switch fieldpath[0] {")
for _, f := range unhandled {
p.P("// unhandled: ", f.GetName())
}
for _, f := range fields {
fName := generator.CamelCase(*f.Name)
if gogoproto.IsCustomName(f) {
fName = gogoproto.GetCustomName(f)
}
p.P(`case "`, f.GetName(), `":`)
p.In()
switch {
case isLabelsField(f):
p.P(`// Labels fields have been special-cased by name. If this breaks,`)
p.P(`// add better special casing to fieldpath plugin.`)
p.P(`if len(m.`, fName, `) == 0 {`)
p.In()
p.P(`return "", false`)
p.Out()
p.P("}")
p.P(`value, ok := m.`, fName, `[strings.Join(fieldpath[1:], ".")]`)
p.P(`return value, ok`)
case isAnyField(f):
p.P(`decoded, err := `, p.typeurlPkg.Use(), `.UnmarshalAny(m.`, fName, `)`)
p.P(`if err != nil {`)
p.In()
p.P(`return "", false`)
p.Out()
p.P(`}`)
p.P()
p.P(`adaptor, ok := decoded.(interface { Field([]string) (string, bool) })`)
p.P(`if !ok {`)
p.In()
p.P(`return "", false`)
p.Out()
p.P(`}`)
p.P(`return adaptor.Field(fieldpath[1:])`)
case isMessageField(f):
p.P(`// NOTE(stevvooe): This is probably not correct in many cases.`)
p.P(`// We assume that the target message also implements the Field`)
p.P(`// method, which isn't likely true in a lot of cases.`)
p.P(`//`)
p.P(`// If you have a broken build and have found this comment,`)
p.P(`// you may be closer to a solution.`)
p.P(`if m.`, fName, ` == nil {`)
p.In()
p.P(`return "", false`)
p.Out()
p.P(`}`)
p.P()
p.P(`return m.`, fName, `.Field(fieldpath[1:])`)
case f.IsString():
p.P(`return string(m.`, fName, `), len(m.`, fName, `) > 0`)
case f.IsBool():
p.P(`return fmt.Sprint(m.`, fName, `), true`)
}
p.Out()
}
p.P(`}`)
} else {
for _, f := range unhandled {
p.P("// unhandled: ", f.GetName())
}
}
p.P(`return "", false`)
p.Out()
p.P("}")
}
func isMessageField(f *descriptor.FieldDescriptorProto) bool {
return !f.IsRepeated() && f.IsMessage() && f.GetTypeName() != ".google.protobuf.Timestamp"
}
func isLabelsField(f *descriptor.FieldDescriptorProto) bool {
return f.IsMessage() && f.GetName() == "labels" && strings.HasSuffix(f.GetTypeName(), ".LabelsEntry")
}
func isAnyField(f *descriptor.FieldDescriptorProto) bool {
return !f.IsRepeated() && f.IsMessage() && f.GetTypeName() == ".google.protobuf.Any"
}

View File

@ -1,28 +0,0 @@
/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package fieldpath
import (
"github.com/containerd/containerd/protobuf/plugin"
"github.com/gogo/protobuf/proto"
"github.com/gogo/protobuf/protoc-gen-gogo/descriptor"
)
// Enabled returns true if E_Fieldpath is enabled
func Enabled(file *descriptor.FileDescriptorProto, message *descriptor.DescriptorProto) bool {
return proto.GetBoolExtension(message.Options, plugin.E_Fieldpath, proto.GetBoolExtension(file.Options, plugin.E_FieldpathAll, false))
}

View File

@ -25,6 +25,9 @@ go install github.com/containerd/protobuild@v0.2.0
go install github.com/containerd/protobuild/cmd/go-fix-acronym@v0.2.0 go install github.com/containerd/protobuild/cmd/go-fix-acronym@v0.2.0
go install github.com/cpuguy83/go-md2man/v2@v2.0.1 go install github.com/cpuguy83/go-md2man/v2@v2.0.1
go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.42.0 go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.42.0
go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.26
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.1
go install github.com/containerd/ttrpc/cmd/protoc-gen-go-ttrpc@944ef4a40df3446714a823207972b7d9858ffac5
# the following packages need to exist in $GOPATH so we can't use # the following packages need to exist in $GOPATH so we can't use
# go modules-aware mode of `go get` for these includes used during # go modules-aware mode of `go get` for these includes used during

View File

@ -58,3 +58,11 @@ ppc64le)
;; ;;
esac esac
rm -rf "$PROTOBUF_DIR" rm -rf "$PROTOBUF_DIR"
# Download status.proto. grpc repos' one seems copied from
# https://github.com/googleapis/googleapis/blob/master/google/rpc/status.proto,
# but we use grpc's since the repos has tags/releases.
mkdir -p /usr/local/include/google/rpc
curl \
-L https://raw.githubusercontent.com/grpc/grpc/v1.45.2/src/proto/grpc/status/status.proto \
-o /usr/local/include/google/rpc/status.proto