class SwarmClusterCliOpe::Kubernetes::Pod

Interfaccia per la comunicazione con il POD

Attributes

context[RW]

@return [String]

pod_description[RW]

@return [Hash]

Public Class Methods

find_by_name(name, namespace: nil, context: nil) click to toggle source
# File lib/swarm_cluster_cli_ope/kubernetes/pod.rb, line 111
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) click to toggle source

@param [String] selector @return [Pod] @param [nil,String] namespace -> se la sciato vuoto utiliziamo il namespace corrente @param [String, nil] context -> contesto di kubectl, nel caso utilizziamo il corrente

# File lib/swarm_cluster_cli_ope/kubernetes/pod.rb, line 86
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
new(pod_description, context:) click to toggle source

@param [Hash] pod_description -> hash con le configurazioni ritornate da kubectl @param [String] context -> se non presente utiliziamo l'attuale

# 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

Public Instance Methods

base_cmd(cmd) click to toggle source

Appende solamente la parte base dei comandi @return [SwarmClusterCliOpe::ShellCommandExecution] @param [String,Array<String>] cmd

# File lib/swarm_cluster_cli_ope/kubernetes/pod.rb, line 42
def base_cmd(cmd)
  ShellCommandExecution.new([base_kubectl_cmd_env, cmd].flatten)
end
copy_in(src, dst) click to toggle source

Proxy class per essere simili al container per swarm @return [TrueClass, FalseClass] @param [String] src @param [String] dst

# File lib/swarm_cluster_cli_ope/kubernetes/pod.rb, line 70
def copy_in(src, dst)
  cp_in(src, dst).execute.success?
end
copy_out(src, dst) click to toggle source

@param [String] src @param [String] dst @return [TrueClass, FalseClass]

# File lib/swarm_cluster_cli_ope/kubernetes/pod.rb, line 77
def copy_out(src, dst)
  cp_out(src, dst).execute.success?
end
cp_in(src, dst) click to toggle source

Comando per la copia del file @param [String] src @param [String] dst @return [SwarmClusterCliOpe::ShellCommandExecution]

# File lib/swarm_cluster_cli_ope/kubernetes/pod.rb, line 52
def cp_in(src, dst)
  base_cmd(["cp", src, "#{name}:#{dst}"])
end
cp_out(src, dst) click to toggle source

Comando per la copia del file dal container @param [String] src @param [String] dst @return [SwarmClusterCliOpe::ShellCommandExecution]

# File lib/swarm_cluster_cli_ope/kubernetes/pod.rb, line 61
def cp_out(src, dst)
  base_cmd(["cp", "#{name}:#{src}", dst])
end
exec(cmd) click to toggle source

@param [String,Array<String>] cmd -> comando da passare a kubectl exec – CMD @return [SwarmClusterCliOpe::ShellCommandResponse]

# File lib/swarm_cluster_cli_ope/kubernetes/pod.rb, line 34
def exec(cmd)
  base_cmd(["exec", name, "--", cmd].flatten).execute
end
name() click to toggle source

@return [String]

# File lib/swarm_cluster_cli_ope/kubernetes/pod.rb, line 24
def name
  @pod_description[:metadata][:name]
end
namespace() click to toggle source
# File lib/swarm_cluster_cli_ope/kubernetes/pod.rb, line 28
def namespace
  @pod_description[:metadata][:namespace]
end

Private Instance Methods

base_kubectl_cmd_env(json: false) click to toggle source

Array con i comandi base di kubectl @return [Array<String>]

# File lib/swarm_cluster_cli_ope/kubernetes/pod.rb, line 134
def base_kubectl_cmd_env(json: false)
  base_cmd = ["kubectl"]
  base_cmd << "--namespace=#{namespace}" unless namespace.blank?
  base_cmd << "--context=#{context}" unless context.blank?
  base_cmd << "--output=json" if json
  base_cmd
end