Reputation: 2840
I have a YAML file as like below which I have exported from an existing cluster:
apiVersion: v1
items:
- apiVersion: v1
kind: ServiceAccount
metadata:
creationTimestamp: 2019-03-20T23:17:42Z
name: default
namespace: dev4
resourceVersion: "80999"
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: |
{"apiVersion":"v1","kind":"ServiceAccount","metadata":{"annotations":{},"name":"pod-labeler","namespace":"dev4"}}
creationTimestamp: 2020-04-21T05:46:25Z
name: pod-labeler
namespace: dev4
resourceVersion: "113455688"
selfLink: /api/v1/namespaces/dev4/serviceaccounts/pod-labeler
uid: 702dadda-8393-11ea-abd9-0a768ca51346
secrets:
- name: pod-labeler-token-6vgp7
kind: List
metadata:
resourceVersion: ""
selfLink: ""
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.
In summary, I have to get rid of the below attributes:
uid:
selfLink:
resourceVersion:
creationTimestamp:
So I got a sed command like below which does the trick
sed -i '/uid: \|selfLink: \|resourceVersion: \|creationTimestamp: /d' dev4-serviceaccounts.yaml
The final YAML file is like below:
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: |
{"apiVersion":"v1","kind":"ServiceAccount","metadata":{"annotations":{},"name":"pod-labeler","namespace":"dev4"}}
name: pod-labeler
namespace: dev4
secrets:
- name: pod-labeler-token-6vgp7
kind: List
metadata:
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)
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.
Upvotes: 1
Views: 369
Reputation: 8142
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.
Historically kubectl had --export flag that was generating yamls ready to apply, but it got depricated because of many bugs. Check out the issue on k8s github repo for more details.
There is also another way to export the resources if you used kubectl apply
to create it.
kubectl apply view-last-applied <api-resource> <name> -oyaml
e.g.:
kubectl apply view-last-applied serviceaccount pod-labeler -oyaml
But remember that this won't work for resources created with helm or other tools.
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.
Upvotes: 1