Sha256: b5d98bfd520426b7db0059b5c6bbcac8bc9a78489de9602f7682705f617241bf

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

require 'open3'

module K8sKit
  class Context
    attr_reader :namespace

    def initialize(namespace: nil)
      @namespace = namespace
    end

    def kubectl
      ns = namespace ? "-n #{namespace}" : ''
      "kubectl #{ns}"
    end

    def run(cmd, silent: true)
      cmd = "#{kubectl} #{cmd}"

      out = []
      Open3.popen2e(cmd) do |stdin, stdout_err, wait_thr|
        while line = stdout_err.gets
          puts line unless silent
          out << line
        end

        exit_status = wait_thr.value
        unless exit_status.success?
          abort "Failed: #{cmd}"
        end
      end

      out.join("\n").chomp
    end

    def pod(name)
      Pod.new(self, name)
    end

    def job(name)
      Job.new(self, name)
    end

    def wait_for_all_pods_ready(timeout: 300)
      t0 = Time.now

      print 'Waiting for all pods to be ready...'
      loop do
        statuses = run('get pods -o jsonpath="{.items.*.status.containerStatuses[*].ready}"')
        return unless statuses.include?('false')

        if t0 + timeout < Time.now
          puts ''
          raise StandardError, 'Timeout while waiting for pods to become ready'
        end

        print '.'
        sleep 2
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
k8s_kit-0.0.2 lib/k8s_kit/context.rb