Skip to content

Commit

Permalink
Fixed Kubernetes Engine names in some methods, and implemented an alt…
Browse files Browse the repository at this point in the history
…ernate mechanism to get the kubernetes namespace (googleapis#2071)
  • Loading branch information
dazuma authored May 4, 2018
1 parent 5e2daa9 commit 3d0dcee
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 30 deletions.
40 changes: 26 additions & 14 deletions google-cloud-env/lib/google/cloud/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module Cloud
# 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.
#
Expand Down Expand Up @@ -87,13 +87,15 @@ def app_engine?
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.
Expand All @@ -107,7 +109,7 @@ def cloud_shell?
##
# 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
Expand All @@ -123,12 +125,12 @@ def compute_engine?
##
# 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

##
Expand Down Expand Up @@ -273,26 +275,36 @@ def app_engine_memory_mb
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.
Expand Down
32 changes: 16 additions & 16 deletions google-cloud-env/test/google/cloud/env_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def gce_stubs
env = ::Google::Cloud::Env.new env: gae_env, connection: gce_conn

env.app_engine?.must_equal true
env.container_engine?.must_equal false
env.kubernetes_engine?.must_equal false
env.cloud_shell?.must_equal false
env.compute_engine?.must_equal true
env.raw_compute_engine?.must_equal false
Expand All @@ -128,15 +128,15 @@ def gce_stubs
env.app_engine_service_version.must_equal gae_version
env.app_engine_memory_mb.must_equal gae_memory_mb

env.container_engine_cluster_name.must_be_nil
env.container_engine_namespace_id.must_be_nil
env.kubernetes_engine_cluster_name.must_be_nil
env.kubernetes_engine_namespace_id.must_be_nil
end

it "returns correct values when running on container engine" do
it "returns correct values when running on kubernetes engine" do
env = ::Google::Cloud::Env.new env: gke_env, connection: gke_conn

env.app_engine?.must_equal false
env.container_engine?.must_equal true
env.kubernetes_engine?.must_equal true
env.cloud_shell?.must_equal false
env.compute_engine?.must_equal true
env.raw_compute_engine?.must_equal false
Expand All @@ -152,15 +152,15 @@ def gce_stubs
env.app_engine_service_version.must_be_nil
env.app_engine_memory_mb.must_be_nil

env.container_engine_cluster_name.must_equal gke_cluster
env.container_engine_namespace_id.must_equal gke_namespace
env.kubernetes_engine_cluster_name.must_equal gke_cluster
env.kubernetes_engine_namespace_id.must_equal gke_namespace
end

it "returns correct values when running on cloud shell" do
env = ::Google::Cloud::Env.new env: cloud_shell_env, connection: gce_conn

env.app_engine?.must_equal false
env.container_engine?.must_equal false
env.kubernetes_engine?.must_equal false
env.cloud_shell?.must_equal true
env.compute_engine?.must_equal true
env.raw_compute_engine?.must_equal false
Expand All @@ -176,15 +176,15 @@ def gce_stubs
env.app_engine_service_version.must_be_nil
env.app_engine_memory_mb.must_be_nil

env.container_engine_cluster_name.must_be_nil
env.container_engine_namespace_id.must_be_nil
env.kubernetes_engine_cluster_name.must_be_nil
env.kubernetes_engine_namespace_id.must_be_nil
end

it "returns correct values when running on compute engine" do
env = ::Google::Cloud::Env.new env: gce_env, connection: gce_conn

env.app_engine?.must_equal false
env.container_engine?.must_equal false
env.kubernetes_engine?.must_equal false
env.cloud_shell?.must_equal false
env.compute_engine?.must_equal true
env.raw_compute_engine?.must_equal true
Expand All @@ -200,15 +200,15 @@ def gce_stubs
env.app_engine_service_version.must_be_nil
env.app_engine_memory_mb.must_be_nil

env.container_engine_cluster_name.must_be_nil
env.container_engine_namespace_id.must_be_nil
env.kubernetes_engine_cluster_name.must_be_nil
env.kubernetes_engine_namespace_id.must_be_nil
end

it "returns correct values when not running on gcp" do
env = ::Google::Cloud::Env.new env: ext_env, connection: ext_conn

env.app_engine?.must_equal false
env.container_engine?.must_equal false
env.kubernetes_engine?.must_equal false
env.cloud_shell?.must_equal false
env.compute_engine?.must_equal false
env.raw_compute_engine?.must_equal false
Expand All @@ -224,8 +224,8 @@ def gce_stubs
env.app_engine_service_version.must_be_nil
env.app_engine_memory_mb.must_be_nil

env.container_engine_cluster_name.must_be_nil
env.container_engine_namespace_id.must_be_nil
env.kubernetes_engine_cluster_name.must_be_nil
env.kubernetes_engine_namespace_id.must_be_nil
end

end

0 comments on commit 3d0dcee

Please sign in to comment.