Python-Kubernetes客户端:相当于kubectl get[自定义资源]



使用kubectl,我可以执行以下命令:

kubectl get serviceentries 

然后我收到一些信息。但是服务条目是一种自定义资源。那么,我该如何在使用kubernetes客户端的情况下获取相同的信息呢?

例如,Yaml看起来像这样:

apiVersion: networking.istio.io/v1alpha3
kind: ServiceEntry
metadata:
name: external-svc-https
spec:
hosts:
- api.dropboxapi.com
- www.googleapis.com
- api.facebook.com
location: MESH_EXTERNAL
ports:
- number: 443
name: https
protocol: TLS
resolution: DNS

有人知道正确的使用方法吗?

您应该能够使用python客户端来提取它,如下所示:

kubernetes.client.CustomObjectsApi().list_cluster_custom_object(group="networking.istio.io", version="v1alpha3", plural="serviceentries")

该方法适用于kubernetes中的每个自定义资源,不需要对python客户端进行任何进一步的定义。

要进一步了解Moshe Shitrit非常有用的答案,您需要检查kubectl响应,看看要使用什么apiVersion,因为它可能因自定义对象的种类而异。例如,对于虚拟服务:

kubectl get virtualservices -o json
{
"apiVersion": "v1",
"items": [
{
"apiVersion": "networking.istio.io/v1beta1",
"kind": "VirtualService",
...

您可以从items数组中的apiVersion中看到,组应该是:networking.istio.io,版本应该是:v1beta1。如果你列出了一个名称空间的虚拟服务,那么它应该是:

kubernetes.client.CustomObjectsApi().list_namespaced_custom_object("networking.istio.io", "v1beta1", namespace, "virtualservices")

最新更新