Class: Kube::Station::Cluster

Inherits:
ApplicationRecord show all
Defined in:
app/models/kube/station/cluster.rb

Instance Method Summary collapse

Instance Method Details

#apply_resource(data) ⇒ Object



32
33
34
35
36
37
38
# File 'app/models/kube/station/cluster.rb', line 32

def apply_resource(data)
  with_connection do |instance|
    kubeconfig = instance.connection.ctl.kubeconfig
    json = data.is_a?(String) ? data : JSON.generate(data)
    sh { kubectl "apply -f - --kubeconfig=#{kubeconfig}", _stdin: json }
  end
end

#get_resource(kind, name, namespace: nil) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'app/models/kube/station/cluster.rb', line 22

def get_resource(kind, name, namespace: nil)
  with_connection do |instance|
    ctl = instance.connection.ctl
    cmd = "get #{kind.downcase} #{name} -o json"
    cmd += " -n #{namespace}" if namespace
    json = ctl.run(cmd)
    JSON.parse(json, symbolize_names: true)
  end
end

#list_resources(kind) ⇒ Object



13
14
15
16
17
18
19
20
# File 'app/models/kube/station/cluster.rb', line 13

def list_resources(kind)
  with_connection do |instance|
    ctl = instance.connection.ctl
    json = ctl.run("get #{kind.downcase.pluralize} -o json")
    parsed = JSON.parse(json, symbolize_names: true)
    parsed[:items] || []
  end
end

#with_connectionObject



7
8
9
10
11
# File 'app/models/kube/station/cluster.rb', line 7

def with_connection
  config.with_kubeconfig_file do |path|
    yield ::Kube::Cluster.connect(kubeconfig: path)
  end
end