Class: Nvoi::External::Kubectl

Inherits:
Object
  • Object
show all
Defined in:
lib/nvoi/external/kubectl.rb

Overview

Kubectl handles kubernetes operations via kubectl on remote servers

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ssh) ⇒ Kubectl

Returns a new instance of Kubectl.



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

def initialize(ssh)
  @ssh = ssh
end

Instance Attribute Details

#sshObject (readonly)

Returns the value of attribute ssh.



7
8
9
# File 'lib/nvoi/external/kubectl.rb', line 7

def ssh
  @ssh
end

Instance Method Details

#apply(manifest) ⇒ Object



13
14
15
16
# File 'lib/nvoi/external/kubectl.rb', line 13

def apply(manifest)
  cmd = "cat <<'EOF' | kubectl apply -f -\n#{manifest}\nEOF"
  @ssh.execute(cmd)
end

#cp(local_path, pod_path, namespace: "default") ⇒ Object



60
61
62
# File 'lib/nvoi/external/kubectl.rb', line 60

def cp(local_path, pod_path, namespace: "default")
  @ssh.execute("kubectl cp #{local_path} #{namespace}/#{pod_path}")
end

#delete(resource_type, name, namespace: "default") ⇒ Object



18
19
20
# File 'lib/nvoi/external/kubectl.rb', line 18

def delete(resource_type, name, namespace: "default")
  @ssh.execute("kubectl delete #{resource_type} #{name} -n #{namespace} --ignore-not-found")
end

#exec(pod_name, command, namespace: "default") ⇒ Object



28
29
30
# File 'lib/nvoi/external/kubectl.rb', line 28

def exec(pod_name, command, namespace: "default")
  @ssh.execute("kubectl exec -n #{namespace} #{pod_name} -- #{command}")
end

#get(resource_type, name, namespace: "default", jsonpath: nil) ⇒ Object



22
23
24
25
26
# File 'lib/nvoi/external/kubectl.rb', line 22

def get(resource_type, name, namespace: "default", jsonpath: nil)
  cmd = "kubectl get #{resource_type} #{name} -n #{namespace}"
  cmd += " -o jsonpath='#{jsonpath}'" if jsonpath
  @ssh.execute(cmd)
end

#get_nodesObject



56
57
58
# File 'lib/nvoi/external/kubectl.rb', line 56

def get_nodes
  @ssh.execute("kubectl get nodes -o name")
end

#label_node(node_name, labels) ⇒ Object



50
51
52
53
54
# File 'lib/nvoi/external/kubectl.rb', line 50

def label_node(node_name, labels)
  labels.each do |key, value|
    @ssh.execute("kubectl label node #{node_name} #{key}=#{value} --overwrite")
  end
end

#logs(pod_name, namespace: "default", tail: nil) ⇒ Object



32
33
34
35
36
# File 'lib/nvoi/external/kubectl.rb', line 32

def logs(pod_name, namespace: "default", tail: nil)
  cmd = "kubectl logs #{pod_name} -n #{namespace}"
  cmd += " --tail=#{tail}" if tail
  @ssh.execute(cmd)
end

#rollout_status(resource_type, name, namespace: "default", timeout: 300) ⇒ Object



38
39
40
# File 'lib/nvoi/external/kubectl.rb', line 38

def rollout_status(resource_type, name, namespace: "default", timeout: 300)
  @ssh.execute("kubectl rollout status #{resource_type}/#{name} -n #{namespace} --timeout=#{timeout}s")
end

#wait_for_deployment(name, namespace: "default", timeout: 300) ⇒ Object



42
43
44
# File 'lib/nvoi/external/kubectl.rb', line 42

def wait_for_deployment(name, namespace: "default", timeout: 300)
  rollout_status("deployment", name, namespace:, timeout:)
end

#wait_for_statefulset(name, namespace: "default", timeout: 300) ⇒ Object



46
47
48
# File 'lib/nvoi/external/kubectl.rb', line 46

def wait_for_statefulset(name, namespace: "default", timeout: 300)
  rollout_status("statefulset", name, namespace:, timeout:)
end