8

I am trying to retrieve the kubernetes last-applied-configuration from a service yaml (under metadata annoations) using kubectl -o jsonpath, but the name of the field is "kubectl.kubernetes.io/last-applied-configuration". I believe the parser is getting confused due to the dots in the actual name field, since it uses dots for child object designation.

e.g. (Running on Windows)

kubectl -lapp=myapp get service -o jsonpath="{range .items[*]}{
.metadata.annotations}{\"\n\"}{end}"

shows the map of resulting values as expected

map[kubectl.kubernetes.io/last-applied-configuration:{"kind":"Service","apiVersion":"v1","metadata":{"name":"myapp","namespace":"mynamespace",
"creationTimestamp":null,"labels":{"app":"myapp","version":"1.0"}},"spec":{"ports":[{"name":"http","protocol":"TCP","port":80,"ta
rgetPort":8080}],"selector":{"app":"myapp","version":"1.0"},"type":"NodePort"},"status":{"loadBalancer":{}}}]

In this case, the kubectl.kubernetes.io/last-applied-configuration is the only thing in the annotations, but that is not always the case.

The problem comes about when I try to drill down to just the last-applied-configuration.

e.g.

kubectl -lapp=myapp get service -o jsonpath="{range .items[*]}{
.metadata.annotations.kubectl.kubernetes.io/last-applied-configuration}{\"\n\"}{end}"

Returns no information.

I have also tried

kubectl -lapp=myapp get service -o jsonpath="{range .items[*]}{
.metadata.annotations['kubectl.kubernetes.io/last-applied-configuration']}{\"\n\"}{end}"

and

kubectl -lapp=myapp get service -o jsonpath="{range .items[*]}{['metadata']['annotations']['kubectl.kubernetes.io/last-applied-configuration']}{\"\n\"}{end}"

to no avail.

I'm expecting the results to be simply the value of the kubectl.kubernetes.io/last-applied-configuration

user4611739
  • 93
  • 1
  • 1
  • 4

2 Answers2

11
kubectl apply view-last-applied service -lapp=myapp -o json
Daniel Bruzual
  • 111
  • 1
  • 2
4

you need to escape the . then it will list out value from the specific annotation name.

kubectl -lapp=myapp get service -o jsonpath="{range .items[*]}{.metadata.annotations.kubectl\.kubernetes\.io/last-applied-configuration}{\"\n\"}{end}"
meles
  • 315
  • 1
  • 4
  • 13
Suresh Vishnoi
  • 17,341
  • 8
  • 47
  • 55