How to get the last applied timestamp of a secret in kube api
Asked Answered
D

1

6

I want to get the last time a secret was modified via the kube api. I cannot seem to find a way to access this information. I had a look at events but I cannot find any for Secrets.

An example would be I create a secret called my-secret, I then update this the next day but I want to know what time it was updated and not the creation time.

Any help would be great thanks.

Dominic answered 5/12, 2021 at 12:20 Comment(0)
H
9

The following command will give you the secret chronological history:

kubectl get secret <name> --namespace <namespace> --show-managed-fields -o jsonpath='{range .metadata.managedFields[*]}{.manager}{" did "}{.operation}{" at "}{.time}{"\n"}{end}'

Example, create a secret: kubectl create secret generic test --from-literal user=$(echo 'somebody' | base64)

Run the above command:

kubectl-create did Update at 2021-12-06T01:12:17Z

Retrieve the created secret kubectl get secret test -o yaml > test.yaml. Replace the value for "user" in the yaml with echo 'nobody' | base64 output and re-apply kubectl apply -f test.yaml.

Run the above command and it reports the last update action and timestamp:

kubectl-create did Update at 2021-12-06T01:12:17Z

kubectl-client-side-apply did Update at 2021-12-06T01:13:33Z

Now do a replace kubectl patch secret test --type='json' -p='[{"op" : "replace" ,"path" : "/data/user" ,"value" : "aGVsbG93b3JsZAo="}]'

Run the above command again:

kubectl-create did Update at 2021-12-06T01:12:17Z

kubectl-client-side-apply did Update at 2021-12-06T01:13:33Z

kubectl-patch did Update at 2021-12-06T01:21:57Z

The command correctly reports all the changes made to the secret.

Habitue answered 5/12, 2021 at 14:40 Comment(2)
Thanks, this does not work though when using an apply. When you create a secret then update it via an apply the data is incorrect. The example below shows the creation time E.g. kubectl-client-side-apply did Update at 2021-12-05T20:38:19ZDominic
See the updated answer with example. Can you repeat the same in your environment and paste the output in your question if you don't see the same.Coucal

© 2022 - 2024 — McMap. All rights reserved.