Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip collecting logs and events on failure via env var #239

Merged
merged 7 commits into from
Jan 18, 2018
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = "Log collection is disabled by the #{DISABLE_FETCHING_LOG_INFO} env var."
DISABLED_EVENT_INFO_MESSAGE = "Event 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 << DISABLED_EVENT_INFO_MESSAGE
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably indent this (and the log message) with " - " like we do with the actual info. Looks a bit out of place:

image

image

(I'm using test_deploy_result_logging_for_mixed_result_deploy to see that)

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 << 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
50 changes: 49 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,44 @@ 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
refute_includes dummy.debug_message, "- Logs"
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
refute_includes dummy.debug_message, "- Events"
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