vmware-archive/runtimes

Query SQL Server database from dotnetcore function error

har07 opened this issue · 9 comments

har07 commented

I have kubeless v1.0.6 installed and I'm trying to do simple query to sql server database from kubeless function using dotnetcore2.1/dotnetcore2.2 runtime, but the pod always crash with the following logs:

Unhandled Exception: System.IO.FileLoadException: Could not load file or assembly 'System.Diagnostics.DiagnosticSource, Version=4.0.5.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
   at System.Runtime.Loader.AssemblyLoadContext.LoadFromPath(IntPtr ptrNativeAssemblyLoadContext, String ilPath, String niPath, ObjectHandleOnStack retAssembly)
   at System.Runtime.Loader.AssemblyLoadContext.LoadFromAssemblyPath(String assemblyPath)
   at System.Reflection.Assembly.LoadFrom(String assemblyFile)
   at Kubeless.Core.Invokers.CompiledFunctionInvoker.LoadDependentAssemblies(String referencesPath) in /app/Kubeless.Core/Invokers/CompiledFunctionInvoker.cs:line 91
   at Kubeless.WebAPI.Startup.ConfigureServices(IServiceCollection services) in /app/Kubeless.WebAPI/Startup.cs:line 31
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection services)
   at Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices()
   at Microsoft.AspNetCore.Hosting.Internal.WebHost.Initialize()
   at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
   at Kubeless.WebAPI.Program.Main(String[] args) in /app/Kubeless.WebAPI/Program.cs:line 18

I have added System.Data.SqlClient in the .csproj:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Kubeless.Functions" Version="0.1.1" />
    <PackageReference Include="System.Data.SqlClient" Version="4.8.1"/>
  </ItemGroup>
</Project>

And this is the function definition:

using System;
using Kubeless.Functions;
using System.Data.SqlClient;

public class module
{
    public string handler(Event k8Event, Context k8Context)
    {
        string value = "";
        string sql = "select SOME_COLUMN from SOME_TABLE where ID = 1";
        using (SqlConnection conn = new SqlConnection("some connection string"))
        {
            SqlCommand cmd = new SqlCommand(sql, conn);
            try
            {
                conn.Open();
                value = cmd.ExecuteScalar().ToString();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        return value;
    }
}
har07 commented

Hi @lorenzo-ange, sorry to mention you. I saw that the issue addressed in your pull request (#61) looks similar to mine. Do you think this issue is actually related and will your pull request possibly fix this error too?

Then if it is, do you (or anybody here) know how to get the latest runtime into my current kubeless, which is exactly the question in my other issue

Thanks for your time

Hi @har07 ,
do you have the same error even if you change the TargetFramework to netcoreapp3.1 ?

har07 commented

@lorenzo-ange thanks for responding. I tried changing the TargetFramework to netcoreapp3.1 and netcoreapp2.1. Both seems to produce similar result: pod crash at initialization (pod status was Init:Error, then changed to Init:CrashLoopBackOff), which is different from the problem I reported above. I think now the pod crashed before it began writing log

Selection_213

I'm still trying to understand more on the inner workings of kubeless..

har07 commented

Now that I successfully ran simple function using dotnetcore3.1 runtime, I came back to this issue and tried this again using dotnetcore3.1 runtime. Sadly, the output is still the same with my previous comment: the pod crashed on initialization. Probably the function failed to compile but I'm not sure where to find what was the compilation error message exactly.

This is the csproj in the latest attempt:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Kubeless.Functions" Version="0.1.1" />
    <PackageReference Include="System.Data.SqlClient" Version="4.8.1"/>
  </ItemGroup>
</Project>

And this the function code:

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

public class module
{
    public async Task<object> handler(Event k8Event, Context k8Context)
    {
        string value = "";
        string sql = "select SOME_COLUMN from SOME_TABLE where ID = 1";
        using (SqlConnection conn = new SqlConnection("some connection string"))
        {
            SqlCommand cmd = new SqlCommand(sql, conn);
            try
            {
                conn.Open();
                value = cmd.ExecuteScalar().ToString();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        return value;
    }
}

hi @har07 can you attach the error log?

Yesterday this PR was merged to fix a problem with native depndencies, can you please update your kubeless .NET runtime to the latest one?

you can find them here: https://github.com/kubeless/runtimes/blob/master/stable/dotnetcore/dotnetcore.jsonnet

compilation image : lorenzoangelini3/kubeless-compile-dotnetcore31@sha256:4e6adfe873f7c4cd5e0d582bb5122f94c6d6ea73baa03b486dd93b8406deb8ca

runtime image: lorenzoangelini3/kubeless-runtime-dotnetcore31@sha256:4a2d94bd6da4aee612a2009d863beb554c43e61199d8d3fc4bf2d28b5673940b

har07 commented

hi @har07 can you attach the error log?

I can not see any error log in the pod using kubectl logs: "container "xxx" in pod "xxx-yyy" is waiting to start: PodInitializing". I can't find any error message using kubectl describe pod either. Any idea where to find more detailed information about the error?

you can see init containers log in this way:

# show prepare step logs 
# in this init container kubeless downloads your function code from ETCD/bucket and does other setup
kubectl logs <POD_NAME> -c prepare
# show compile step logs 
# in this init container the .NET runtime compiles your function code
kubectl logs <POD_NAME> -c compile 
har07 commented

Thanks a lot @lorenzo-ange , that is a very helpful piece of information. There was a small copy-paste error in my actual function definition which I can easily fix with the help of detailed compilation error log.

Now that my function definition is correct, and using the latest dotnetcore3.1 runtime image that you suggest, I can get some data from my SQL server database!