Class: Rhelm::Subcommand::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/rhelm/subcommand/base.rb

Overview

Abstract base class for Helm subcommands.

Defined Under Namespace

Classes: OptionError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Base

Returns a new instance of Base.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rhelm/subcommand/base.rb', line 22

def initialize(options = {})
  @client = options.delete(:client) || Client.new
  @debug = options[:debug]
  @kube_apiserver = options[:kube_apiserver]
  @kube_as_group = options[:kube_as_group]
  @kube_as_user = options[:kube_as_user]
  @kube_context = options[:kube_context]
  @kube_token = options[:kube_token]
  @kubeconfig = options[:kubeconfig]
  @namespace = options[:namespace]
  @registry_config = options[:registry_config]
  @repository_cache = options[:repository_cache]
  @repository_config = options[:repository_config]
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



9
10
11
# File 'lib/rhelm/subcommand/base.rb', line 9

def client
  @client
end

#debugObject (readonly)

Returns the value of attribute debug.



9
10
11
# File 'lib/rhelm/subcommand/base.rb', line 9

def debug
  @debug
end

#kube_apiserverObject (readonly)

Returns the value of attribute kube_apiserver.



9
10
11
# File 'lib/rhelm/subcommand/base.rb', line 9

def kube_apiserver
  @kube_apiserver
end

#kube_as_groupObject (readonly)

Returns the value of attribute kube_as_group.



9
10
11
# File 'lib/rhelm/subcommand/base.rb', line 9

def kube_as_group
  @kube_as_group
end

#kube_as_userObject (readonly)

Returns the value of attribute kube_as_user.



9
10
11
# File 'lib/rhelm/subcommand/base.rb', line 9

def kube_as_user
  @kube_as_user
end

#kube_contextObject (readonly)

Returns the value of attribute kube_context.



9
10
11
# File 'lib/rhelm/subcommand/base.rb', line 9

def kube_context
  @kube_context
end

#kube_tokenObject (readonly)

Returns the value of attribute kube_token.



9
10
11
# File 'lib/rhelm/subcommand/base.rb', line 9

def kube_token
  @kube_token
end

#kubeconfigObject (readonly)

Returns the value of attribute kubeconfig.



9
10
11
# File 'lib/rhelm/subcommand/base.rb', line 9

def kubeconfig
  @kubeconfig
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



9
10
11
# File 'lib/rhelm/subcommand/base.rb', line 9

def namespace
  @namespace
end

#registry_configObject (readonly)

Returns the value of attribute registry_config.



9
10
11
# File 'lib/rhelm/subcommand/base.rb', line 9

def registry_config
  @registry_config
end

#repository_cacheObject (readonly)

Returns the value of attribute repository_cache.



9
10
11
# File 'lib/rhelm/subcommand/base.rb', line 9

def repository_cache
  @repository_cache
end

#repository_configObject (readonly)

Returns the value of attribute repository_config.



9
10
11
# File 'lib/rhelm/subcommand/base.rb', line 9

def repository_config
  @repository_config
end

Instance Method Details

#argsObject



61
62
63
# File 'lib/rhelm/subcommand/base.rb', line 61

def args
  [subcommand_name, cli_args].flatten
end

#cli_argsObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rhelm/subcommand/base.rb', line 46

def cli_args
  [].tap do |args|
    args << "--debug" if debug
    args << ["--kube-apiserver", kube_apiserver] if kube_apiserver
    args << ["--kube-as-group", kube_as_group ] if kube_as_group
    args << ["--kube-context", kube_context] if kube_context
    args << ["--kube-token", kube_token] if kube_token
    args << ["--kubeconfig", kubeconfig ] if kubeconfig
    args << ["--namespace", namespace] if namespace
    args << ["--registry-config", registry_config] if registry_config
    args << ["--repository-cache", repository_cache] if repository_cache
    args << ["--repository-config", repository_config] if repository_config
  end.flatten
end

#full_cli_callObject



65
66
67
# File 'lib/rhelm/subcommand/base.rb', line 65

def full_cli_call
  [@client.program, args].flatten.map(&:to_s)
end

#report_failure(lines, status, and_raise: true) ⇒ Object

Raises:



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rhelm/subcommand/base.rb', line 69

def report_failure(lines, status, and_raise: true)
  sanitized_full_cli_call = full_cli_call
  kube_token_index = sanitized_full_cli_call.find_index("--kube-token")
  sanitized_full_cli_call[kube_token_index + 1] = "[REDACTED]" if kube_token_index
  preamble = "#{sanitized_full_cli_call} failed with exit status #{status}. Output follows:"
  if @client.logger
    client.logger.error(preamble)
    client.logger.error(lines)
  else
    STDERR.puts premable
    STDERR.puts lines
  end
  raise(Error, "#{sanitized_full_cli_call} failed with exit_status #{status}") if and_raise
end

#run(client: nil, raise_on_error: true, &block) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/rhelm/subcommand/base.rb', line 37

def run(client: nil, raise_on_error: true, &block)
  b = block_given? ? block : Proc.new { |_lines, status| status }

  lines, status = Open3.capture2e(*full_cli_call)
  report_failure(lines, status.exitstatus) if raise_on_error && status.exitstatus != 0

  b.yield lines, status.exitstatus
end