Class: Kubes::Kubectl

Inherits:
Object
  • Object
show all
Extended by:
Util::Sh, Memoist
Includes:
Hooks::Concern, Util::Sh
Defined in:
lib/kubes/kubectl.rb,
lib/kubes/kubectl/batch.rb,
lib/kubes/kubectl/ordering.rb,
lib/kubes/kubectl/kustomize.rb,
lib/kubes/kubectl/dispatcher.rb

Defined Under Namespace

Modules: Args, Fetch, Ordering Classes: Batch, Dispatcher, Kustomize

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util::Sh

sh, sh_capture

Methods included from Logging

#logger

Methods included from Hooks::Concern

#run_hooks

Constructor Details

#initialize(name, options = {}) ⇒ Kubectl

Returns a new instance of Kubectl.



7
8
9
# File 'lib/kubes/kubectl.rb', line 7

def initialize(name, options={})
  @name, @options = name, options
end

Class Method Details

.capture(args, options = {}) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/kubes/kubectl.rb', line 73

def capture(args, options={})
  resp = sh_capture("kubectl #{args}", options)
  if args.include?('-o json')
    JSON.load(resp) # data
  else
    resp
  end
end

.execute(args, options = {}) ⇒ Object



69
70
71
# File 'lib/kubes/kubectl.rb', line 69

def execute(args, options={})
  sh("kubectl #{args}", options)
end

.run(name, options = {}) ⇒ Object



65
66
67
# File 'lib/kubes/kubectl.rb', line 65

def run(name, options={})
  new(name, options).run
end

Instance Method Details

#argsObject



45
46
47
48
# File 'lib/kubes/kubectl.rb', line 45

def args
  # base at end in case of redirection. IE: command > /path
  custom.args + default.args
end

#customObject



50
51
52
53
54
# File 'lib/kubes/kubectl.rb', line 50

def custom
  custom = Kubes::Args::Custom.new(@name, "#{Kubes.root}/.kubes/config/kubectl/args.rb")
  custom.build
  custom
end

#defaultObject



57
58
59
60
# File 'lib/kubes/kubectl.rb', line 57

def default
  klass = Kubes.kustomize? ? Args::Kustomize : Args::Standard
  klass.new(@name, @options)
end

#exit_on_failObject



40
41
42
43
# File 'lib/kubes/kubectl.rb', line 40

def exit_on_fail
  return false if ENV['KUBES_EXIT_ON_FAIL'] == '0'
  Kubes.config.kubectl.exit_on_fail[@name]
end

#runObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/kubes/kubectl.rb', line 11

def run
  validate!

  options = @options.dup
  options[:exit_on_fail] = exit_on_fail unless exit_on_fail.nil?

  params = args.flatten.join(' ')
  args = "#{@name} #{params}" # @name: apply or delete

  run_hooks("kubectl.rb", name: @name, file: @options[:file]) do
    if options[:capture]
      self.class.capture(args, options) # already includes kubectl
    else
      self.class.execute(args, options)
    end
  end
end

#validate!Object

Useful for kustomize mode



30
31
32
33
34
35
36
37
38
# File 'lib/kubes/kubectl.rb', line 30

def validate!
  return true unless Kubes.kustomize?

  unless @options[:role]
    logger.error "Missing argument: A folder must be provided when using kustomization.yaml files".color(:red)
    logger.info "Please provide a folder"
    exit 1
  end
end