Class: Kplay::Pod

Inherits:
Object
  • Object
show all
Defined in:
lib/kplay/pod.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path_host, config = nil, options = {}) ⇒ Pod

Creates a Pod from a path on host

Parameters:

  • path_host (String)
  • config (Config) (defaults to: nil)

    local pod configuration options

  • options (Hash) (defaults to: {})

    command execution options



18
19
20
21
22
23
24
25
26
27
28
29
30
# 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

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

#mount_pathObject (readonly)

Returns the value of attribute mount_path.



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

def mount_path
  @mount_path
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#path_hostObject (readonly)

Returns the value of attribute path_host.



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

def path_host
  @path_host
end

#path_vmObject (readonly)

Returns the value of attribute path_vm.



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

def path_vm
  @path_vm
end

#volume_nameObject (readonly)

Returns the value of attribute volume_name.



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

def volume_name
  @volume_name
end

Instance Method Details

#configurationHash

Kubernetes configuration to run this pod

Returns:

  • (Hash)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# 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_host }
        }
        # <-- ssh forwarding socket in VM mounted here
      ]
    }
  }
  return c unless Kplay::Minikube.ssh_forwarding_available?
  # enable SSH forwarding
  c['spec']['containers'].first['env'] <<
    { 'name' => 'SSH_AUTH_SOCK', 'value' => Kplay::Minikube.ssh_forwarding_socket_vm.to_s }
  c['spec']['containers'].first['volumeMounts'] <<
    { 'name' => 'ssh-auth-sock', 'mountPath' => Kplay::Minikube.ssh_forwarding_socket_vm.to_s }
  c['spec']['volumes'] <<
    { 'name' => 'ssh-auth-sock', 'hostPath' => { 'path' => Kplay::Minikube.ssh_forwarding_socket_vm.to_s } }
  c
end

#configuration_yamlObject

Returns Kubernetes pod configuration in YAML



83
84
85
# File 'lib/kplay/pod.rb', line 83

def configuration_yaml
  configuration.to_yaml
end

#shellObject

Runs a shell session inside the pod



107
108
109
110
111
112
113
# File 'lib/kplay/pod.rb', line 107

def shell
  Kplay.sh(
    ['kubectl', 'exec', name, '-ti', config[:shell], '--', *config[:shell_args]],
    tty: true,
    echo: options[:verbose]
  )
end

#start!Object

Runs the pod in Kubernetes cluster



89
90
91
92
93
94
# File 'lib/kplay/pod.rb', line 89

def start!
  with_configuration_file do |conf_file|
    Kplay.sh ['kubectl', 'apply', '-f', conf_file.path], echo: options[:verbose]
  end
  sleep 1
end

#stop!Object

Stops the pod



98
99
100
101
102
103
# File 'lib/kplay/pod.rb', line 98

def stop!
  Kplay.sh(
    ['kubectl', 'delete', 'pod', name, "--grace-period=#{config[:stop_grace_period]}", '--force'],
    echo: options[:verbose]
  )
end

#temp_configuration_fileTempfile

Creates a temporary configuration file and returns it

Returns:

  • (Tempfile)


119
120
121
122
123
124
125
126
# File 'lib/kplay/pod.rb', line 119

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(&_block) ⇒ Object

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



132
133
134
135
136
137
# File 'lib/kplay/pod.rb', line 132

def with_configuration_file(&_block)
  yield temp_configuration_file
ensure
  temp_configuration_file.unlink
  @temp_configuration_file = nil
end