doudao1837 2019-08-30 20:14
浏览 1022
已采纳

从protobuf中的其他包导入类型定义

I am trying to create a grpc service with a very basic single action which is GetDeployment, takes a namespace and a name as an input, and returns a Kubernetes deployment. The thing is that I do not want to define my own message for the Deployment as it already exists on the official Kubernetes repository.

I am pretty new to grpc and probably do not understand well enough how it works but can I import this message to my own file in a way I could then write the following .proto file ?

syntax = "proto3";
package api;

import "google/api/annotations.proto";
import "k8s.io/kubernetes/pkg/api/v1/generated.proto";

message GetDeploymentOptions {
  string namespace = 1;
  string name = 2;
}

service AppsV1 {
  rpc GetDeployment(GetDeploymentOptions) returns (k8s.io.kubernetes.pkg.api.v1.Deployment) {}
}

Thank you in advance

  • 写回答

1条回答 默认 最新

  • douhe4336 2019-08-30 21:31
    关注

    GRPC codegen is just a protoc plugin. It generates code for service and rpc but it follows the normal protobuf rules for imports.

    In your example, if your file is in src/api.proto and the k8s api repo is a git submodule checked out into thirdparty/k8s.io/api folder you would generate the files you'd need by running:

    root>protoc.exe -I thirdparty k8s.io/api/core/v1/generated.proto --go_out=go
    root>protoc.exe -I thirdparty src/api.proto --go_out=plugins=grpc:go
    

    The first command is generating the .pb.go file which contains the k8s messages, while the second command is generating the .pb.go file which contains your messages and your service.

    Looking at the transient imports of that file, you may also need to checkout api-machinery into k8s.io/apimachinery and run protoc on that file as well.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误