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



21
22
23
24
25
26
27
# File 'lib/kube/cluster/resource/persistence.rb', line 21

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

#deleteObject



46
47
48
49
50
51
52
53
# File 'lib/kube/cluster/resource/persistence.rb', line 46

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



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

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

#patch(type: "strategic") ⇒ Object



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

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)


17
18
19
# File 'lib/kube/cluster/resource/persistence.rb', line 17

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

#reloadObject



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

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