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 ) = @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 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 |
# 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_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_yaml ⇒ Object
Returns Kubernetes pod configuration in YAML
83 84 85 |
# File 'lib/kplay/pod.rb', line 83 def configuration_yaml configuration.to_yaml end |
#shell ⇒ Object
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: [: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: [: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: [:verbose] ) end |
#temp_configuration_file ⇒ Tempfile
Creates a temporary configuration file and returns it
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 |