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

从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 微信小程序 用oss下载 aliyun-oss-sdk-6.18.0.min client报错
  • ¥15 ArcGIS批量裁剪
  • ¥15 labview程序设计
  • ¥15 为什么在配置Linux系统的时候执行脚本总是出现E: Failed to fetch http:L/cn.archive.ubuntu.com
  • ¥15 Cloudreve保存用户组存储空间大小时报错
  • ¥15 伪标签为什么不能作为弱监督语义分割的结果?
  • ¥15 编一个判断一个区间范围内的数字的个位数的立方和是否等于其本身的程序在输入第1组数据后卡住了(语言-c语言)
  • ¥15 Mac版Fiddler Everywhere4.0.1提示强制更新
  • ¥15 android 集成sentry上报时报错。
  • ¥15 抖音看过的视频,缓存在哪个文件