See if a specific resource exists in a Kubernetes namespace I want to have a function that checks to see if a certain resource (deployment, job, cronjob etc) exist in a namespace. Using the Kubernetes Python client, I can create a boolean function (using deployments as a concrete example).

from kubernetes import client, config

def deployment_exists(name, namespace="default"):
    config.load_kube_config()
    client = client.AppsV1Api()
    try:
        client.read_namespaced_deployment(name, namespace)
        return True
    except ApiException as e:
        if e.status == 404:
            return False
    return False


Is there a way to achieve the same without relying on the ApiException explicitly?
Without knowing more about the kubernetes API that's hard to say, maybe you'd be more lucky on StackOverflow instead.

That said, if there's no explicit function to check, probably not.

Especially the status code 404 sounds like it would probably mean "not present", so this doesn't seem overly bad actually.



Is it intentional that all other ApiExceptions also return false?  If so, than there's no point to check for the 404.  If not, they should be thrown again.