【问题标题】:Python Kubernetes Client: equivalent of kubectl get [custom-resource]Python Kubernetes 客户端:相当于 kubectl get [custom-resource]
【发布时间】:2022-04-27 00:33:09
【问题描述】:

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

kubectl get serviceentries 

然后我收到一些信息。但 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


    【解决方案1】:

    你应该可以像这样使用 python 客户端来拉取它:

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

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

    【讨论】:

      【解决方案2】:

      要比 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")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-07-18
        • 2021-05-02
        • 2022-12-19
        • 2023-04-04
        • 2020-06-14
        • 2022-10-21
        • 2011-09-18
        相关资源
        最近更新 更多