Class: Krane::ResourceDeployer

Inherits:
Object
  • Object
show all
Extended by:
StatsD::MeasureMethods
Includes:
TemplateReporting
Defined in:
lib/krane/resource_deployer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from StatsD::MeasureMethods

measure_method

Methods included from TemplateReporting

#add_para_from_list, #record_invalid_template

Constructor Details

#initialize(task_config:, prune_whitelist:, global_timeout:, current_sha: nil, selector:, statsd_tags:) ⇒ ResourceDeployer

Returns a new instance of ResourceDeployer.


14
15
16
17
18
19
20
21
# File 'lib/krane/resource_deployer.rb', line 14

def initialize(task_config:, prune_whitelist:, global_timeout:, current_sha: nil, selector:, statsd_tags:)
  @task_config = task_config
  @prune_whitelist = prune_whitelist
  @global_timeout = global_timeout
  @current_sha = current_sha
  @selector = selector
  @statsd_tags = statsd_tags
end

Instance Attribute Details

#statsd_tagsObject (readonly)

Returns the value of attribute statsd_tags.


12
13
14
# File 'lib/krane/resource_deployer.rb', line 12

def statsd_tags
  @statsd_tags
end

Instance Method Details

#deploy!(resources, verify_result, prune) ⇒ Object


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/krane/resource_deployer.rb', line 30

def deploy!(resources, verify_result, prune)
  if verify_result
    deploy_all_resources(resources, prune: prune, verify: true)
    failed_resources = resources.reject(&:deploy_succeeded?)
    success = failed_resources.empty?
    if !success && failed_resources.all?(&:deploy_timed_out?)
      raise DeploymentTimeoutError
    end
    raise FatalDeploymentError unless success
  else
    deploy_all_resources(resources, prune: prune, verify: false)
    logger.summary.add_action("deployed #{resources.length} #{'resource'.pluralize(resources.length)}")
    warning = <<~MSG
      Deploy result verification is disabled for this deploy.
      This means the desired changes were communicated to Kubernetes, but the deploy did not make sure they actually succeeded.
    MSG
    logger.summary.add_paragraph(ColorizedString.new(warning).yellow)
  end
end

#dry_run(resources) ⇒ Object


23
24
25
26
27
28
# File 'lib/krane/resource_deployer.rb', line 23

def dry_run(resources)
  apply_all(resources, true, dry_run: true)
  true
rescue FatalDeploymentError
  false
end

#predeploy_priority_resources(resource_list, predeploy_sequence) ⇒ Object


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/krane/resource_deployer.rb', line 50

def predeploy_priority_resources(resource_list, predeploy_sequence)
  bare_pods = resource_list.select { |resource| resource.is_a?(Pod) }
  if bare_pods.count == 1
    bare_pods.first.stream_logs = true
  end

  predeploy_sequence.each do |resource_type, attributes|
    matching_resources = resource_list.select do |r|
      r.type == resource_type &&
      (!attributes[:group] || r.group == attributes[:group])
    end
    StatsD.client.gauge('priority_resources.count', matching_resources.size, tags: statsd_tags)

    next if matching_resources.empty?
    deploy_resources(matching_resources, verify: true, record_summary: false)

    failed_resources = matching_resources.reject(&:deploy_succeeded?)
    fail_count = failed_resources.length
    if fail_count > 0
      Krane::Concurrency.split_across_threads(failed_resources) do |r|
        r.sync_debug_info(kubectl)
      end
      failed_resources.each { |r| logger.summary.add_paragraph(r.debug_message) }
      raise FatalDeploymentError, "Failed to deploy #{fail_count} priority #{'resource'.pluralize(fail_count)}"
    end
    logger.blank_line
  end
end