How do I list pods sorted by label version in Kubernetes?



  • I need to list some pods sorted by version and get the last index of it.

    I'm trying to do like:

    kubectl get pods 
    --namespace my-namespace
    --selector "app.kubernetes.io/name=my-cool-pod"
    --sort-by='.items[*].metadata.labels["app.kubernetes.io/version"]'
    --output jsonpath="{.items[-1:].metadata.name}
    

    But it is not working.

    Also, my pod labels are:

        labels:
          app.kubernetes.io/instance: my-instance
          app.kubernetes.io/managed-by: my-manager
          app.kubernetes.io/name: my-name
          app.kubernetes.io/version: x.y.z
    

    How to I list pods sorted my version?

    Thanks



  • I've figured it out

    Lables in K8S are for filtering, and not sorting.

    I've just added a version field outside labels and it worked.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2