从Kubernetes CRD到Go导入

huangapple go评论88阅读模式
英文:

From Kubernetes CRD to Go import

问题

假设你想从 Kubernetes API 服务器获取名为 KubeadmControlPlane 的 kind/struct。

这意味着你需要将相关的 struct 导入到你的代码中。

KubeadmControlPlane 的匹配导入语句如下:

kubeadm "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1"

到目前为止,我需要花费很多时间来找到匹配的导入语句。

我使用的是 vscode。

你如何从 CRD kind 获取到导入语句?

英文:

Imagine you want to get kind/struct called KubeadmControlPlane from the kubernetes API server.

This means you need to import the related struct into your code.

A matching import statement for KubeadmControlPlane would be:

> kubeadm "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1"

Up to now I need to much time to find a matching import statement.

I use vscode.

How do you get from the CRD kind to an import statement?

答案1

得分: 3

原则上,在你的go.mod所在的文件夹中执行go get sigs.k8s.io/cluster-api@v1.4.2命令应该足够完成以下操作:

  • 更新你的go.mod文件,
  • 将库添加到你的$GOPATH中,
  • 启用VSCode的自动导入功能。

这意味着,当你开始输入结构体的名称,比如KubeadmControlPlane时,VSCode Go扩展应该会在你的GOPATH或项目的vendor目录中找到匹配的包,并建议自动导入。

如果没有的话,手动的过程如下:

  1. 确定CRD的API Group和Version: 这个信息通常可以在CRD的YAML文件的apiVersion字段中找到。例如,KubeadmControlPlane属于controlplane.cluster.x-k8s.io/v1beta1 API Group和Version。

  2. 找到API Group对应的Go Package: 你需要找到这个API Group对应的Go Package。对于KubeadmControlPlane来说,它属于sigs.k8s.io/cluster-api项目,具体的包路径是sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1。你也可以在pkg.go.dev的搜索页面上进行搜索,暂时还没有官方的API来查找包(issue 36785)。

  3. 确定CRD对应的Go Struct: Go Struct的命名通常与CRD的Kind类似。在这个例子中,它是KubeadmControlPlane

  4. 创建Go Import语句: 一旦你有了包路径和结构体名称,你就可以创建Go Import语句。例如:

import (
    kubeadm "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1"
)
英文:

In principle, a go get sigs.k8s.io/cluster-api@v1.4.2 (done in the folder where your go.mod is) should be enough to:

  • update your go.mod,
  • add the library in your $GOPATH and
  • enable VSCode auto-import to work.

That means, when you start typing the name of a struct, like KubeadmControlPlane, the VSCode Go extension should suggest an auto-import if it can find a matching package in your GOPATH or in your project's vendor directory.


If not, the manual process would be:

  1. Identify the API Group and Version of the CRD: This information is usually found in the apiVersion field of the CRD YAML file. For example, the KubeadmControlPlane is part of the controlplane.cluster.x-k8s.io/v1beta1 API group and version.

  2. Find the Go Package for the API Group: You need to find the corresponding Go package for this API group.
    In the case of the KubeadmControlPlane, it is part of the sigs.k8s.io/cluster-api project and the specific package path is sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1.
    A search in pkg.go.dev works too, pending an official API to lookup packages (issue 36785).

  3. Identify the Go Struct for the CRD: The Go struct is usually named similarly to the Kind of the CRD. In this case, it is KubeadmControlPlane.

  4. Create the Go Import Statement: Once you have the package path and struct name, you can create the Go import statement. For example:

import (
    kubeadm "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1"
)

huangapple
  • 本文由 发表于 2023年5月26日 14:53:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76338302.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定