Class: Krane::RunnerTask

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

Overview

Run a pod that exits upon completing a task

Defined Under Namespace

Classes: TaskTemplateMissingError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Initializes the runner task

Parameters:

  • namespace (String)

    Kubernetes namespace (required)

  • context (String)

    Kubernetes context / cluster (required)

  • logger (Object) (defaults to: nil)

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

  • global_timeout (Integer) (defaults to: nil)

    Timeout in seconds


29
30
31
32
33
34
35
# File 'lib/krane/runner_task.rb', line 29

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

Instance Attribute Details

#pod_nameObject (readonly)

Returns the value of attribute pod_name.


19
20
21
# File 'lib/krane/runner_task.rb', line 19

def pod_name
  @pod_name
end

#task_configObject (readonly)

Returns the value of attribute task_config.


19
20
21
# File 'lib/krane/runner_task.rb', line 19

def task_config
  @task_config
end

Instance Method Details

#run(**args) ⇒ Boolean

Runs the task, returning a boolean representing success or failure

Returns:

  • (Boolean)

40
41
42
43
44
45
# File 'lib/krane/runner_task.rb', line 40

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

#run!(template:, command:, arguments:, env_vars: [], image_tag: nil, verify_result: true) ⇒ nil

Runs the task, raising exceptions in case of issues

Parameters:

  • template (String)

    The filename of the template you'll be rendering (required)

  • command (Array<String>)

    Override the default command in the container image

  • arguments (Array<String>)

    Override the default arguments for the command

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

    List of env vars

  • verify_result (Boolean) (defaults to: true)

    Wait for completion and verify pod success

Returns:

  • (nil)

56
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
88
89
90
91
92
93
# File 'lib/krane/runner_task.rb', line 56

def run!(template:, command:, arguments:, env_vars: [], image_tag: nil, verify_result: true)
  start = Time.now.utc
  @logger.reset

  @logger.phase_heading("Initializing task")

  @logger.info("Validating configuration")
  verify_config!(template)
  @logger.info("Using namespace '#{@namespace}' in context '#{@context}'")
  container_overrides = ContainerOverrides.new(
    command: command,
    arguments: arguments,
    env_vars: env_vars,
    image_tag: image_tag
  )
  pod = build_pod(template, container_overrides, verify_result)
  validate_pod(pod)

  @logger.phase_heading("Running pod")
  create_pod(pod)

  if verify_result
    @logger.phase_heading("Streaming logs")
    watch_pod(pod)
  else
    record_status_once(pod)
  end
  StatsD.client.distribution('task_runner.duration', StatsD.duration(start), tags: statsd_tags('success'))
  @logger.print_summary(:success)
rescue DeploymentTimeoutError
  StatsD.client.distribution('task_runner.duration', StatsD.duration(start), tags: statsd_tags('timeout'))
  @logger.print_summary(:timed_out)
  raise
rescue FatalDeploymentError
  StatsD.client.distribution('task_runner.duration', StatsD.duration(start), tags: statsd_tags('failure'))
  @logger.print_summary(:failure)
  raise
end