Skip to content

Add json.marshal and json.unmarshal cel functions#1033

Merged
k8s-ci-robot merged 2 commits intokubernetes-sigs:mainfrom
NicholasBlaskey:json_lib
Feb 26, 2026
Merged

Add json.marshal and json.unmarshal cel functions#1033
k8s-ci-robot merged 2 commits intokubernetes-sigs:mainfrom
NicholasBlaskey:json_lib

Conversation

@NicholasBlaskey
Copy link
Copy Markdown
Contributor

@NicholasBlaskey NicholasBlaskey commented Feb 10, 2026

Add a json cel library to allow easier integration from / to json in RGDs

initially has

  • json.marshal
  • json.unmarshal

Tested with example.

Also tested error case of

                    - name: JSON_UNMARSHAL_ERROR_EXAMPLE
                      value: "${json.unmarshal('{\"exampleJSONKey\": \"exampleJSONValue\"}').exampleJSONKey}"

which causes a reconciliation failure

    Last Transition Time:  2026-02-10T19:45:47Z
    Message:               resource reconciliation failed: node "deployment": eval "json.unmarshal('not valid json at all').key": json.unmarshal failed to parse JSON: invalid character 'o' in literal null (expecting 'u')
    Observed Generation:   1
    Reason:                NotReady
    Status:                False
    Type:                  Ready

@k8s-ci-robot k8s-ci-robot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Feb 10, 2026
@linux-foundation-easycla
Copy link
Copy Markdown

linux-foundation-easycla bot commented Feb 10, 2026

CLA Signed

The committers listed above are authorized under a signed CLA.

@k8s-ci-robot
Copy link
Copy Markdown
Contributor

Welcome @NicholasBlaskey!

It looks like this is your first PR to kubernetes-sigs/kro 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-sigs/kro has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Feb 10, 2026
@k8s-ci-robot
Copy link
Copy Markdown
Contributor

Hi @NicholasBlaskey. Thanks for your PR.

I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@k8s-ci-robot k8s-ci-robot added cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Feb 10, 2026
@NicholasBlaskey NicholasBlaskey marked this pull request as ready for review February 10, 2026 20:46
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Feb 10, 2026
@a-hilaly
Copy link
Copy Markdown
Member

/ok-to-test

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Feb 11, 2026
Copy link
Copy Markdown
Member

@a-hilaly a-hilaly left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you Nick - i left one nit comment below

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that the cel package deserves a better organization. Can we make this change in a seperate PR?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes please, this should be focused to the functions only.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep can do

I moved these functions into a new package to resolve a circular import error I was getting

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

created #1043

@a-hilaly
Copy link
Copy Markdown
Member

/retest

Copy link
Copy Markdown
Member

@a-hilaly a-hilaly left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NicholasBlaskey NicholasBlaskey force-pushed the json_lib branch 2 times, most recently from 2e486d5 to d4e98f5 Compare February 12, 2026 01:08
@NicholasBlaskey
Copy link
Copy Markdown
Contributor Author

Can you please update the CEL library docs? https://kro.run/docs/concepts/rgd/cel-expressions#available-cel-libraries

updated

Copy link
Copy Markdown
Member

@a-hilaly a-hilaly left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @NicholasBlaskey !
/approve

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Feb 12, 2026
@a-hilaly
Copy link
Copy Markdown
Member

@jakobmoellerdev @matthchr shall we pin this for 0.9.0? or are we good with a 0.8.6? personally i'm fine with either

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes please, this should be focused to the functions only.

Comment thread pkg/cel/library/json.go

func (l *jsonLibrary) CompileOptions() []cel.EnvOption {
return []cel.EnvOption{
cel.Function("json.unmarshal",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be versioned internally to v1alpha1

Copy link
Copy Markdown
Contributor Author

@NicholasBlaskey NicholasBlaskey Feb 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you clarify what you mean for versioned internally here?

I see some other libraries have integer versioning like this but not seeing any use a string version like v1alpha1. The random library also doesn't seem to have any versioning currently.
https://github.com/google/cel-go/blob/v0.27.0/ext/lists.go#L213

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mabye take a look at https://github.com/google/cel-go/blob/v0.27.0/ext/lists.go#L175-L189 or https://github.com/google/cel-go/blob/v0.27.0/ext/sets.go#L88-L97. Its basically about introducing a version so we can control it later. i dont really mind if its v1alpha1 or an integer tbh.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added support for setting a version

currently the version integer is unused but can be referenced for future updates to the library

Comment thread pkg/cel/library/json.go Outdated
}

func unmarshalJSON(jsonString ref.Val) ref.Val {
if jsonString.Type() != types.StringType {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you could use the native default type adapter here to get a string.

@jakobmoellerdev
Copy link
Copy Markdown
Member

I would favor a pin but won't block on 0.8.6

Copy link
Copy Markdown
Member

@jakobmoellerdev jakobmoellerdev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm
/hold

holding until 0.9.0 or if we have another approve ack this for next patch

@k8s-ci-robot k8s-ci-robot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Feb 13, 2026
@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Feb 13, 2026
@jakobmoellerdev
Copy link
Copy Markdown
Member

On another note: https://github.com/google/cel-go/pull/1261/changes introduced a JSON Value compatibility. Do you think this is relevant here?

@NicholasBlaskey
Copy link
Copy Markdown
Contributor Author

On another note: https://github.com/google/cel-go/pull/1261/changes introduced a JSON Value compatibility. Do you think this is relevant here?

doesn't seem relevant for unmarshal

For marshalJSON we could use value.ConvertToNative(types.JSONValueType) instead of our helper conversion.GoNativeType(value).

As far as I can tell they should be equivalent but don't have a strong opinion either way, open to changing it after #1047 merges

Copy link
Copy Markdown
Member

@a-hilaly a-hilaly left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Feb 24, 2026
@a-hilaly
Copy link
Copy Markdown
Member

@NicholasBlaskey can you please rebase this one?

@k8s-ci-robot k8s-ci-robot removed lgtm "Looks good to me", indicates that a PR is ready to be merged. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. labels Feb 25, 2026
@k8s-ci-robot
Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: a-hilaly, NicholasBlaskey

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@a-hilaly
Copy link
Copy Markdown
Member

cc @jakobmoellerdev

@a-hilaly
Copy link
Copy Markdown
Member

/unhold

@k8s-ci-robot k8s-ci-robot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Feb 25, 2026
Copy link
Copy Markdown
Member

@jakobmoellerdev jakobmoellerdev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Feb 26, 2026
@k8s-ci-robot k8s-ci-robot merged commit f8f5946 into kubernetes-sigs:main Feb 26, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants