Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch Secrets with new data, instead of appending. #201

Closed
cpressland opened this issue Nov 4, 2023 · 6 comments · Fixed by #202
Closed

Patch Secrets with new data, instead of appending. #201

cpressland opened this issue Nov 4, 2023 · 6 comments · Fixed by #202
Labels
enhancement New feature or request

Comments

@cpressland
Copy link

Which project are you requesting an enhancement for?

kr8s

What do you need?

Currently kr8s exposes a .patch() mechanism for Secret objects which allows us to add new keys:

>>> from kr8s.objects import Secret
>>> a = Secret("my-secret")
>>> a.refresh()
>>> a.raw["data"]
{"aaa": "ZXhhbXBsZQ=="}
>>> a.patch({"data": {"bbb": "bW9yZV9leGFtcGxl"}})
>>> a.raw["data"]
{"aaa": "ZXhhbXBsZQ==", "bbb": "bW9yZV9leGFtcGxl"}

I'd like the ability to set a replace=True arg to the .patch() call to have the secret data set to the contents of the patch call. Example:

>>> from kr8s.objects import Secret
>>> a = Secret("my-secret")
>>> a.refresh()
>>> a.raw["data"]
{"aaa": "ZXhhbXBsZQ=="}
>>> a.patch({"data": {"bbb": "bW9yZV9leGFtcGxl"}}, replace=True)
>>> a.raw["data"]
{"bbb": "bW9yZV9leGFtcGxl"}

Alternatively, supporting JSON 6902 style patching would also be a valid solution here, similar to how kubectl handles this:

$ kubectl patch secret my-secret \
    --type='json' \
    -p='[{"op": "replace", "path": "/data", "value":{"bbb": "bW9yZV9leGFtcGxl"}}]'

This could be implemented as a new function:

>>> a.patch6902([{"op": "replace", "path": "/data", "value":{"bbb": "bW9yZV9leGFtcGxl"}}])
>>> a.raw["data"]
{"bbb": "bW9yZV9leGFtcGxl"}
@cpressland cpressland added the enhancement New feature or request label Nov 4, 2023
@jacobtomlinson
Copy link
Member

Thanks for raising this @cpressland. Given our goal of being similar to kubectl I think supporting the JSON 6902 style would make a lot of sense.

Perhaps with an API like this would feel natural to kubectl users?

a.patch([{"op": "replace", "path": "/data", "value":{"bbb": "bW9yZV9leGFtcGxl"}}], type="json")

In #75 I've also been exploring implementing apply which along with a setter on Secret.data could provide an API like this.

a.data = {"bbb": "bW9yZV9leGFtcGxl"}
a.apply()

@cpressland
Copy link
Author

Both options look completely valid, I'd go further and suggest both approaches should be implemented, option 1 because it's what a kubectl user would expect to "just work", and option 2 because it looks more pythonic and provides a shorthand solution for small cases like this.

@jacobtomlinson
Copy link
Member

Yup totally agree. The apply option is already in hand as part of that larger PR. For the JSON 6902 implementation, I expect this is just the case of adding an extra header when we make the API call. Do you have any interest in contributing this @cpressland? Otherwise I'll take a look when I can.

@cpressland
Copy link
Author

More than happy to try @jacobtomlinson but my async python isn't great! I'll see if I have time to look at this in the next week or so. Thanks!

@jacobtomlinson
Copy link
Member

Thanks for the offer, it actually turned out to be a small change so I threw #202 together.

@jacobtomlinson
Copy link
Member

JSON 6902 style patching is now available in v0.10.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants