Module: Kdep::Kubectl

Defined in:
lib/kdep/kubectl.rb

Defined Under Namespace

Classes: Error

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.runnerObject

Returns the value of attribute runner.



9
10
11
# File 'lib/kdep/kubectl.rb', line 9

def runner
  @runner
end

Class Method Details

.apply(file_path, namespace:) ⇒ Object



30
31
32
# File 'lib/kdep/kubectl.rb', line 30

def self.apply(file_path, namespace:)
  run("apply", "-f", file_path, "-n", namespace)
end

.current_contextObject



26
27
28
# File 'lib/kdep/kubectl.rb', line 26

def self.current_context
  run("config", "current-context").strip
end

.diff(file_path) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/kdep/kubectl.rb', line 34

def self.diff(file_path)
  stdout, stderr, status = runner.call("diff", "-f", file_path)
  case status.exitstatus
  when 0 then nil              # no diff
  when 1 then stdout           # diffs present on stdout
  else raise Error, "kubectl diff failed (exit #{status.exitstatus}): #{stderr.strip}"
  end
end

.get(kind, name, namespace:, output: "yaml") ⇒ Object

Live-fetch helper. Returns nil when the resource is absent (NotFound). Caller distinguishes “not yet created” from real errors.



55
56
57
58
59
# File 'lib/kdep/kubectl.rb', line 55

def self.get(kind, name, namespace:, output: "yaml")
  run("get", kind, name, "-n", namespace, "-o", output)
rescue Error
  nil
end

.patch(kind, name, namespace:, type:, patch:) ⇒ Object

JSON-patch helper used by Kdep::ConfigmapOverlay (Feature A) with merge: patch.



49
50
51
# File 'lib/kdep/kubectl.rb', line 49

def self.patch(kind, name, namespace:, type:, patch:)
  run("patch", kind, name, "-n", namespace, "--type", type, "-p", patch)
end

.rollout_restart(kind, name, namespace:) ⇒ Object

Declarative rollout-restart helper used by Kdep::RolloutTracker (Feature F).



44
45
46
# File 'lib/kdep/kubectl.rb', line 44

def self.rollout_restart(kind, name, namespace:)
  run("rollout", "restart", "#{kind}/#{name}", "-n", namespace)
end

.run(*args) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/kdep/kubectl.rb', line 14

def self.run(*args)
  stdout, stderr, status = runner.call(*args)
  unless status.success?
    raise Error, "kubectl #{args.first} failed: #{stderr.strip}"
  end
  stdout
end

.run_json(*args) ⇒ Object



22
23
24
# File 'lib/kdep/kubectl.rb', line 22

def self.run_json(*args)
  JSON.parse(run(*args, "-o", "json"))
end