'Python Kubernetes Client: equivalent of kubectl get [custom-resource]

With kubectl I can execute the following command:

kubectl get serviceentries 

Then I receive some information back. But serviceentries is a custom resource. So how do I go about getting the same information back but then with the kubernetes client?

Yaml looks like this for example:

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

Anyone know the right method to use?



Solution 1:[1]

you should be able to pull it using the python client like this:

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

That method applies to every custom resource within kubernetes and doesn't require any further definition to the python client.

Solution 2:[2]

To take it 1 step further from Moshe Shitrit's very helpful answer, you need to examine the kubectl response to see what apiVersion to use, since it can vary by kind of custom object. For example, for virtualservices:

kubectl get virtualservices -o json

{
    "apiVersion": "v1",
    "items": [
        {
            "apiVersion": "networking.istio.io/v1beta1",
            "kind": "VirtualService",
            ...

You can see from the apiVersion within the items array that group should be: networking.istio.io and version should be: v1beta1. If you're listing the virtualservices for a namespace it would then be:

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Moshe Shitrit
Solution 2 BrianEff