class Krane::KubernetesResource::Event

Constants

EVENT_SEPARATOR
FIELDS
FIELD_EMPTY_VALUE
FIELD_SEPARATOR

Public Class Methods

extract_all_from_go_template_blob(blob) click to toggle source
# 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) click to toggle source
# 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
new(subject_kind:, last_timestamp:, reason:, message:, count:, subject_name:) click to toggle source
# 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

Private Class Methods

extract_event_count(pieces) click to toggle source
# File lib/krane/kubernetes_resource.rb, line 439
def self.extract_event_count(pieces)
  series = pieces[FIELDS.index(".series")]
  count = pieces[FIELDS.index(".count")]
  deprecated_count = pieces[FIELDS.index(".deprecatedCount")]

  # Find the right event count according to Kubernetes API and kubectl version
  if count.present? && count != FIELD_EMPTY_VALUE
    count # This is the default field, so let's try to use it first
  elsif series.present? && series != FIELD_EMPTY_VALUE
    # kubectl 1.16 uses Events/v1, which has the .series/.count field
    count_regex = /count:(?<value>\S+?(?=\s))/
    count_regex.match(series)['value']
  elsif deprecated_count.present? && deprecated_count != FIELD_EMPTY_VALUE
    # kubectl < 1.16 uses events.k8s.io/v1beta1, which has .deprecatedCount
    deprecated_count
  else
    "1" # Fallback to 1 when all count fields are null
  end
end
extract_event_timestamp(pieces) click to toggle source
# File lib/krane/kubernetes_resource.rb, line 459
def self.extract_event_timestamp(pieces)
  series = pieces[FIELDS.index(".series")]
  last_timestamp = pieces[FIELDS.index(".lastTimestamp")]
  deprecated_timestamp = pieces[FIELDS.index(".deprecatedLastTimestamp")]

  # Find the right event timestamp according to Kubernetes API and kubectl version
  if last_timestamp.present? && last_timestamp != FIELD_EMPTY_VALUE
    last_timestamp # kubernetes 1.16 also exposes .last_timestamp field, so let's support it
  elsif series.present? && series != FIELD_EMPTY_VALUE
    # kubectl 1.16 uses Events/v1, which has the .series/.lastObservedTime field
    timestamp_regex = /lastObservedTime:(?<value>\S+?(?=\]))/
    timestamp_regex.match(series)['value']
  elsif deprecated_timestamp.present? && deprecated_timestamp != FIELD_EMPTY_VALUE
    # kubectl < 1.16 uses events.k8s.io/v1beta1, which has .deprecatedLastTimestamp
    deprecated_timestamp
  else
    pieces[FIELDS.index(".eventTime")] # Fallback to eventTime when other timestamp fields are null
  end
end

Public Instance Methods

seen_since?(time) click to toggle source
# File lib/krane/kubernetes_resource.rb, line 489
def seen_since?(time)
  time.to_i <= @last_timestamp.to_i
end
to_s() click to toggle source
# File lib/krane/kubernetes_resource.rb, line 493
def to_s
  "#{@reason}: #{@message} (#{@count} events)"
end