Sha256: e3a45292e8f7a8841c30af3a6512e0d4c0c77283e7c374ae3e2a9bdbe566ac79

Contents?: true

Size: 1.7 KB

Versions: 37

Compression:

Stored size: 1.7 KB

Contents

class Kubes::CLI
  class Exec < Base
    extend Memoist
    include Kubes::Logging
    include Kubes::Util::Sh

    def run
      compile
      pod = find_pod

      unless pod
        logger.info <<~EOL
          Unable to find a pod to exec into. This means there was no deployment found.
          You can also try using the -p option and specifying enough of the pod name. Example:

              kubes exec -p web

        EOL
        exit 1
      end

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

    def default_exec
      ENV['KUBES_DEFAULT_EXEC'] || "sh"
    end

    def find_pod
      pod_name || deployment_pod
    end

    def ns
      "-n #{metadata['namespace']}" if metadata
    end

    def metadata
      deployment = Kubes::Kubectl::Fetch::Deployment.new(@options)
      deployment.metadata if deployment.found
    end
    memoize :metadata

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

      resp = sh_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

      pod['metadata']['name']
    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

37 entries across 37 versions & 1 rubygems

Version Path
kubes-0.9.3 lib/kubes/cli/exec.rb
kubes-0.9.2 lib/kubes/cli/exec.rb
kubes-0.9.1 lib/kubes/cli/exec.rb
kubes-0.9.0 lib/kubes/cli/exec.rb
kubes-0.8.10 lib/kubes/cli/exec.rb
kubes-0.8.9 lib/kubes/cli/exec.rb
kubes-0.8.8 lib/kubes/cli/exec.rb
kubes-0.8.7 lib/kubes/cli/exec.rb
kubes-0.8.6 lib/kubes/cli/exec.rb
kubes-0.8.5 lib/kubes/cli/exec.rb
kubes-0.8.4 lib/kubes/cli/exec.rb
kubes-0.8.3 lib/kubes/cli/exec.rb
kubes-0.8.2 lib/kubes/cli/exec.rb
kubes-0.8.1 lib/kubes/cli/exec.rb
kubes-0.8.0 lib/kubes/cli/exec.rb
kubes-0.7.10 lib/kubes/cli/exec.rb
kubes-0.7.9 lib/kubes/cli/exec.rb
kubes-0.7.8 lib/kubes/cli/exec.rb
kubes-0.7.7 lib/kubes/cli/exec.rb
kubes-0.7.6 lib/kubes/cli/exec.rb