Class: Krane::DeployTask

Inherits:
Object
  • Object
show all
Extended by:
StatsD::MeasureMethods
Includes:
TemplateReporting
Defined in:
lib/krane/deploy_task.rb

Overview

Ship resources to a namespace

Constant Summary collapse

PROTECTED_NAMESPACES =
%w(
  default
  kube-system
  kube-public
)

Instance Method Summary collapse

Methods included from StatsD::MeasureMethods

measure_method

Methods included from TemplateReporting

#add_para_from_list, #record_invalid_template, #record_warnings

Constructor Details

#initialize(namespace:, context:, current_sha: nil, logger: nil, kubectl_instance: nil, bindings: {}, global_timeout: nil, selector: nil, filenames: [], protected_namespaces: nil, render_erb: false) ⇒ DeployTask

Initializes the deploy task

Parameters:

  • namespace (String)

    Kubernetes namespace (required)

  • context (String)

    Kubernetes context (required)

  • current_sha (String) (defaults to: nil)

    The SHA of the commit

  • logger (Object) (defaults to: nil)

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

  • kubectl_instance (Kubectl) (defaults to: nil)

    Kubectl instance

  • bindings (Hash) (defaults to: {})

    Bindings parsed by Krane::BindingsParser

  • global_timeout (Integer) (defaults to: nil)

    Timeout in seconds

  • selector (Hash) (defaults to: nil)

    Selector(s) parsed by Krane::LabelSelector

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

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

  • protected_namespaces (Array<String>) (defaults to: nil)

    Array of protected Kubernetes namespaces (defaults to Krane::DeployTask::PROTECTED_NAMESPACES)

  • render_erb (Boolean) (defaults to: false)

    Enable ERB rendering



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/krane/deploy_task.rb', line 98

def initialize(namespace:, context:, current_sha: nil, logger: nil, kubectl_instance: nil, bindings: {},
  global_timeout: nil, selector: nil, filenames: [], protected_namespaces: nil,
  render_erb: false)
  @logger = logger || Krane::FormattedLogger.build(namespace, context)
  @template_sets = TemplateSets.from_dirs_and_files(paths: filenames, logger: @logger, render_erb: render_erb)
  @task_config = Krane::TaskConfig.new(context, namespace, @logger)
  @bindings = bindings
  @namespace = namespace
  @namespace_tags = []
  @context = context
  @current_sha = current_sha
  @kubectl = kubectl_instance
  @global_timeout = global_timeout
  @selector = selector
  @protected_namespaces = protected_namespaces || PROTECTED_NAMESPACES
  @render_erb = render_erb
end

Instance Method Details

#predeploy_sequenceObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/krane/deploy_task.rb', line 58

def predeploy_sequence
  before_crs = %w(
    ResourceQuota
    NetworkPolicy
    ConfigMap
    PersistentVolumeClaim
    ServiceAccount
    Role
    RoleBinding
    Secret
  )
  after_crs = %w(
    Pod
  )

  before_crs + cluster_resource_discoverer.crds.select(&:predeployed?).map(&:kind) + after_crs
end

#prune_whitelistObject



76
77
78
# File 'lib/krane/deploy_task.rb', line 76

def prune_whitelist
  cluster_resource_discoverer.prunable_resources(namespaced: true)
end

#run(*args) ⇒ Boolean

Runs the task, returning a boolean representing success or failure

Returns:

  • (Boolean)


119
120
121
122
123
124
# File 'lib/krane/deploy_task.rb', line 119

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 do not appear in the template dir

Returns:

  • (nil)


132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/krane/deploy_task.rb', line 132

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

  @logger.phase_heading("Initializing deploy")
  validate_configuration(prune: prune)
  resources = discover_resources
  validate_resources(resources)

  @logger.phase_heading("Checking initial resource statuses")
  check_initial_status(resources)

  if deploy_has_priority_resources?(resources)
    @logger.phase_heading("Predeploying priority resources")
    resource_deployer.predeploy_priority_resources(resources, predeploy_sequence)
  end

  @logger.phase_heading("Deploying all resources")
  if @protected_namespaces.include?(@namespace) && prune
    raise FatalDeploymentError, "Refusing to deploy to protected namespace '#{@namespace}' with pruning enabled"
  end

  resource_deployer.deploy!(resources, verify_result, prune)

  StatsD.client.event("Deployment of #{@namespace} succeeded",
    "Successfully deployed all #{@namespace} resources to #{@context}",
    alert_type: "success", tags: statsd_tags + %w(status:success))
  StatsD.client.distribution('all_resources.duration', StatsD.duration(start),
    tags: statsd_tags + %w(status:success))
  @logger.print_summary(:success)
rescue DeploymentTimeoutError
  @logger.print_summary(:timed_out)
  StatsD.client.event("Deployment of #{@namespace} timed out",
    "One or more #{@namespace} 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 + %w(status:timeout))
  raise
rescue FatalDeploymentError => error
  @logger.summary.add_action(error.message) if error.message != error.class.to_s
  @logger.print_summary(:failure)
  StatsD.client.event("Deployment of #{@namespace} failed",
    "One or more #{@namespace} 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 + %w(status:failed))
  raise
end

#server_versionObject



80
81
82
# File 'lib/krane/deploy_task.rb', line 80

def server_version
  kubectl.server_version
end