Class: Stax::Helm::Cmd

Inherits:
Base
  • Object
show all
Defined in:
lib/stax/helm/cmd.rb,
lib/stax/helm/pod.rb,
lib/stax/helm/jobs.rb,
lib/stax/helm/stern.rb,
lib/stax/helm/runcmd.rb,
lib/stax/helm/ingress.rb,
lib/stax/helm/kubectl.rb,
lib/stax/helm/cronjobs.rb,
lib/stax/helm/deployment.rb

Instance Method Summary collapse

Instance Method Details

#containersObject



29
30
31
32
# File 'lib/stax/helm/pod.rb', line 29

def containers
  columns = 'NAME:.metadata.name,CONTAINERS:.spec.containers[*].name'
  kubectl_run(:get, :pods, '-o', "custom-columns=#{columns}", '-l', helm_selector)
end

#createObject



50
51
52
53
# File 'lib/stax/helm/cmd.rb', line 50

def create
  debug("Creating helm release #{helm_release_name}")
  helm_run(:install, helm_release_name, helm_dir, helm_update_args)
end

#cronjobsObject



7
8
9
# File 'lib/stax/helm/cronjobs.rb', line 7

def cronjobs
  kubectl_run(:get, :cronjobs, '-l', helm_selector)
end

#deleteObject



62
63
64
65
# File 'lib/stax/helm/cmd.rb', line 62

def delete
  debug("Deleting helm release #{helm_release_name}")
  helm_run(:delete, helm_release_name)
end

#deploymentsObject



28
29
30
# File 'lib/stax/helm/deployment.rb', line 28

def deployments
  kubectl_run(:get, :deployments, '-l', helm_selector)
end

#dnsObject



12
13
14
15
# File 'lib/stax/helm/ingress.rb', line 12

def dns
  jsonpath = '{.items[].metadata.annotations.external-dns\.alpha\.kubernetes\.io/hostname}' + "\n"
  kubectl_run(:get, :ingresses, "-o=jsonpath='#{jsonpath}'", '-l', helm_selector)
end

#exec(cmd = 'sh') ⇒ Object



42
43
44
45
# File 'lib/stax/helm/pod.rb', line 42

def exec(cmd = 'sh')
  pod = helm_ask_pod('choose a pod')
  kubectl_run(:exec, '-it', pod, '--', cmd)
end

#historyObject



83
84
85
# File 'lib/stax/helm/cmd.rb', line 83

def history
  helm_run(:history, helm_release_name)
end

#ingressesObject



7
8
9
# File 'lib/stax/helm/ingress.rb', line 7

def ingresses
  kubectl_run(:get, :ingresses, '-l', helm_selector)
end

#jobsObject



7
8
9
# File 'lib/stax/helm/jobs.rb', line 7

def jobs
  kubectl_run(:get, :jobs, '-l', helm_selector)
end

#logs(*args) ⇒ Object



35
36
37
38
39
# File 'lib/stax/helm/pod.rb', line 35

def logs(*args)
  trap('SIGINT', 'EXIT')    # clean exit with ctrl-c
  args = [ '--all-containers', '--prefix', '--follow' ] if args.empty? # helpful default args
  kubectl_run(:logs, '-l', helm_selector, *args)
end

#lsObject



93
94
95
# File 'lib/stax/helm/cmd.rb', line 93

def ls
  helm_run(:ls, "--filter '^#{helm_release_name}$'")
end

#podsObject



24
25
26
# File 'lib/stax/helm/pod.rb', line 24

def pods
  kubectl_run(:get, :pods, '-l', helm_selector)
end

#restartObject



33
34
35
36
37
# File 'lib/stax/helm/deployment.rb', line 33

def restart
  helm_ask_deployments('choose deployments').each do |deployment|
    kubectl_run(:rollout, :restart, :deployment, deployment)
  end
end

#rollback(revision = nil) ⇒ Object



88
89
90
# File 'lib/stax/helm/cmd.rb', line 88

def rollback(revision = nil)
  helm_run(:rollback, helm_release_name, revision)
end

#runcmd(*cmd) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/stax/helm/runcmd.rb', line 58

def runcmd(*cmd)
  ## use default if not set
  cmd = Array(helm_run_cmd) if cmd.empty?

  ## name of k8s Job to create, and basic template
  job = helm_run_job
  template = helm_run_template(job)

  ## get deployment and extract container spec
  deployment = kubectl_json(:get, :deployment, helm_run_deployment)
  spec = deployment.dig('spec', 'template', 'spec', 'containers').find do |c|
    c['name'] == helm_run_container
  end

  ## cleanup the container spec so we can use it in a Job
  spec.delete('livenessProbe')
  spec.delete('readinessProbe')
  spec.delete('startupProbe')
  spec.delete('volumeMounts')
  spec['name'] = 'run'
  spec['args'] = ['sleep', options[:sleep]]

  ## add container to Job template
  template[:spec][:template][:spec][:containers] = [ spec ]

  ## add ttl to Job so it will be cleaned up after Pod terminates
  template[:spec][:ttlSecondsAfterFinished] = options[:ttl]

  ## get service account and add to template
   = deployment.dig('spec', 'template', 'spec', 'serviceAccountName')
  template[:spec][:template][:spec][:serviceAccountName] =  if 

  ## create new unique Job based on the container spec
  debug("Creating job #{job}")
  Open3.popen2('kubectl create -f -') { |stdin, stdout, _|
    stdin.print(template.to_json)
    stdin.close
    puts stdout.gets
  }

  ## get name of the Pod created by the Job
  pod = kubectl_json(:get, :pod, '-l', "job-name=#{job}")['items'].first['metadata']['name']

  ## exec into the pod and run interactive command
  debug("Connecting to pod #{pod}")
  kubectl_run(:wait, '--for=condition=Ready', '--timeout=5m', :pod, pod)
  kubectl_run(:exec, '-it', pod, '--', *cmd)
rescue JSON::ParserError
  fail_task('cannot get kubernetes resource')
ensure
  ## delete Job
  kubectl_run(:delete, :job, job) unless options[:keep]
end

#scaleObject



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/stax/helm/deployment.rb', line 41

def scale
  if options[:replicas]
    deployments = helm_ask_deployments('choose deployments').join(' ')
    kubectl_run(:scale, :deployment, deployments, '--replicas', options[:replicas])
  else
    debug("Deployment replicas for #{helm_release_name}")
    deployments = kubectl_json(:get, :deployments, '-l', helm_selector)
    print_table deployments['items'].map { |i|
      [ i['metadata']['name'], i['status']['replicas'] || 0 ]
    }
  end
end

#servicesObject



28
29
30
# File 'lib/stax/helm/kubectl.rb', line 28

def services
  kubectl_run(:get, :services, '-l', helm_selector)
end

#statusObject



68
69
70
# File 'lib/stax/helm/cmd.rb', line 68

def status
  helm_run(:status, helm_release_name)
end

#stern(*args) ⇒ Object



18
19
20
21
# File 'lib/stax/helm/stern.rb', line 18

def stern(*args)
  trap('SIGINT', 'EXIT')    # clean exit with ctrl-c
  stern_run('-l', helm_selector, *args)
end

#templateObject



73
74
75
# File 'lib/stax/helm/cmd.rb', line 73

def template
  helm_run(:get, :all, helm_release_name)
end

#updateObject



56
57
58
59
# File 'lib/stax/helm/cmd.rb', line 56

def update
  debug("Updating helm release #{helm_release_name}")
  helm_run(:upgrade, '-i', helm_release_name, helm_dir, helm_update_args)
end

#valuesObject



78
79
80
# File 'lib/stax/helm/cmd.rb', line 78

def values
  helm_run(:get, :values, helm_release_name)
end