You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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/v1kind: CustomResourceDefinitionmetadata:
name: certificates.cert-manager.iospec:
names:
kind: Certificateversions:
- name: v1schema:
openAPIV3Schema:
type: objectproperties:
spec:
type: objectproperties:
duration:
type: stringdefault: 2160h0m0ssubject: # This is the target key we are searching fortype: objectproperties:
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"}