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_namespaced_secret does not delete keys when passing them via string_data #2262

Open
mpw96 opened this issue Aug 12, 2024 · 4 comments
Open
Assignees
Labels
kind/support Categorizes issue or PR as a support question.

Comments

@mpw96
Copy link

mpw96 commented Aug 12, 2024

What happened (please include outputs or screenshots):
I want to remove a key from a secret. So I'm creating the patch data with a None value for this key. This key is not deleted (secret is unchanged).

What you expected to happen:
The key is deleted.

How to reproduce it (as minimally and precisely as possible):
Create a secret my-secret in some namespace mpw. It should contain a key test with some value.
Then run this program:

import kubernetes
import yaml

with open("my-kubeconfig.yaml", encoding="utf-8") as k_config_file:
    k_config = yaml.safe_load(k_config_file) 

k8s_api_client = kubernetes.config.new_client_from_config_dict(k_config)
core_v1_api = kubernetes.client.CoreV1Api(k8s_api_client)

secret_data = {
    "test": None,
}

core_v1_api.patch_namespaced_secret("my-secret", "mpw", kubernetes.client.V1Secret(string_data=secret_data))

Anything else we need to know?:
Note that I'm passing the secret_data to the parameter string_data. When I passing it to data, the key is deleted as expected.

Environment:

  • Kubernetes version (kubectl version): Server Version: v1.29.3
  • OS (e.g., MacOS 10.13.6): MacOS 14.6.1
  • Python version (python --version): Python 3.11.9
  • Python client version (pip list | grep kubernetes): kubernetes 30.1.0
@mpw96 mpw96 added the kind/bug Categorizes issue or PR as related to a bug. label Aug 12, 2024
@roycaihw
Copy link
Member

/assign

We should have some example on patch types (JSON, JSONMERGE, STRATEGIC). Let me find some examples

@roycaihw roycaihw added kind/support Categorizes issue or PR as a support question. and removed kind/bug Categorizes issue or PR as related to a bug. labels Aug 14, 2024
@mpw96
Copy link
Author

mpw96 commented Aug 15, 2024

@roycaihw thank you for responding. Please note that I think I understand how to remove a key from a secret by patching it.

In my code example above, let's look at the last line. This deletes the key test as expected:

core_v1_api.patch_namespaced_secret("my-secret", "mpw", kubernetes.client.V1Secret(data=secret_data))
                                                                                   ^^^^

Where this does not delete it, but I would expect it to be deleted:

core_v1_api.patch_namespaced_secret("my-secret", "mpw", kubernetes.client.V1Secret(string_data=secret_data))
                                                                                   ^^^^^^^^^^^

@mpw96
Copy link
Author

mpw96 commented Oct 21, 2024

@roycaihw Any updates?

@roycaihw
Copy link
Member

Sorry for the delay and thanks for the clarification! @mpw96 #2262 (comment)

I suspect this may be a server-side issue. As I've seen multiple upstream issue discussing the different behavior between data and string_data:

The behavior in kubernetes/kubernetes#123843 seems to be similar to your use case, and it was suggested that server-side apply may help in this case. It's supported in the dynamic client:

def server_side_apply(self, resource, body=None, name=None, namespace=None, force_conflicts=None, **kwargs):

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/support Categorizes issue or PR as a support question.
Projects
None yet
Development

No branches or pull requests

2 participants