英文:
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目录中找到匹配的包,并建议自动导入。
如果没有的话,手动的过程如下:
-
确定CRD的API Group和Version: 这个信息通常可以在CRD的YAML文件的
apiVersion
字段中找到。例如,KubeadmControlPlane
属于controlplane.cluster.x-k8s.io/v1beta1
API Group和Version。 -
找到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)。 -
确定CRD对应的Go Struct: Go Struct的命名通常与CRD的Kind类似。在这个例子中,它是
KubeadmControlPlane
。 -
创建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:
-
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, theKubeadmControlPlane
is part of thecontrolplane.cluster.x-k8s.io/v1beta1
API group and version. -
Find the Go Package for the API Group: You need to find the corresponding Go package for this API group.
In the case of theKubeadmControlPlane
, it is part of thesigs.k8s.io/cluster-api
project and the specific package path issigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1
.
A search inpkg.go.dev
works too, pending an official API to lookup packages (issue 36785). -
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
. -
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"
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论