sensu-plugins / sensu-plugins-kubernetes

Sensu plugins for Kubernetes
http://sensu-plugins.io
MIT License
28 stars 36 forks source link

check-kube-pods-running.rb - no implicit conversion of nil into String #68

Closed krmard closed 4 years ago

krmard commented 5 years ago

A master with multiple nodes. When any new pod cannot start right away and gets in a Pending state the check fails with following error:

Check failed to run: no implicit conversion of nil into String, ["/opt/sensu/embedded/lib/ruby/2.4.0/time.rb:367:in `_parse'", "/opt/sensu/embedded/lib/ruby/2.4.0/time.rb:367:in `parse'", "/opt/sensu/embedded/lib/ruby/gems/2.4.0/gems/sensu-plugins-kubernetes-3.3.0/bin/check-kube-pods-running.rb:120:in `block in run'", "/opt/sensu/embedded/lib/ruby/2.4.0/delegate.rb:341:in `each'", "/opt/sensu/embedded/lib/ruby/2.4.0/delegate.rb:341:in `block in delegating_block'", "/opt/sensu/embedded/lib/ruby/gems/2.4.0/gems/sensu-plugins-kubernetes-3.3.0/bin/check-kube-pods-running.rb:114:in `run'", "/opt/sensu/embedded/lib/ruby/gems/2.4.0/gems/sensu-plugin-1.4.5/lib/sensu-plugin/cli.rb:58:in `block in <class:CLI>'"]

The part which breaks is

    pods.each do |pod|
      next if pod.nil?
      next if should_exclude_namespace(pod.metadata.namespace)
      next if should_exclude_node(pod.spec.nodeName)
      next unless pods_list.include?(pod.metadata.name) || pods_list.include?('all')
      next unless pod.status.phase != 'Succeeded' && !pod.status.conditions.nil?
      pod_stamp = Time.parse(pod.status.startTime)
      runtime = (Time.now.utc - pod_stamp.utc).to_i
      next if runtime < config[:not_ready_time]
      failed_pods << pod.metadata.name unless ready? pod
    end

If the pod is in a Pending state it doesn't have a startTime.

Possible fix

Skip the check if pod.status.phase is Pending:

next unless pod.status.phase != 'Succeeded' && pod.status.phase != 'Pending' && !pod.status.conditions.nil?
caiconkhicon commented 5 years ago

I face the exact same issue. Applying the fix like the recommend above works. Please create a PR to fix it.

caiconkhicon commented 5 years ago

I have a better solution, that change line 120 from

pod_stamp = Time.parse(pod.status.startTime)

to

if pod.status.phase == 'Pending' pod_stamp = Time.parse(pod.metadata.creationTimestamp) else pod_stamp = Time.parse(pod.status.startTime) end

ttarczynski commented 4 years ago

I've seen the same exception when there are POD's in pending state. Tested the fix proposed above and it works well in my environment. Prepared a PR: #98

majormoses commented 4 years ago

this should be fixed in https://rubygems.org/gems/sensu-plugins-kubernetes/versions/4.0.1