Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

assist with patch to remove a label #1354

Closed
lknite opened this issue Jul 29, 2023 · 1 comment
Closed

assist with patch to remove a label #1354

lknite opened this issue Jul 29, 2023 · 1 comment

Comments

@lknite
Copy link
Contributor

lknite commented Jul 29, 2023

I've been using a V1Patch.PatchType.MergePatch to add labels to a secret, but when I tried the same technique to remove a label it didn't work. After some research I think I need to use a JsonPatch. Just changing MergePatch to JsonPatch resulted in an error. Found an older posting with some examples, and used those to try and get things working, #772.

Here's the latest iteration, not sure why it isn't working...

Initially, after acquiring "V1Secret secret", I'd remove the label with:
secret.SetLabel(key, null);

Then I displayed the secret labels to verify it was in fact removed:

            Console.WriteLine("resulting label list:");
            foreach (var next in secret.Labels())
            {
                Console.WriteLine("- "+ next.Key +": "+ next.Value);
            }

Originally I was trying to do this, but it was erroring out:

            kubeclient.CoreV1.PatchNamespacedSecret(
                        new V1Patch(secret, V1Patch.PatchType.JsonPatch), secret.Name(), secret.Namespace());

My latest attempt is this:

            string patch = @"{""metadata"":{""labels"":";
            patch += JsonSerializer.Serialize(secret.Metadata.Labels);
            patch += "}}";
            Console.WriteLine("patch:");
            Console.WriteLine(patch);

            Globals.service.kubeclient.CoreV1.PatchNamespacedSecret(
                        new V1Patch(patch, V1Patch.PatchType.JsonPatch), secret.Name(), secret.Namespace());

This results in the following output & error:

patch:
{"metadata":{"labels":{"argocd.argoproj.io/secret-type":"cluster","addons-test":"true","addons-test_d":"true","cluster.x-k8s.io/cluster-name":"vc-test"}}}
Operation returned an invalid status code 'BadRequest', response body {"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"error decoding patch: json: cannot unmarshal object into Go value of type []handlers.jsonPatchOp","reason":"BadRequest","code":400}

I copy the patch string built up into 'json lint' and it shows as valid json. My tests are nearly identical to the article I found, so I think I've got the right technique?

I can see in the minikube testing on line 120 to 131 of seemingly exactly what I'm looking to do. I just tried to copy that technique and noticed that after doing a JsonSerializer.SerializeToDocument(secret) that the resulting variable doesn't have a .CreatePatch method.

Just found the article from Microsoft saying that System.Text.Json doesn't support Json Doc patching. Followed the section "Add support for JSON Patch when using System.Text.Json" tried the following, still no go:

        Globals.service.kubeclient.CoreV1.PatchNamespacedSecret(
                        new V1Patch(Newtonsoft.Json.JsonConvert.SerializeObject(patch), V1Patch.PatchType.JsonPatch), secret.Name(), secret.Namespace());
                        
Operation returned an invalid status code 'BadRequest', response body {"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"error decoding patch: json: cannot unmarshal string into Go value of type []handlers.jsonPatchOp","reason":"BadRequest","code":400}
 

Kubernetes v1.25.9
.NET 7

@lknite
Copy link
Contributor Author

lknite commented Jul 29, 2023

I just noticed your note on that older ticket referring to this library, investigating ...
https://www.nuget.org/packages/JsonPatch.Net/

@kubernetes-client kubernetes-client locked and limited conversation to collaborators Jul 29, 2023
@tg123 tg123 converted this issue into discussion #1355 Jul 29, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant