<p>My Bash script using <code>kubectl create/apply -f ...</code> to deploy lots of Kubernetes resources has grown too large for Bash. I'm converting it to Python using the PyPI <code>kubernetes</code> package.</p>

<p>Is there a generic way to create resources given the YAML manifest? Otherwise, the only way I can see to do it would be to create and maintain a mapping from Kind to API method <code>create_namespaced_&lt;kind&gt;</code>. That seems tedious and error prone to me.</p>

<p>Update: I'm deploying many (10-20) resources to many (10+) GKE clusters.</p>

<p>Update in the year 2020, for anyone still interested in this (since the docs for the python library is mostly empty).</p>
<p>At the end of 2018 <a href="https://github.com/kubernetes-client/python/pull/655" rel="nofollow noreferrer">this</a> pull request has been merged,
so it's now possible to do:</p>
<pre class="lang-py prettyprint-override"><code>from kubernetes import client, config
from kubernetes import utils

config.load_kube_config()
api = client.ApiClient()

file_path = ... # A path to a deployment file
namespace = 'default'

utils.create_from_yaml(api, file_path, namespace=namespace)
</code></pre>
<hr />
<p>EDIT: from a request in a comment, a snippet for skipping the python error if the deployment already exists</p>
<pre class="lang-py prettyprint-override"><code>from kubernetes import client, config
from kubernetes import utils

config.load_kube_config()
api = client.ApiClient()


def skip_if_already_exists(e):
    import json
    # found in https://github.com/kubernetes-client/python/blob/master/kubernetes/utils/create_from_yaml.py#L165
    info = json.loads(e.api_exceptions[0].body)
    if info.get('reason').lower() == 'alreadyexists':
        pass
    else
        raise e


file_path = ... # A path to a deployment file
namespace = 'default'

try:
    utils.create_from_yaml(api, file_path, namespace=namespace)
except utils.FailToCreateError as e:
    skip_if_already_exists(e)
</code></pre>
