Skip to content

Instantly share code, notes, and snippets.

@spyesx
Last active October 27, 2025 09:36
Show Gist options
  • Select an option

  • Save spyesx/b51b38c81c46d007a097c394adcee935 to your computer and use it in GitHub Desktop.

Select an option

Save spyesx/b51b38c81c46d007a097c394adcee935 to your computer and use it in GitHub Desktop.
yq command to find the full path by a key name (using the go implementation by mikefarah/yq)

yq command to find the full path by a key name – (using the go implementation by mikefarah/yq)

TL;DR

yq '.. | select(tag != "!!map" and tag != "!!seq" and (parent | key) == "<keyname>") | path | join(".")' \
 <filename.yaml>

Example

apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: certificates.cert-manager.io
spec:
  names:
    kind: Certificate
  versions:
    - name: v1
      schema:
        openAPIV3Schema:
          type: object
          properties:
            spec:
              type: object
              properties:
                duration:
                  type: string
                  default: 2160h0m0s
                subject: # This is the target key we are searching for
                  type: object
                  properties:
                    organizations:
                      type: array
yq '.. | select(tag != "!!map" and tag != "!!seq" and (parent | key) == "subject") | path | join(".")' config.yaml
> spec.versions.0.schema.openAPIV3Schema.properties.spec.properties.subject.type

Explanation

  • ..: Recursively traverses all nodes in the document.

  • select(...): Filters the nodes that are passed down the pipeline.

  • tag != "!!map" and tag != "!!seq": Ensures the selected node is a scalar value, not a map or sequence.

  • (parent | key) == "subject": Checks if the key name of the selected node's parent is exactly "subject".

  • | path: Returns the full path to the matching node as a list (e.g., ["spec", "subject"]).

  • | join("."): Joins the path list into a single, dot-separated string.

Going further

kubectl get crd certificates.cert-manager.io -o yaml | yq '.. | select(tag != "!!map" and tag != "!!seq" and (parent | key) == "subject") | path | join(".")'
> spec.versions.0.schema.openAPIV3Schema.properties.spec.properties.subject.description
> spec.versions.0.schema.openAPIV3Schema.properties.spec.properties.subject.type

kubectl get crd certificates.cert-manager.io -o jsonpath='{.spec.versions[0].schema.openAPIV3Schema.properties.spec.properties.subject}'
> {"description":"Requested set of X509 certificate subject attributes.\nMore info: https://datatracker.ietf.org/doc/html/rfc5280#section-4.1.2.6\n\nThe common name attribute is specified separately in the `commonName` field.\nCannot be set if the `literalSubject` field is set.","properties":{"countries":{"description":"Countries to be used on the Certificate.","items":{"type":"string"},"type":"array","x-kubernetes-list-type":"atomic"},"localities":{"description":"Cities to be used on the Certificate.","items":{"type":"string"},"type":"array","x-kubernetes-list-type":"atomic"},"organizationalUnits":{"description":"Organizational Units to be used on the Certificate.","items":{"type":"string"},"type":"array","x-kubernetes-list-type":"atomic"},"organizations":{"description":"Organizations to be used on the Certificate.","items":{"type":"string"},"type":"array","x-kubernetes-list-type":"atomic"},"postalCodes":{"description":"Postal codes to be used on the Certificate.","items":{"type":"string"},"type":"array","x-kubernetes-list-type":"atomic"},"provinces":{"description":"State/Provinces to be used on the Certificate.","items":{"type":"string"},"type":"array","x-kubernetes-list-type":"atomic"},"serialNumber":{"description":"Serial number to be used on the Certificate.","type":"string"},"streetAddresses":{"description":"Street addresses to be used on the Certificate.","items":{"type":"string"},"type":"array","x-kubernetes-list-type":"atomic"}},"type":"object"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment