Class: Kuberun::Commands::RunPod

Inherits:
Kuberun::Command show all
Defined in:
lib/kuberun/commands/run_pod.rb

Constant Summary collapse

NEW_POD =
'Start new one'

Instance Method Summary collapse

Methods inherited from Kuberun::Command

#command, #cursor, #editor, #exec_exist?, #generator, #pager, #platform, #screen, #which

Constructor Details

#initialize(deployment_name, options) ⇒ RunPod

Returns a new instance of RunPod.



10
11
12
13
14
# File 'lib/kuberun/commands/run_pod.rb', line 10

def initialize(deployment_name, options)
  @deployment_name = deployment_name
  @options = options
  super(options)
end

Instance Method Details

#execute(input: $stdin, output: $stdout) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/kuberun/commands/run_pod.rb', line 16

def execute(input: $stdin, output: $stdout)
  if @options['perform-auth-check']
    output.print(Kuberun::Pastel.yellow('Checking access to needed commands'))
    Kuberun::Kubectl.auth_check('create', resource: 'pods')
    Kuberun::Kubectl.auth_check('exec', resource: 'pods')

    output.puts
    output.puts(Kuberun::Pastel.green('You have all permissions needed'))
  end

  output.print(Kuberun::Pastel.yellow('Searching for existing pods'))
  existing_pods = Kuberun::Kubectl.get(resource: 'pods', options: "-l kuberun-provisioned=true,kuberun-source=#{@deployment_name}")
  if existing_pods['items'].size > 0
    if @options['use-first-pod']
      @generated_pod_name = existing_pods['items'].first.dig('metadata', 'name')
    else
      select = existing_pods['items'].map { |item| item.dig('metadata', 'name') }
      select << NEW_POD

      output.puts
      output.puts
      selection = prompt.select('I found some already running pods. Do you want to use one?', select)

      if selection == NEW_POD
        create_pod_from_deployment(output)
      else
        @generated_pod_name = selection
      end
    end
  else
    create_pod_from_deployment(output)
  end

  execute_command(input, output)

  if @options['cleanup-pod'] || prompt.yes?(Kuberun::Pastel.yellow('Should I delete pod?'))
    Kuberun::Kubectl.delete(resource: 'pod', resource_name: generated_pod_name)
    Kuberun::Pastel.green("Pod #{generated_pod_name} has been deleted!")
  end

  output.puts
end