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"
RESTART_TRIGGER_ANNOTATION =
"kubectl.kubernetes.io/restartedAt"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Initializes the restart task



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

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

Instance Attribute Details

#task_configObject (readonly)

Returns the value of attribute task_config.



26
27
28
# File 'lib/krane/restart_task.rb', line 26

def task_config
  @task_config
end

Instance Method Details

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

Runs the task, returning a boolean representing success or failure



47
48
49
50
51
52
# File 'lib/krane/restart_task.rb', line 47

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

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

Runs the task, raising exceptions in case of issues



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
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/krane/restart_task.rb', line 62

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

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

  @logger.phase_heading("Triggering restart")
  restart_deployments!(deployments)
  restart_statefulsets!(statefulsets)
  restart_daemonsets!(daemonsets)

  if verify_result
    @logger.phase_heading("Waiting for rollout")
    resources = build_watchables(deployments, start, Deployment)
    resources += build_watchables(statefulsets, start, StatefulSet)
    resources += build_watchables(daemonsets, start, DaemonSet)
    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, statefulsets, daemonsets))
  @logger.print_summary(:success)
rescue DeploymentTimeoutError
  StatsD.client.distribution('restart.duration', StatsD.duration(start),
    tags: tags('timeout', deployments, statefulsets, daemonsets))
  @logger.print_summary(:timed_out)
  raise
rescue FatalDeploymentError => error
  StatsD.client.distribution('restart.duration', StatsD.duration(start),
    tags: tags('failure', deployments, statefulsets, daemonsets))
  @logger.summary.add_action(error.message) if error.message != error.class.to_s
  @logger.print_summary(:failure)
  raise
end