Class: Krane::RestartTask

Inherits:
Object
  • Object
show all
Defined in:
lib/krane/restart_task.rb

Overview

Restart the pods in one or more deployments

Defined Under Namespace

Classes: FatalRestartError, RestartAPIError

Constant Summary collapse

HTTP_OK_RANGE =
200..299
ANNOTATION =
"shipit.shopify.io/restart"

Instance Method Summary collapse

Constructor Details

#initialize(context:, namespace:, logger: nil, global_timeout: nil) ⇒ RestartTask

Initializes the restart task



31
32
33
34
35
36
37
# File 'lib/krane/restart_task.rb', line 31

def initialize(context:, namespace:, logger: nil, global_timeout: nil)
  @logger = logger || Krane::FormattedLogger.build(namespace, context)
  @task_config = Krane::TaskConfig.new(context, namespace, @logger)
  @context = context
  @namespace = namespace
  @global_timeout = global_timeout
end

Instance Method Details

#run(**args) ⇒ Boolean Also known as: perform

Runs the task, returning a boolean representing success or failure



42
43
44
45
46
47
# File 'lib/krane/restart_task.rb', line 42

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

#run!(deployments: nil, selector: nil, verify_result: true) ⇒ nil Also known as: perform!

Runs the task, raising exceptions in case of issues



57
58
59
60
61
62
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
# File 'lib/krane/restart_task.rb', line 57

def run!(deployments: nil, selector: nil, verify_result: true)
  start = Time.now.utc
  @logger.reset

  @logger.phase_heading("Initializing restart")
  verify_config!
  deployments = identify_target_deployments(deployments, selector: selector)

  @logger.phase_heading("Triggering restart by touching ENV[RESTARTED_AT]")
  patch_kubeclient_deployments(deployments)

  if verify_result
    @logger.phase_heading("Waiting for rollout")
    resources = build_watchables(deployments, start)
    verify_restart(resources)
  else
    warning = "Result verification is disabled for this task"
    @logger.summary.add_paragraph(ColorizedString.new(warning).yellow)
  end
  StatsD.client.distribution('restart.duration', StatsD.duration(start), tags: tags('success', deployments))
  @logger.print_summary(:success)
rescue DeploymentTimeoutError
  StatsD.client.distribution('restart.duration', StatsD.duration(start), tags: tags('timeout', deployments))
  @logger.print_summary(:timed_out)
  raise
rescue FatalDeploymentError => error
  StatsD.client.distribution('restart.duration', StatsD.duration(start), tags: tags('failure', deployments))
  @logger.summary.add_action(error.message) if error.message != error.class.to_s
  @logger.print_summary(:failure)
  raise
end