Class: Krane::KubernetesResource
- Inherits:
-
Object
- Object
- Krane::KubernetesResource
show all
- Defined in:
- lib/krane/kubernetes_resource.rb
Direct Known Subclasses
Cloudsql, ConfigMap, CronJob, CustomResource, CustomResourceDefinition, Deployment, HorizontalPodAutoscaler, Ingress, Job, NetworkPolicy, PersistentVolumeClaim, PersistentVolumeClaim::StorageClass, Pod, PodDisruptionBudget, PodSetBase, PodTemplate, ResourceQuota, Role, RoleBinding, Secret, Service, ServiceAccount
Defined Under Namespace
Classes: Event
Constant Summary
collapse
- GLOBAL =
false
- TIMEOUT =
5.minutes
- LOG_LINE_COUNT =
250
- SERVER_DRY_RUN_DISABLED_ERROR =
/(unknown flag: --server-dry-run)|(doesn't support dry-run)|(dryRun alpha feature is disabled)/
- DISABLE_FETCHING_LOG_INFO =
'DISABLE_FETCHING_LOG_INFO'
- DISABLE_FETCHING_EVENT_INFO =
'DISABLE_FETCHING_EVENT_INFO'
- DISABLED_LOG_INFO_MESSAGE =
"collection is disabled by the #{DISABLE_FETCHING_LOG_INFO} env var."
- DISABLED_EVENT_INFO_MESSAGE =
"collection is disabled by the #{DISABLE_FETCHING_EVENT_INFO} env var."
- DEBUG_RESOURCE_NOT_FOUND_MESSAGE =
"None found. Please check your usual logging service (e.g. Splunk)."
- UNUSUAL_FAILURE_MESSAGE =
<<~MSG
It is very unusual for this resource type to fail to deploy. Please try the deploy again.
If that new deploy also fails, contact your cluster administrator.
MSG
- STANDARD_TIMEOUT_MESSAGE =
<<~MSG
Kubernetes will continue to attempt to deploy this resource in the cluster, but at this point it is considered unlikely that it will succeed.
If you have reason to believe it will succeed, retry the deploy to continue to monitor the rollout.
MSG
- TIMEOUT_OVERRIDE_ANNOTATION_SUFFIX =
"timeout-override"
- TIMEOUT_OVERRIDE_ANNOTATION_DEPRECATED =
"kubernetes-deploy.shopify.io/#{TIMEOUT_OVERRIDE_ANNOTATION_SUFFIX}"
- TIMEOUT_OVERRIDE_ANNOTATION =
"krane.shopify.io/#{TIMEOUT_OVERRIDE_ANNOTATION_SUFFIX}"
- LAST_APPLIED_ANNOTATION =
"kubectl.kubernetes.io/last-applied-configuration"
- SENSITIVE_TEMPLATE_CONTENT =
false
- SERVER_DRY_RUNNABLE =
false
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#<=>(other) ⇒ Object
-
#after_sync ⇒ Object
-
#current_generation ⇒ Object
-
#debug_message(cause = nil, info_hash = {}) ⇒ Object
-
#deploy_failed? ⇒ Boolean
-
#deploy_method ⇒ Object
Expected values: :apply, :create, :replace, :replace_force.
-
#deploy_started? ⇒ Boolean
-
#deploy_succeeded? ⇒ Boolean
-
#deploy_timed_out? ⇒ Boolean
-
#disappeared? ⇒ Boolean
-
#exists? ⇒ Boolean
-
#failure_message ⇒ Object
-
#fetch_events(kubectl) ⇒ Object
Returns a hash in the following format: { “pod/web-1” => [ “Pulling: pulling image ”hello-world:latest“ (1 events)”, “Pulled: Successfully pulled image ”hello-world:latest“ (1 events)” ] }.
-
#file_path ⇒ Object
-
#global? ⇒ Boolean
-
#has_warnings? ⇒ Boolean
-
#id ⇒ Object
-
#initialize(namespace:, context:, definition:, logger:, statsd_tags: []) ⇒ KubernetesResource
constructor
A new instance of KubernetesResource.
-
#kubectl_resource_type ⇒ Object
-
#observed_generation ⇒ Object
-
#pretty_status ⇒ Object
-
#pretty_timeout_type ⇒ Object
-
#report_status_to_statsd(watch_time) ⇒ Object
-
#sensitive_template_content? ⇒ Boolean
-
#server_dry_run_validated? ⇒ Boolean
-
#server_dry_runnable_resource? ⇒ Boolean
-
#status ⇒ Object
-
#sync(cache) ⇒ Object
-
#sync_debug_info(kubectl) ⇒ Object
-
#terminating? ⇒ Boolean
-
#timeout ⇒ Object
-
#timeout_message ⇒ Object
-
#timeout_override ⇒ Object
-
#to_kubeclient_resource ⇒ Object
-
#use_generated_name(instance_data) ⇒ Object
If a resource uses generateName, we don’t know the full name of the resource until it’s deployed to the cluster.
-
#uses_generate_name? ⇒ Boolean
-
#validate_definition(kubectl, selector: nil) ⇒ Object
-
#validation_error_msg ⇒ Object
-
#validation_failed? ⇒ Boolean
-
#validation_warning_msg ⇒ Object
Constructor Details
#initialize(namespace:, context:, definition:, logger:, statsd_tags: []) ⇒ KubernetesResource
Returns a new instance of KubernetesResource.
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
# File 'lib/krane/kubernetes_resource.rb', line 114
def initialize(namespace:, context:, definition:, logger:, statsd_tags: [])
@name = (definition.dig("metadata", "name") || definition.dig("metadata", "generateName")).to_s
@optional_statsd_tags = statsd_tags
@namespace = namespace
@context = context
@logger = logger
@definition = definition
@statsd_report_done = false
@disappeared = false
@validation_errors = []
@validation_warnings = []
@instance_data = {}
@server_dry_run_validated = false
end
|
Instance Attribute Details
#context ⇒ Object
Returns the value of attribute context.
12
13
14
|
# File 'lib/krane/kubernetes_resource.rb', line 12
def context
@context
end
|
#deploy_started_at=(value) ⇒ Object
Sets the attribute deploy_started_at
13
14
15
|
# File 'lib/krane/kubernetes_resource.rb', line 13
def deploy_started_at=(value)
@deploy_started_at = value
end
|
#global=(value) ⇒ Object
Sets the attribute global
13
14
15
|
# File 'lib/krane/kubernetes_resource.rb', line 13
def global=(value)
@global = value
end
|
#name ⇒ Object
Returns the value of attribute name.
12
13
14
|
# File 'lib/krane/kubernetes_resource.rb', line 12
def name
@name
end
|
#namespace ⇒ Object
Returns the value of attribute namespace.
12
13
14
|
# File 'lib/krane/kubernetes_resource.rb', line 12
def namespace
@namespace
end
|
#type ⇒ Object
226
227
228
|
# File 'lib/krane/kubernetes_resource.rb', line 226
def type
@type || self.class.kind
end
|
Class Method Details
.build(namespace: nil, context:, definition:, logger:, statsd_tags:, crd: nil, global_names: []) ⇒ Object
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/krane/kubernetes_resource.rb', line 43
def build(namespace: nil, context:, definition:, logger:, statsd_tags:, crd: nil, global_names: [])
validate_definition_essentials(definition)
opts = { namespace: namespace, context: context, definition: definition, logger: logger,
statsd_tags: statsd_tags }
if (klass = class_for_kind(definition["kind"]))
return klass.new(**opts)
end
if crd
CustomResource.new(crd: crd, **opts)
else
type = definition["kind"]
inst = new(**opts)
inst.type = type
inst.global = global_names.map(&:downcase).include?(type.downcase)
inst
end
end
|
.class_for_kind(kind) ⇒ Object
61
62
63
64
65
66
67
|
# File 'lib/krane/kubernetes_resource.rb', line 61
def class_for_kind(kind)
if Krane.const_defined?(kind)
Krane.const_get(kind)
end
rescue NameError
nil
end
|
.kind ⇒ Object
73
74
75
|
# File 'lib/krane/kubernetes_resource.rb', line 73
def kind
name.demodulize
end
|
.timeout ⇒ Object
69
70
71
|
# File 'lib/krane/kubernetes_resource.rb', line 69
def timeout
self::TIMEOUT
end
|
Instance Method Details
#<=>(other) ⇒ Object
164
165
166
|
# File 'lib/krane/kubernetes_resource.rb', line 164
def <=>(other)
id <=> other.id
end
|
#after_sync ⇒ Object
179
180
|
# File 'lib/krane/kubernetes_resource.rb', line 179
def after_sync
end
|
#current_generation ⇒ Object
211
212
213
214
|
# File 'lib/krane/kubernetes_resource.rb', line 211
def current_generation
return -1 unless exists? @instance_data.dig("metadata", "generation")
end
|
#debug_message(cause = nil, info_hash = {}) ⇒ Object
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
# File 'lib/krane/kubernetes_resource.rb', line 253
def debug_message(cause = nil, info_hash = {})
helpful_info = []
if cause == :gave_up
debug_heading = ColorizedString.new("#{id}: GLOBAL WATCH TIMEOUT (#{info_hash[:timeout]} seconds)").yellow
helpful_info << "If you expected it to take longer than #{info_hash[:timeout]} seconds for your deploy"\
" to roll out, increase --max-watch-seconds."
elsif deploy_failed?
debug_heading = ColorizedString.new("#{id}: FAILED").red
helpful_info << failure_message if failure_message.present?
elsif deploy_timed_out?
debug_heading = ColorizedString.new("#{id}: TIMED OUT (#{pretty_timeout_type})").yellow
helpful_info << timeout_message if timeout_message.present?
else
debug_heading = ColorizedString.new("#{id}: MONITORING ERROR").red
helpful_info << failure_message if failure_message.present?
helpful_info << timeout_message if timeout_message.present? && timeout_message != STANDARD_TIMEOUT_MESSAGE
end
final_status = " - Final status: #{status}"
final_status = "\n#{final_status}" if helpful_info.present? && !helpful_info.last.end_with?("\n")
helpful_info.prepend(debug_heading)
helpful_info << final_status
if @debug_events.present?
helpful_info << " - Events (common success events excluded):"
@debug_events.each do |identifier, event_hashes|
event_hashes.each { |event| helpful_info << " [#{identifier}]\t#{event}" }
end
elsif ENV[DISABLE_FETCHING_EVENT_INFO]
helpful_info << " - Events: #{DISABLED_EVENT_INFO_MESSAGE}"
else
helpful_info << " - Events: #{DEBUG_RESOURCE_NOT_FOUND_MESSAGE}"
end
if print_debug_logs?
if ENV[DISABLE_FETCHING_LOG_INFO]
helpful_info << " - Logs: #{DISABLED_LOG_INFO_MESSAGE}"
elsif @debug_logs.blank?
helpful_info << " - Logs: #{DEBUG_RESOURCE_NOT_FOUND_MESSAGE}"
else
container_logs = @debug_logs.container_logs.sort_by { |c| c.lines.length }
container_logs.each do |logs|
if logs.empty?
helpful_info << " - Logs from container '#{logs.container_name}': #{DEBUG_RESOURCE_NOT_FOUND_MESSAGE}"
next
end
if logs.lines.length == ContainerLogs::DEFAULT_LINE_LIMIT
truncated = " (last #{ContainerLogs::DEFAULT_LINE_LIMIT} lines shown)"
end
helpful_info << " - Logs from container '#{logs.container_name}'#{truncated}:"
logs.lines.each do |line|
helpful_info << " #{line}"
end
end
end
end
helpful_info.join("\n")
end
|
#deploy_failed? ⇒ Boolean
190
191
192
|
# File 'lib/krane/kubernetes_resource.rb', line 190
def deploy_failed?
false
end
|
#deploy_method ⇒ Object
Expected values: :apply, :create, :replace, :replace_force
240
241
242
243
244
245
246
|
# File 'lib/krane/kubernetes_resource.rb', line 240
def deploy_method
if @definition.dig("metadata", "name").blank? && uses_generate_name?
:create
else
:apply
end
end
|
#deploy_started? ⇒ Boolean
194
195
196
|
# File 'lib/krane/kubernetes_resource.rb', line 194
def deploy_started?
@deploy_started_at.present?
end
|
#deploy_succeeded? ⇒ Boolean
198
199
200
201
202
203
204
205
|
# File 'lib/krane/kubernetes_resource.rb', line 198
def deploy_succeeded?
return false unless deploy_started?
unless @success_assumption_warning_shown
@logger.warn("Don't know how to monitor resources of type #{type}. Assuming #{id} deployed successfully.")
@success_assumption_warning_shown = true
end
true
end
|
#deploy_timed_out? ⇒ Boolean
234
235
236
237
|
# File 'lib/krane/kubernetes_resource.rb', line 234
def deploy_timed_out?
return false unless deploy_started?
!deploy_succeeded? && !deploy_failed? && (Time.now.utc - @deploy_started_at > timeout)
end
|
#disappeared? ⇒ Boolean
186
187
188
|
# File 'lib/krane/kubernetes_resource.rb', line 186
def disappeared?
@disappeared
end
|
#exists? ⇒ Boolean
207
208
209
|
# File 'lib/krane/kubernetes_resource.rb', line 207
def exists?
@instance_data.present?
end
|
#failure_message ⇒ Object
338
339
|
# File 'lib/krane/kubernetes_resource.rb', line 338
def failure_message
end
|
#fetch_events(kubectl) ⇒ Object
Returns a hash in the following format:
"pod/web-1" => [
"Pulling: pulling image "hello-world:latest" (1 events)",
"Pulled: Successfully pulled image "hello-world:latest" (1 events)"
]
322
323
324
325
326
327
328
329
330
331
332
|
# File 'lib/krane/kubernetes_resource.rb', line 322
def fetch_events(kubectl)
return {} unless exists?
out, _err, st = kubectl.run("get", "events", "--output=go-template=#{Event.go_template_for(type, name)}",
log_failure: false, use_namespace: !global?)
return {} unless st.success?
event_collector = Hash.new { |hash, key| hash[key] = [] }
Event.(out).each_with_object(event_collector) do |candidate, events|
events[id] << candidate.to_s if candidate.seen_since?(@deploy_started_at - 5.seconds)
end
end
|
#file_path ⇒ Object
168
169
170
|
# File 'lib/krane/kubernetes_resource.rb', line 168
def file_path
file.path
end
|
#global? ⇒ Boolean
441
442
443
|
# File 'lib/krane/kubernetes_resource.rb', line 441
def global?
@global || self.class::GLOBAL
end
|
#has_warnings? ⇒ Boolean
148
149
150
|
# File 'lib/krane/kubernetes_resource.rb', line 148
def has_warnings?
@validation_warnings.present?
end
|
#id ⇒ Object
160
161
162
|
# File 'lib/krane/kubernetes_resource.rb', line 160
def id
"#{type}/#{name}"
end
|
#kubectl_resource_type ⇒ Object
230
231
232
|
# File 'lib/krane/kubernetes_resource.rb', line 230
def kubectl_resource_type
type
end
|
#observed_generation ⇒ Object
216
217
218
219
220
|
# File 'lib/krane/kubernetes_resource.rb', line 216
def observed_generation
return -2 unless exists?
@instance_data.dig('status', 'observedGeneration')
end
|
#pretty_status ⇒ Object
341
342
343
344
|
# File 'lib/krane/kubernetes_resource.rb', line 341
def pretty_status
padding = " " * [50 - id.length, 1].max
"#{id}#{padding}#{status}"
end
|
#pretty_timeout_type ⇒ Object
110
111
112
|
# File 'lib/krane/kubernetes_resource.rb', line 110
def pretty_timeout_type
"timeout: #{timeout}s"
end
|
#report_status_to_statsd(watch_time) ⇒ Object
346
347
348
349
350
351
|
# File 'lib/krane/kubernetes_resource.rb', line 346
def report_status_to_statsd(watch_time)
unless @statsd_report_done
StatsD.client.distribution('resource.duration', watch_time, tags: statsd_tags)
@statsd_report_done = true
end
end
|
#sensitive_template_content? ⇒ Boolean
353
354
355
|
# File 'lib/krane/kubernetes_resource.rb', line 353
def sensitive_template_content?
self.class::SENSITIVE_TEMPLATE_CONTENT
end
|
#server_dry_run_validated? ⇒ Boolean
367
368
369
|
# File 'lib/krane/kubernetes_resource.rb', line 367
def server_dry_run_validated?
@server_dry_run_validated
end
|
#server_dry_runnable_resource? ⇒ Boolean
357
358
359
360
361
|
# File 'lib/krane/kubernetes_resource.rb', line 357
def server_dry_runnable_resource?
self.class::SERVER_DRY_RUNNABLE && !uses_generate_name?
end
|
#status ⇒ Object
222
223
224
|
# File 'lib/krane/kubernetes_resource.rb', line 222
def status
exists? ? "Exists" : "Not Found"
end
|
#sync(cache) ⇒ Object
172
173
174
175
176
177
|
# File 'lib/krane/kubernetes_resource.rb', line 172
def sync(cache)
@instance_data = cache.get_instance(kubectl_resource_type, name, raise_if_not_found: true)
rescue Krane::Kubectl::ResourceNotFoundError
@disappeared = true if deploy_started?
@instance_data = {}
end
|
#sync_debug_info(kubectl) ⇒ Object
248
249
250
251
|
# File 'lib/krane/kubernetes_resource.rb', line 248
def sync_debug_info(kubectl)
@debug_events = fetch_events(kubectl) unless ENV[DISABLE_FETCHING_EVENT_INFO]
@debug_logs = fetch_debug_logs if print_debug_logs? && !ENV[DISABLE_FETCHING_LOG_INFO]
end
|
#terminating? ⇒ Boolean
182
183
184
|
# File 'lib/krane/kubernetes_resource.rb', line 182
def terminating?
@instance_data.dig('metadata', 'deletionTimestamp').present?
end
|
#timeout ⇒ Object
97
98
99
100
|
# File 'lib/krane/kubernetes_resource.rb', line 97
def timeout
return timeout_override if timeout_override.present?
self.class.timeout
end
|
#timeout_message ⇒ Object
#timeout_override ⇒ Object
#to_kubeclient_resource ⇒ Object
130
131
132
|
# File 'lib/krane/kubernetes_resource.rb', line 130
def to_kubeclient_resource
Kubeclient::Resource.new(@definition)
end
|
#use_generated_name(instance_data) ⇒ Object
If a resource uses generateName, we don’t know the full name of the resource until it’s deployed to the cluster. In this case, we need to update our local definition with the realized name in order to accurately track the resource during deploy
374
375
376
377
378
379
|
# File 'lib/krane/kubernetes_resource.rb', line 374
def use_generated_name(instance_data)
@name = instance_data.dig('metadata', 'name')
@definition['metadata']['name'] = @name
@definition['metadata'].delete('generateName')
@file = create_definition_tempfile
end
|
#uses_generate_name? ⇒ Boolean
363
364
365
|
# File 'lib/krane/kubernetes_resource.rb', line 363
def uses_generate_name?
@definition.dig('metadata', 'generateName').present?
end
|
#validate_definition(kubectl, selector: nil) ⇒ Object
134
135
136
137
138
139
140
141
142
|
# File 'lib/krane/kubernetes_resource.rb', line 134
def validate_definition(kubectl, selector: nil)
@validation_errors = []
@validation_warnings = []
validate_selector(selector) if selector
validate_timeout_annotation
validate_annotation_version
validate_spec_with_kubectl(kubectl)
@validation_errors.present?
end
|
#validation_error_msg ⇒ Object
152
153
154
|
# File 'lib/krane/kubernetes_resource.rb', line 152
def validation_error_msg
@validation_errors.join("\n")
end
|
#validation_failed? ⇒ Boolean
156
157
158
|
# File 'lib/krane/kubernetes_resource.rb', line 156
def validation_failed?
@validation_errors.present?
end
|
#validation_warning_msg ⇒ Object
144
145
146
|
# File 'lib/krane/kubernetes_resource.rb', line 144
def validation_warning_msg
@validation_warnings.join("\n")
end
|