Skip to content

Conversation

@joamaki
Copy link
Contributor

@joamaki joamaki commented Oct 6, 2025

This provides simple "jq" style facility for transforming a JSON/YAML document to make it easier to write assertions against more complex data.

While a Go implementation of jq does exist it is very large, so opting instead for this much simpler one.

This uses the sigs.k8s.io/yaml to load a YAML document as this way we handle them the same way as Kubernetes and can work on e.g. CRDs in the way one expects.

This provides simple "jq" style facility for transforming a JSON/YAML document
to make it easier to write assertions against more complex data.

While a Go implementation of jq does exist it is very large, so opting
instead for this much simpler one.

Signed-off-by: Jussi Maki <[email protected]>
@joamaki
Copy link
Contributor Author

joamaki commented Oct 6, 2025

Another option here could be to use k8s.io/client-go/util/jsonpath instead as that's already vendored into cilium/cilium. It would be less expressive than JMESPath, but might be enough for most use-cases.

Both still have the problem that they don't fully support comparisons. JMESPath can do some filtering to do some of it but would be nice to find a lightweight option that allows doing jq-style comparisons, e.g.

echo '{"foo": 1}' | jq '.foo >= 1'
true

We could consider embedding gojq and only exposing it via scripttest to avoid it ending up in production binaries, but I'm not sure if it's worth pulling such a big dependency in. Linking the test binaries in cilium/cilium is already incredibly slow due the massive generated typed k8s client code...

@christarazi
Copy link
Member

christarazi commented Oct 9, 2025

Did you consider whether we can use this in combination with db/show --format ...? The issue currently is that both the json and yaml commands only support loading one object, instead of multiple which is how db/show prints a table formatted in JSON or YAML. I'm not sure if this is a limitation on the JMESPath side or not.

@joamaki
Copy link
Contributor Author

joamaki commented Oct 21, 2025

Did you consider whether we can use this in combination with db/show --format ...? The issue currently is that both the json and yaml commands only support loading one object, instead of multiple which is how db/show prints a table formatted in JSON or YAML. I'm not sure if this is a limitation on the JMESPath side or not.

Ah good point. I'll think about this.

@joamaki
Copy link
Contributor Author

joamaki commented Oct 29, 2025

I'm now starting to lean towards just using gojq even though it's massive. If it's only a dependency of scripttest it shouldn't be too bad, or can even just have an extras package that has these sort of commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants