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
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
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, = {}) @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.
10 11 12 |
# File 'lib/kplay/pod.rb', line 10 def config @config end |
#mount_path ⇒ Object (readonly)
Returns the value of attribute mount_path.
10 11 12 |
# File 'lib/kplay/pod.rb', line 10 def mount_path @mount_path end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
10 11 12 |
# File 'lib/kplay/pod.rb', line 10 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
10 11 12 |
# File 'lib/kplay/pod.rb', line 10 def @options end |
#path_host ⇒ Object (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_vm ⇒ Object (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_name ⇒ Object (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
#configuration ⇒ Hash
Kubernetes configuration to run this pod
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 80 81 82 |
# 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' => [: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).) 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
86 87 88 |
# File 'lib/kplay/pod.rb', line 86 def configuration_yaml configuration.to_yaml end |
#shell ⇒ Object
Runs a shell session inside the pod
110 111 112 113 114 115 116 |
# File 'lib/kplay/pod.rb', line 110 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
92 93 94 95 96 97 |
# File 'lib/kplay/pod.rb', line 92 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
101 102 103 104 105 106 |
# File 'lib/kplay/pod.rb', line 101 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
122 123 124 125 126 127 128 129 |
# 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(&_block) ⇒ Object
Creates a temporary configuration file for the pod, yields it to the given block and then deletes it.
135 136 137 138 139 140 |
# 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 |