<p>I have a YAML file as like below which I have exported from an existing cluster:</p>
<pre><code>apiVersion: v1
items:
- apiVersion: v1
  kind: ServiceAccount
  metadata:
    creationTimestamp: 2019-03-20T23:17:42Z
    name: default
    namespace: dev4
    resourceVersion: &quot;80999&quot;
    selfLink: /api/v1/namespaces/dev4/serviceaccounts/default
    uid: 5c6e0d09-4b66-11e9-b4e3-0a779a87bb40
  secrets:
  - name: default-token-tl4dd
- apiVersion: v1
  kind: ServiceAccount
  metadata:
    annotations:
      kubectl.kubernetes.io/last-applied-configuration: |
        {&quot;apiVersion&quot;:&quot;v1&quot;,&quot;kind&quot;:&quot;ServiceAccount&quot;,&quot;metadata&quot;:{&quot;annotations&quot;:{},&quot;name&quot;:&quot;pod-labeler&quot;,&quot;namespace&quot;:&quot;dev4&quot;}}
    creationTimestamp: 2020-04-21T05:46:25Z
    name: pod-labeler
    namespace: dev4
    resourceVersion: &quot;113455688&quot;
    selfLink: /api/v1/namespaces/dev4/serviceaccounts/pod-labeler
    uid: 702dadda-8393-11ea-abd9-0a768ca51346
  secrets:
  - name: pod-labeler-token-6vgp7
kind: List
metadata:
  resourceVersion: &quot;&quot;
  selfLink: &quot;&quot;

</code></pre>
<p>If I do the above YAML and apply to a new cluster, I get an error, which is out of the scope of this question.</p>
<p>In summary, I have to get rid of the below attributes:</p>
<pre><code>uid:
selfLink:
resourceVersion:
creationTimestamp:
</code></pre>
<p>So I got a sed command like below which does the trick</p>
<pre><code>sed -i '/uid: \|selfLink: \|resourceVersion: \|creationTimestamp: /d' dev4-serviceaccounts.yaml
</code></pre>
<p>The final YAML file is like below:</p>
<pre><code>apiVersion: v1
items:
- apiVersion: v1
  kind: ServiceAccount
  metadata:
    name: default
    namespace: dev4
  secrets:
  - name: default-token-tl4dd
- apiVersion: v1
  kind: ServiceAccount
  metadata:
    annotations:
      kubectl.kubernetes.io/last-applied-configuration: |
        {&quot;apiVersion&quot;:&quot;v1&quot;,&quot;kind&quot;:&quot;ServiceAccount&quot;,&quot;metadata&quot;:{&quot;annotations&quot;:{},&quot;name&quot;:&quot;pod-labeler&quot;,&quot;namespace&quot;:&quot;dev4&quot;}}
    name: pod-labeler
    namespace: dev4
  secrets:
  - name: pod-labeler-token-6vgp7
kind: List
metadata:
</code></pre>
<p>My question is, is it the correct YAML file as it erases the empty tag and values for metadata...(AT the very much last of the YAML file)</p>
<p>I can create objects - serviceaccounts in this instance, but I just want to ensure if I am doing is correct or any other better approach.</p>

learner
<p>In this specific case this is correct but you need to be carefull with it since there is no consistent way to do this for all types of resources.</p>
<p>Historically kubectl had --export flag that was generating yamls ready to apply, but it got depricated because of many bugs. Check out <a href="https://github.com/kubernetes/kubernetes/pull/73787" rel="nofollow noreferrer">the issue on k8s github repo</a> for more details.</p>
<p>There is also another way to export the resources if you used <code>kubectl apply</code> to create it.</p>
<pre><code>kubectl apply view-last-applied &lt;api-resource&gt; &lt;name&gt; -oyaml
e.g.:
kubectl apply view-last-applied serviceaccount pod-labeler -oyaml
</code></pre>
<p>But remember that this won't work for resources created with helm or other tools.</p>
<p>The best thing you could do is to always keep all your source files in git or similar, so you don't ever need to export it.</p>
