Class: KubernetesCLI
- Inherits:
-
Object
- Object
- KubernetesCLI
- Defined in:
- lib/kubernetes-cli/version.rb,
lib/kubernetes-cli.rb
Overview
typed: ignore
Defined Under Namespace
Classes: AnnotateResourceError, DeleteResourceError, GetResourceError, GetVersionError, InvalidResourceError, InvalidResourceUriError, KubernetesError, PatchResourceError
Constant Summary collapse
- STATUS_KEY =
:kubernetes_cli_last_status- STDOUT_KEY =
:kubernetes_cli_stdout- STDERR_KEY =
:kubernetes_cli_stderr- VERSION =
'0.4.0'.freeze
Instance Attribute Summary collapse
-
#executable ⇒ Object
readonly
T::Sig::WithoutRuntime.sig { returns(String) }.
-
#kubeconfig_path ⇒ Object
readonly
T::Sig::WithoutRuntime.sig { returns(String) }.
Instance Method Summary collapse
-
#after_execute(&block) ⇒ Object
T::Sig::WithoutRuntime.sig { params(block: AfterCallback).void }.
-
#annotate(type, namespace, name, annotations, overwrite: true) ⇒ Object
T::Sig::WithoutRuntime.sig { params( type: String, namespace: String, name: String, annotations: T::Hash[String, String], overwrite: T::Boolean ).void }.
-
#api_resources ⇒ Object
T::Sig::WithoutRuntime.sig { returns(String) }.
-
#apply(res, dry_run: false) ⇒ Object
T::Sig::WithoutRuntime.sig { params(res: ::KubeDSL::DSLObject, dry_run: T::Boolean).void }.
-
#apply_uri(uri, dry_run: false) ⇒ Object
T::Sig::WithoutRuntime.sig { params(uri: String, dry_run: T::Boolean).void }.
-
#before_execute(&block) ⇒ Object
T::Sig::WithoutRuntime.sig { params(block: BeforeCallback).void }.
-
#current_context ⇒ Object
T::Sig::WithoutRuntime.sig { returns(String) }.
-
#delete_object(type, namespace, name) ⇒ Object
T::Sig::WithoutRuntime.sig { params( type: String, namespace: String, name: String ).void }.
-
#delete_objects(type, namespace, match_labels = {}) ⇒ Object
T::Sig::WithoutRuntime.sig { params( type: String, namespace: T.any(String, Symbol), match_labels: T::Hash[String, String] ).void }.
-
#exec_cmd(container_cmd, namespace, pod, tty = true, container = nil, out_file = nil) ⇒ Object
T::Sig::WithoutRuntime.sig { params( container_cmd: T.any(String, T::Array), namespace: String, pod: String, tty: T::Boolean, container: T.nilable(String), out_file: T.nilable(String) ).void }.
-
#get_object(type, namespace, name) ⇒ Object
T::Sig::WithoutRuntime.sig { params( type: String, namespace: String, name: String ).returns( T::Hash[String, T.untyped] ) }.
-
#get_objects(type, namespace, match_labels = {}) ⇒ Object
T::Sig::WithoutRuntime.sig { params( type: String, namespace: T.any(String, Symbol), match_labels: T::Hash[String, String] ).returns( T::Array ) }.
-
#initialize(kubeconfig_path, executable = KubectlRb.executable) ⇒ KubernetesCLI
constructor
T::Sig::WithoutRuntime.sig { params(kubeconfig_path: String, executable: String).void }.
-
#last_status ⇒ Object
T::Sig::WithoutRuntime.sig { returns(T.nilable(Process::Status)) }.
-
#logtail(namespace, selector, follow: true) ⇒ Object
T::Sig::WithoutRuntime.sig { params( namespace: String, selector: T::Hash[String, String], follow: T::Boolean ).void }.
-
#on_last_status_failure(&block) ⇒ Object
T::Sig::WithoutRuntime.sig { params(block: T.proc.params(last_status: Process::Status).void).void }.
-
#patch_object(type, namespace, name, patch_data, patch_type = 'merge') ⇒ Object
T::Sig::WithoutRuntime.sig { params( type: String, namespace: String, name: String, patch_data: String, patch_type: String ).void }.
-
#restart_deployment(namespace, deployment) ⇒ Object
T::Sig::WithoutRuntime.sig { params(namespace: String, deployment: String).void }.
-
#run_cmd(cmd) ⇒ Object
T::Sig::WithoutRuntime.sig { params(cmd: T.any(String, T::Array)).void }.
-
#stderr ⇒ Object
T::Sig::WithoutRuntime.sig { returns(T.any(StringIO, IO)) }.
-
#stderr=(new_stderr) ⇒ Object
T::Sig::WithoutRuntime.sig { params(new_stderr: T.nilable(T.any(StringIO, IO))).void }.
-
#stdout ⇒ Object
T::Sig::WithoutRuntime.sig { returns(T.any(StringIO, IO)) }.
-
#stdout=(new_stdout) ⇒ Object
T::Sig::WithoutRuntime.sig { params(new_stdout: T.nilable(T.any(StringIO, IO))).void }.
-
#system_cmd(container_cmd, namespace, pod, tty = true, container = nil) ⇒ Object
T::Sig::WithoutRuntime.sig { params( container_cmd: T.any(String, T::Array), namespace: String, pod: String, tty: T::Boolean, container: T.nilable(String) ).void }.
-
#version ⇒ Object
T::Sig::WithoutRuntime.sig { returns(T::Hash[T.untyped, T.untyped]) }.
-
#with_last_status(&block) ⇒ Object
T::Sig::WithoutRuntime.sig { params(block: T.proc.params(last_status: Process::Status).void).void }.
-
#with_pipes(out = STDOUT, err = STDERR, &block) ⇒ Object
T::Sig::WithoutRuntime.sig { params(out: T.any(StringIO, IO), err: T.any(StringIO, IO), block: T.proc.void).void }.
Constructor Details
#initialize(kubeconfig_path, executable = KubectlRb.executable) ⇒ KubernetesCLI
T::Sig::WithoutRuntime.sig { params(kubeconfig_path: String, executable: String).void }
68 69 70 71 72 73 74 |
# File 'lib/kubernetes-cli.rb', line 68 def initialize(kubeconfig_path, executable = KubectlRb.executable) @kubeconfig_path = kubeconfig_path @executable = executable @before_execute = [] @after_execute = [] # @env = T.let(@env, T.nilable(T::Hash[String, String])) end |
Instance Attribute Details
#executable ⇒ Object (readonly)
T::Sig::WithoutRuntime.sig { returns(String) }
60 61 62 |
# File 'lib/kubernetes-cli.rb', line 60 def executable @executable end |
#kubeconfig_path ⇒ Object (readonly)
T::Sig::WithoutRuntime.sig { returns(String) }
57 58 59 |
# File 'lib/kubernetes-cli.rb', line 57 def kubeconfig_path @kubeconfig_path end |
Instance Method Details
#after_execute(&block) ⇒ Object
T::Sig::WithoutRuntime.sig { params(block: AfterCallback).void }
82 83 84 |
# File 'lib/kubernetes-cli.rb', line 82 def after_execute(&block) @after_execute << block end |
#annotate(type, namespace, name, annotations, overwrite: true) ⇒ Object
T::Sig::WithoutRuntime.sig
params(
type: String,
namespace: String,
name: String,
annotations: T::Hash[String, String],
overwrite: T::Boolean
).void
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 |
# File 'lib/kubernetes-cli.rb', line 337 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) on_last_status_failure do |last_status| raise AnnotateResourceError, "could not annotate resource '#{name}': kubectl "\ "exited with status code #{last_status.exitstatus}" end end |
#api_resources ⇒ Object
T::Sig::WithoutRuntime.sig { returns(String) }
382 383 384 385 386 387 388 389 390 391 392 |
# File 'lib/kubernetes-cli.rb', line 382 def api_resources cmd = [executable, '--kubeconfig', kubeconfig_path, 'api-resources'] result = backticks(cmd) on_last_status_failure do |last_status| raise KubernetesError, 'could not fetch API resources: kubectl exited with '\ "status code #{last_status.exitstatus}. #{result}" end result end |
#apply(res, dry_run: false) ⇒ Object
T::Sig::WithoutRuntime.sig { params(res: ::KubeDSL::DSLObject, dry_run: T::Boolean).void }
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/kubernetes-cli.rb', line 159 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| stdin.puts(res.to_resource.to_yaml) end on_last_status_failure do |last_status| err = InvalidResourceError.new("Could not apply #{res.kind_sym} "\ "'#{res..name}': kubectl exited with status code #{last_status.exitstatus}" ) err.resource = res raise err end end |
#apply_uri(uri, dry_run: false) ⇒ Object
T::Sig::WithoutRuntime.sig { params(uri: String, dry_run: T::Boolean).void }
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/kubernetes-cli.rb', line 179 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) on_last_status_failure do |last_status| 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
T::Sig::WithoutRuntime.sig { params(block: BeforeCallback).void }
77 78 79 |
# File 'lib/kubernetes-cli.rb', line 77 def before_execute(&block) @before_execute << block end |
#current_context ⇒ Object
T::Sig::WithoutRuntime.sig { returns(String) }
376 377 378 379 |
# File 'lib/kubernetes-cli.rb', line 376 def current_context cmd = [executable, '--kubeconfig', kubeconfig_path, 'config', 'current-context'] backticks(cmd).strip end |
#delete_object(type, namespace, name) ⇒ Object
T::Sig::WithoutRuntime.sig
params(
type: String,
namespace: String,
name: String
).void
261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/kubernetes-cli.rb', line 261 def delete_object(type, namespace, name) cmd = [executable, '--kubeconfig', kubeconfig_path] cmd += ['-n', namespace] if namespace cmd += ['delete', type, name] systemm(cmd) on_last_status_failure do |last_status| raise DeleteResourceError, "couldn't delete resource of type '#{type}' named '#{name}' "\ "in namespace #{namespace}: kubectl exited with status code #{last_status.exitstatus}" end end |
#delete_objects(type, namespace, match_labels = {}) ⇒ Object
T::Sig::WithoutRuntime.sig
params(
type: String,
namespace: T.any(String, Symbol),
match_labels: T::Hash[String, String]
).void
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
# File 'lib/kubernetes-cli.rb', line 281 def delete_objects(type, namespace, match_labels = {}) cmd = [executable, '--kubeconfig', kubeconfig_path] if namespace == :all cmd << '--all-namespaces' elsif namespace cmd += ['-n', namespace.to_s] end cmd += ['delete', type] unless match_labels.empty? cmd += ['--selector', match_labels.map { |key, value| "#{key}=#{value}" }.join(',')] end systemm(cmd) on_last_status_failure do |last_status| raise DeleteResourceError, "couldn't delete resources of type '#{type}' "\ "in namespace #{namespace}: kubectl exited with status code #{last_status.exitstatus}" end end |
#exec_cmd(container_cmd, namespace, pod, tty = true, container = nil, out_file = nil) ⇒ Object
T::Sig::WithoutRuntime.sig
params(
container_cmd: T.any(String, T::Array[String]),
namespace: String,
pod: String,
tty: T::Boolean,
container: T.nilable(String),
out_file: T.nilable(String)
).void
132 133 134 135 136 137 138 139 |
# File 'lib/kubernetes-cli.rb', line 132 def exec_cmd(container_cmd, namespace, pod, tty = true, container = nil, out_file = nil) cmd = [executable, '--kubeconfig', kubeconfig_path, '-n', namespace, 'exec'] cmd += ['-it'] if tty cmd += ['-c', container] if container cmd += [pod, '--', *Array(container_cmd)] cmd += ['>', out_file] if out_file execc(cmd) end |
#get_object(type, namespace, name) ⇒ Object
T::Sig::WithoutRuntime.sig
params(
type: String,
namespace: String,
name: String
).returns(
T::Hash[String, T.untyped]
)
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/kubernetes-cli.rb', line 204 def get_object(type, namespace, name) cmd = [executable, '--kubeconfig', kubeconfig_path] cmd += ['-n', namespace] if namespace cmd += ['get', type, name] cmd += ['-o', 'json'] result = backticks(cmd) on_last_status_failure do |last_status| raise GetResourceError, "couldn't get resource of type '#{type}' named '#{name}' "\ "in namespace #{namespace}: kubectl exited with status code #{last_status.exitstatus}" end JSON.parse(result) end |
#get_objects(type, namespace, match_labels = {}) ⇒ Object
T::Sig::WithoutRuntime.sig
params(
type: String,
namespace: T.any(String, Symbol),
match_labels: T::Hash[String, String]
).returns(
T::Array[T.untyped]
)
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/kubernetes-cli.rb', line 229 def get_objects(type, namespace, match_labels = {}) cmd = [executable, '--kubeconfig', kubeconfig_path, 'get', type] if namespace == :all cmd << '--all-namespaces' elsif namespace cmd += ['-n', namespace.to_s] end unless match_labels.empty? cmd += ['--selector', match_labels.map { |key, value| "#{key}=#{value}" }.join(',')] end cmd += ['-o', 'json'] result = backticks(cmd) on_last_status_failure do |last_status| 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
T::Sig::WithoutRuntime.sig { returns(T.nilable(Process::Status)) }
87 88 89 |
# File 'lib/kubernetes-cli.rb', line 87 def last_status Thread.current[STATUS_KEY] end |
#logtail(namespace, selector, follow: true) ⇒ Object
T::Sig::WithoutRuntime.sig
params(
namespace: String,
selector: T::Hash[String, String],
follow: T::Boolean
).void
367 368 369 370 371 372 373 |
# File 'lib/kubernetes-cli.rb', line 367 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 |
#on_last_status_failure(&block) ⇒ Object
T::Sig::WithoutRuntime.sig { params(block: T.proc.params(last_status: Process::Status).void).void }
97 98 99 100 101 |
# File 'lib/kubernetes-cli.rb', line 97 def on_last_status_failure(&block) with_last_status do |ls| block.call(ls) unless ls.success? end end |
#patch_object(type, namespace, name, patch_data, patch_type = 'merge') ⇒ Object
T::Sig::WithoutRuntime.sig
params(
type: String,
namespace: String,
name: String,
patch_data: String,
patch_type: String
).void
313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
# File 'lib/kubernetes-cli.rb', line 313 def patch_object(type, namespace, name, patch_data, patch_type = 'merge') cmd = [executable, '--kubeconfig', kubeconfig_path] cmd += ['-n', namespace] if namespace cmd += ['patch', type, name] cmd += ['-p', Shellwords.shellescape(patch_data)] cmd += ['--type', patch_type] systemm(cmd) on_last_status_failure do |last_status| raise PatchResourceError, "couldn't patch resource of type '#{type}' named '#{name}' "\ "in namespace #{namespace}: kubectl exited with status code #{last_status.exitstatus}" end end |
#restart_deployment(namespace, deployment) ⇒ Object
T::Sig::WithoutRuntime.sig { params(namespace: String, deployment: String).void }
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 |
# File 'lib/kubernetes-cli.rb', line 395 def restart_deployment(namespace, deployment) cmd = [ executable, '--kubeconfig', kubeconfig_path, '-n', namespace, 'rollout', 'restart', 'deployment', deployment ] systemm(cmd) on_last_status_failure do |last_status| raise KubernetesError, 'could not restart deployment: kubectl exited with '\ "status code #{last_status.exitstatus}" end end |
#run_cmd(cmd) ⇒ Object
T::Sig::WithoutRuntime.sig { params(cmd: T.any(String, T::Array)).void }
117 118 119 120 |
# File 'lib/kubernetes-cli.rb', line 117 def run_cmd(cmd) cmd = [executable, '--kubeconfig', kubeconfig_path, *Array(cmd)] execc(cmd) end |
#stderr ⇒ Object
T::Sig::WithoutRuntime.sig { returns(T.any(StringIO, IO)) }
434 435 436 |
# File 'lib/kubernetes-cli.rb', line 434 def stderr Thread.current[STDERR_KEY] || STDERR end |
#stderr=(new_stderr) ⇒ Object
T::Sig::WithoutRuntime.sig { params(new_stderr: T.nilable(T.any(StringIO, IO))).void }
439 440 441 |
# File 'lib/kubernetes-cli.rb', line 439 def stderr=(new_stderr) Thread.current[STDERR_KEY] = new_stderr end |
#stdout ⇒ Object
T::Sig::WithoutRuntime.sig { returns(T.any(StringIO, IO)) }
424 425 426 |
# File 'lib/kubernetes-cli.rb', line 424 def stdout Thread.current[STDOUT_KEY] || STDOUT end |
#stdout=(new_stdout) ⇒ Object
T::Sig::WithoutRuntime.sig { params(new_stdout: T.nilable(T.any(StringIO, IO))).void }
429 430 431 |
# File 'lib/kubernetes-cli.rb', line 429 def stdout=(new_stdout) Thread.current[STDOUT_KEY] = new_stdout end |
#system_cmd(container_cmd, namespace, pod, tty = true, container = nil) ⇒ Object
T::Sig::WithoutRuntime.sig
params(
container_cmd: T.any(String, T::Array[String]),
namespace: String,
pod: String,
tty: T::Boolean,
container: T.nilable(String)
).void
150 151 152 153 154 155 156 |
# File 'lib/kubernetes-cli.rb', line 150 def system_cmd(container_cmd, namespace, pod, tty = true, container = nil) cmd = [executable, '--kubeconfig', kubeconfig_path, '-n', namespace, 'exec'] cmd += ['-it'] if tty cmd += ['-c', container] if container cmd += [pod, '--', *Array(container_cmd)] systemm(cmd) end |
#version ⇒ Object
T::Sig::WithoutRuntime.sig { returns(T::Hash[T.untyped, T.untyped]) }
104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/kubernetes-cli.rb', line 104 def version cmd = [executable, '--kubeconfig', kubeconfig_path, 'version', '-o', 'json'] result = backticks(cmd) on_last_status_failure do |last_status| raise GetVersionError, "couldn't get version info: "\ "kubectl exited with status code #{last_status.exitstatus}" end JSON.parse(result) end |
#with_last_status(&block) ⇒ Object
T::Sig::WithoutRuntime.sig { params(block: T.proc.params(last_status: Process::Status).void).void }
92 93 94 |
# File 'lib/kubernetes-cli.rb', line 92 def with_last_status(&block) block.call(last_status) end |
#with_pipes(out = STDOUT, err = STDERR, &block) ⇒ Object
T::Sig::WithoutRuntime.sig { params(out: T.any(StringIO, IO), err: T.any(StringIO, IO), block: T.proc.void).void }
412 413 414 415 416 417 418 419 420 421 |
# File 'lib/kubernetes-cli.rb', line 412 def with_pipes(out = STDOUT, err = STDERR, &block) previous_stdout = self.stdout previous_stderr = self.stderr self.stdout = out self.stderr = err yield ensure self.stdout = previous_stdout self.stderr = previous_stderr end |