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 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(context:, global_timeout: nil, selector: nil, selector_as_filter: false, filenames: [], logger: nil, kubeconfig: 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)

  • selector_as_filter (Boolean) (defaults to: false)

    Allow selecting a subset of Kubernetes resource templates to deploy

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

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



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/krane/global_deploy_task.rb', line 38

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

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

Instance Attribute Details

#task_configObject (readonly)

Returns the value of attribute task_config.



29
30
31
# File 'lib/krane/global_deploy_task.rb', line 29

def task_config
  @task_config
end

Instance Method Details

#run(**args) ⇒ Boolean

Runs the task, returning a boolean representing success or failure

Returns:

  • (Boolean)


53
54
55
56
57
58
# File 'lib/krane/global_deploy_task.rb', line 53

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)


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
102
103
104
105
# File 'lib/krane/global_deploy_task.rb', line 67

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