[英]Check if a pod is running on a k8s cluster in a go program
I am writing a command line tool in Go which will perform an action based on the existence of a particular pod on a k8s
cluster, in a specific namespace.我正在用 Go 编写一个命令行工具,它将根据特定命名空间中k8s
集群上特定 pod 的存在执行操作。
I could do via command line (shell) invocations within my go
program something like我可以在我的go
程序中通过命令行(shell)调用来做类似的事情
kubectl get pods -n mynapespace l app=myapp
or in case I am not certain about the labels, something even less elegant as:或者如果我不确定标签,那么一些更不优雅的东西是:
kubectl get pods -n mynapespace | grep -i somepatternIamcertainabout
However, given that I am using the k8s native language (Go) I was wondering whether there might be a more Go native/specific way of making such an inquiry to the k8s api server, without resorting to shell invocations from within my cli tool.但是,考虑到我使用的是 k8s 本机语言 (Go),我想知道是否有更多的 Go 本机/特定方法来对 k8s api 服务器进行此类查询,而无需从我的 cli 工具中求助于 shell 调用。
The kubectl utility is just a convenience wrapper that talks to the Kubernetes API using bog standard HTTP. kubectl 实用程序只是一个方便的包装器,它使用标准 HTTP 与 Kubernetes API 通信。 The Go standard library has a great http package . Go 标准库有一个很棒的http 包。 The perfect fit for what you're trying to accomplish.非常适合您要完成的任务。
In fact, you could just use this official client package from the Kubernetes project itself.事实上,您可以直接使用 Kubernetes 项目本身的这个官方客户端包。
However, given that I am using the k8s native language (Go) I was wondering whether there might be a more Go native/specific way of making such an inquiry to the k8s api server, without resorting to shell invocations from within my cli tool.但是,考虑到我使用的是 k8s 本机语言 (Go),我想知道是否有更多的 Go 本机/特定方法来对 k8s api 服务器进行此类查询,而无需从我的 cli 工具中求助于 shell 调用。
If you want to talk with k8s cluster in your programs written in go without resorting to shell invocations, client-go library is the way to go.如果你想在用go编写的程序中与k8s 集群对话而不求助于 shell 调用, client-go库是你的不二之选。 It contains everything you need to query your k8s api server in your go programs.它包含您在 go 程序中查询 k8s api 服务器所需的一切。
What's included包括什么
- The
kubernetes
package contains the clientset to access Kubernetes API.kubernetes
包包含访问 Kubernetes API 的客户端集。- The
discovery
package is used to discover APIs supported by a Kubernetes API server.discovery
包用于发现 Kubernetes API 服务器支持的 API。- The
dynamic
package contains a dynamic client that can perform generic operations on arbitrary Kubernetes API objects.dynamic
包包含一个动态客户端,可以对任意 Kubernetes API 对象执行通用操作。- The
plugin/pkg/client/auth
packages contain optional authentication plugins for obtaining credentials from external sources.plugin/pkg/client/auth
包包含可选的身份验证插件,用于从外部来源获取凭证。- The
transport
package is used to set up auth and start a connection.transport
包用于设置身份验证和启动连接。- The
tools/cache
package is useful for writing controllers.tools/cache
包对于编写控制器很有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.