In #15537 we implemented a kubernetes detector based on an env var pattern. But 99.99% of the time KUBERNETES_SERVICE_HOST and KUBERNETES_SERVICE_PORT will be defined, and they can be looked up quickly without looping around the enumerable property source. The negative case, unfortunately, remains inefficient.

Comment From: dsyer

https://stackoverflow.com/a/54130803/1368236

I like this one:

if File.exists?('/var/run/secrets/kubernetes.io')
  puts "I'm also running in a Kubernetes pod"
end

That's cheap and efficient. Probably. I guess if /var/run/secrets exists and is network attached it might be slow.

Comment From: philwebb

Thomas Risberg also suggested /var/run/secrets/kubernetes.io/serviceaccount/namespace that even gives you the namespace

https://kubernetes.io/docs/tasks/access-application-cluster/access-cluster/#accessing-the-api-from-a-pod

Comment From: dsyer

The namespace is handy if you are going to use the k8s API, but for the purpose of this issue (the @Condition which for sure has no business making API calls) it’s not relevant, I would say. IMO we should check the specific env var, then the file, and then maybe (or maybe not - I really think it should be optional), check all the env vars for a pattern match. Possibly some measurements should be made to confirm, but in systems with large numbers of env vars, the fallback to looking at all of them for a pattern match seems like a bad idea.

Comment From: mbhave

Closing in favor of PR #19002.