Skip to content

Commit 9d83b1e

Browse files
committed
move scripts into something that tracks script dependencies
trying just here as well because we are already doing cool stuff with sd and gron. Signed-off-by: clux <[email protected]>
1 parent d121f44 commit 9d83b1e

File tree

2 files changed

+77
-47
lines changed

2 files changed

+77
-47
lines changed

README.md

+15-47
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,34 @@
11
Experimenting with Kubernetes protobufs.
22

3-
## Protobufs
3+
## Build Dependencies
44

5-
### Download
5+
- [gron](https://github.com/tomnomnom/gron)
6+
- [just](https://github.com/casey/just)
7+
- [sd](https://github.com/chmln/sd)
8+
- [jq](https://stedolan.github.io/jq/)
69

7-
Get protos by extracting them from Kubernetes releases:
10+
## Protobufs
11+
We get protos by extracting them from pinned Kubernetes releases:
812

913
- https://github.com/kubernetes/api/releases
1014
- https://github.com/kubernetes/apimachinery/releases
1115
- https://github.com/kubernetes/apiextensions-apiserver/releases
1216
- https://github.com/kubernetes/kube-aggregator/releases
1317
- https://github.com/kubernetes/metrics/releases
1418

15-
```bash
16-
# In `protos/`
17-
VERSION=1.22.0
18-
for x in api apimachinery apiextensions-apiserver kube-aggregator metrics; do
19-
mkdir ./$x -p;
20-
curl -sSL https://github.com/kubernetes/$x/archive/refs/tags/kubernetes-$VERSION.tar.gz | tar xzf - -C ./$x/ --strip-components=1;
21-
fd -e proto -x sh -c "mkdir -p k8s.io/'{//}'; mv '{}' k8s.io/'{}'" ';' . ./$x;
22-
rm -rf ./$x;
23-
done
24-
```
19+
We then do minor transforms on top of that to prepare for building.
20+
Results of this step is committed already. But to run, invoke `just protos`
2521

26-
### Patch
22+
## Openapi
23+
To complement the protos with generic information, we also download the swagger schema, patch it, and transform it as described below.
2724

28-
Removing `k8s.io.`:
25+
Results of this step is committed already. But to run, invoke `just swagger`.
2926

30-
```bash
31-
fd -e proto -x sd 'k8s\.io\.(.+);' '$1;' {}
32-
fd -e proto -x sd 'import "k8s\.io/(.+)";' 'import "$1";' {}
33-
mv protos/k8s.io/* protos/
34-
rmdir protos/k8s.io/
35-
```
3627

37-
### Generate
38-
Refresh input paths to generate, then build:
28+
## Building
29+
With all dependent `swagger` and `protos` files built, run:
3930

4031
```bash
41-
fd -e proto | sort > protos.list
4232
cargo build
4333
```
4434

@@ -68,7 +58,7 @@ See [`prost_build`](https://github.com/tokio-rs/prost/blob/32bc87cd0b7301f6af1a3
6858
[`FileDescriptorSet`]: https://github.com/tokio-rs/prost/blob/32bc87cd0b7301f6af1a338e9afd7717d0f42ca9/prost-types/src/protobuf.rs#L1-L7
6959

7060

71-
## OpenAPI
61+
## OpenAPI Strategy
7262

7363
We need to use `swagger.json` to fill in some information.
7464

@@ -91,28 +81,6 @@ We should be able to find the following:
9181
- May also have paths for all namespaces for some verbs (e.g., `list` all pods)
9282
- Subresource if path contains `/{name}/` (`/` after `{name}`)
9383

94-
### Download
95-
96-
In `openapi/`
97-
98-
```bash
99-
VERSION=1.22.0
100-
curl -sSL -o swagger.json \
101-
https://raw.githubusercontent.com/kubernetes/kubernetes/v$VERSION/api/openapi-spec/swagger.json
102-
```
103-
104-
### Bug Fix
105-
106-
Fix path operation annotated with a `x-kubernetes-group-version-kind` that references a type that doesn't exist in the schema. (See [`k8s-openapi`](https://github.com/Arnavion/k8s-openapi/blob/445e89ec444ebb1c68e61361e64eec4c4a3f4785/k8s-openapi-codegen/src/fixups/upstream_bugs.rs#L9)).
107-
108-
```bash
109-
gron swagger.json \
110-
| perl -pe 's/(?<=kind = ")(Pod|Node|Service)(?:Attach|Exec|PortForward|Proxy)Options(?=")/$1/' \
111-
| gron -u \
112-
> swagger-patched.json
113-
mv swagger-patched.json swagger.json
114-
```
115-
11684
### Transforming
11785

11886
Transform `swagger.json` to something easier to explore.

justfile

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
VERSION := "1.22.0"
2+
3+
default:
4+
@just --list
5+
6+
# download proto schemas from upstream
7+
protos-sync:
8+
#!/usr/bin/env bash
9+
set -exuo pipefail
10+
rm -rf protos && mkdir protos && cd protos
11+
for x in api apimachinery apiextensions-apiserver kube-aggregator metrics; do
12+
mkdir ./$x -p
13+
curl -sSL https://github.com/kubernetes/$x/archive/refs/tags/kubernetes-{{VERSION}}.tar.gz | tar xzf - -C ./$x/ --strip-components=1
14+
fd -e proto -x sh -c "mkdir -p k8s.io/'{//}'; mv '{}' k8s.io/'{}'" ';' . ./$x
15+
rm -rf ./$x
16+
done
17+
18+
# fix import paths in downloaded schemas
19+
protos-patch:
20+
#!/usr/bin/env bash
21+
set -exuo pipefail
22+
fd -e proto -x sd 'k8s\.io\.(.+);' '$1;' {}
23+
fd -e proto -x sd 'import "k8s\.io/(.+)";' 'import "$1";' {}
24+
mv protos/k8s.io/* protos/
25+
rmdir protos/k8s.io/
26+
27+
# Generate path list for prost
28+
protos-list:
29+
fd -e proto | sort > protos.list
30+
31+
# Download and generate all protos dependent files
32+
protos: protos-sync protos-patch protos-list
33+
34+
# Download swagger
35+
swagger-sync:
36+
#!/usr/bin/env bash
37+
set -exuo pipefail
38+
curl -sSL -o openapi/swagger.json \
39+
https://raw.githubusercontent.com/kubernetes/kubernetes/v{{VERSION}}/api/openapi-spec/swagger.json
40+
41+
# Fix patch operation
42+
swagger-patch:
43+
#!/usr/bin/env bash
44+
set -exuo pipefail
45+
cd openapi
46+
# Fix path operation annotated with a `x-kubernetes-group-version-kind` that references a type that doesn't exist in the schema.
47+
# See https://github.com/Arnavion/k8s-openapi/blob/445e89ec444ebb1c68e61361e64eec4c4a3f4785/k8s-openapi-codegen/src/fixups/upstream_bugs.rs#L9
48+
gron swagger.json \
49+
| perl -pe 's/(?<=kind = ")(Pod|Node|Service)(?:Attach|Exec|PortForward|Proxy)Options(?=")/$1/' \
50+
| gron -u \
51+
> swagger-patched.json
52+
mv swagger-patched.json swagger.json
53+
54+
# Generate api-resources from patched swagger
55+
swagger-transform:
56+
#!/usr/bin/env bash
57+
set -exuo pipefail
58+
cd openapi
59+
jq -f list-resources.jq < swagger.json > api-resources.json
60+
61+
# Download and generate all swagger dependent files
62+
swagger: swagger-sync swagger-patch swagger-transform

0 commit comments

Comments
 (0)