Class: Krane::Pod::Container

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definition, init_container: false) ⇒ Container

Returns a new instance of Container.



210
211
212
213
214
215
216
217
# File 'lib/krane/kubernetes_resource/pod.rb', line 210

def initialize(definition, init_container: false)
  @init_container = init_container
  @name = definition["name"]
  @image = definition["image"]
  @http_probe_location = definition.dig("readinessProbe", "httpGet", "path")
  @exec_probe_command = definition.dig("readinessProbe", "exec", "command")
  @status = {}
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



208
209
210
# File 'lib/krane/kubernetes_resource/pod.rb', line 208

def name
  @name
end

Instance Method Details

#doom_reasonObject



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/krane/kubernetes_resource/pod.rb', line 223

def doom_reason
  limbo_reason = @status.dig("state", "waiting", "reason")
  limbo_message = @status.dig("state", "waiting", "message")

  if limbo_reason == "CrashLoopBackOff"
    exit_code = @status.dig('lastState', 'terminated', 'exitCode')
    "Crashing repeatedly (exit #{exit_code}). See logs for more information."
  elsif limbo_reason == "ErrImagePull" && limbo_message.match(/not found/i)
    "Failed to pull image #{@image}. "\
    "Did you wait for it to be built and pushed to the registry before deploying?"
    # Only fail fast when message doesn't include `failed to sync %s cache`.
    # It's possible that a secret/configmap is still trying to be mounted to the pod, it seems related
    # to too many pods referencing the same secret/configmap: https://github.com/kubernetes/kubernetes/pull/74755
    # Error message format source: https://github.com/kubernetes/kubernetes/pull/75260
  elsif limbo_reason == "CreateContainerConfigError" && !limbo_message.match("failed to sync (.*?) cache")
    "Failed to generate container configuration: #{limbo_message}"
  elsif @status.dig("lastState", "terminated", "reason") == "ContainerCannotRun"
    # ref: https://github.com/kubernetes/kubernetes/blob/562e721ece8a16e05c7e7d6bdd6334c910733ab2/pkg/kubelet/dockershim/docker_container.go#L353
    exit_code = @status.dig('lastState', 'terminated', 'exitCode')
    # We've observed failures here that are actually issues with the node or kube infra, and not with the
    # container. These issues have been transient and result in a 128 exit code, so do not treat these as fatal.
    return if exit_code == 128
    "Failed to start (exit #{exit_code}): #{@status.dig('lastState', 'terminated', 'message')}"
  elsif @status.dig("state", "terminated", "reason") == "ContainerCannotRun"
    exit_code = @status.dig('state', 'terminated', 'exitCode')
    return if exit_code == 128
    "Failed to start (exit #{exit_code}): #{@status.dig('state', 'terminated', 'message')}"
  end
end

#doomed?Boolean

Returns:

  • (Boolean)


219
220
221
# File 'lib/krane/kubernetes_resource/pod.rb', line 219

def doomed?
  doom_reason.present?
end

#init_container?Boolean

Returns:

  • (Boolean)


269
270
271
# File 'lib/krane/kubernetes_resource/pod.rb', line 269

def init_container?
  @init_container
end

#readiness_fail_reasonObject



253
254
255
256
257
258
259
260
261
262
263
# File 'lib/krane/kubernetes_resource/pod.rb', line 253

def readiness_fail_reason
  return if ready? || init_container?
  return unless (@http_probe_location || @exec_probe_command).present?

  yellow_name = ColorizedString.new(name).yellow
  if @http_probe_location
    "> #{yellow_name} must respond with a good status code at '#{@http_probe_location}'"
  elsif @exec_probe_command
    "> #{yellow_name} must exit 0 from the following command: '#{@exec_probe_command.join(' ')}'"
  end
end

#ready?Boolean

Returns:

  • (Boolean)


265
266
267
# File 'lib/krane/kubernetes_resource/pod.rb', line 265

def ready?
  @status['ready'] == true
end

#reset_statusObject



277
278
279
# File 'lib/krane/kubernetes_resource/pod.rb', line 277

def reset_status
  @status = {}
end

#update_status(data) ⇒ Object



273
274
275
# File 'lib/krane/kubernetes_resource/pod.rb', line 273

def update_status(data)
  @status = data || {}
end