Class: Krane::KubernetesResource::Event

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

Constant Summary collapse

EVENT_SEPARATOR =
"ENDEVENT--BEGINEVENT"
FIELD_SEPARATOR =
"ENDFIELD--BEGINFIELD"
FIELDS =
%w(
  .involvedObject.kind
  .involvedObject.name
  .count
  .lastTimestamp
  .reason
  .message
  .eventTime
  .deprecatedCount
  .deprecatedLastTimestamp
  .series
)
FIELD_EMPTY_VALUE =
'<no value>'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(subject_kind:, last_timestamp:, reason:, message:, count:, subject_name:) ⇒ Event

Returns a new instance of Event.



480
481
482
483
484
485
486
487
# File 'lib/krane/kubernetes_resource.rb', line 480

def initialize(subject_kind:, last_timestamp:, reason:, message:, count:, subject_name:)
  @subject_kind = subject_kind
  @subject_name = subject_name
  @last_timestamp = Time.parse(last_timestamp)
  @reason = reason
  @message = message.tr("\n", '')
  @count = count.to_i
end

Class Method Details

.extract_all_from_go_template_blob(blob) ⇒ Object



422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
# File 'lib/krane/kubernetes_resource.rb', line 422

def self.extract_all_from_go_template_blob(blob)
  blob.split(EVENT_SEPARATOR).map do |event_blob|
    pieces = event_blob.split(FIELD_SEPARATOR, FIELDS.length)
    count = extract_event_count(pieces)
    timestamp = extract_event_timestamp(pieces)

    new(
      subject_kind: pieces[FIELDS.index(".involvedObject.kind")],
      subject_name: pieces[FIELDS.index(".involvedObject.name")],
      count: count,
      last_timestamp: timestamp,
      reason: pieces[FIELDS.index(".reason")],
      message: pieces[FIELDS.index(".message")]
    )
  end
end

.go_template_for(kind, name) ⇒ Object



406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# File 'lib/krane/kubernetes_resource.rb', line 406

def self.go_template_for(kind, name)
  and_conditions = [
    %[(eq .involvedObject.kind "#{kind}")],
    %[(eq .involvedObject.name "#{name}")],
    '(ne .reason "Started")',
    '(ne .reason "Created")',
    '(ne .reason "SuccessfulCreate")',
    '(ne .reason "Scheduled")',
    '(ne .reason "Pulling")',
    '(ne .reason "Pulled")',
  ]
  condition_start = "{{if and #{and_conditions.join(' ')}}}"
  field_part = FIELDS.map { |f| "{{#{f}}}" }.join(%({{print "#{FIELD_SEPARATOR}"}}))
  %({{range .items}}#{condition_start}#{field_part}{{print "#{EVENT_SEPARATOR}"}}{{end}}{{end}})
end

Instance Method Details

#seen_since?(time) ⇒ Boolean

Returns:

  • (Boolean)


489
490
491
# File 'lib/krane/kubernetes_resource.rb', line 489

def seen_since?(time)
  time.to_i <= @last_timestamp.to_i
end

#to_sObject



493
494
495
# File 'lib/krane/kubernetes_resource.rb', line 493

def to_s
  "#{@reason}: #{@message} (#{@count} events)"
end