Skip to content

Commit

Permalink
Use more consistent formatting and add comments
Browse files Browse the repository at this point in the history
Signed-off-by: kazk <[email protected]>
  • Loading branch information
kazk committed Oct 27, 2021
1 parent 582b9f1 commit 89a574f
Showing 1 changed file with 95 additions and 54 deletions.
149 changes: 95 additions & 54 deletions k8s-pb-codegen/openapi/transform.jq
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,26 @@ def gvk_string: [.group, .version, .kind] | map(select(. != "")) | join("/");

(
[
.definitions as $defs
| .definitions | to_entries[]
.definitions as $defs |
.definitions |
to_entries[] |
# Only process definitions with GVK array.
# Exclude List. .properties.metadata.$ref "#/definitions/io.k8s.apimachinery.pkg.apis.meta.v1.ListMeta"
| .value["x-kubernetes-group-version-kind"]? as $gvks
| select($gvks != null and ($gvks | length == 1) and (.value.properties?.metadata?["$ref"]? != "#/definitions/io.k8s.apimachinery.pkg.apis.meta.v1.ListMeta"))
| (.value.properties?.metadata?["$ref"] | fmap(strip_ref_prefix | to_rust)) as $metadata
| (.value.properties?.spec?["$ref"] | fmap(strip_ref_prefix | to_rust)) as $spec
| (.value.properties?.status?["$ref"] | fmap(strip_ref_prefix)) as $statusName
| ($statusName | fmap($defs[.].properties?.conditions?.items?["$ref"]) | fmap(strip_ref_prefix | to_rust)) as $condition
| {
.value["x-kubernetes-group-version-kind"]? as $gvks |
select(
$gvks != null and
($gvks | length == 1) and
(.value.properties?.metadata?["$ref"]? != "#/definitions/io.k8s.apimachinery.pkg.apis.meta.v1.ListMeta")
) |
(.value.properties?.metadata?["$ref"] | fmap(strip_ref_prefix | to_rust)) as $metadata |
(.value.properties?.spec?["$ref"] | fmap(strip_ref_prefix | to_rust)) as $spec |
(.value.properties?.status?["$ref"] | fmap(strip_ref_prefix)) as $statusName |
(
$statusName |
fmap($defs[.].properties?.conditions?.items?["$ref"]) |
fmap(strip_ref_prefix | to_rust)
) as $condition |
{
key: $gvks[0] | gvk_string,
value: {
proto: .key | sub("^io\\.k8s\\."; "") | gsub("-"; "_"),
Expand All @@ -28,33 +37,44 @@ def gvk_string: [.group, .version, .kind] | map(select(. != "")) | join("/");
condition: $condition,
},
}
]
| sort_by(.key)
| from_entries
) as $definitions
] |
sort_by(.key) |
from_entries
) as $definitions |

| [
.paths | to_entries[]
| .key as $path
| .value | to_entries[]
[
.paths |
to_entries[] |
.key as $path |
.value |
to_entries[] |
# Only process path infos with GVK (methods) and ignore deprecated.
| .value["x-kubernetes-group-version-kind"]? as $gvk
| select($gvk != null and (.value.description | test("deprecated: "; "i") | not))
# Use group and version from path to group by because subresource's GVK might be different. e.g., `autoscale/v1` in `apps/v1`.
| ($path | capture("^/(?:(?:api/(?<coreVersion>[^/]+))|(?:apis/(?<group>[^/]+)/(?<version>[^/]+)))/")) as $gv
| (if $gv.coreVersion != null then "\($gv.coreVersion)" else "\($gv.group)/\($gv.version)" end) as $apiGroupVersion
.value["x-kubernetes-group-version-kind"]? as $gvk |
select($gvk != null and (.value.description | test("deprecated: "; "i") | not)) |
# Use group and version from path to group by because subresource's GVK might be different.
# e.g., `autoscale/v1` in `apps/v1`.
(
$path |
capture("^/(?:(?:api/(?<coreVersion>[^/]+))|(?:apis/(?<group>[^/]+)/(?<version>[^/]+)))/")
) as $gv |
(
if $gv.coreVersion != null then
"\($gv.coreVersion)"
else
"\($gv.group)/\($gv.version)"
end
) as $apiGroupVersion |
# Fall back to method name.
| .key as $method
| (.value["x-kubernetes-action"] // $method) as $verb
| $definitions[$gvk | gvk_string] as $definition
| {
(.value["x-kubernetes-action"] // .key) as $verb |
$definitions[$gvk | gvk_string] as $definition |
{
# Plural name. Includes a subresource name like in `APIResourceList`.
name: (
$path
| sub("^/apis?/\($apiGroupVersion)/(?:namespaces/\\{namespace\\}/)?"; "")
| split("/")
| map(select(. | (startswith("{") | not)))
| join("/")
$path |
sub("^/apis?/\($apiGroupVersion)/(?:namespaces/\\{namespace\\}/)?"; "") |
split("/") |
map(select(. | (startswith("{") | not))) |
join("/")
),
namespaced: ($path | test("/namespaces/\\{namespace\\}/")),
kind: $gvk.kind,
Expand All @@ -71,15 +91,19 @@ def gvk_string: [.group, .version, .kind] | map(select(. != "")) | join("/");
condition: $definition.condition,
path: $path,
}
]
| group_by(.apiGroupVersion)
| map({
] |
# Group resources by `apiGroupVersion` like `APIResourceList`, then
# combine subresources within the group with the parent.
group_by(.apiGroupVersion) |
map({
apiGroupVersion: .[0].apiGroupVersion,
# Collect all `paths` and `scopedVerbs` for this resource/subresource.
resources: (
group_by(.name)
| map({
group_by(.name) |
map({
name: .[0].name,
# Some resources can be both namespaced and cluster scoped.
# `namespaced` is true if it can be namespaced.
namespaced: (map(.namespaced) | any),
subresource: .[0].subresource,
apiGroupVersion: .[0].apiGroupVersion,
Expand All @@ -93,27 +117,44 @@ def gvk_string: [.group, .version, .kind] | map(select(. != "")) | join("/");
status: .[0].status,
condition: .[0].condition,
scopedVerbs: (
group_by(.namespaced)
| map({
group_by(.namespaced) |
map({
key: (if .[0].namespaced then "namespaced" else "all" end),
value: (map(.verb) | unique)
})
| from_entries
}) |
from_entries
),
paths: (map(.path) | unique | sort_by(length)),
})
}) |
# Add subresource infos to parent and remove them
| ([.[] | select(.subresource) | {name, scopedVerbs, paths}]) as $subresources
| [
.[]
| select(.subresource | not)
| (.name + "/") as $parent
| ([$subresources | .[] | select(.name | startswith($parent)) | {name: (.name | sub($parent; "")), scopedVerbs, paths}]) as $subs
| . + {subresources: $subs}
| del(.subresource)
]
([.[] | select(.subresource) | {name, scopedVerbs, paths}]) as $subresources |
[
.[] |
select(.subresource | not) |
(.name + "/") as $parent |
. +
{
subresources: [
$subresources |
.[] |
select(.name | startswith($parent)) |
{
name: (.name | sub($parent; "")),
scopedVerbs,
paths
}
]
} |
del(.subresource)
] as $resources |
# basic sanity check
if ($resources | map(.subresources | length) | add) == ($subresources | length) then
$resources
else
error("some subresources were not associated with their parent")
end
)
})
| [.[].resources[] | {key: .proto, value: .}]
| sort_by(.key)
| from_entries
}) |
[.[].resources[] | {key: .proto, value: .}] |
sort_by(.key) |
from_entries

0 comments on commit 89a574f

Please sign in to comment.