{{- /* Determine if Path for requested GVR is at /api or /apis based on emptiness of group */ -}} {{- $prefix := (ternary "/api" (join "" "/apis/" $.GVR.Group) (not $.GVR.Group)) -}} {{- /* Search both cluster-scoped and namespaced-scoped paths for the GVR to find its GVK */ -}} {{- /* Looks for path /apis/// or /apis////namespaces/{namespace}/ */ -}} {{- $clusterScopedSearchPath := join "/" $prefix $.GVR.Version $.GVR.Resource -}} {{- $namespaceScopedSearchPath := join "/" $prefix $.GVR.Version "namespaces" "\\{namespace\\}" $.GVR.Resource -}} {{- $path := or (index $.Document "paths" $clusterScopedSearchPath) (index $.Document "paths" $clusterScopedSearchPath) -}} {{- /* Pull GVK from operation */ -}} {{- with $gvk := and $path (index $path "get" "x-kubernetes-group-version-kind") -}} {{- if $gvk.group -}} GROUP: {{ $gvk.group }}{{"\n" -}} {{- end -}} KIND: {{ $gvk.kind}}{{"\n" -}} VERSION: {{ $gvk.version }}{{"\n" -}} {{- "\n" -}} {{- with include "schema" (dict "gvk" $gvk "Document" $.Document "FieldPath" $.FieldPath "Recursive" $.Recursive) -}} {{- . -}} {{- else -}} {{- throw "error: GVK %v not found in OpenAPI schema" $gvk -}} {{- end -}} {{- else -}} {{- throw "error: GVR (%v) not found in OpenAPI schema" $.GVR.String -}} {{- end -}} {{- "\n" -}} {{- /* Finds a schema with the given GVK and prints its explain output or empty string if GVK was not found Takes dictionary as argument with keys: gvk: openapiv3 JSON schema Document: entire doc FieldPath: field path to follow Recursive: print recursive */ -}} {{- define "schema" -}} {{- /* Find definition with this GVK by filtering out the components/schema with the given x-kubernetes-group-version-kind */ -}} {{- range index $.Document "components" "schemas" -}} {{- if contains (index . "x-kubernetes-group-version-kind") $.gvk -}} {{- with include "output" (set $ "schema" .) -}} {{- . -}} {{- else -}} {{- $fieldName := (index $.FieldPath (sub (len $.FieldPath) 1)) -}} {{- throw "error: field \"%v\" does not exist" $fieldName}} {{- end -}} {{- break -}} {{- end -}} {{- end -}} {{- end -}} {{- /* Follows FieldPath until the FieldPath is empty. Then prints field name and field list of resultant schema. If field path is not found. Prints nothing. Example output: FIELD: spec DESCRIPTION: