Module: Kube::Cluster::Resource::Persistence

Included in:
Kube::Cluster::Resource
Defined in:
lib/kube/cluster/resource/persistence.rb

Instance Method Summary collapse

Instance Method Details

#applyObject



18
19
20
21
22
23
24
# File 'lib/kube/cluster/resource/persistence.rb', line 18

def apply
  JSON.generate(deep_stringify_keys(to_h)).then do |json|
    kubectl("apply", "-f", "-", stdin: json)
    reload
    true
  end
end

#deleteObject



43
44
45
46
47
48
49
50
# File 'lib/kube/cluster/resource/persistence.rb', line 43

def delete
  if persisted?
    kubectl("delete", kind.downcase, name, *namespace_flags)
    true
  else
    raise Kube::CommandError, "cannot delete a resource without a name"
  end
end

#nameObject



10
11
12
# File 'lib/kube/cluster/resource/persistence.rb', line 10

def name
  to_h.dig(:metadata, :name)&.to_s
end

#patch(type: "strategic") ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/kube/cluster/resource/persistence.rb', line 26

def patch(type: "strategic")
  if persisted?
    diff = patch_data

    if diff.empty?
      false
    else
      json = JSON.generate(deep_stringify_keys(diff))
      kubectl("patch", kind.downcase, name, *namespace_flags, "--type", type, "-p", json)
      reload
      true
    end
  else
    raise Kube::CommandError, "cannot patch a resource without a name"
  end
end

#persisted?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/kube/cluster/resource/persistence.rb', line 14

def persisted?
  !name.nil? && !name.empty?
end

#reloadObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/kube/cluster/resource/persistence.rb', line 52

def reload
  if persisted?
    tap do
      kubectl("get", kind.downcase, name, *namespace_flags, "-o", "json").then do |json|
        JSON.parse(json).then do |hash|
          @data = deep_symbolize_keys(hash)
          snapshot!
        end
      end
    end
  else
    raise Kube::CommandError, "cannot reload a resource without a name"
  end
end