@@ -7,17 +7,26 @@ def gvk_string: [.group, .version, .kind] | map(select(. != "")) | join("/");
7
7
8
8
(
9
9
[
10
- .definitions as $defs
11
- | .definitions | to_entries []
10
+ .definitions as $defs |
11
+ .definitions |
12
+ to_entries [] |
12
13
# Only process definitions with GVK array.
13
14
# 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
+ {
21
30
key : $gvks [0 ] | gvk_string ,
22
31
value : {
23
32
proto : .key | sub ("^io\\ .k8s\\ ." ; "" ) | gsub ("-" ; "_" ),
@@ -28,33 +37,44 @@ def gvk_string: [.group, .version, .kind] | map(select(. != "")) | join("/");
28
37
condition : $condition ,
29
38
},
30
39
}
31
- ]
32
- | sort_by (.key )
33
- | from_entries
34
- ) as $definitions
40
+ ] |
41
+ sort_by (.key ) |
42
+ from_entries
43
+ ) as $definitions |
35
44
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 [] |
40
51
# 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 |
46
67
# 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
+ {
51
71
# Plural name. Includes a subresource name like in `APIResourceList`.
52
72
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 ("/" )
58
78
),
59
79
namespaced : ($path | test ("/namespaces/\\ {namespace\\ }/" )),
60
80
kind : $gvk .kind ,
@@ -71,15 +91,19 @@ def gvk_string: [.group, .version, .kind] | map(select(. != "")) | join("/");
71
91
condition : $definition .condition ,
72
92
path : $path ,
73
93
}
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 ({
77
99
apiGroupVersion : . [0 ].apiGroupVersion ,
100
+ # Collect all `paths` and `scopedVerbs` for this resource/subresource.
78
101
resources : (
79
- group_by (.name )
80
- | map ({
102
+ group_by (.name ) |
103
+ map ({
81
104
name : . [0 ].name ,
82
105
# Some resources can be both namespaced and cluster scoped.
106
+ # `namespaced` is true if it can be namespaced.
83
107
namespaced : (map (.namespaced ) | any ),
84
108
subresource : . [0 ].subresource ,
85
109
apiGroupVersion : . [0 ].apiGroupVersion ,
@@ -93,27 +117,44 @@ def gvk_string: [.group, .version, .kind] | map(select(. != "")) | join("/");
93
117
status : . [0 ].status ,
94
118
condition : . [0 ].condition ,
95
119
scopedVerbs : (
96
- group_by (.namespaced )
97
- | map ({
120
+ group_by (.namespaced ) |
121
+ map ({
98
122
key : (if . [0 ].namespaced then "namespaced" else "all" end ),
99
123
value : (map (.verb ) | unique )
100
- })
101
- | from_entries
124
+ }) |
125
+ from_entries
102
126
),
103
127
paths : (map (.path ) | unique | sort_by (length )),
104
- })
128
+ }) |
105
129
# 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
115
156
)
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