Class: KubernetesCLI
- Inherits:
-
Object
- Object
- KubernetesCLI
- Defined in:
- lib/kubernetes-cli.rb,
lib/kubernetes-cli/version.rb
Defined Under Namespace
Classes: GetResourceError, InvalidResourceError, InvalidResourceUriError, KubernetesError
Constant Summary collapse
- STATUS_KEY =
:kubernetes_cli_last_status- VERSION =
'0.2.1'
Instance Attribute Summary collapse
-
#executable ⇒ Object
readonly
Returns the value of attribute executable.
-
#kubeconfig_path ⇒ Object
readonly
Returns the value of attribute kubeconfig_path.
Instance Method Summary collapse
- #after_execute(&block) ⇒ Object
- #annotate(type, namespace, name, annotations, overwrite: true) ⇒ Object
- #apply(res, dry_run: false) ⇒ Object
- #apply_uri(uri, dry_run: false) ⇒ Object
- #before_execute(&block) ⇒ Object
- #current_context ⇒ Object
- #exec_cmd(container_cmd, namespace, pod, tty = true) ⇒ Object
- #get_object(type, namespace, name = nil, match_labels = {}) ⇒ Object
- #get_objects(type, namespace, match_labels = {}) ⇒ Object
-
#initialize(kubeconfig_path, executable = KubectlRb.executable) ⇒ KubernetesCLI
constructor
A new instance of KubernetesCLI.
- #last_status ⇒ Object
- #logtail(namespace, selector, follow: true) ⇒ Object
- #run_cmd(cmd) ⇒ Object
Constructor Details
#initialize(kubeconfig_path, executable = KubectlRb.executable) ⇒ KubernetesCLI
Returns a new instance of KubernetesCLI.
21 22 23 24 25 26 |
# File 'lib/kubernetes-cli.rb', line 21 def initialize(kubeconfig_path, executable = KubectlRb.executable) @kubeconfig_path = kubeconfig_path @executable = executable @before_execute = [] @after_execute = [] end |
Instance Attribute Details
#executable ⇒ Object (readonly)
Returns the value of attribute executable.
19 20 21 |
# File 'lib/kubernetes-cli.rb', line 19 def executable @executable end |
#kubeconfig_path ⇒ Object (readonly)
Returns the value of attribute kubeconfig_path.
19 20 21 |
# File 'lib/kubernetes-cli.rb', line 19 def kubeconfig_path @kubeconfig_path end |
Instance Method Details
#after_execute(&block) ⇒ Object
32 33 34 |
# File 'lib/kubernetes-cli.rb', line 32 def after_execute(&block) @after_execute << block end |
#annotate(type, namespace, name, annotations, overwrite: true) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/kubernetes-cli.rb', line 127 def annotate(type, namespace, name, annotations, overwrite: true) cmd = [ executable, '--kubeconfig', kubeconfig_path, '-n', namespace, 'annotate' ] cmd << '--overwrite' if overwrite cmd += [type, name] annotations.each do |key, value| cmd << "'#{key}'='#{value}'" end systemm(cmd) unless last_status.success? raise KubernetesError, "could not annotate resource '#{name}': kubectl "\ "exited with status code #{last_status.exitstatus}" end end |
#apply(res, dry_run: false) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/kubernetes-cli.rb', line 52 def apply(res, dry_run: false) cmd = [executable, '--kubeconfig', kubeconfig_path, 'apply', '--validate'] cmd << '--dry-run=client' if dry_run cmd += ['-f', '-'] open3_w(env, cmd) do |stdin, _wait_thread| stdin.puts(res.to_resource.to_yaml) end unless last_status.success? err = InvalidResourceError.new("Could not apply #{res.kind_sym.to_s.humanize.downcase} "\ "'#{res..name}': kubectl exited with status code #{last_status.exitstatus}" ) err.resource = res raise err end end |
#apply_uri(uri, dry_run: false) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/kubernetes-cli.rb', line 71 def apply_uri(uri, dry_run: false) cmd = [executable, '--kubeconfig', kubeconfig_path, 'apply', '--validate'] cmd << '--dry-run=client' if dry_run cmd += ['-f', uri] systemm(cmd) unless last_status.success? err = InvalidResourceUriError.new("Could not apply #{uri}: "\ "kubectl exited with status code #{last_status.exitstatus}" ) err.resource_uri = uri raise err end end |
#before_execute(&block) ⇒ Object
28 29 30 |
# File 'lib/kubernetes-cli.rb', line 28 def before_execute(&block) @before_execute << block end |
#current_context ⇒ Object
158 159 160 161 |
# File 'lib/kubernetes-cli.rb', line 158 def current_context cmd = [executable, '--kubeconfig', kubeconfig_path, 'config', 'current-context'] backticks(cmd).strip end |
#exec_cmd(container_cmd, namespace, pod, tty = true) ⇒ Object
45 46 47 48 49 50 |
# File 'lib/kubernetes-cli.rb', line 45 def exec_cmd(container_cmd, namespace, pod, tty = true) cmd = [executable, '--kubeconfig', kubeconfig_path, '-n', namespace, 'exec'] cmd += ['-it'] if tty cmd += [pod, '--', *Array(container_cmd)] execc(cmd) end |
#get_object(type, namespace, name = nil, match_labels = {}) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/kubernetes-cli.rb', line 87 def get_object(type, namespace, name = nil, match_labels = {}) cmd = [executable, '--kubeconfig', kubeconfig_path, '-n', namespace] cmd += ['get', type, name] unless match_labels.empty? cmd += ['--selector', match_labels.map { |key, value| "#{key}=#{value}" }.join(',')] end cmd += ['-o', 'json'] result = backticks(cmd) unless last_status.success? raise GetResourceError, "couldn't get resources of type '#{type}' "\ "in namespace #{namespace}: kubectl exited with status code #{last_status.exitstatus}" end JSON.parse(result) end |
#get_objects(type, namespace, match_labels = {}) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/kubernetes-cli.rb', line 107 def get_objects(type, namespace, match_labels = {}) cmd = [executable, '--kubeconfig', kubeconfig_path, '-n', namespace] cmd += ['get', type] unless match_labels.empty? cmd += ['--selector', match_labels.map { |key, value| "#{key}=#{value}" }.join(',')] end cmd += ['-o', 'json'] result = backticks(cmd) unless last_status.success? raise GetResourceError, "couldn't get resources of type '#{type}' "\ "in namespace #{namespace}: kubectl exited with status code #{last_status.exitstatus}" end JSON.parse(result)['items'] end |
#last_status ⇒ Object
36 37 38 |
# File 'lib/kubernetes-cli.rb', line 36 def last_status Thread.current[STATUS_KEY] end |
#logtail(namespace, selector, follow: true) ⇒ Object
150 151 152 153 154 155 156 |
# File 'lib/kubernetes-cli.rb', line 150 def logtail(namespace, selector, follow: true) cmd = [executable, '--kubeconfig', kubeconfig_path, '-n', namespace, 'logs'] cmd << '-f' if follow cmd << '--selector' cmd << selector.map { |k, v| "#{k}=#{v}" }.join(',') execc(cmd) end |
#run_cmd(cmd) ⇒ Object
40 41 42 43 |
# File 'lib/kubernetes-cli.rb', line 40 def run_cmd(cmd) cmd = [executable, '--kubeconfig', kubeconfig_path, *Array(cmd)] execc(cmd) end |