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"):
    client = client.AppsV1Api()
        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?

  • 2
    \$\begingroup\$ From the return statements it seems like this is actually inside a function. If that is the case please include the whole function for context. \$\endgroup\$ – Graipher Feb 13 at 7:53
  • \$\begingroup\$ Updated the code snippet as a function \$\endgroup\$ – user3079474 Feb 13 at 9:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.