Class: Vidar::K8s::Container

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

Overview

Represents a single Kubernetes container and its current state.

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.



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

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.



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

def data
  @data
end

#kindObject (readonly)

Returns the value of attribute kind.



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

def kind
  @kind
end

#messageObject (readonly)

Returns the value of attribute message.



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

def message
  @message
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



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

def namespace
  @namespace
end

#pod_nameObject (readonly)

Returns the value of attribute pod_name.



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

def pod_name
  @pod_name
end

#reasonObject (readonly)

Returns the value of attribute reason.



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

def reason
  @reason
end

#stateObject (readonly)

Returns the value of attribute state.



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

def state
  @state
end

Instance Method Details

#deployed?Boolean

Returns true if the container is considered successfully deployed.

Returns:

  • (Boolean)

    true if the container is considered successfully deployed



27
28
29
30
31
# File 'lib/vidar/k8s/container.rb', line 27

def deployed?
  return terminated? if job?

  ready? && running?
end

#istio?Boolean

Returns true if this is an istio-proxy sidecar container.

Returns:

  • (Boolean)

    true if this is an istio-proxy sidecar container



141
142
143
# File 'lib/vidar/k8s/container.rb', line 141

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

#job?Boolean

Returns true if this container belongs to a Job.

Returns:

  • (Boolean)

    true if this container belongs to a Job



130
131
132
# File 'lib/vidar/k8s/container.rb', line 130

def job?
  kind == JOB_KIND
end

#nameString

Returns container name, falling back to pod name.

Returns:

  • (String)

    container name, falling back to pod name



22
23
24
# File 'lib/vidar/k8s/container.rb', line 22

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


45
46
47
# File 'lib/vidar/k8s/container.rb', line 45

def print
  puts to_text
end

#ready?Boolean

Returns true if container is ready.

Returns:

  • (Boolean)

    true if container is ready



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

def ready?
  data["ready"]
end

#ready_and_running?Boolean

Returns true if the container is ready and running.

Returns:

  • (Boolean)

    true if the container is ready and running



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

def ready_and_running?
  ready? && running?
end

#running?Boolean

Returns true if container is running.

Returns:

  • (Boolean)

    true if container is running



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

def running?
  !running_started_at.nil?
end

#running_started_atObject



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

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

#sidecar?(sidecar_names = ["istio-proxy"]) ⇒ Boolean

Returns true if this container is a known sidecar.

Parameters:

  • sidecar_names (Array<String>) (defaults to: ["istio-proxy"])

    list of sidecar container names to match

Returns:

  • (Boolean)

    true if this container is a known sidecar



136
137
138
# File 'lib/vidar/k8s/container.rb', line 136

def sidecar?(sidecar_names = ["istio-proxy"])
  sidecar_names.include?(name.to_s)
end

#success?Boolean

Returns true if the container completed successfully.

Returns:

  • (Boolean)

    true if the container completed successfully



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

def success?
  return terminated_completed? if job?

  ready_and_running?
end

#terminated?Boolean

Returns true if container has terminated (any reason).

Returns:

  • (Boolean)

    true if container has terminated (any reason)



100
101
102
# File 'lib/vidar/k8s/container.rb', line 100

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

#terminated_completed?Boolean

Returns true if container terminated successfully.

Returns:

  • (Boolean)

    true if container terminated successfully



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

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

#terminated_error?Boolean

Returns true if container terminated with an error exit code.

Returns:

  • (Boolean)

    true if container terminated with an error exit code



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

def terminated_error?
  exit_code = state.dig("terminated", "exitCode")
  state.dig("terminated", "reason") == "Error" || (!exit_code.nil? && exit_code != 0)
end

#terminated_finished_atObject



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

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

#text_statusesArray<String>

Returns two-element array with status label and detail.

Returns:

  • (Array<String>)

    two-element array with status label and detail



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/vidar/k8s/container.rb', line 58

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["Unknown"].light_yellow, state.empty? ? "" : state.inspect]
  end
end

#to_textObject



49
50
51
52
53
54
55
# File 'lib/vidar/k8s/container.rb', line 49

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

#unknown?Boolean

Returns true if container state is unknown.

Returns:

  • (Boolean)

    true if container state is unknown



120
121
122
# File 'lib/vidar/k8s/container.rb', line 120

def unknown?
  !unschedulable? && !running? && !terminated? && !waiting?
end

#unschedulable?Boolean

Returns true if container reason is Unschedulable.

Returns:

  • (Boolean)

    true if container reason is Unschedulable



125
126
127
# File 'lib/vidar/k8s/container.rb', line 125

def unschedulable?
  reason == "Unschedulable"
end

#waiting?Boolean

Returns true if container state is “waiting”.

Returns:

  • (Boolean)

    true if container state is “waiting”



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

def waiting?
  state["waiting"]
end