Module: Dapp::Kube::Dapp::Command::MinikubeSetup

Included in:
Dapp
Defined in:
lib/dapp/kube/dapp/command/minikube_setup.rb

Instance Method Summary collapse

Instance Method Details

#_minikube_find_registry_podObject



245
246
247
# File 'lib/dapp/kube/dapp/command/minikube_setup.rb', line 245

def _minikube_find_registry_pod
  _minikube_kubernetes.pod_list(labelSelector: "k8s-app=kube-registry")['items'].first
end

#_minikube_kubernetesObject



253
254
255
256
257
258
259
260
# File 'lib/dapp/kube/dapp/command/minikube_setup.rb', line 253

def _minikube_kubernetes
  @_minikube_kubernetes ||= Kubernetes::Client.new(
    kubernetes_config,
    kubernetes_config.current_context_name,
    'kube-system',
    timeout: options[:kubernetes_timeout],
  )
end

#_minikube_registry_pod_nameObject



249
250
251
# File 'lib/dapp/kube/dapp/command/minikube_setup.rb', line 249

def _minikube_registry_pod_name
  @_minikube_registry_pod_name
end

#_minikube_registry_proxy_pod_specObject



361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# File 'lib/dapp/kube/dapp/command/minikube_setup.rb', line 361

def _minikube_registry_proxy_pod_spec
  {
    'metadata' => {
      'name' => 'kube-registry-proxy',
      'namespace' => 'kube-system'
    },
    'spec' => {
      'containers' => [
        {
          'name' => 'kube-registry-proxy',
          'image' => 'gcr.io/google_containers/kube-registry-proxy:0.3',
          'env' => [
            {
              'name' => 'REGISTRY_HOST',
              'value' => 'kube-registry.kube-system.svc.cluster.local'
            },
            {
              'name' => 'REGISTRY_PORT',
              'value' => '5000'
            },
            {
              'name' => 'FORWARD_PORT',
              'value' => '5000'
            }
          ],
          'ports' => [
            {
              'name' => 'registry',
              'containerPort' => 5000,
              'hostPort' => 5000
            }
          ]
        }
      ]
    }
  }
end

#_minikube_registry_replicationcontroller_specObject



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/dapp/kube/dapp/command/minikube_setup.rb', line 262

def _minikube_registry_replicationcontroller_spec
  {
    'metadata' => {
      'name' => 'kube-registry',
      'namespace' => 'kube-system',
      'labels' => {
        'k8s-app' => 'kube-registry',
        'version' => 'v0',
        'kubernetes.io/cluster-service' => 'true'
      }
    },
    'spec' => {
      'replicas' => 1,
      'selector' => {
        'k8s-app' => 'kube-registry',
        'version' => 'v0'
      },
      'template' => {
        'metadata' => {
          'labels' => {
            'k8s-app' => 'kube-registry',
            'version' => 'v0',
            'kubernetes.io/cluster-service' => 'true'
          }
        },
        'spec' => {
          'containers' => [
            {
              'name' => 'registry',
              'image' => 'registry:2',
              'env' => [
                {
                  'name' => 'REGISTRY_HTTP_ADDR',
                  'value' => ':5000'
                },
                {
                  'name' => 'REGISTRY_STORAGE_FILESYSTEM_ROOTDIRECTORY',
                  'value' => '/var/lib/registry'
                },
                {
                  'name' => 'REGISTRY_STORAGE_DELETE_ENABLED',
                  'value' => 'true',
                }
              ],
              'volumeMounts' => [
                {
                  'name' => 'image-store',
                  'mountPath' => '/var/lib/registry'
                }
              ],
              'ports' => [
                {
                  'containerPort' => 5000,
                  'name' => 'registry',
                  'protocol' => 'TCP'
                }
              ]
            }
          ],
          'volumes' => [
            {
              'name' => 'image-store',
              'hostPath' => {
                'path' => '/var/lib/docker/registry_data'
              }
            }
          ]
        }
      }
    }
  }
end

#_minikube_registry_service_specObject



335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# File 'lib/dapp/kube/dapp/command/minikube_setup.rb', line 335

def _minikube_registry_service_spec
  {
    'metadata' => {
      'name' => 'kube-registry',
      'namespace' => 'kube-system',
      'labels' => {
        'k8s-app' => 'kube-registry',
        'kubernetes.io/cluster-service' => 'true',
        'kubernetes.io/name' => 'KubeRegistry'
      }
    },
    'spec' => {
      'selector' => {
        'k8s-app' => 'kube-registry'
      },
      'ports' => [
        {
          'name' => 'registry',
          'port' => 5000,
          'protocol' => 'TCP'
        }
      ]
    }
  }
end

#_minikube_restart_minikubeObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/dapp/kube/dapp/command/minikube_setup.rb', line 29

def _minikube_restart_minikube
  log_process("Restart minikube") do
    raise ::Dapp::Error::Command, code: :minikube_not_found if shellout('which minikube').exitstatus == 1

    Process.fork do
      _minikube_set_original_sudo_caller_process_user!

      begin
        if shellout('minikube status').stdout.split("\n").map(&:strip).first =~ /minikube(VM)?: Running/
          shellout! 'minikube stop', verbose: true
        end
        shellout! 'minikube start --insecure-registry localhost:5000', verbose: true
      rescue ::Dapp::Error::Shellout
        exit 1
      end
    end

    _, status = Process.wait2
    raise ::Dapp::Error::Command, code: :cannot_restart_minikube unless status.success?
  end
end

#_minikube_run_daemon(name, &blk) ⇒ Object



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
94
95
# File 'lib/dapp/kube/dapp/command/minikube_setup.rb', line 51

def _minikube_run_daemon(name, &blk)
  uid = Process.euid
  if uid != 0 && File.exists?("/run/user/#{uid}")
    pid_file_path = "/run/user/#{uid}/dapp_#{name}_daemon.pid"
  elsif uid != 0
    pid_file_path = "/tmp/dapp_#{name}_daemon.pid"
  else
    pid_file_path = "/run/dapp_#{name}_daemon.pid"
  end

  old_daemon_pid = begin
    File.open(pid_file_path, 'r').read.strip.to_i
  rescue Errno::ENOENT
  end
  if old_daemon_pid
    begin
      if Process.kill(0, old_daemon_pid)
        Process.kill('TERM', old_daemon_pid)
      end
      if Process.kill(0, old_daemon_pid)
        Process.kill('KILL', old_daemon_pid)
      end
    rescue Errno::ESRCH
    end
  end

  daemon_pid = Process.fork do
    File.open(pid_file_path, 'w') {|f| f.write "#{Process.pid}\n"}
    yield
  end
  Process.detach(daemon_pid)

  daemon_ok = true
  begin
    Process.kill(0, daemon_pid)

    5.times do
      sleep 1
      Process.kill(0, daemon_pid)
    end
  rescue Errno::ESRCH
    daemon_ok = false
  end
  raise ::Dapp::Error::Command, code: :"#{name}_daemon_failed" unless daemon_ok
end

#_minikube_run_minikube_persisted_storage_daemonObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/dapp/kube/dapp/command/minikube_setup.rb', line 97

def _minikube_run_minikube_persisted_storage_daemon
  log_process("Run minikube persisted storage daemon") do
    _minikube_run_daemon(:minikube_persisted_storage) do
      _minikube_set_original_sudo_caller_process_user!

      STDIN.reopen '/dev/null'
      STDOUT.reopen '/dev/null', 'a'

      mountdir = File.join(ENV['HOME'], '.minikube', 'persisted_storage')
      FileUtils.mkdir_p mountdir

      exec "minikube mount #{mountdir}"
    end
  end
end

#_minikube_run_registryObject



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/dapp/kube/dapp/command/minikube_setup.rb', line 128

def _minikube_run_registry
  log_process("Run registry") do
    _minikube_kubernetes.with_query(gracePeriodSeconds: 0) do
      if _minikube_kubernetes.replicationcontroller? _minikube_registry_replicationcontroller_spec['metadata']['name']
        _minikube_kubernetes.delete_replicationcontroller! _minikube_registry_replicationcontroller_spec['metadata']['name']

        shutdown_ok = false
        600.times do
          unless _minikube_kubernetes.replicationcontroller? _minikube_registry_replicationcontroller_spec['metadata']['name']
            shutdown_ok = true
            break
          end
          sleep 1
        end
        raise ::Dapp::Error::Command, code: :registry_replicationcontroller_shutdown_failed unless shutdown_ok
      end

      _minikube_kubernetes.delete_pods! labelSelector: 'k8s-app=kube-registry'
      shutdown_ok = false
      600.times do
        unless _minikube_find_registry_pod
          shutdown_ok = true
          break
        end
        sleep 1
      end
      raise ::Dapp::Error::Command, code: :registry_pod_shutdown_failed unless shutdown_ok

      if _minikube_kubernetes.service? _minikube_registry_service_spec['metadata']['name']
        _minikube_kubernetes.delete_service! _minikube_registry_service_spec['metadata']['name']

        shutdown_ok = false
        600.times do
          unless _minikube_kubernetes.service? _minikube_registry_service_spec['metadata']['name']
            shutdown_ok = true
            break
          end
          sleep 1
        end
        raise ::Dapp::Error::Command, code: :registry_service_shutdown_failed unless shutdown_ok
      end

      if _minikube_kubernetes.pod? _minikube_registry_proxy_pod_spec['metadata']['name']
        _minikube_kubernetes.delete_pod! _minikube_registry_proxy_pod_spec['metadata']['name']

        shutdown_ok = false
        600.times do
          unless _minikube_kubernetes.pod? _minikube_registry_proxy_pod_spec['metadata']['name']
            shutdown_ok = true
            break
          end
          sleep 1
        end
        raise ::Dapp::Error::Command, code: :registry_proxy_pod_shutdown_failed unless shutdown_ok
      end

      _minikube_kubernetes.create_replicationcontroller!(_minikube_registry_replicationcontroller_spec)
      registry_pod_ok = false
      600.times do
        if registry_pod = _minikube_find_registry_pod
          if registry_pod['status']['phase'] == 'Running'
            registry_pod_ok = true
            @_minikube_registry_pod_name = registry_pod['metadata']['name']
            break
          end
        end
        sleep 1
      end
      raise ::Dapp::Error::Command, code: :registry_pod_not_ok unless registry_pod_ok

      _minikube_kubernetes.create_service!(_minikube_registry_service_spec)
      registry_service_ok = false
      600.times do
        if _minikube_kubernetes.service? _minikube_registry_service_spec['metadata']['name']
          registry_service_ok = true
          break
        end
        sleep 1
      end
      raise ::Dapp::Error::Command, code: :registry_service_not_ok unless registry_service_ok

      _minikube_kubernetes.create_pod! _minikube_registry_proxy_pod_spec
      registry_proxy_pod_ok = false
      600.times do
        if _minikube_kubernetes.pod? _minikube_registry_proxy_pod_spec['metadata']['name']
          registry_proxy_pod = _minikube_kubernetes.pod(_minikube_registry_proxy_pod_spec['metadata']['name'])
          if registry_proxy_pod['status']['phase'] == 'Running'
            registry_proxy_pod_ok = true
            break
          end
        end
        sleep 1
      end
      raise ::Dapp::Error::Command, code: :registry_proxy_pod_not_ok unless registry_proxy_pod_ok
    end
  end
end

#_minikube_run_registry_forwarder_daemonObject



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/dapp/kube/dapp/command/minikube_setup.rb', line 226

def _minikube_run_registry_forwarder_daemon
  log_process("Run registry forwarder daemon") do
    registry_port_in_use = begin
      Socket.tcp('localhost', 5000).close
      true
    rescue Errno::ECONNREFUSED
      false
    end
    raise ::Dapp::Error::Command, code: :registry_port_in_use, data: {host: 'localhost', port: 5000} if registry_port_in_use

    _minikube_run_daemon(:registry_forwarder) do
      STDIN.reopen '/dev/null'
      STDOUT.reopen '/dev/null', 'a'

      exec "kubectl port-forward --namespace kube-system #{_minikube_registry_pod_name} 5000:5000"
    end
  end
end

#_minikube_set_original_sudo_caller_process_user!Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/dapp/kube/dapp/command/minikube_setup.rb', line 18

def _minikube_set_original_sudo_caller_process_user!
  if ENV['SUDO_UID'] and ENV['SUDO_GID']
    original_uid = ENV['SUDO_UID'].to_i
    original_gid = ENV['SUDO_GID'].to_i
    if original_uid != Process.uid && ENV['HOME'] == Etc.getpwuid(original_uid).dir
      Process::Sys.setgid(original_gid)
      Process::Sys.setuid(original_uid)
    end
  end
end

#_minikube_wait_till_readyObject



113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/dapp/kube/dapp/command/minikube_setup.rb', line 113

def _minikube_wait_till_ready
  log_process("Wait till minikube ready") do
    600.times do
      begin
        return if _minikube_kubernetes.service?('kube-dns')
      rescue Kubernetes::Client::Error::ConnectionRefused
      end

      sleep 1
    end

    raise ::Dapp::Error::Command, code: :minikube_readiness_timeout
  end
end

#kube_minikube_setupObject



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/dapp/kube/dapp/command/minikube_setup.rb', line 6

def kube_minikube_setup
  _minikube_restart_minikube

  # NOTICE: На данный момент выключено из-за непригодной
  # NOTICE: для использования медленности 'minikube mount' томов
  # _minikube_run_minikube_persisted_storage_daemon

  _minikube_wait_till_ready
  _minikube_run_registry
  _minikube_run_registry_forwarder_daemon
end