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

从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

图片转代码服务由CSDN问答提供 功能建议

我正在尝试使用非常基本的单个操作(即 GetDeployment )创建grpc服务 ,以名称空间和名称作为输入,并返回Kubernetes部署。 事实是,我不想为部署定义自己的消息,因为它已经存在于 Kubernetes官方存储库

我对grpc相当陌生,可能还不太了解 它可以正常工作,但是我可以将此消息导入我自己的文件中,然后再编写以下 .proto 文件吗?

  syntax =“ proto3  “; 
package api; 
 
import” google / api / annotations.proto“; 
import” k8s.io/kubernetes/pkg/api/v1/generated.proto";

message GetDeploymentOptions {
字符串名称空间 = 1; 
字符串名称= 2; 
} 
 
service AppsV1 {
 rpc GetDeployment(GetDeploymentOptions)返回(k8s.io.kubernetes.pkg.api.v1.Deployment){} 
} 
 <  / code>  
 
 

谢谢您

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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.

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题