vmware-archive/runtimes

Restriction of .NET functions to a single namespace prevents usage of some C# features

GGG-KILLER opened this issue · 5 comments

The restriction that a function's assembly can only have a single namespace prevents the usage of some C# features that embeds attributes in the resulting assembly:

  • readonly structs
    (I will update this list as more features that embed code into the resulting assembly are found.)

My proposed solution is to ignore types that have attributes named EmbeddedAttribute and/or CompilerGeneratedAttribute in the namespace counting code:

https://github.com/kubeless/runtimes/blob/31b42a99b0827170ce36833a4dcce95e09bfdda3/stable/dotnetcore/src/Kubeless.Core/Invokers/CompiledFunctionInvoker.cs#L32-L37

Hi @GGG-KILLER,
The #58 probably solves that. Once this is merged, can you please check if that is the case?

Hi @allantargino, unfortunately I no longer work for the company that was using this, however I will however attempt to test this in a VM when I get some time.

Ok @allantargino, today I tried to setup the new 3.1 runtime on master but it proved to be nontrivial so I'm unable to test.

The following should be enough to test if that fixed it or not though:

using Kubeless.Functions;

public class module
{
    private readonly struct Box<T>
    {
        public T Boxed { get; }

        public Box(T boxed)
        {
            this.Boxed = boxed;
        }
    }

    public object handler(Event k8Event, Context k8Context)
    {
        return new Box<object>(k8Event.Data);
    }
}
har07 commented

@GGG-KILLER I'm not sure I understand the actual problem about single namespace restriction (I don't see multiple namespaces in the test function?). But since I have dotnetcore3.1 runtime from #58 in my kubeless deployment, I tested your function (with a slight modification required by the runtime) and it works.

Selection_219

This is the exact function definition that I used for testing:

using System;
using System.Threading.Tasks;
using Kubeless.Functions;

public class module
{
    private readonly struct Box<T>
    {
        public T Boxed { get; }

        public Box(T boxed)
        {
            this.Boxed = boxed;
        }
    }
    
    public async Task<object> handler(Event k8Event, Context k8Context)
    {
        return new Box<object>(k8Event.Data);
    }
}

Do you think this test is enough to say that the pull request solved this issue?

I'm not sure I understand the actual problem about single namespace restriction (I don't see multiple namespaces in the test function?)

Actually, when you compile that code, the readonly struct is marked with an IsReadonlyAttribute, which in turn, is generated during compilation and stored on the System.Runtime.CompilerServices namespace inside your assembly (and that IsReadonlyAttribute in turn is marked with the EmbeddedAttribute which is stored on the Microsoft.CodeAnalysis namespace):
image

But yes, since your test passed, it's enough to say the pull request has solved this issue.