Sha256: 3d8300172d2ddf5f31a5cb6dfcc6af934958bb1e80413ffef36ea86e76fb5b20

Contents?: true

Size: 1 KB

Versions: 5

Compression:

Stored size: 1 KB

Contents

class Kubes::CLI
  class Exec < Base
    include Kubes::Util::Sh

    def run
      compile
      metadata = Kubes::Kubectl::Fetch::Deployment.new(@options).metadata

      labels = metadata['labels'].map { |k,v| "#{k}=#{v}" }.join(',')
      ns = metadata['namespace']

      resp = capture("kubectl get pod -l #{labels} -n #{ns} -o json")
      data = JSON.load(resp)
      pod = latest_pod(data['items'])

      unless pod
        logger.error "ERROR: Unable to find a running pod".color(:red)
        exit 1
      end

      name = pod['metadata']['name']
      container = " -c #{@options[:container]}" unless @options[:container].nil?
      cmd = @options[:cmd].empty? ? "bash" : @options[:cmd].join(' ')
      sh("kubectl exec -n #{ns} -ti #{name}#{container} -- #{cmd}")
    end

    # get latest running pod
    def latest_pod(items)
      running = items.select { |i| i['status']['phase'] == 'Running' }
      sorted = running.sort_by { |i| i['metadata']['creationTimestamp'] || 0 }
      sorted.last
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
kubes-0.3.5 lib/kubes/cli/exec.rb
kubes-0.3.4 lib/kubes/cli/exec.rb
kubes-0.3.3 lib/kubes/cli/exec.rb
kubes-0.3.2 lib/kubes/cli/exec.rb
kubes-0.3.1 lib/kubes/cli/exec.rb