diff --git a/Makefile b/Makefile index d74d90be5..f6c268180 100644 --- a/Makefile +++ b/Makefile @@ -159,7 +159,7 @@ generate: protos @echo "$(WHALE) $@" @PATH="${ROOTDIR}/bin:${PATH}" $(GO) generate -x ${PACKAGES} -protos: bin/protoc-gen-gogoctrd ## generate protobuf +protos: bin/protoc-gen-go-fieldpath @echo "$(WHALE) $@" @find . -path ./vendor -prune -false -o -name '*.pb.go' | xargs rm $(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}) @mv ${TMPDIR}/vendor ${ROOTDIR} @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 @echo "$(WHALE) $@" diff --git a/Protobuild.toml b/Protobuild.toml index bb68f7ce2..e5a441531 100644 --- a/Protobuild.toml +++ b/Protobuild.toml @@ -1,6 +1,5 @@ -version = "1" -generator = "gogoctrd" -plugins = ["grpc", "fieldpath"] +version = "2" +generators = ["go"] # Control protoc include paths. Below are usually some good defaults, but feel # 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 # directory. These will be calculated with the vendor directory nearest the # 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 # `/usr/local/include` to pickup the common install location of protobuf. # This is the default. 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]] # enable ttrpc and disable fieldpath and grpc for the shim prefixes = [ @@ -38,7 +25,7 @@ prefixes = [ "github.com/containerd/containerd/api/runtime/task/v2", "github.com/containerd/containerd/api/runtime/sandbox/v1", ] -plugins = ["ttrpc"] +generators = ["go", "go-ttrpc"] # Lock down runc config [[descriptors]] diff --git a/api/Protobuild.toml b/api/Protobuild.toml index d1815786e..9a01909d4 100644 --- a/api/Protobuild.toml +++ b/api/Protobuild.toml @@ -1,6 +1,5 @@ -version = "1" -generator = "gogoctrd" -plugins = ["grpc", "fieldpath"] +version = "2" +generators = ["go", "go-grpc"] # Control protoc include paths. Below are usually some good defaults, but feel # 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 # directory. These will be calculated with the vendor directory nearest the # 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 # `/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 # `-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" + "google/rpc/status.proto" = "google.golang.org/genproto/googleapis/rpc/status" [[overrides]] prefixes = ["github.com/containerd/containerd/api/events"] -plugins = ["fieldpath"] # disable grpc for this package +generators = ["go", "go-ttrpc", "go-fieldpath"] [[overrides]] prefixes = ["github.com/containerd/containerd/api/services/ttrpc/events/v1"] -plugins = ["ttrpc", "fieldpath"] +generators = ["go", "go-ttrpc", "go-fieldpath"] [[overrides]] # 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/sandbox/v1", ] -plugins = ["ttrpc"] +generators = ["go", "go-ttrpc"] # Aggregrate the API descriptors to lock down API changes. [[descriptors]] diff --git a/cmd/protoc-gen-gogoctrd/customnameid.go b/cmd/protoc-gen-gogoctrd/customnameid.go deleted file mode 100644 index 50aaf4a9e..000000000 --- a/cmd/protoc-gen-gogoctrd/customnameid.go +++ /dev/null @@ -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) -} diff --git a/cmd/protoc-gen-gogoctrd/main.go b/cmd/protoc-gen-gogoctrd/main.go deleted file mode 100644 index 47c3f8a96..000000000 --- a/cmd/protoc-gen-gogoctrd/main.go +++ /dev/null @@ -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) -} diff --git a/protobuf/plugin/fieldpath/fieldpath.go b/protobuf/plugin/fieldpath/fieldpath.go deleted file mode 100644 index b5f3ff117..000000000 --- a/protobuf/plugin/fieldpath/fieldpath.go +++ /dev/null @@ -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" -} diff --git a/protobuf/plugin/fieldpath/helpers.go b/protobuf/plugin/fieldpath/helpers.go deleted file mode 100644 index 5bd1b784c..000000000 --- a/protobuf/plugin/fieldpath/helpers.go +++ /dev/null @@ -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)) -} diff --git a/script/setup/install-dev-tools b/script/setup/install-dev-tools index e0e238818..c615b1c3f 100755 --- a/script/setup/install-dev-tools +++ b/script/setup/install-dev-tools @@ -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/cpuguy83/go-md2man/v2@v2.0.1 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 # go modules-aware mode of `go get` for these includes used during diff --git a/script/setup/install-protobuf b/script/setup/install-protobuf index ffb00fba6..8c603a79a 100755 --- a/script/setup/install-protobuf +++ b/script/setup/install-protobuf @@ -58,3 +58,11 @@ ppc64le) ;; esac 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