<p>The Python API is available to read objects from a cluster. By cloning we can say:</p>

<ol>
<li>Get a copy of an existing Kubernetes object using <code>kubectl get</code></li>
<li>Change the properties of the object</li>
<li>Apply the new object</li>
</ol>

<p>Until recently, the option to <a href="https://medium.com/@jonathan.johnson/export-has-been-deprecated-in-1-14-51cfef5a0cb7" rel="nofollow noreferrer"><code>--export</code> api was deprecated in 1.14</a>. How can we use the Python Kubernetes API to do the steps from 1-3 described above?</p>

<p>There are multiple questions about how to extract the <a href="https://github.com/kubernetes-client/python/issues/977" rel="nofollow noreferrer">code from Python API to YAML</a>, but it's unclear how to transform the Kubernetes API object. </p>

Marcello DeSales
<p>Just use <code>to_dict()</code> which is now offered by Kubernetes Client objects. Note that it creates a partly deep copy. So to be safe:</p>
<pre><code>copied_obj = copy.deepcopy(obj.to_dict())
</code></pre>
<p>Dicts can be passed to <code>create*</code> and <code>patch*</code> methods.</p>
<p>For convenience, you can also wrap the dict in <a href="https://github.com/ramazanpolat/prodict" rel="nofollow noreferrer"><code>Prodict</code></a>.</p>
<pre><code>copied_obj = Prodict.from_dict(copy.deepcopy(obj.to_dict()))
</code></pre>
<p>The final issue is getting rid of superfluous fields. (Unfortunately, Kubernetes sprinkles them throughout the object.) I use <a href="https://kopf.readthedocs.io/en/stable/" rel="nofollow noreferrer"><code>kopf</code></a>'s internal facility for getting the &quot;essence&quot; of an object. (It takes care of the deep copy.)</p>
<pre><code>copied_obj = kopf.AnnotationsDiffBaseStorage().build(body=kopf.Body(obj.to_dict()))
copied_obj = Prodic.from_dict(copied_obj)
</code></pre>
