Skip to content

Commit a159c2b

Browse files
committed
Use more consistent formatting and add comments
Signed-off-by: kazk <[email protected]>
1 parent 3c2c713 commit a159c2b

File tree

1 file changed

+95
-54
lines changed

1 file changed

+95
-54
lines changed

k8s-pb-codegen/openapi/transform.jq

Lines changed: 95 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,26 @@ def gvk_string: [.group, .version, .kind] | map(select(. != "")) | join("/");
77

88
(
99
[
10-
.definitions as $defs
11-
| .definitions | to_entries[]
10+
.definitions as $defs |
11+
.definitions |
12+
to_entries[] |
1213
# Only process definitions with GVK array.
1314
# Exclude List. .properties.metadata.$ref "#/definitions/io.k8s.apimachinery.pkg.apis.meta.v1.ListMeta"
14-
| .value["x-kubernetes-group-version-kind"]? as $gvks
15-
| select($gvks != null and ($gvks | length == 1) and (.value.properties?.metadata?["$ref"]? != "#/definitions/io.k8s.apimachinery.pkg.apis.meta.v1.ListMeta"))
16-
| (.value.properties?.metadata?["$ref"] | fmap(strip_ref_prefix | to_rust)) as $metadata
17-
| (.value.properties?.spec?["$ref"] | fmap(strip_ref_prefix | to_rust)) as $spec
18-
| (.value.properties?.status?["$ref"] | fmap(strip_ref_prefix)) as $statusName
19-
| ($statusName | fmap($defs[.].properties?.conditions?.items?["$ref"]) | fmap(strip_ref_prefix | to_rust)) as $condition
20-
| {
15+
.value["x-kubernetes-group-version-kind"]? as $gvks |
16+
select(
17+
$gvks != null and
18+
($gvks | length == 1) and
19+
(.value.properties?.metadata?["$ref"]? != "#/definitions/io.k8s.apimachinery.pkg.apis.meta.v1.ListMeta")
20+
) |
21+
(.value.properties?.metadata?["$ref"] | fmap(strip_ref_prefix | to_rust)) as $metadata |
22+
(.value.properties?.spec?["$ref"] | fmap(strip_ref_prefix | to_rust)) as $spec |
23+
(.value.properties?.status?["$ref"] | fmap(strip_ref_prefix)) as $statusName |
24+
(
25+
$statusName |
26+
fmap($defs[.].properties?.conditions?.items?["$ref"]) |
27+
fmap(strip_ref_prefix | to_rust)
28+
) as $condition |
29+
{
2130
key: $gvks[0] | gvk_string,
2231
value: {
2332
proto: .key | sub("^io\\.k8s\\."; "") | gsub("-"; "_"),
@@ -28,33 +37,44 @@ def gvk_string: [.group, .version, .kind] | map(select(. != "")) | join("/");
2837
condition: $condition,
2938
},
3039
}
31-
]
32-
| sort_by(.key)
33-
| from_entries
34-
) as $definitions
40+
] |
41+
sort_by(.key) |
42+
from_entries
43+
) as $definitions |
3544

36-
| [
37-
.paths | to_entries[]
38-
| .key as $path
39-
| .value | to_entries[]
45+
[
46+
.paths |
47+
to_entries[] |
48+
.key as $path |
49+
.value |
50+
to_entries[] |
4051
# Only process path infos with GVK (methods) and ignore deprecated.
41-
| .value["x-kubernetes-group-version-kind"]? as $gvk
42-
| select($gvk != null and (.value.description | test("deprecated: "; "i") | not))
43-
# Use group and version from path to group by because subresource's GVK might be different. e.g., `autoscale/v1` in `apps/v1`.
44-
| ($path | capture("^/(?:(?:api/(?<coreVersion>[^/]+))|(?:apis/(?<group>[^/]+)/(?<version>[^/]+)))/")) as $gv
45-
| (if $gv.coreVersion != null then "\($gv.coreVersion)" else "\($gv.group)/\($gv.version)" end) as $apiGroupVersion
52+
.value["x-kubernetes-group-version-kind"]? as $gvk |
53+
select($gvk != null and (.value.description | test("deprecated: "; "i") | not)) |
54+
# Use group and version from path to group by because subresource's GVK might be different.
55+
# e.g., `autoscale/v1` in `apps/v1`.
56+
(
57+
$path |
58+
capture("^/(?:(?:api/(?<coreVersion>[^/]+))|(?:apis/(?<group>[^/]+)/(?<version>[^/]+)))/")
59+
) as $gv |
60+
(
61+
if $gv.coreVersion != null then
62+
"\($gv.coreVersion)"
63+
else
64+
"\($gv.group)/\($gv.version)"
65+
end
66+
) as $apiGroupVersion |
4667
# Fall back to method name.
47-
| .key as $method
48-
| (.value["x-kubernetes-action"] // $method) as $verb
49-
| $definitions[$gvk | gvk_string] as $definition
50-
| {
68+
(.value["x-kubernetes-action"] // .key) as $verb |
69+
$definitions[$gvk | gvk_string] as $definition |
70+
{
5171
# Plural name. Includes a subresource name like in `APIResourceList`.
5272
name: (
53-
$path
54-
| sub("^/apis?/\($apiGroupVersion)/(?:namespaces/\\{namespace\\}/)?"; "")
55-
| split("/")
56-
| map(select(. | (startswith("{") | not)))
57-
| join("/")
73+
$path |
74+
sub("^/apis?/\($apiGroupVersion)/(?:namespaces/\\{namespace\\}/)?"; "") |
75+
split("/") |
76+
map(select(. | (startswith("{") | not))) |
77+
join("/")
5878
),
5979
namespaced: ($path | test("/namespaces/\\{namespace\\}/")),
6080
kind: $gvk.kind,
@@ -71,15 +91,19 @@ def gvk_string: [.group, .version, .kind] | map(select(. != "")) | join("/");
7191
condition: $definition.condition,
7292
path: $path,
7393
}
74-
]
75-
| group_by(.apiGroupVersion)
76-
| map({
94+
] |
95+
# Group resources by `apiGroupVersion` like `APIResourceList`, then
96+
# combine subresources within the group with the parent.
97+
group_by(.apiGroupVersion) |
98+
map({
7799
apiGroupVersion: .[0].apiGroupVersion,
100+
# Collect all `paths` and `scopedVerbs` for this resource/subresource.
78101
resources: (
79-
group_by(.name)
80-
| map({
102+
group_by(.name) |
103+
map({
81104
name: .[0].name,
82105
# Some resources can be both namespaced and cluster scoped.
106+
# `namespaced` is true if it can be namespaced.
83107
namespaced: (map(.namespaced) | any),
84108
subresource: .[0].subresource,
85109
apiGroupVersion: .[0].apiGroupVersion,
@@ -93,27 +117,44 @@ def gvk_string: [.group, .version, .kind] | map(select(. != "")) | join("/");
93117
status: .[0].status,
94118
condition: .[0].condition,
95119
scopedVerbs: (
96-
group_by(.namespaced)
97-
| map({
120+
group_by(.namespaced) |
121+
map({
98122
key: (if .[0].namespaced then "namespaced" else "all" end),
99123
value: (map(.verb) | unique)
100-
})
101-
| from_entries
124+
}) |
125+
from_entries
102126
),
103127
paths: (map(.path) | unique | sort_by(length)),
104-
})
128+
}) |
105129
# Add subresource infos to parent and remove them
106-
| ([.[] | select(.subresource) | {name, scopedVerbs, paths}]) as $subresources
107-
| [
108-
.[]
109-
| select(.subresource | not)
110-
| (.name + "/") as $parent
111-
| ([$subresources | .[] | select(.name | startswith($parent)) | {name: (.name | sub($parent; "")), scopedVerbs, paths}]) as $subs
112-
| . + {subresources: $subs}
113-
| del(.subresource)
114-
]
130+
([.[] | select(.subresource) | {name, scopedVerbs, paths}]) as $subresources |
131+
[
132+
.[] |
133+
select(.subresource | not) |
134+
(.name + "/") as $parent |
135+
. +
136+
{
137+
subresources: [
138+
$subresources |
139+
.[] |
140+
select(.name | startswith($parent)) |
141+
{
142+
name: (.name | sub($parent; "")),
143+
scopedVerbs,
144+
paths
145+
}
146+
]
147+
} |
148+
del(.subresource)
149+
] as $resources |
150+
# basic sanity check
151+
if ($resources | map(.subresources | length) | add) == ($subresources | length) then
152+
$resources
153+
else
154+
error("some subresources were not associated with their parent")
155+
end
115156
)
116-
})
117-
| [.[].resources[] | {key: .proto, value: .}]
118-
| sort_by(.key)
119-
| from_entries
157+
}) |
158+
[.[].resources[] | {key: .proto, value: .}] |
159+
sort_by(.key) |
160+
from_entries

0 commit comments

Comments
 (0)