pulumi-kubernetes icon indicating copy to clipboard operation
pulumi-kubernetes copied to clipboard

Kubernetes NamespacePatch - Removing label does not work anymore

Open Phil1972 opened this issue 11 months ago • 2 comments

What happened?

Hi, Using the latest kubernetes module as well as the latest azure-native. Code that used to work does not anymore and the documentation does not seem up to date (at least for the c# version) because it crashes with an exception (System.NullReferenceException: Object reference not set to an instance of an object.).

Example

This does not work anymore (see the marking). There is a warning/error that prevents to build. To remove the warning either put a pragma directive or put a 'null!' instead but it still fails with an exception:

image

any help into resolving this would be appreciated.

Output of pulumi about

image image

Additional context

No response

Contributing

Vote on this issue by adding a 👍 reaction. To contribute a fix for this issue, leave a comment (and link to your pull request, if you've opened one already).

Phil1972 avatar Mar 26 '24 18:03 Phil1972

@Phil1972, I apologize for the delayed response and the inconvenience caused by the issues you've encountered with our documentation. I can confirm that these pages require updating. To clarify, we have never supported setting null or undefined objects for labels and annotations. This was an oversight on our part during the documentation drafting process.

For your specific use case, you can safely remove the { "foo", null }, line from your code. According to the documentation's explanation of and then set it to null/undefined in a subsequent step, in Pulumi programs, omitting the declaration indicates to the engine that we want to drop/delete such fields.

An updated, fully functional example would thus be as follows:

using System.Collections.Generic;
using Pulumi;
using Kubernetes = Pulumi.Kubernetes;

return await Deployment.RunAsync(() =>
{
    var uuid = "ee0d1b94-e749-4de4-ae8a-e4319a7f3ef2"; // Arbitrary UUID

    var provider = new Kubernetes.Provider("provider", new()
    {
        EnableServerSideApply = true,
    });

    var patch1 = new Kubernetes.Core.V1.NamespacePatch("patch1", new()
    {
        Metadata = new Kubernetes.Types.Inputs.Meta.V1.ObjectMetaPatchArgs
        {
            Name = "foo",
            Annotations =
            {
                { "pulumi.com/patchForce", "true" },
                { "pulumi.com/patchFieldManager", uuid },
            },
            Labels =
            {
                { "foo", "" },
            },
        },
    }, new CustomResourceOptions
    {
        Provider = provider,
    });

    var patch2 = new Kubernetes.Core.V1.NamespacePatch("patch2", new()
    {
        Metadata = new Kubernetes.Types.Inputs.Meta.V1.ObjectMetaPatchArgs
        {
            Name = "foo",
            Annotations =
            {
                { "pulumi.com/patchForce", "true" },
                { "pulumi.com/patchFieldManager", uuid },
            },
            Labels =
            {
            // Not declaring the label here will cause it to be deleted after the manager has taken control of the field.
            },
        },
    }, new CustomResourceOptions
    {
        Provider = provider,
        DependsOn = new[]
        {
            patch1,
        },
    });
});

I will keep this issue open as a tracker item for us to revise our documentation. Thanks for reporting this!

rquitales avatar Apr 01 '24 17:04 rquitales

Thanks for your time on this. So there is no way to perform this in a single step I guess? Because I fear that pulumi might not like the fact that the label is already there at first. AKS puts that label automaticly on namespaces and we have to remove that label so that istio injection can work properly.

ie: is there a way to run that kubectl command directly using pulumi?

kubectl patch namespace MyNamespace --type='json' -p='[{"op": "remove", "path": "/metadata/labels/kubernetes.azure.com~1managedby"}]'

Phil1972 avatar Apr 02 '24 12:04 Phil1972