lib/google/cloud/env.rb in google-cloud-env-1.0.1 vs lib/google/cloud/env.rb in google-cloud-env-1.0.2
- old
+ new
@@ -1,12 +1,12 @@
-# Copyright 2017 Google Inc. All rights reserved.
+# Copyright 2017 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
-# http://www.apache.org/licenses/LICENSE-2.0
+# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
@@ -23,11 +23,11 @@
# # Google Cloud hosting environment
#
# This library provides access to information about the application's
# hosting environment if it is running on Google Cloud Platform. You may
# use this library to determine which Google Cloud product is hosting your
- # application (e.g. app engine, container engine), information about the
+ # application (e.g. App Engine, Kubernetes Engine), information about the
# Google Cloud project hosting the application, information about the
# virtual machine instance, authentication information, and so forth.
#
# ## Usage
#
@@ -65,13 +65,14 @@
# instance of the information from `Google::Cloud.env`. This constructor
# is provided for internal testing and allows mocking of the data.
#
# @param [Hash] env Mock environment variables.
# @param [Faraday::Connection] connection Faraday connection to use.
+ # @param [Hash] metadata_cache Mock cache.
#
- def initialize env: nil, connection: nil
- @metadata_cache = {}
+ def initialize env: nil, connection: nil, metadata_cache: nil
+ @metadata_cache = metadata_cache || {}
@env = env || ::ENV
@connection = connection ||
::Faraday.new(url: METADATA_HOST,
request: { timeout: 0.1 })
end
@@ -84,17 +85,19 @@
def app_engine?
env["GAE_INSTANCE"] ? true : false
end
##
- # Determine whether the application is running on Google Container Engine.
+ # Determine whether the application is running on Google Kubernetes
+ # Engine (GKE).
#
# @return [Boolean]
#
- def container_engine?
- container_engine_cluster_name ? true : false
+ def kubernetes_engine?
+ kubernetes_engine_cluster_name ? true : false
end
+ alias container_engine? kubernetes_engine?
##
# Determine whether the application is running on Google Cloud Shell.
#
# @return [Boolean]
@@ -104,11 +107,11 @@
end
##
# Determine whether the application is running on Google Compute Engine.
#
- # Note that most other products (e.g. App Engine, Container Engine,
+ # Note that most other products (e.g. App Engine, Kubernetes Engine,
# Cloud Shell) themselves use Compute Engine under the hood, so this
# method will return true for all the above products. If you want to
# determine whether the application is running on a "raw" Compute Engine
# VM without using a higher level hosting product, use
# {Env#raw_compute_engine?}.
@@ -120,16 +123,16 @@
end
##
# Determine whether the application is running on "raw" Google Compute
# Engine without using a higher level hosting product such as App
- # Engine or Container Engine.
+ # Engine or Kubernetes Engine.
#
# @return [Boolean]
#
def raw_compute_engine?
- !app_engine? && !cloud_shell? && metadata? && !container_engine?
+ !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.
@@ -270,30 +273,40 @@
result = env["GAE_MEMORY_MB"]
result.nil? ? nil : result.to_i
end
##
- # Returns the name of the Container Engine cluster hosting the
+ # Returns the name of the Kubernetes Engine cluster hosting the
# application, or `nil` if the current code is not running in
- # Container Engine.
+ # Kubernetes Engine.
#
# @return [String,nil]
#
- def container_engine_cluster_name
+ def kubernetes_engine_cluster_name
instance_attribute "cluster-name"
end
+ alias container_engine_cluster_name kubernetes_engine_cluster_name
##
- # Returns the name of the Container Engine namespace hosting the
+ # Returns the name of the Kubernetes Engine namespace hosting the
# application, or `nil` if the current code is not running in
- # Container Engine.
+ # Kubernetes Engine.
#
# @return [String,nil]
#
- def container_engine_namespace_id
- env["GKE_NAMESPACE_ID"]
+ def kubernetes_engine_namespace_id
+ # 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")
+ rescue SystemCallError
+ nil
end
+ alias container_engine_namespace_id kubernetes_engine_namespace_id
##
# Determine whether the Google Compute Engine Metadata Service is running.
#
# @return [Boolean]
@@ -302,11 +315,12 @@
unless metadata_cache.include?(METADATA_ROOT_PATH)
begin
resp = connection.get METADATA_ROOT_PATH
metadata_cache[METADATA_ROOT_PATH] = \
resp.status == 200 && resp.headers["Metadata-Flavor"] == "Google"
- rescue ::Faraday::TimeoutError, ::Faraday::ConnectionFailed
+ rescue ::Faraday::TimeoutError, ::Faraday::ConnectionFailed,
+ Errno::EHOSTDOWN
metadata_cache[METADATA_ROOT_PATH] = false
end
end
metadata_cache[METADATA_ROOT_PATH]
end
@@ -327,10 +341,11 @@
begin
resp = connection.get path do |req|
req.headers = { "Metadata-Flavor" => "Google" }
end
metadata_cache[path] = resp.status == 200 ? resp.body.strip : nil
- rescue ::Faraday::TimeoutError, ::Faraday::ConnectionFailed
+ rescue ::Faraday::TimeoutError, ::Faraday::ConnectionFailed,
+ Errno::EHOSTDOWN
metadata_cache[path] = nil
end
end
metadata_cache[path]
end