118
119
120
121
122
123
124
125
126
127
128
129
130
131
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
# File 'lib/kubernetes-deploy/deploy_task.rb', line 118
def run!(verify_result: true, allow_protected_ns: false, prune: true)
start = Time.now.utc
@logger.reset
@logger.phase_heading("Initializing deploy")
validate_configuration(allow_protected_ns: allow_protected_ns, prune: prune)
confirm_context_exists
confirm_namespace_exists
@namespace_tags |= tags_from_namespace_labels
resources = discover_resources
validate_definitions(resources)
@logger.phase_heading("Checking initial resource statuses")
@sync_mediator.sync(resources)
resources.each { |r| @logger.info(r.pretty_status) }
ejson = EjsonSecretProvisioner.new(
namespace: @namespace,
context: @context,
template_dir: @template_dir,
logger: @logger,
prune: prune,
)
if ejson.secret_changes_required?
@logger.phase_heading("Deploying kubernetes secrets from #{EjsonSecretProvisioner::EJSON_SECRETS_FILE}")
ejson.run
end
if deploy_has_priority_resources?(resources)
@logger.phase_heading("Predeploying priority resources")
start_priority_resource = Time.now.utc
predeploy_priority_resources(resources)
::StatsD.measure('priority_resources.duration', StatsD.duration(start_priority_resource), tags: statsd_tags)
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
if verify_result
start_normal_resource = Time.now.utc
deploy_resources(resources, prune: prune, verify: true)
::StatsD.measure('normal_resources.duration', StatsD.duration(start_normal_resource), tags: statsd_tags)
failed_resources = resources.reject(&:deploy_succeeded?)
success = failed_resources.empty?
if !success && failed_resources.all?(&:deploy_timed_out?)
raise DeploymentTimeoutError
end
raise FatalDeploymentError unless success
else
deploy_resources(resources, prune: prune, verify: false)
@logger.summary.add_action("deployed #{resources.length} #{'resource'.pluralize(resources.length)}")
warning = <<~MSG
Deploy result verification is disabled for this deploy.
This means the desired changes were communicated to Kubernetes, but the deploy did not make sure they actually succeeded.
MSG
@logger.summary.add_paragraph(ColorizedString.new(warning).yellow)
end
::StatsD.event("Deployment of #{@namespace} succeeded",
"Successfully deployed all #{@namespace} resources to #{@context}",
alert_type: "success", tags: statsd_tags << "status:success")
::StatsD.measure('all_resources.duration', StatsD.duration(start), tags: statsd_tags << "status:success")
@logger.print_summary(:success)
rescue DeploymentTimeoutError
@logger.print_summary(:timed_out)
::StatsD.event("Deployment of #{@namespace} timed out",
"One or more #{@namespace} resources failed to deploy to #{@context} in time",
alert_type: "error", tags: statsd_tags << "status:timeout")
::StatsD.measure('all_resources.duration', StatsD.duration(start), tags: statsd_tags << "status:timeout")
raise
rescue FatalDeploymentError => error
@logger.summary.add_action(error.message) if error.message != error.class.to_s
@logger.print_summary(:failure)
::StatsD.event("Deployment of #{@namespace} failed",
"One or more #{@namespace} resources failed to deploy to #{@context}",
alert_type: "error", tags: statsd_tags << "status:failed")
::StatsD.measure('all_resources.duration', StatsD.duration(start), tags: statsd_tags << "status:failed")
raise
end
|