Class: SwarmClusterCliOpe::Kubernetes::Pod

Inherits:
Object
  • Object
show all
Includes:
LoggerConcern
Defined in:
lib/swarm_cluster_cli_ope/kubernetes/pod.rb

Overview

Interfaccia per la comunicazione con il POD

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from LoggerConcern

#logger

Constructor Details

#initialize(pod_description, context:) ⇒ Pod

Returns a new instance of Pod.

Parameters:

  • pod_description (Hash)

    -> hash con le configurazioni ritornate da kubectl

  • context (String)

    -> se non presente utiliziamo l’attuale



17
18
19
20
# File 'lib/swarm_cluster_cli_ope/kubernetes/pod.rb', line 17

def initialize(pod_description, context:)
  @pod_description = pod_description.to_h.deep_symbolize_keys
  @context = context
end

Instance Attribute Details

#contextString

Returns:

  • (String)


13
14
15
# File 'lib/swarm_cluster_cli_ope/kubernetes/pod.rb', line 13

def context
  @context
end

#pod_descriptionHash

Returns:

  • (Hash)


10
11
12
# File 'lib/swarm_cluster_cli_ope/kubernetes/pod.rb', line 10

def pod_description
  @pod_description
end

Class Method Details

.find_by_name(name, namespace: nil, context: nil) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/swarm_cluster_cli_ope/kubernetes/pod.rb', line 112

def self.find_by_name(name, namespace: nil, context: nil)
  base_cmd = ["kubectl"]
  base_cmd << "--namespace=#{namespace}" unless namespace.blank?
  base_cmd << "--context=#{context}" unless context.blank?
  base_cmd << "get pod #{name}"
  base_cmd << "--output=json"

  cmd = ShellCommandExecution.new(base_cmd)
  ris = cmd.execute(allow_failure: true)
  if ris.failed?
    puts "Problemi nella ricerca del pod"
    exit
  else
    self.new(ris.single_obj, context: context)
  end
end

.find_by_selector(selector, namespace: nil, context: nil) ⇒ Pod

Parameters:

  • selector (String)
  • namespace (nil, String) (defaults to: nil)

    -> se la sciato vuoto utiliziamo il namespace corrente

  • context (String, nil) (defaults to: nil)

    -> contesto di kubectl, nel caso utilizziamo il corrente

Returns:



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/swarm_cluster_cli_ope/kubernetes/pod.rb', line 87

def self.find_by_selector(selector, namespace: nil, context: nil)

  base_cmd = ["kubectl"]
  base_cmd << "--namespace=#{namespace}" unless namespace.blank?
  base_cmd << "--context=#{context}" unless context.blank?
  base_cmd << "get pod"
  base_cmd << "--selector=#{selector}"
  base_cmd << "--field-selector=status.phase=Running" #solo pod che stanno girando teniamo sotto controllo
  base_cmd << "--output=json"

  cmd = ShellCommandExecution.new(base_cmd)
  ris = cmd.execute(allow_failure: true)
  if ris.failed?
    puts "Problemi nella ricerca del pod"
    exit
  else
    if ris.single_obj[:items].empty?
      puts  "non abbiamo trovato il pod"
      exit
    else
      self.new(ris.single_obj[:items].first, context: context)
    end
  end
end

Instance Method Details

#base_cmd(cmd) ⇒ SwarmClusterCliOpe::ShellCommandExecution

Appende solamente la parte base dei comandi

Parameters:

  • cmd (String, Array<String>)

Returns:



43
44
45
# File 'lib/swarm_cluster_cli_ope/kubernetes/pod.rb', line 43

def base_cmd(cmd)
  ShellCommandExecution.new([base_kubectl_cmd_env, cmd].flatten)
end

#copy_in(src, dst) ⇒ TrueClass, FalseClass

Proxy class per essere simili al container per swarm

Parameters:

  • src (String)
  • dst (String)

Returns:

  • (TrueClass, FalseClass)


71
72
73
# File 'lib/swarm_cluster_cli_ope/kubernetes/pod.rb', line 71

def copy_in(src, dst)
  cp_in(src, dst).execute.success?
end

#copy_out(src, dst) ⇒ TrueClass, FalseClass

Parameters:

  • src (String)
  • dst (String)

Returns:

  • (TrueClass, FalseClass)


78
79
80
# File 'lib/swarm_cluster_cli_ope/kubernetes/pod.rb', line 78

def copy_out(src, dst)
  cp_out(src, dst).execute.success?
end

#cp_in(src, dst) ⇒ SwarmClusterCliOpe::ShellCommandExecution

Comando per la copia del file

Parameters:

  • src (String)
  • dst (String)

Returns:



53
54
55
# File 'lib/swarm_cluster_cli_ope/kubernetes/pod.rb', line 53

def cp_in(src, dst)
  base_cmd(["cp", src, "#{name}:#{dst}"])
end

#cp_out(src, dst) ⇒ SwarmClusterCliOpe::ShellCommandExecution

Comando per la copia del file dal container

Parameters:

  • src (String)
  • dst (String)

Returns:



62
63
64
# File 'lib/swarm_cluster_cli_ope/kubernetes/pod.rb', line 62

def cp_out(src, dst)
  base_cmd(["cp", "#{name}:#{src}", dst])
end

#exec(cmd, allow_failure: false) ⇒ SwarmClusterCliOpe::ShellCommandResponse

Parameters:

  • cmd (String, Array<String>)

    -> comando da passare a kubectl exec – CMD

  • allow_failure (FalseClass) (defaults to: false)

    -> parametro per lasciar fallire o meno il comando senza bloccarsi e poi gestirlo

Returns:



35
36
37
# File 'lib/swarm_cluster_cli_ope/kubernetes/pod.rb', line 35

def exec(cmd, allow_failure: false)
  base_cmd(["exec", name, "--", cmd].flatten).execute(allow_failure: allow_failure)
end

#nameString

Returns:

  • (String)


24
25
26
# File 'lib/swarm_cluster_cli_ope/kubernetes/pod.rb', line 24

def name
  @pod_description[:metadata][:name]
end

#namespaceObject



28
29
30
# File 'lib/swarm_cluster_cli_ope/kubernetes/pod.rb', line 28

def namespace
  @pod_description[:metadata][:namespace]
end