class Kplay::Pod

Pod represents a pod associated with a folder on a host machine

Attributes

config[R]
mount_path[R]
name[R]
options[R]
path_host[R]
path_vm[R]
volume_name[R]

Public Class Methods

new(path_host, config = nil, options = {}) click to toggle source

Creates a Pod from a path on host

@param path_host [String] @param config [Config] local pod configuration options @param options [Hash] command execution options

# File lib/kplay/pod.rb, line 18
def initialize(path_host, config = nil, options = {})
  @path_host = path_host
  @path_vm = Kplay::Minikube.host_path_in_vm(path_host)
  @name = File.basename(path_host)
  @config = config ? config : Kplay::Config.global
  @config.expand_templates!(
    name: name,
    path_host: path_host,
    path_vm: path_vm
  )
  @options = options
  @volume_name = name + '-volume'
end

Public Instance Methods

configuration() click to toggle source

Kubernetes configuration to run this pod

@return [Hash]

# File lib/kplay/pod.rb, line 36
def configuration
  host_aliases = config[:etc_hosts].map do |host|
    ip, *hostnames = host.strip.split(' ')
    { 'ip' => ip, 'hostnames' => hostnames }
  end
  c = {
    'apiVersion' => 'v1',
    'kind' => 'Pod',
    'metadata' => { 'name' => name },
    'spec' => {
      'hostAliases' => host_aliases,
      'containers' => [
        {
          'name' => name,
          'image' => options[:image] || config[:image],
          'imagePullPolicy' => 'IfNotPresent',
          'env' => [
            # { 'name' => ..., 'value' => ... }
          ],
          'volumeMounts' => [
            { 'mountPath' => config[:mount_path], 'name' => volume_name },
            # <-- ssh forwarding socket should be mounted a CONTAINER here
          ]
        }
      ],
      'volumes' => [
        {
          'name' => volume_name,
          'hostPath' => { 'path' => path_vm.to_s }
        }
        # <-- ssh forwarding socket in VM mounted here
      ]
    }
  }
  # add custom volumes
  config[:volumes].each_with_index do |volume_def, i|
    v_from, v_to = volume_def.split(':')
    v_from = Kplay::Minikube.host_path_in_vm(Pathname.new(v_from).expand_path)
    v_to   = Pathname.new(v_to) # do not expand path locally
    name = 'volume-' + i.to_s
    c['spec']['containers'].first['volumeMounts'] <<
      { 'name' => name, 'mountPath' => v_to.to_s }
    c['spec']['volumes'] <<
      { 'name' => name, 'hostPath' => { 'path' => v_from.to_s } }
  end
  c
end
configuration_yaml() click to toggle source

Returns Kubernetes pod configuration in YAML

# File lib/kplay/pod.rb, line 86
def configuration_yaml
  configuration.to_yaml
end
shell() click to toggle source

Runs a shell session inside the pod

# File lib/kplay/pod.rb, line 110
def shell
  Kplay.sh(
    ['kubectl', 'exec', '-ti', name, '--', config[:shell], *config[:shell_args]],
    tty: true,
    echo: options[:verbose]
  )
end
start!() click to toggle source

Runs the pod in Kubernetes cluster

# File lib/kplay/pod.rb, line 92
def start!
  with_configuration_file do |conf_file|
    Kplay.sh ['kubectl', 'apply', '-f', conf_file.path], echo: options[:verbose]
  end
  sleep 1
end
stop!() click to toggle source

Stops the pod

# File lib/kplay/pod.rb, line 101
def stop!
  Kplay.sh(
    ['kubectl', 'delete', 'pod', name, "--grace-period=#{config[:stop_grace_period]}"],
    echo: options[:verbose]
  )
end
temp_configuration_file() click to toggle source

Creates a temporary configuration file and returns it

@return [Tempfile]

# File lib/kplay/pod.rb, line 122
def temp_configuration_file
  @temp_configuration_file ||= begin
    tempfile = Tempfile.new("kplay-#{name}")
    tempfile.write(configuration_yaml)
    tempfile.close
    tempfile
  end
end
with_configuration_file() { |temp_configuration_file| ... } click to toggle source

Creates a temporary configuration file for the pod, yields it to the given block and then deletes it.

# File lib/kplay/pod.rb, line 135
def with_configuration_file(&_block)
  yield temp_configuration_file
ensure
  temp_configuration_file.unlink
  @temp_configuration_file = nil
end