<p>I have a python script which basically runs the <a href="https://www.debontonline.com/2020/11/kubernetes-part-11-how-to-configure.html" rel="noreferrer">following three commands</a>:</p>
<pre><code>kubectl apply -f class.yaml
kubectl apply -f rbac.yaml
kubectl apply -f deployment-arm.yaml
</code></pre>
<p>I want to use the <a href="https://github.com/kubernetes-client/python" rel="noreferrer">kubernetes-client</a> written in python to replace it. My current code, loads the there yaml files (using <code>pyyaml</code>), edits them a bit, inserts into a file and use the command line kubectl to execute those three commands. Some of the code:</p>
<pre><code># load files, edit them and dump into new files, part ...
result = run(['kubectl', 'apply', '-f', class_file_path])
# status check part ...
result = run(['kubectl', 'apply', '-f', rbac_file_path])
# status check part ...
result = run(['kubectl', 'apply', '-f', deployment_file_path])
# status check part ...
</code></pre>
<p>What I want to do: Replace those three commands with the python kubernetes-client. Reading the docs and seeing the <a href="https://stackoverflow.com/questions/56673919/kubernetes-python-api-client-execute-full-yaml-file">topic</a>, I came across with the <code>create_namespaced_deployment</code> method which I think I need to use for the <code>deployment_file_path</code> file. But I can't seem to figure out what I need to do with the two other files.</p>
<p>Assuming that I already loaded the three yaml files (using pyyaml) and edited them (without dumping into new files) and now you have free yaml dicts <code>deployment_dict</code>, <code>class_dict</code>, and <code>rbac_dict</code>, How can I use the client to execute the three above methods?</p>
<p><strong>EDIT</strong>: BTW if it's not possible to pass the three dicts, I could just dump them into files again but I want to use the python client instead of the kubectl. How to do it?</p>

<p>There is a separate function for every object and action:</p>
<pre class="lang-py prettyprint-override"><code>from kubernetes import client, config
import yaml

body = yaml.safe_load(&quot;my_deployment.yml&quot;)
config.load_kube_config()
apps_api = client.AppsV1Api()
apps_api.create_namespaced_deployment(body=body, namespace=&quot;default&quot;)
apps_api.replace_namespaced_deployment(body=body, namespace=&quot;default&quot;)
apps_api.patch_namespaced_deployment(body=body, namespace=&quot;default&quot;)
apps_api.delete_namespaced_deployment(body=body, namespace=&quot;default&quot;)

body = yaml.safe_load(&quot;my_cluster_role.yml&quot;)
rbac_api = client.RbacAuthorizationV1Api()
rbac_api.create_cluster_role(body=body)
rbac_api.patch_cluster_role(body=body)
rbac_api.replace_cluster_role(body=body)
rbac_api.delete_cluster_role(body=body)

# And so on
</code></pre>
<p>When you use <code>kubectl apply</code> you don't care if the object already exists, what API to use, which method to apply, etc. With the client library, as you can see from the example above, you need to:</p>
<ol>
<li>Load kube-config.</li>
<li>Select the right API to use.</li>
<li>Select the method you want to use. Note that <code>create_something</code> will not work if that <code>something</code> already exists.</li>
</ol>
<p>I recommend you to go through the <a href="https://github.com/kubernetes-client/python/tree/master/examples" rel="nofollow noreferrer">examples</a> that the library provides, they really are great to learn the thing.</p>
