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



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

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

#deleteObject



35
36
37
38
39
40
41
42
# File 'lib/kube/cluster/resource/persistence.rb', line 35

def delete
  if persisted?
    kubectl("delete", resource_type, name, *ns_flags)
    true
  else
    raise Kube::CommandError, "cannot delete a resource without a name"
  end
end

#patch(type: "strategic") ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/kube/cluster/resource/persistence.rb', line 18

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

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

#reloadObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/kube/cluster/resource/persistence.rb', line 44

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