Skip to content

Commit

Permalink
Skip collecting logs and events on failure via env var (#239)
Browse files Browse the repository at this point in the history
* Disable collecting logs and events in debug_message separately
* Make error message formatting match other messages
  • Loading branch information
dturn authored Jan 18, 2018
1 parent ef200ee commit 1df2474
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
14 changes: 11 additions & 3 deletions lib/kubernetes-deploy/kubernetes_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ class KubernetesResource
TIMEOUT = 5.minutes
LOG_LINE_COUNT = 250

DISABLE_FETCHING_LOG_INFO = 'DISABLE_FETCHING_LOG_INFO'
DISABLE_FETCHING_EVENT_INFO = 'DISABLE_FETCHING_EVENT_INFO'
DISABLED_LOG_INFO_MESSAGE = "collection is disabled by the #{DISABLE_FETCHING_LOG_INFO} env var."
DISABLED_EVENT_INFO_MESSAGE = "collection is disabled by the #{DISABLE_FETCHING_EVENT_INFO} env var."
DEBUG_RESOURCE_NOT_FOUND_MESSAGE = "None found. Please check your usual logging service (e.g. Splunk)."
UNUSUAL_FAILURE_MESSAGE = <<~MSG
It is very unusual for this resource type to fail to deploy. Please try the deploy again.
Expand Down Expand Up @@ -141,8 +145,8 @@ def deploy_method
end

def sync_debug_info
@events = fetch_events
@logs = fetch_logs if supports_logs?
@events = fetch_events unless ENV[DISABLE_FETCHING_EVENT_INFO]
@logs = fetch_logs if supports_logs? && !ENV[DISABLE_FETCHING_EVENT_INFO]
@debug_info_synced = true
end

Expand All @@ -169,12 +173,16 @@ def debug_message
@events.each do |identifier, event_hashes|
event_hashes.each { |event| helpful_info << " [#{identifier}]\t#{event}" }
end
elsif ENV[DISABLE_FETCHING_EVENT_INFO]
helpful_info << " - Events: #{DISABLED_EVENT_INFO_MESSAGE}"
else
helpful_info << " - Events: #{DEBUG_RESOURCE_NOT_FOUND_MESSAGE}"
end

if supports_logs?
if @logs.blank? || @logs.values.all?(&:blank?)
if ENV[DISABLE_FETCHING_LOG_INFO]
helpful_info << " - Logs: #{DISABLED_LOG_INFO_MESSAGE}"
elsif @logs.blank? || @logs.values.all?(&:blank?)
helpful_info << " - Logs: #{DEBUG_RESOURCE_NOT_FOUND_MESSAGE}"
else
sorted_logs = @logs.sort_by { |_, log_lines| log_lines.length }
Expand Down
48 changes: 47 additions & 1 deletion test/unit/kubernetes-deploy/kubernetes_resource_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

class KubernetesResourceTest < KubernetesDeploy::TestCase
class DummyResource < KubernetesDeploy::KubernetesResource
attr_writer :succeeded
attr_writer :succeeded, :deploy_failed

def initialize(definition_extras: {})
definition = { "kind" => "DummyResource", "metadata" => { "name" => "test" } }.merge(definition_extras)
Expand All @@ -15,6 +15,18 @@ def exists?
true
end

def deploy_failed?
@deploy_failed
end

def supports_logs?
true
end

def fetch_logs
[]
end

def deploy_succeeded?
@succeeded
end
Expand Down Expand Up @@ -183,8 +195,42 @@ def test_deploy_timed_out_respects_annotation_based_timeouts
end
end

def test_debug_message_with_no_log_info
with_env(KubernetesDeploy::KubernetesResource::DISABLE_FETCHING_LOG_INFO, 'true') do
dummy = DummyResource.new
dummy.deploy_failed = true

assert_includes dummy.debug_message, "DummyResource/test: FAILED\n - Final status: Unknown\n"
assert_includes dummy.debug_message, KubernetesDeploy::KubernetesResource::DISABLED_LOG_INFO_MESSAGE
end
end

def test_debug_message_with_no_event_info
with_env(KubernetesDeploy::KubernetesResource::DISABLE_FETCHING_EVENT_INFO, 'true') do
dummy = DummyResource.new
dummy.deploy_failed = true

assert_includes dummy.debug_message, "DummyResource/test: FAILED\n - Final status: Unknown\n"
assert_includes dummy.debug_message, KubernetesDeploy::KubernetesResource::DISABLED_EVENT_INFO_MESSAGE
end
end

private

def with_env(key, value)
old_env_id = ENV[key]

if value.nil?
ENV.delete(key)
else
ENV[key] = value.to_s
end

yield
ensure
ENV[key] = old_env_id
end

def timeout_override_err_prefix
"kubernetes-deploy.shopify.io/timeout-override annotation is invalid"
end
Expand Down

0 comments on commit 1df2474

Please sign in to comment.