Skip to content

Commit b452340

Browse files
committed
Load transformed JSON in codegen
Signed-off-by: kazk <[email protected]>
1 parent a159c2b commit b452340

File tree

3 files changed

+68
-3
lines changed

3 files changed

+68
-3
lines changed

justfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ swagger-transform:
5656
#!/usr/bin/env bash
5757
set -exuo pipefail
5858
cd k8s-pb-codegen/openapi
59-
jq -f list-resources.jq < swagger.json > api-resources.json
59+
jq -f transform.jq < swagger.json > transformed.json
6060

6161
# Download and generate all swagger dependent files
6262
swagger: swagger-dl swagger-patch swagger-transform

k8s-pb-codegen/src/lib.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#[derive(Clone, Debug, serde::Deserialize)]
2+
#[serde(rename_all = "camelCase")]
3+
pub struct Resource {
4+
/// Name of the resource used in URL.
5+
pub name: String,
6+
/// True if the resource is namespaced. May still have `all` verbs like `list`.
7+
pub namespaced: bool,
8+
pub api_group_version: String,
9+
pub group: String,
10+
pub version: String,
11+
pub kind: String,
12+
/// Protobuf type path.
13+
pub proto: String,
14+
/// Rust type path.
15+
pub rust: String,
16+
/// Metadata type.
17+
pub metadata: Option<String>,
18+
/// Spec type if any.
19+
pub spec: Option<String>,
20+
/// Status type if any.
21+
pub status: Option<String>,
22+
/// Condition type if the resource has `.status.conditions`.
23+
pub condition: Option<String>,
24+
/// Verbs grouped by scope. `all` or `namespaced`.
25+
pub scoped_verbs: ScopedVerbs,
26+
/// All paths associated with this resource.
27+
pub paths: Vec<String>,
28+
// TODO `Option<Vec<Subresource>>`
29+
/// Any subresources.
30+
pub subresources: Vec<Subresource>,
31+
}
32+
33+
#[derive(Clone, Debug, serde::Deserialize)]
34+
#[serde(rename_all = "camelCase")]
35+
pub struct ScopedVerbs {
36+
pub all: Option<Vec<String>>,
37+
/// Namespaced actions.
38+
pub namespaced: Option<Vec<String>>,
39+
}
40+
41+
#[derive(Clone, Debug, serde::Deserialize)]
42+
#[serde(rename_all = "camelCase")]
43+
pub struct Subresource {
44+
/// Name of the subresouce used in URL.
45+
pub name: String,
46+
/// Verbs grouped by scope. `all` or `namespaced`.
47+
pub scoped_verbs: ScopedVerbs,
48+
/// All paths associated with this subresource.
49+
pub paths: Vec<String>,
50+
}

k8s-pb-codegen/src/main.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ use prost_types::FileDescriptorSet;
33
#[macro_use]
44
extern crate log;
55
use anyhow::{Context, Result};
6-
use std::{fs, path::Path};
6+
use std::{collections::HashMap, fs, path::Path};
7+
8+
use k8s_pb_codegen::Resource;
79

810
fn read_file<P: AsRef<Path>>(path: P) -> Result<String> {
911
fs::read_to_string(&path).with_context(|| format!("read {}", path.as_ref().display()))
@@ -26,6 +28,11 @@ fn main() -> Result<()> {
2628
.out_dir("./out")
2729
.compile_protos(protos.as_slice(), &["protos/"])?;
2830

31+
info!("loading json");
32+
let apif: String = read_file(root.join("openapi/transformed.json"))?;
33+
let resources: HashMap<String, Resource> =
34+
serde_json::from_str(&apif).with_context(|| "parse transformed.json".to_string())?;
35+
2936
let buf =
3037
std::fs::read(root.join("protos.fds")).with_context(|| "read protos.fds".to_string())?;
3138
let fds = FileDescriptorSet::decode(&*buf).unwrap(); // pulls in proto::Message
@@ -34,7 +41,15 @@ fn main() -> Result<()> {
3441
// FDS usage: https://github.com/tokio-rs/prost/blob/32bc87cd0b7301f6af1a338e9afd7717d0f42ca9/prost-build/src/lib.rs#L765-L825
3542
for f in fds.file {
3643
if let Some(pkg) = f.package {
37-
info!("generating {}", pkg);
44+
for m in f.message_type {
45+
if let Some(name) = m.name {
46+
let path = format!("{}.{}", pkg, name);
47+
if let Some(resource) = resources.get(&path) {
48+
info!("generating resource {}", path);
49+
debug!("{:?}", resource);
50+
}
51+
}
52+
}
3853
}
3954
}
4055

0 commit comments

Comments
 (0)