Class: Vidar::K8s::Container

Inherits:
Object
  • Object
show all
Defined in:
lib/vidar/k8s/container.rb

Constant Summary collapse

JOB_KIND =
"Job".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Container

Returns a new instance of Container.



10
11
12
13
14
15
16
17
18
# File 'lib/vidar/k8s/container.rb', line 10

def initialize(data)
  @data = data
  @state = data["state"] || {}
  @namespace = data["namespace"]
  @kind = data["kind"]
  @pod_name = data["pod_name"]
  @reason = data["reason"]
  @message = data["message"]
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



6
7
8
# File 'lib/vidar/k8s/container.rb', line 6

def data
  @data
end

#kindObject (readonly)

Returns the value of attribute kind.



6
7
8
# File 'lib/vidar/k8s/container.rb', line 6

def kind
  @kind
end

#messageObject (readonly)

Returns the value of attribute message.



6
7
8
# File 'lib/vidar/k8s/container.rb', line 6

def message
  @message
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



6
7
8
# File 'lib/vidar/k8s/container.rb', line 6

def namespace
  @namespace
end

#pod_nameObject (readonly)

Returns the value of attribute pod_name.



6
7
8
# File 'lib/vidar/k8s/container.rb', line 6

def pod_name
  @pod_name
end

#reasonObject (readonly)

Returns the value of attribute reason.



6
7
8
# File 'lib/vidar/k8s/container.rb', line 6

def reason
  @reason
end

#stateObject (readonly)

Returns the value of attribute state.



6
7
8
# File 'lib/vidar/k8s/container.rb', line 6

def state
  @state
end

Instance Method Details

#deployed?Boolean

Returns:

  • (Boolean)


24
25
26
27
28
# File 'lib/vidar/k8s/container.rb', line 24

def deployed?
  return terminated? if job?

  ready? && running?
end

#istio?Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/vidar/k8s/container.rb', line 114

def istio?
  name == "istio-proxy"
end

#job?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/vidar/k8s/container.rb', line 110

def job?
  kind == JOB_KIND
end

#nameObject



20
21
22
# File 'lib/vidar/k8s/container.rb', line 20

def name
  data["name"] || pod_name
end


40
41
42
# File 'lib/vidar/k8s/container.rb', line 40

def print
  puts to_text
end

#ready?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/vidar/k8s/container.rb', line 78

def ready?
  data["ready"]
end

#ready_and_running?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/vidar/k8s/container.rb', line 36

def ready_and_running?
  ready? && running?
end

#running?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/vidar/k8s/container.rb', line 82

def running?
  !running_started_at.nil?
end

#running_started_atObject



86
87
88
# File 'lib/vidar/k8s/container.rb', line 86

def running_started_at
  state.dig("running", "startedAt")
end

#success?Boolean

Returns:

  • (Boolean)


30
31
32
33
34
# File 'lib/vidar/k8s/container.rb', line 30

def success?
  return terminated_completed? if job?

  ready_and_running?
end

#terminated?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/vidar/k8s/container.rb', line 90

def terminated?
  !state["terminated"].nil?
end

#terminated_completed?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/vidar/k8s/container.rb', line 94

def terminated_completed?
  state.dig("terminated", "reason") == "Completed" || state.dig("terminated", "exitCode") == 0
end

#terminated_error?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/vidar/k8s/container.rb', line 102

def terminated_error?
  state.dig("terminated", "reason") == "Error" || state.dig("terminated", "exitCode")
end

#terminated_finished_atObject



98
99
100
# File 'lib/vidar/k8s/container.rb', line 98

def terminated_finished_at
  state.dig("terminated", "finishedAt")
end

#text_statusesObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/vidar/k8s/container.rb', line 52

def text_statuses
  if unschedulable?
    [ColorizedString["Unschedulable"].light_red, ColorizedString[message].light_red]
  elsif running?
    if job?
      [ColorizedString["Running"].light_yellow, "Started at: #{running_started_at}"]
    elsif ready?
      [ColorizedString["Ready & Running"].light_green, "Started at: #{running_started_at}"]
    else
      [ColorizedString["Not ready"].light_red, "Started at: #{running_started_at}"]
    end
  elsif terminated_completed?
    [ColorizedString["Terminated/Completed"].light_green, terminated_finished_at ? "Finished at: #{terminated_finished_at}" : ""]
  elsif terminated_error?
    [ColorizedString["Terminated/Error"].light_red, ""]
  elsif waiting?
    [ColorizedString["Waiting"].light_yellow, ""]
  else
    [ColorizedString[state.inspect].light_red, ""]
  end
end

#to_textObject



44
45
46
47
48
49
50
# File 'lib/vidar/k8s/container.rb', line 44

def to_text
  parts = []
  parts << namespace.to_s.ljust(20, " ")
  parts << name.to_s.ljust(35, " ")
  parts += text_statuses.map { |s| s.ljust(45, " ") }
  "| #{parts.join(' | ')} |"
end

#unschedulable?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/vidar/k8s/container.rb', line 106

def unschedulable?
  reason == "Unschedulable"
end

#waiting?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/vidar/k8s/container.rb', line 74

def waiting?
  state["waiting"]
end