Skip to content

assist with patch to remove a label #1354

Closed
@lknite

Description

@lknite

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions