lib/google/cloud/env.rb in google-cloud-env-1.2.1 vs lib/google/cloud/env.rb in google-cloud-env-1.3.0

- old
+ new

@@ -112,24 +112,53 @@ @retry_count = retry_count @retry_interval = retry_interval @retry_backoff_factor = retry_backoff_factor @retry_max_interval = retry_max_interval request_opts = { timeout: request_timeout, open_timeout: open_timeout } - @connection = connection || - ::Faraday.new(url: METADATA_HOST, request: request_opts) + @connection = connection || ::Faraday.new(url: METADATA_HOST, request: request_opts) end ## + # Determine whether the application is running on a Knative-based + # hosting platform, such as Cloud Run or Cloud Functions. + # + # @return [Boolean] + # + def knative? + env["K_SERVICE"] ? true : false + end + + ## # Determine whether the application is running on Google App Engine. # # @return [Boolean] # def app_engine? env["GAE_INSTANCE"] ? true : false end ## + # Determine whether the application is running on Google App Engine + # Flexible Environment. + # + # @return [Boolean] + # + def app_engine_flexible? + app_engine? && env["GAE_ENV"] != "standard" + end + + ## + # Determine whether the application is running on Google App Engine + # Standard Environment. + # + # @return [Boolean] + # + def app_engine_standard? + app_engine? && env["GAE_ENV"] == "standard" + end + + ## # Determine whether the application is running on Google Kubernetes # Engine (GKE). # # @return [Boolean] # @@ -169,22 +198,21 @@ # Engine or Kubernetes Engine. # # @return [Boolean] # def raw_compute_engine? - !app_engine? && !cloud_shell? && metadata? && !kubernetes_engine? + !knative? && !app_engine? && !cloud_shell? && metadata? && !kubernetes_engine? end ## # Returns the unique string ID of the project hosting the application, # or `nil` if the application is not running on Google Cloud. # # @return [String,nil] # def project_id - env["GCLOUD_PROJECT"] || env["DEVSHELL_PROJECT_ID"] || - lookup_metadata("project", "project-id") + env["GCLOUD_PROJECT"] || env["DEVSHELL_PROJECT_ID"] || lookup_metadata("project", "project-id") end ## # Returns the unique numeric ID of the project hosting the application, # or `nil` if the application is not running on Google Cloud. @@ -283,18 +311,40 @@ def instance_attribute key lookup_metadata "instance", "attributes/#{key}" end ## + # Returns the name of the running Knative service, or `nil` if the + # current code is not running on Knative. + # + # @return [String,nil] + # + def knative_service_id + env["K_SERVICE"] + end + alias knative_service_name knative_service_id + + ## + # Returns the revision of the running Knative service, or `nil` if the + # current code is not running on Knative. + # + # @return [String,nil] + # + def knative_service_revision + env["K_REVISION"] + end + + ## # Returns the name of the running App Engine service, or `nil` if the # current code is not running in App Engine. # # @return [String,nil] # def app_engine_service_id env["GAE_SERVICE"] end + alias app_engine_service_name app_engine_service_id ## # Returns the version of the running App Engine service, or `nil` if the # current code is not running in App Engine. # @@ -338,12 +388,11 @@ # The Kubernetes namespace is difficult to obtain without help from # the application using the Downward API. The environment variable # below is set in some older versions of GKE, and the file below is # present in Kubernetes as of version 1.9, but it is possible that # alternatives will need to be found in the future. - env["GKE_NAMESPACE_ID"] || - ::IO.read("/var/run/secrets/kubernetes.io/serviceaccount/namespace") + env["GKE_NAMESPACE_ID"] || ::IO.read("/var/run/secrets/kubernetes.io/serviceaccount/namespace") rescue SystemCallError nil end alias container_engine_namespace_id kubernetes_engine_namespace_id @@ -413,12 +462,10 @@ rescue *METADATA_FAILURE_EXCEPTIONS retries_remaining -= 1 if retries_remaining >= 0 sleep retry_interval retry_interval *= @retry_backoff_factor - if retry_interval > @retry_max_interval - retry_interval = @retry_max_interval - end + retry_interval = @retry_max_interval if retry_interval > @retry_max_interval retry end error_result end end