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

Parameters:

  • context (String)

    Kubernetes context / cluster (required)

  • namespace (String)

    Kubernetes namespace (required)

  • logger (Object) (defaults to: nil)

    Logger object (defaults to an instance of Krane::FormattedLogger)

  • global_timeout (Integer) (defaults to: nil)

    Timeout in seconds



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

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.



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

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

Returns:

  • (Boolean)


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

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

Parameters:

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

    Array of workload names to restart

  • selector (Hash) (defaults to: nil)

    Selector(s) parsed by Krane::LabelSelector

  • verify_result (Boolean) (defaults to: true)

    Wait for completion and verify success

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/restart_task.rb', line 63

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 by annotating pod template #{RESTART_TRIGGER_ANNOTATION} annotation")
  patch_kubeclient_deployments(deployments)
  patch_kubeclient_statefulsets(statefulsets)
  patch_kubeclient_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