From 285f18472a8ee516cd01c7eab2db5be0890e89be Mon Sep 17 00:00:00 2001 From: Stefan Majer Date: Fri, 10 Mar 2023 09:38:46 +0100 Subject: [PATCH] Migrate to buf (#44) --- Makefile | 3 +- api/v1/initializer.pb.go | 98 ++++--------------------------- api/v1/initializer.proto | 22 ------- api/v1/initializer_grpc.pb.go | 107 ++++++++++++++++++++++++++++++++++ proto/Makefile | 19 ++++++ proto/buf.gen.yaml | 14 +++++ proto/buf.yaml | 7 +++ proto/v1/initializer.proto | 21 +++++++ 8 files changed, 179 insertions(+), 112 deletions(-) delete mode 100644 api/v1/initializer.proto create mode 100644 api/v1/initializer_grpc.pb.go create mode 100644 proto/Makefile create mode 100644 proto/buf.gen.yaml create mode 100644 proto/buf.yaml create mode 100644 proto/v1/initializer.proto diff --git a/Makefile b/Makefile index f9035d9..5ffe8c9 100644 --- a/Makefile +++ b/Makefile @@ -22,8 +22,7 @@ all: .PHONY: proto proto: - docker pull metalstack/builder - docker run -it --rm --user $$(id -u):$$(id -g) -v ${PWD}/api:/work/api metalstack/builder protoc -I api/ api/v1/*.proto --go_out=plugins=grpc:api + make -C proto protoc .PHONY: dockerimage dockerimage: diff --git a/api/v1/initializer.pb.go b/api/v1/initializer.pb.go index 06acd7f..41e1fbc 100644 --- a/api/v1/initializer.pb.go +++ b/api/v1/initializer.pb.go @@ -1,16 +1,12 @@ // Code generated by protoc-gen-go. DO NOT EDIT. // versions: -// protoc-gen-go v1.27.1 -// protoc v3.19.1 +// protoc-gen-go v1.28.1 +// protoc (unknown) // source: v1/initializer.proto package v1 import ( - context "context" - grpc "google.golang.org/grpc" - codes "google.golang.org/grpc/codes" - status "google.golang.org/grpc/status" protoreflect "google.golang.org/protobuf/reflect/protoreflect" protoimpl "google.golang.org/protobuf/runtime/protoimpl" reflect "reflect" @@ -185,8 +181,14 @@ var file_v1_initializer_proto_rawDesc = []byte{ 0x69, 0x74, 0x69, 0x61, 0x6c, 0x69, 0x7a, 0x65, 0x72, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x27, 0x0a, 0x06, 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x12, 0x09, 0x2e, 0x76, 0x31, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x1a, 0x12, 0x2e, 0x76, 0x31, 0x2e, 0x53, 0x74, 0x61, 0x74, 0x75, - 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x42, 0x06, 0x5a, 0x04, 0x2e, 0x2f, 0x76, - 0x31, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x42, 0x6c, 0x0a, 0x06, 0x63, 0x6f, 0x6d, + 0x2e, 0x76, 0x31, 0x42, 0x10, 0x49, 0x6e, 0x69, 0x74, 0x69, 0x61, 0x6c, 0x69, 0x7a, 0x65, 0x72, + 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x50, 0x01, 0x5a, 0x28, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, + 0x63, 0x6f, 0x6d, 0x2f, 0x6d, 0x65, 0x74, 0x61, 0x6c, 0x2d, 0x73, 0x74, 0x61, 0x63, 0x6b, 0x2f, + 0x64, 0x72, 0x6f, 0x70, 0x74, 0x61, 0x69, 0x6c, 0x65, 0x72, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x76, + 0x31, 0xa2, 0x02, 0x03, 0x56, 0x58, 0x58, 0xaa, 0x02, 0x02, 0x56, 0x31, 0xca, 0x02, 0x02, 0x56, + 0x31, 0xe2, 0x02, 0x0e, 0x56, 0x31, 0x5c, 0x47, 0x50, 0x42, 0x4d, 0x65, 0x74, 0x61, 0x64, 0x61, + 0x74, 0x61, 0xea, 0x02, 0x02, 0x56, 0x31, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -270,83 +272,3 @@ func file_v1_initializer_proto_init() { file_v1_initializer_proto_goTypes = nil file_v1_initializer_proto_depIdxs = nil } - -// Reference imports to suppress errors if they are not otherwise used. -var _ context.Context -var _ grpc.ClientConnInterface - -// This is a compile-time assertion to ensure that this generated file -// is compatible with the grpc package it is being compiled against. -const _ = grpc.SupportPackageIsVersion6 - -// InitializerServiceClient is the client API for InitializerService service. -// -// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream. -type InitializerServiceClient interface { - Status(ctx context.Context, in *Empty, opts ...grpc.CallOption) (*StatusResponse, error) -} - -type initializerServiceClient struct { - cc grpc.ClientConnInterface -} - -func NewInitializerServiceClient(cc grpc.ClientConnInterface) InitializerServiceClient { - return &initializerServiceClient{cc} -} - -func (c *initializerServiceClient) Status(ctx context.Context, in *Empty, opts ...grpc.CallOption) (*StatusResponse, error) { - out := new(StatusResponse) - err := c.cc.Invoke(ctx, "/v1.InitializerService/Status", in, out, opts...) - if err != nil { - return nil, err - } - return out, nil -} - -// InitializerServiceServer is the server API for InitializerService service. -type InitializerServiceServer interface { - Status(context.Context, *Empty) (*StatusResponse, error) -} - -// UnimplementedInitializerServiceServer can be embedded to have forward compatible implementations. -type UnimplementedInitializerServiceServer struct { -} - -func (*UnimplementedInitializerServiceServer) Status(context.Context, *Empty) (*StatusResponse, error) { - return nil, status.Errorf(codes.Unimplemented, "method Status not implemented") -} - -func RegisterInitializerServiceServer(s *grpc.Server, srv InitializerServiceServer) { - s.RegisterService(&_InitializerService_serviceDesc, srv) -} - -func _InitializerService_Status_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { - in := new(Empty) - if err := dec(in); err != nil { - return nil, err - } - if interceptor == nil { - return srv.(InitializerServiceServer).Status(ctx, in) - } - info := &grpc.UnaryServerInfo{ - Server: srv, - FullMethod: "/v1.InitializerService/Status", - } - handler := func(ctx context.Context, req interface{}) (interface{}, error) { - return srv.(InitializerServiceServer).Status(ctx, req.(*Empty)) - } - return interceptor(ctx, in, info, handler) -} - -var _InitializerService_serviceDesc = grpc.ServiceDesc{ - ServiceName: "v1.InitializerService", - HandlerType: (*InitializerServiceServer)(nil), - Methods: []grpc.MethodDesc{ - { - MethodName: "Status", - Handler: _InitializerService_Status_Handler, - }, - }, - Streams: []grpc.StreamDesc{}, - Metadata: "v1/initializer.proto", -} diff --git a/api/v1/initializer.proto b/api/v1/initializer.proto deleted file mode 100644 index a073c86..0000000 --- a/api/v1/initializer.proto +++ /dev/null @@ -1,22 +0,0 @@ -syntax = "proto3"; - -package v1; -option go_package = "./v1"; - -service InitializerService { - rpc Status(Empty) returns (StatusResponse); -} - -message Empty { - -} - -message StatusResponse { - enum InitializerStatus { - CHECKING = 0; - RESTORING = 1; - DONE = 2; - } - InitializerStatus status = 1; - string message = 2; -} diff --git a/api/v1/initializer_grpc.pb.go b/api/v1/initializer_grpc.pb.go new file mode 100644 index 0000000..2535214 --- /dev/null +++ b/api/v1/initializer_grpc.pb.go @@ -0,0 +1,107 @@ +// Code generated by protoc-gen-go-grpc. DO NOT EDIT. +// versions: +// - protoc-gen-go-grpc v1.3.0 +// - protoc (unknown) +// source: v1/initializer.proto + +package v1 + +import ( + context "context" + grpc "google.golang.org/grpc" + codes "google.golang.org/grpc/codes" + status "google.golang.org/grpc/status" +) + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the grpc package it is being compiled against. +// Requires gRPC-Go v1.32.0 or later. +const _ = grpc.SupportPackageIsVersion7 + +const ( + InitializerService_Status_FullMethodName = "/v1.InitializerService/Status" +) + +// InitializerServiceClient is the client API for InitializerService service. +// +// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream. +type InitializerServiceClient interface { + Status(ctx context.Context, in *Empty, opts ...grpc.CallOption) (*StatusResponse, error) +} + +type initializerServiceClient struct { + cc grpc.ClientConnInterface +} + +func NewInitializerServiceClient(cc grpc.ClientConnInterface) InitializerServiceClient { + return &initializerServiceClient{cc} +} + +func (c *initializerServiceClient) Status(ctx context.Context, in *Empty, opts ...grpc.CallOption) (*StatusResponse, error) { + out := new(StatusResponse) + err := c.cc.Invoke(ctx, InitializerService_Status_FullMethodName, in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// InitializerServiceServer is the server API for InitializerService service. +// All implementations should embed UnimplementedInitializerServiceServer +// for forward compatibility +type InitializerServiceServer interface { + Status(context.Context, *Empty) (*StatusResponse, error) +} + +// UnimplementedInitializerServiceServer should be embedded to have forward compatible implementations. +type UnimplementedInitializerServiceServer struct { +} + +func (UnimplementedInitializerServiceServer) Status(context.Context, *Empty) (*StatusResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method Status not implemented") +} + +// UnsafeInitializerServiceServer may be embedded to opt out of forward compatibility for this service. +// Use of this interface is not recommended, as added methods to InitializerServiceServer will +// result in compilation errors. +type UnsafeInitializerServiceServer interface { + mustEmbedUnimplementedInitializerServiceServer() +} + +func RegisterInitializerServiceServer(s grpc.ServiceRegistrar, srv InitializerServiceServer) { + s.RegisterService(&InitializerService_ServiceDesc, srv) +} + +func _InitializerService_Status_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(Empty) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(InitializerServiceServer).Status(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: InitializerService_Status_FullMethodName, + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(InitializerServiceServer).Status(ctx, req.(*Empty)) + } + return interceptor(ctx, in, info, handler) +} + +// InitializerService_ServiceDesc is the grpc.ServiceDesc for InitializerService service. +// It's only intended for direct use with grpc.RegisterService, +// and not to be introspected or modified (even as a copy) +var InitializerService_ServiceDesc = grpc.ServiceDesc{ + ServiceName: "v1.InitializerService", + HandlerType: (*InitializerServiceServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "Status", + Handler: _InitializerService_Status_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "v1/initializer.proto", +} diff --git a/proto/Makefile b/proto/Makefile new file mode 100644 index 0000000..fce7ea7 --- /dev/null +++ b/proto/Makefile @@ -0,0 +1,19 @@ +MAKEFLAGS += --no-print-directory +BUF_VERSION := 1.15.0 + +_buf: + docker run --rm \ + --entrypoint sh \ + -v $(PWD)/..:/workspace \ + -w /workspace/proto \ + bufbuild/buf:$(BUF_VERSION) \ + -c "buf $(CMD) && chown -R $(shell id -u):$(shell id -g) /workspace" + +.PHONY: protolint +protolint: + @$(MAKE) _buf CMD="format -w v1" + @$(MAKE) _buf CMD="lint -v" + +.PHONY: protoc +protoc: protolint + @$(MAKE) _buf CMD="generate -v" diff --git a/proto/buf.gen.yaml b/proto/buf.gen.yaml new file mode 100644 index 0000000..6377dc2 --- /dev/null +++ b/proto/buf.gen.yaml @@ -0,0 +1,14 @@ +version: v1 +managed: + enabled: true + go_package_prefix: + default: github.com/metal-stack/droptailer/api +plugins: + # generate go structs for protocol buffer definition + - plugin: buf.build/protocolbuffers/go:v1.28.1 + out: ../api + opt: paths=source_relative + - plugin: buf.build/grpc/go:v1.3.0 + out: ../api + opt: paths=source_relative,require_unimplemented_servers=false + diff --git a/proto/buf.yaml b/proto/buf.yaml new file mode 100644 index 0000000..d4ff52d --- /dev/null +++ b/proto/buf.yaml @@ -0,0 +1,7 @@ +version: v1 +breaking: + use: + - FILE +lint: + use: + - BASIC diff --git a/proto/v1/initializer.proto b/proto/v1/initializer.proto new file mode 100644 index 0000000..899f9e6 --- /dev/null +++ b/proto/v1/initializer.proto @@ -0,0 +1,21 @@ +syntax = "proto3"; + +package v1; + +option go_package = "./v1"; + +service InitializerService { + rpc Status(Empty) returns (StatusResponse); +} + +message Empty {} + +message StatusResponse { + enum InitializerStatus { + CHECKING = 0; + RESTORING = 1; + DONE = 2; + } + InitializerStatus status = 1; + string message = 2; +}