microsoft / mindaro

Bridge to Kubernetes - for Visual Studio and Visual Studio Code
MIT License
307 stars 106 forks source link

Detect when running in "Bridge to Kubernetes" #219

Closed Vaccano closed 3 years ago

Vaccano commented 3 years ago

I have some library code (a NuGet package) that I am building. That code that access files from the container. This means that all the users of my library will need to configure a volume for it to not throw an exception when they are debugging in Visual Studio via Bridge to Kuberentes.

To set this up write, my NuGet would need to add a file to their NuGet, or if it its already there, it would need to merge with the one that is there. Both are not really solutions I want. (One is confusing to the users of my NuGet, and the other is very complicated to pull off.)

Thinking it over, I would much rather just skip accessing the files when the container is running debugging via "Bridge to Kubernetes".

Is there a way that I can programmatically detect when the current execution context is running in "Bridge to Kubernetes"?

If this is not possible, then I can just swallow the file not found exception/check for the file first, but I would rather, not (because if the file is not there in a real running situation, then it is a problem).

rakeshvanga commented 3 years ago

@Vaccano We specifically don't add any additional metadata to know if the service is being debugged by Bridge To Kubernetes. You could try to skip checking for the file from the volume when the service is being debugged (in C# one can use Debbuger.IsAttached to identify if a project is being debugged) otherwise if the service is running on the cluster you would expect the file to be present.

Do you think this solution would work for you?

Vaccano commented 3 years ago

@rakeshvanga Yeah, that works.

I ended up doing something like this:

        if (Debugger.IsAttached)
        {
            var memoryUsageExists = System.IO.File.Exists(memoryUsageFile);
            var memoryLimitExists = System.IO.File.Exists(memoryUsageFile);
            var cpuPeriodExists = System.IO.File.Exists(memoryUsageFile);
            var cpuQuotaUsageExists = System.IO.File.Exists(memoryUsageFile);
            if (!(memoryUsageExists && memoryLimitExists && cpuPeriodExists && cpuQuotaUsageExists))
            {
                return null;
            }
        }

I did not know about the Debugger.IsAttached property.

Thanks for the help!