Class: Krane::GlobalDeployTask

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

Overview

Ship global resources to a context

Instance Method Summary collapse

Methods included from StatsD::MeasureMethods

measure_method

Methods included from TemplateReporting

#add_para_from_list, #record_invalid_template, #record_warnings

Constructor Details

#initialize(context:, global_timeout: nil, selector: nil, filenames: [], logger: nil) ⇒ GlobalDeployTask

Initializes the deploy task

Parameters:

  • context (String)

    Kubernetes context (required)

  • global_timeout (Integer) (defaults to: nil)

    Timeout in seconds

  • selector (Hash) (defaults to: nil)

    Selector(s) parsed by Krane::LabelSelector (required)

  • filenames (Array<String>) (defaults to: [])

    An array of filenames and/or directories containing templates (required)



36
37
38
39
40
41
42
43
44
# File 'lib/krane/global_deploy_task.rb', line 36

def initialize(context:, global_timeout: nil, selector: nil, filenames: [], logger: nil)
  template_paths = filenames.map { |path| File.expand_path(path) }

  @task_config = TaskConfig.new(context, nil, logger)
  @template_sets = TemplateSets.from_dirs_and_files(paths: template_paths,
    logger: @task_config.logger, render_erb: false)
  @global_timeout = global_timeout
  @selector = selector
end

Instance Method Details

#run(*args) ⇒ Boolean

Runs the task, returning a boolean representing success or failure

Returns:

  • (Boolean)


49
50
51
52
53
54
# File 'lib/krane/global_deploy_task.rb', line 49

def run(*args)
  run!(*args)
  true
rescue FatalDeploymentError
  false
end

#run!(verify_result: true, prune: true) ⇒ nil

Runs the task, raising exceptions in case of issues

Parameters:

  • verify_result (Boolean) (defaults to: true)

    Wait for completion and verify success

  • prune (Boolean) (defaults to: true)

    Enable deletion of resources that match the provided selector and do not appear in the template dir

Returns:

  • (nil)


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/krane/global_deploy_task.rb', line 63

def run!(verify_result: true, prune: true)
  start = Time.now.utc
  logger.reset

  logger.phase_heading("Initializing deploy")
  validate_configuration
  resources = discover_resources
  validate_resources(resources)

  logger.phase_heading("Checking initial resource statuses")
  check_initial_status(resources)

  logger.phase_heading("Deploying all resources")
  deploy!(resources, verify_result, prune)

  StatsD.client.event("Deployment succeeded",
    "Successfully deployed all resources to #{context}",
    alert_type: "success", tags: statsd_tags + %w(status:success))
  StatsD.client.distribution('all_resources.duration', StatsD.duration(start),
    tags: statsd_tags << "status:success")
  logger.print_summary(:success)
rescue Krane::DeploymentTimeoutError
  logger.print_summary(:timed_out)
  StatsD.client.event("Deployment timed out",
    "One or more resources failed to deploy to #{context} in time",
    alert_type: "error", tags: statsd_tags + %w(status:timeout))
  StatsD.client.distribution('all_resources.duration', StatsD.duration(start),
    tags: statsd_tags << "status:timeout")
  raise
rescue Krane::FatalDeploymentError => error
  logger.summary.add_action(error.message) if error.message != error.class.to_s
  logger.print_summary(:failure)
  StatsD.client.event("Deployment failed",
    "One or more resources failed to deploy to #{context}",
    alert_type: "error", tags: statsd_tags + %w(status:failed))
  StatsD.client.distribution('all_resources.duration', StatsD.duration(start),
    tags: statsd_tags << "status:failed")
  raise
end