Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
KnVerey committed May 27, 2017
1 parent 9c973aa commit acbd4a0
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 52 deletions.
5 changes: 2 additions & 3 deletions lib/kubernetes-deploy/deferred_summary_logging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def print_summary(success)

public_send(level, summary.actions_sentence)
summary.paragraphs.each do |para|
blank_line
blank_line(level)
msg_lines = para.split("\n")
msg_lines.each { |line| public_send(level, line) }
end
Expand All @@ -58,8 +58,7 @@ def initialize

def actions_sentence
sent = case actions_taken.length
when 0 then
return "No actions taken"
when 0 then "No actions taken"
when 1 then actions_taken.first
when 2 then actions_taken.join(" and ")
else
Expand Down
79 changes: 38 additions & 41 deletions lib/kubernetes-deploy/kubernetes_resource.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
require 'json'
require 'open3'
require 'shellwords'
Expand All @@ -10,6 +11,16 @@ class KubernetesResource

TIMEOUT = 5.minutes

DEBUG_RESOURCE_NOT_FOUND_MESSAGE = "None found. Please check your usual logging service (e.g. Splunk)."
UNUSUAL_FAILURE_MESSAGE = <<-MSG.strip_heredoc.strip
It is very unusual for this resource type to fail to deploy. Please try the deploy again,
and contact your cluster administrator if it still fails.
MSG
STANDARD_TIMEOUT_MESSAGE = <<-MSG.strip_heredoc.strip
Kubernetes will continue to attempt to deploy this resource in the cluster, but at this point it is considered unlikely that it will succeed.
If you have reason to believe it will succeed, retry the deploy to continue to monitor the rollout.
MSG

def self.for_type(type:, name:, namespace:, context:, file:, logger:)
subclass = case type
when 'cloudsql' then Cloudsql
Expand Down Expand Up @@ -101,52 +112,46 @@ def deploy_method
tpr? ? :replace : :apply
end

def status_data
{
group: group_name,
name: name,
status_string: status,
exists: exists?,
succeeded: deploy_succeeded?,
failed: deploy_failed?,
timed_out: deploy_timed_out?
}
end

def group_name
type.downcase.pluralize
end

def debug_message
helpful_info = [deploy_failure_message]
not_found_msg = "None found. Please check your usual logging service (e.g. Splunk)."
helpful_info = []
if deploy_failed?
helpful_info << ColorizedString.new("#{id}: FAILED").red
helpful_info << failure_message if failure_message.present?
else
helpful_info << "#{ColorizedString.new("#{id }: TIMED OUT").yellow} (limit: #{timeout}s)"
helpful_info << timeout_message if timeout_message.present?
end
helpful_info << " - Final status: #{status}"

events = get_events
if events.present?
helpful_info << " - Events:"
events.each { |event| helpful_info << " [#{id}/events]\t#{event.to_json}" }
else
helpful_info << " - Events: #{not_found_msg}"
helpful_info << " - Events: #{DEBUG_RESOURCE_NOT_FOUND_MESSAGE}"
end

container_logs = get_logs
if container_logs.blank? || container_logs.values.all?(&:blank?)
helpful_info << " - Logs: #{not_found_msg}"
else
helpful_info << " - Logs:"
container_logs.each do |container_name, logs|
logs.split("\n").each do |line|
helpful_info << " [#{id}/#{container_name}/logs]\t#{line}"
if respond_to?(:get_logs)
container_logs = get_logs
if container_logs.blank? || container_logs.values.all?(&:blank?)
helpful_info << " - Logs: #{DEBUG_RESOURCE_NOT_FOUND_MESSAGE}"
else
helpful_info << " - Logs:"
container_logs.each do |container_name, logs|
logs.split("\n").each do |line|
helpful_info << " [#{id}/#{container_name}/logs]\t#{line}"
end
end
end
end

helpful_info.join("\n")
end

def get_logs
end

def get_events
return unless exists?
out, _err, st = kubectl.run("get", "events", %(--output=jsonpath={range .items[?(@.involvedObject.name=="#{name}")]}{.involvedObject.kind}\t{.count}\t{.message}\t{.reason}\t{.type}\n{end}))
Expand All @@ -165,25 +170,17 @@ def get_events
event_hashes
end

def deploy_failure_message
if deploy_failed?
<<-MSG.strip_heredoc.chomp
#{ColorizedString.new("#{id}: FAILED").red}
- Final status: #{status}
MSG
elsif deploy_timed_out?
<<-MSG.strip_heredoc.chomp
#{ColorizedString.new("#{id }: TIMED OUT").yellow} (limit: #{timeout}s)
Kubernetes will continue to attempt to deploy this resource in the cluster, but at this point it is considered unlikely that it will succeed.
If you have reason to believe it will succeed, retry the deploy to continue to monitor the rollout.
- Final status: #{status}
MSG
end
def timeout_message
STANDARD_TIMEOUT_MESSAGE
end

def failure_message
end

def pretty_status
padding = " " * (50 - id.length)
"#{id}#{padding}#{exists? ? status : "not found"}"
msg = exists? ? status : "not found"
"#{id}#{padding}#{msg}"
end

def kubectl
Expand Down
6 changes: 4 additions & 2 deletions lib/kubernetes-deploy/kubernetes_resource/cloudsql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ class Cloudsql < KubernetesResource
def sync
_, _err, st = kubectl.run("get", type, @name)
@found = st.success?
@status = if cloudsql_proxy_deployment_exists? && mysql_service_exists?
@deployment_exists = cloudsql_proxy_deployment_exists?
@service_exists = mysql_service_exists?
@status = if @deployment_exists && @service_exists
"Provisioned"
else
"Unknown"
end
end

def deploy_succeeded?
cloudsql_proxy_deployment_exists? && mysql_service_exists?
@service_exists && @deployment_exists
end

def deploy_failed?
Expand Down
4 changes: 4 additions & 0 deletions lib/kubernetes-deploy/kubernetes_resource/config_map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ def deploy_failed?
false
end

def timeout_message
UNUSUAL_FAILURE_MESSAGE
end

def exists?
@found
end
Expand Down
4 changes: 0 additions & 4 deletions lib/kubernetes-deploy/kubernetes_resource/deployment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,5 @@ def deploy_timed_out?
def exists?
@found
end

def status_data
super.merge(replicas: @rollout_data, num_pods: @pods.length)
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ def deploy_method
:replace_force
end

def timeout_message
UNUSUAL_FAILURE_MESSAGE
end

def exists?
@found
end
Expand Down
4 changes: 4 additions & 0 deletions lib/kubernetes-deploy/kubernetes_resource/pod_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ def deploy_failed?
false
end

def timeout_message
UNUSUAL_FAILURE_MESSAGE
end

def exists?
@found
end
Expand Down
7 changes: 5 additions & 2 deletions lib/kubernetes-deploy/kubernetes_resource/redis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@ class Redis < KubernetesResource
def sync
_, _err, st = kubectl.run("get", type, @name)
@found = st.success?
@status = if redis_deployment_exists? && redis_service_exists?
@deployment_exists = redis_deployment_exists?
@service_exists = redis_service_exists?

@status = if @deployment_exists && @service_exists
"Provisioned"
else
"Unknown"
end
end

def deploy_succeeded?
redis_deployment_exists? && redis_service_exists?
@deployment_exists && @service_exists
end

def deploy_failed?
Expand Down
7 changes: 7 additions & 0 deletions lib/kubernetes-deploy/kubernetes_resource/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ def deploy_failed?
false
end

def timeout_message
<<-MSG.strip_heredoc.chomp
This service does not have any endpoints. If the related pods are failing, fixing them will solve this as well.
If the related pods are up, this service's selector is probably incorrect.
MSG
end

def exists?
@found
end
Expand Down

0 comments on commit acbd4a0

Please sign in to comment.