How to remove creationTimestamp from dry-run output?

Hi,

When I run this command: kubectl create configmap testing --from-file=... -o yaml --dry-run, I get the following output:

kind: ConfigMap
metadata:
  creationTimestamp: null
  name: testing

Is there a way I can get rid of the line creationTimestamp: null without having to save the file and manipulate it? Or if I can pass a real timestamp to display in the output is also better than having null.

Thanks,
Visay

You can use kubectl patch to remove this field, with a command similar to (not tested):

kubectl create configmap testing --from-file=... -o yaml --dry-run | \
kubectl patch --type=json -p='[{"op": "remove", "path": "metadata.createTimestamp"}]'

Hi @feloy,

Thanks for the response. I tested but that doesn’t work.

Do you have other suggestions?

Thanks,
Visay

kubectl create namespace your-name -o yaml --dry-run > ./namespace.yaml
kubectl patch --local -f ./namespace.yaml --type=json -p='[{"op": "remove", "path": "/metadata/creationTimestamp"}]' -o yaml > ./namespace.yaml
1 Like

Hi @queil,

Following that, I got this error:

error: no objects passed to patch

Thanks,
Visay

I don’t know if this is crude or elegant :thinking: but can’t you just grep it out?

kubectl create configmap testing --from-file=... -o yaml --dry-run | grep -v creationTimestamp

But I don’t see the advantage of removing it, null is a valid JSON value.
There are other fields (in my output anyway) such as strategy, status and resources which are also empty placeholders.

Kind regards,
Stephen

Hi @stephendotcarter,

The reason I want to remove it is because I’m using Ansible module k8s and enable k8s validation parameter of that module would fail on null value of the creationTimestamp line.

Should I raise this issue with Ansible instead?

Thanks,
Visay