class Kubectl

Handles running kubectl

Constants

CAT_MULTILINE
KUBECTL_OPTIONS

Attributes

kubectl_options[RW]
options[RW]

Public Instance Methods

auth_check(verb, resource:, resource_name: nil) click to toggle source
# File lib/kuberun/kubectl.rb, line 18
def auth_check(verb, resource:, resource_name: nil)
  cmd.run(kubectl_base_command("auth can-i #{verb}", resource: resource, resource_name: resource_name))
end
create(configuration:, options: nil) click to toggle source
# File lib/kuberun/kubectl.rb, line 29
def create(configuration:, options: nil)
  cmd.run(kubectl_base_input_command('create', configuration: configuration, options: options))
end
delete(resource:, resource_name:) click to toggle source
# File lib/kuberun/kubectl.rb, line 37
def delete(resource:, resource_name:)
  cmd.run(kubectl_base_command('delete', resource: resource, resource_name: resource_name))
end
exec(pod:, command:) click to toggle source
# File lib/kuberun/kubectl.rb, line 33
def exec(pod:, command:)
  Kubectl::Exec.new(self).exec(pod: pod, command: command)
end
get(resource:, resource_name: nil, options: nil) click to toggle source
# File lib/kuberun/kubectl.rb, line 22
def get(resource:, resource_name: nil, options: nil)
  options = "#{options} --output=json"
  parsed_json do
    cmd.run(kubectl_base_command('get', resource: resource, resource_name: resource_name, options: options)).out
  end
end
kubectl_base_command(verb, resource:, resource_name: nil, options: nil) click to toggle source
# File lib/kuberun/kubectl.rb, line 41
def kubectl_base_command(verb, resource:, resource_name: nil, options: nil)
  base = "#{kubectl} #{options} #{verb} #{resource}"
  base = "#{base}/#{resource_name}" if resource_name
  base
end
load_options(options) click to toggle source
# File lib/kuberun/kubectl.rb, line 13
def load_options(options)
  self.options = options
  self.kubectl_options = parsed_options(options)
end

Private Instance Methods

cmd(tty_options = {}) click to toggle source
# File lib/kuberun/kubectl.rb, line 51
def cmd(tty_options = {})
  tty_options[:printer] = :progress unless options['debug']
  TTY::Command.new(tty_options)
end
kubectl() click to toggle source
# File lib/kuberun/kubectl.rb, line 60
def kubectl
  "kubectl #{kubectl_options}"
end
kubectl_base_input_command(verb, configuration:, options:) click to toggle source
# File lib/kuberun/kubectl.rb, line 56
def kubectl_base_input_command(verb, configuration:, options:)
  "cat << '#{CAT_MULTILINE}' | #{kubectl} #{options} #{verb} -f - 2>&1\n#{configuration.to_json}\n#{CAT_MULTILINE}"
end
parsed_json() { || ... } click to toggle source
# File lib/kuberun/kubectl.rb, line 72
def parsed_json
  JSON.parse(yield)
end
parsed_options(options) click to toggle source
# File lib/kuberun/kubectl.rb, line 64
def parsed_options(options)
  options.each_with_object([]) do |(option_name, option_value), arr|
    next if !KUBECTL_OPTIONS.include?(option_name) || option_value == ''

    arr << "--#{option_name}=#{option_value}"
  end.join(' ')
end