Class: Kplay::Pod
- Inherits:
-
Object
- Object
- Kplay::Pod
- Defined in:
- lib/kplay/pod.rb
Overview
Pod represents a pod associated with a folder on a host machine
Constant Summary collapse
- DEFAULT_SHM_VOLUME_NAME =
"pod-shm-volume"- DEFAULT_SHM_VOLUME_PATH =
"/dev/shm"
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#mount_path ⇒ Object
readonly
Returns the value of attribute mount_path.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#path_host ⇒ Object
readonly
Returns the value of attribute path_host.
-
#path_vm ⇒ Object
readonly
Returns the value of attribute path_vm.
-
#volume_name ⇒ Object
readonly
Returns the value of attribute volume_name.
Instance Method Summary collapse
-
#configuration ⇒ Hash
Kubernetes configuration to run this pod.
-
#configuration_yaml ⇒ Object
Returns Kubernetes pod configuration in YAML.
-
#initialize(path_host, config = nil, options = {}) ⇒ Pod
constructor
Creates a Pod from a path on host.
-
#shell ⇒ Object
Runs a shell session inside the pod.
-
#start! ⇒ Object
Runs the pod in Kubernetes cluster.
-
#stop! ⇒ Object
Stops the pod.
-
#temp_configuration_file ⇒ Tempfile
Creates a temporary configuration file and returns it.
-
#with_configuration_file(&_block) ⇒ Object
Creates a temporary configuration file for the pod, yields it to the given block and then deletes it.
Constructor Details
#initialize(path_host, config = nil, options = {}) ⇒ Pod
Creates a Pod from a path on host
21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/kplay/pod.rb', line 21 def initialize(path_host, config = nil, = {}) @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.( name: name, path_host: path_host, path_vm: path_vm ) @options = @volume_name = name + '-volume' end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
13 14 15 |
# File 'lib/kplay/pod.rb', line 13 def config @config end |
#mount_path ⇒ Object (readonly)
Returns the value of attribute mount_path.
13 14 15 |
# File 'lib/kplay/pod.rb', line 13 def mount_path @mount_path end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
13 14 15 |
# File 'lib/kplay/pod.rb', line 13 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
13 14 15 |
# File 'lib/kplay/pod.rb', line 13 def @options end |
#path_host ⇒ Object (readonly)
Returns the value of attribute path_host.
13 14 15 |
# File 'lib/kplay/pod.rb', line 13 def path_host @path_host end |
#path_vm ⇒ Object (readonly)
Returns the value of attribute path_vm.
13 14 15 |
# File 'lib/kplay/pod.rb', line 13 def path_vm @path_vm end |
#volume_name ⇒ Object (readonly)
Returns the value of attribute volume_name.
13 14 15 |
# File 'lib/kplay/pod.rb', line 13 def volume_name @volume_name end |
Instance Method Details
#configuration ⇒ Hash
Kubernetes configuration to run this pod
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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/kplay/pod.rb', line 39 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' => [:image] || config[:image], 'imagePullPolicy' => 'IfNotPresent', 'env' => [ # { 'name' => ..., 'value' => ... } ], 'volumeMounts' => [ { 'mountPath' => config[:mount_path], 'name' => volume_name }, { 'mountPath' => DEFAULT_SHM_VOLUME_PATH, 'name' => DEFAULT_SHM_VOLUME_NAME }, # <-- ssh forwarding socket should be mounted a CONTAINER here ] } ], 'volumes' => [ { 'name' => volume_name, 'hostPath' => { 'path' => path_vm.to_s } }, { 'name' => DEFAULT_SHM_VOLUME_NAME, 'emptyDir' => { 'medium' => 'Memory', 'sizeLimit' => config[:shm_size] } } # <-- 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).) 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 ⇒ Object
Returns Kubernetes pod configuration in YAML
97 98 99 |
# File 'lib/kplay/pod.rb', line 97 def configuration_yaml configuration.to_yaml end |
#shell ⇒ Object
Runs a shell session inside the pod
121 122 123 124 125 126 127 |
# File 'lib/kplay/pod.rb', line 121 def shell Kplay.sh( ['kubectl', 'exec', '-ti', name, '--', config[:shell], *config[:shell_args]], tty: true, echo: [:verbose] ) end |
#start! ⇒ Object
Runs the pod in Kubernetes cluster
103 104 105 106 107 108 |
# File 'lib/kplay/pod.rb', line 103 def start! with_configuration_file do |conf_file| Kplay.sh ['kubectl', 'apply', '-f', conf_file.path], echo: [:verbose] end sleep 1 end |
#stop! ⇒ Object
Stops the pod
112 113 114 115 116 117 |
# File 'lib/kplay/pod.rb', line 112 def stop! Kplay.sh( ['kubectl', 'delete', 'pod', name, "--grace-period=#{config[:stop_grace_period]}"], echo: [:verbose] ) end |
#temp_configuration_file ⇒ Tempfile
Creates a temporary configuration file and returns it
133 134 135 136 137 138 139 140 |
# File 'lib/kplay/pod.rb', line 133 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.
146 147 148 149 150 151 |
# File 'lib/kplay/pod.rb', line 146 def with_configuration_file(&_block) yield temp_configuration_file ensure temp_configuration_file.unlink @temp_configuration_file = nil end |