dapp 0.29.2 → 0.30.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. checksums.yaml +5 -5
  2. data/bin/dapp +1 -23
  3. data/config/en/net_status.yml +5 -4
  4. data/lib/dapp.rb +1 -1
  5. data/lib/dapp/dapp/sentry.rb +11 -8
  6. data/lib/dapp/dimg/dapp/command/cleanup_repo.rb +33 -21
  7. data/lib/dapp/dimg/dimg/git_artifact.rb +2 -5
  8. data/lib/dapp/dimg/git_artifact.rb +41 -23
  9. data/lib/dapp/dimg/git_repo/base.rb +19 -39
  10. data/lib/dapp/dimg/git_repo/local.rb +15 -14
  11. data/lib/dapp/dimg/git_repo/remote.rb +32 -19
  12. data/lib/dapp/kube/cli/command/kube/deploy.rb +4 -0
  13. data/lib/dapp/kube/cli/command/kube/dismiss.rb +4 -0
  14. data/lib/dapp/kube/cli/command/kube/lint.rb +4 -0
  15. data/lib/dapp/kube/cli/command/kube/render.rb +4 -0
  16. data/lib/dapp/kube/cli/command/kube/value_get.rb +4 -0
  17. data/lib/dapp/kube/dapp/command/common.rb +21 -18
  18. data/lib/dapp/kube/dapp/command/minikube_setup.rb +6 -2
  19. data/lib/dapp/kube/helm/release.rb +1 -1
  20. data/lib/dapp/kube/helm/values.rb +3 -3
  21. data/lib/dapp/kube/kubernetes/client.rb +21 -90
  22. data/lib/dapp/kube/kubernetes/config.rb +129 -0
  23. data/lib/dapp/version.rb +1 -1
  24. metadata +6 -44
  25. data/lib/dapp/deployment/app.rb +0 -120
  26. data/lib/dapp/deployment/cli/cli.rb +0 -1
  27. data/lib/dapp/deployment/cli/command/base.rb +0 -14
  28. data/lib/dapp/deployment/cli/command/deployment.rb +0 -18
  29. data/lib/dapp/deployment/cli/command/deployment/apply.rb +0 -26
  30. data/lib/dapp/deployment/cli/command/deployment/mrproper.rb +0 -13
  31. data/lib/dapp/deployment/config/config.rb +0 -46
  32. data/lib/dapp/deployment/config/directive/app.rb +0 -28
  33. data/lib/dapp/deployment/config/directive/app/instance_methods.rb +0 -51
  34. data/lib/dapp/deployment/config/directive/base.rb +0 -13
  35. data/lib/dapp/deployment/config/directive/deployment.rb +0 -11
  36. data/lib/dapp/deployment/config/directive/expose.rb +0 -68
  37. data/lib/dapp/deployment/config/directive/group.rb +0 -27
  38. data/lib/dapp/deployment/config/directive/job.rb +0 -28
  39. data/lib/dapp/deployment/config/directive/mod/group.rb +0 -22
  40. data/lib/dapp/deployment/config/directive/mod/jobs.rb +0 -26
  41. data/lib/dapp/deployment/config/directive/namespace.rb +0 -26
  42. data/lib/dapp/deployment/config/directive/namespace/instance_methods.rb +0 -37
  43. data/lib/dapp/deployment/core_ext/hash.rb +0 -19
  44. data/lib/dapp/deployment/dapp/command/apply.rb +0 -71
  45. data/lib/dapp/deployment/dapp/command/common.rb +0 -17
  46. data/lib/dapp/deployment/dapp/command/mrproper.rb +0 -12
  47. data/lib/dapp/deployment/dapp/dapp.rb +0 -15
  48. data/lib/dapp/deployment/dapp/dappfile.rb +0 -23
  49. data/lib/dapp/deployment/deployment.rb +0 -46
  50. data/lib/dapp/deployment/error/app.rb +0 -7
  51. data/lib/dapp/deployment/error/default.rb +0 -7
  52. data/lib/dapp/deployment/error/deployment.rb +0 -7
  53. data/lib/dapp/deployment/error/kubernetes.rb +0 -7
  54. data/lib/dapp/deployment/kube_app.rb +0 -146
  55. data/lib/dapp/deployment/kube_base.rb +0 -97
  56. data/lib/dapp/deployment/kube_deployment.rb +0 -51
  57. data/lib/dapp/deployment/kubernetes.rb +0 -174
  58. data/lib/dapp/deployment/kubernetes/error.rb +0 -20
  59. data/lib/dapp/deployment/mod/jobs.rb +0 -37
  60. data/lib/dapp/deployment/mod/namespace.rb +0 -58
  61. data/lib/dapp/deployment/mod/system_environments.rb +0 -30
  62. data/lib/dapp/deployment/secret.rb +0 -93
  63. data/lib/dapp/kube/error/kubernetes.rb +0 -7
@@ -2,15 +2,14 @@ module Dapp
2
2
  module Dimg
3
3
  module GitRepo
4
4
  class Remote < Base
5
- CACHE_VERSION = 1
5
+ CACHE_VERSION = 2
6
6
 
7
7
  attr_reader :url
8
- attr_reader :path
9
8
 
10
9
  class << self
11
- def get_or_create(dapp, name, url:, branch:, ignore_git_fetch: false)
12
- key = [url, branch, ignore_git_fetch]
13
- inverse_key = [url, branch, !ignore_git_fetch]
10
+ def get_or_create(dapp, name, url:, ignore_git_fetch: false)
11
+ key = [url, ignore_git_fetch]
12
+ inverse_key = [url, !ignore_git_fetch]
14
13
 
15
14
  repositories[key] ||= begin
16
15
  if repositories.key?(inverse_key)
@@ -18,7 +17,7 @@ module Dapp
18
17
  else
19
18
  new(dapp, name, url: url)
20
19
  end.tap do |repo|
21
- repo.fetch!(branch, ignore_git_fetch: ignore_git_fetch) unless ignore_git_fetch
20
+ repo.fetch! unless ignore_git_fetch
22
21
  end
23
22
  end
24
23
  end
@@ -63,13 +62,11 @@ module Dapp
63
62
  end
64
63
 
65
64
  def path
66
- Pathname(dapp.build_path("remote_git_repo", CACHE_VERSION.to_s, name).to_s)
65
+ Pathname(dapp.build_path("remote_git_repo", CACHE_VERSION.to_s, dapp.consistent_uniq_slugify(name)).to_s)
67
66
  end
68
67
 
69
- def fetch!(branch = nil, ignore_git_fetch: false)
68
+ def fetch!
70
69
  _with_lock do
71
- branch ||= self.branch
72
-
73
70
  cfg_path = path.join("config")
74
71
  cfg = IniFile.load(cfg_path)
75
72
  remote_origin_cfg = cfg['remote "origin"']
@@ -82,21 +79,19 @@ module Dapp
82
79
 
83
80
  dapp.log_secondary_process(dapp.t(code: 'process.git_artifact_fetch', data: { url: url }), short: true) do
84
81
  begin
85
- git.fetch('origin', [branch], credentials: _rugged_credentials)
82
+ git.remotes.each { |remote| remote.fetch(credentials: _rugged_credentials) }
86
83
  rescue Rugged::SshError => e
87
84
  raise Error::Rugged, code: :rugged_remote_error, data: { url: url, message: e.message.strip }
88
85
  end
89
- raise Error::Rugged, code: :branch_not_exist_in_remote_git_repository, data: { branch: branch, url: url } unless branch_exist?(branch)
90
86
  end
91
- end unless ignore_git_fetch || dapp.dry_run?
92
- end
93
-
94
- def branch_exist?(name)
95
- git.branches.exist?(branch_format(name))
87
+ end unless dapp.dry_run?
96
88
  end
97
89
 
98
- def latest_commit(name)
99
- git.ref("refs/remotes/#{branch_format(name)}").target_id
90
+ def latest_commit(branch)
91
+ git.ref("refs/remotes/#{branch_format(branch)}").tap do |ref|
92
+ raise Error::Rugged, code: :branch_not_exist_in_remote_git_repository, data: { branch: branch, url: url } if ref.nil?
93
+ break ref.target_id
94
+ end
100
95
  end
101
96
 
102
97
  def lookup_commit(commit)
@@ -105,6 +100,24 @@ module Dapp
105
100
  raise Error::Rugged, code: :commit_not_found_in_remote_git_repository, data: { commit: commit, url: url }
106
101
  end
107
102
 
103
+ def submodules_git(commit)
104
+ submodules_git_path(commit).tap do |git_path|
105
+ break begin
106
+ if git_path.directory?
107
+ Rugged::Repository.new(git_path.to_s)
108
+ else
109
+ Rugged::Repository.clone_at(path.to_s, git_path.to_s).tap do |submodules_git|
110
+ submodules_git.checkout(commit)
111
+ end
112
+ end
113
+ end
114
+ end
115
+ end
116
+
117
+ def submodules_git_path(commit)
118
+ Pathname(File.join(dapp.host_docker_tmp_config_dir, "submodule", dapp.consistent_uniq_slugify(name), commit).to_s)
119
+ end
120
+
108
121
  protected
109
122
 
110
123
  def git
@@ -15,6 +15,10 @@ BANNER
15
15
  long: '--namespace NAME',
16
16
  default: nil
17
17
 
18
+ option :context,
19
+ long: '--context NAME',
20
+ default: nil
21
+
18
22
  option :image_version,
19
23
  long: '--image-version TAG',
20
24
  description: "Custom tag (alias for --tag)",
@@ -13,6 +13,10 @@ BANNER
13
13
  long: '--namespace NAME',
14
14
  default: nil
15
15
 
16
+ option :context,
17
+ long: '--context NAME',
18
+ default: nil
19
+
16
20
  option :with_namespace,
17
21
  long: '--with-namespace',
18
22
  default: false
@@ -14,6 +14,10 @@ BANNER
14
14
  long: '--namespace NAME',
15
15
  default: nil
16
16
 
17
+ option :context,
18
+ long: '--context NAME',
19
+ default: nil
20
+
17
21
  option :helm_set_options,
18
22
  long: '--set STRING_ARRAY',
19
23
  default: [],
@@ -14,6 +14,10 @@ BANNER
14
14
  long: '--namespace NAME',
15
15
  default: nil
16
16
 
17
+ option :context,
18
+ long: '--context NAME',
19
+ default: nil
20
+
17
21
  option :tmp_dir_prefix,
18
22
  long: '--tmp-dir-prefix PREFIX',
19
23
  description: 'Tmp directory prefix (/tmp by default). Used for build process service directories.'
@@ -14,6 +14,10 @@ BANNER
14
14
  long: '--namespace NAME',
15
15
  default: nil
16
16
 
17
+ option :context,
18
+ long: '--context NAME',
19
+ default: nil
20
+
17
21
  option :registry_username,
18
22
  long: '--registry-username USERNAME'
19
23
 
@@ -341,29 +341,32 @@ image: {{ tuple $name $context | include "_dimg2" }}
341
341
  options[:namespace].nil? ? nil : consistent_uniq_slugify(options[:namespace])
342
342
  end
343
343
 
344
+ def context_option
345
+ options[:context]
346
+ end
347
+
348
+ def kube_context
349
+ ENV["KUBECONTEXT"] ||
350
+ context_option ||
351
+ kubernetes_config.current_context_name
352
+ end
353
+
344
354
  def kube_namespace
345
- namespace_option || begin
346
- namespace = "default"
347
-
348
- Kubernetes::Client.tap do |kube|
349
- kube_config = kube.kube_config(kube.kube_config_path)
350
- if kube_config
351
- kube_context_name = kube.kube_context_name(kube_config)
352
- kube_context_config = kube.kube_context_config(kube_config, kube_context_name)
353
-
354
- if kube_context_config
355
- context_namespace = kube.kube_context_namespace(kube_context_config)
356
- namespace = context_namespace if context_namespace
357
- end
358
- end
359
- end
355
+ namespace_option ||
356
+ kubernetes_config.namespace(kube_context) ||
357
+ "default"
358
+ end
360
359
 
361
- namespace
362
- end
360
+ def kubernetes_config
361
+ @kubernetes_config ||= Kubernetes::Config.new_auto
363
362
  end
364
363
 
365
364
  def kubernetes
366
- @kubernetes ||= Kubernetes::Client.new(namespace: kube_namespace)
365
+ @kubernetes ||= Kubernetes::Client.new(
366
+ kubernetes_config,
367
+ kube_context,
368
+ kube_namespace,
369
+ )
367
370
  end
368
371
  end
369
372
  end
@@ -34,7 +34,7 @@ module Dapp
34
34
  _minikube_set_original_sudo_caller_process_user!
35
35
 
36
36
  begin
37
- if shellout!('minikube status', verbose: true).stdout.split("\n").map(&:strip).first == 'minikubeVM: Running'
37
+ if shellout('minikube status').stdout.split("\n").map(&:strip).first =~ /minikube(VM)?: Running/
38
38
  shellout! 'minikube stop', verbose: true
39
39
  end
40
40
  shellout! 'minikube start --insecure-registry localhost:5000', verbose: true
@@ -251,7 +251,11 @@ module Dapp
251
251
  end
252
252
 
253
253
  def _minikube_kubernetes
254
- @_minikube_kubernetes ||= Kubernetes::Client.new(namespace: 'kube-system')
254
+ @_minikube_kubernetes ||= Kubernetes::Client.new(
255
+ kubernetes_config,
256
+ kubernetes_config.current_context_name,
257
+ 'kube-system'
258
+ )
255
259
  end
256
260
 
257
261
  def _minikube_registry_replicationcontroller_spec
@@ -151,7 +151,7 @@ module Dapp
151
151
 
152
152
  def helm_set_options(without_registry: false, fake: false)
153
153
  [].tap do |options|
154
- options.concat set.map {|opt| "--set #{opt}"}
154
+ options.concat set.map {|opt| "--set '#{opt}'"}
155
155
 
156
156
  service_values = Helm::Values.service_values(dapp, repo, namespace, docker_tag,
157
157
  without_registry: self.without_registry || without_registry,
@@ -1,7 +1,7 @@
1
1
  module Dapp
2
2
  module Kube
3
3
  class Helm::Values
4
- TEMPLATE_EMPTY_VALUE = "\\\"-\\\""
4
+ TEMPLATE_EMPTY_VALUE = '"-"'.freeze
5
5
 
6
6
  class << self
7
7
  def service_values(*a, &b)
@@ -17,7 +17,7 @@ module Dapp
17
17
  "repo" => repo,
18
18
  "docker_tag" => docker_tag,
19
19
  },
20
- "ci" => ENV.select { |k, _| k.start_with?("CI_") } ,
20
+ "ci" => ENV.select { |k, _| k.start_with?('CI_') },
21
21
  }
22
22
  }
23
23
 
@@ -140,7 +140,7 @@ module Dapp
140
140
  end
141
141
 
142
142
  def to_set_options
143
- as_set_options.map {|k, v| "--set #{k}=#{v}"}
143
+ as_set_options.map {|k, v| "--set '#{k}=#{v}'"}
144
144
  end
145
145
  end # Helm::ServiceValues
146
146
  end # Kube
@@ -35,12 +35,6 @@ module Dapp
35
35
  '/apis/batch/v1' => [:job, ],
36
36
  '/apis/batch/v1beta1' => [:cronjob, ],
37
37
  },
38
- '1.11' => {
39
- '/api/v1' => [:service, :replicationcontroller, :pod, :podtemplate, ],
40
- '/apis/apps/v1' => [:daemonset, :deployment, :replicaset, :statefulset, ],
41
- '/apis/batch/v1' => [:job, ],
42
- '/apis/batch/v1beta1' => [:cronjob, ],
43
- },
44
38
  'stable' => {
45
39
  '/api/v1' => [:service, :replicationcontroller, :pod, :podtemplate, ],
46
40
  '/apis/batch/v1' => [:job, ],
@@ -53,17 +47,18 @@ module Dapp
53
47
 
54
48
  ::Dapp::Dapp::Shellout::Base.default_env_keys << 'KUBECONFIG'
55
49
 
56
- #
57
- def initialize(namespace: nil)
50
+ attr_reader :config
51
+ attr_reader :context
52
+ attr_reader :namespace
53
+
54
+ def initialize(config, context, namespace)
55
+ @config = config
56
+ @context = context
58
57
  @namespace = namespace
59
58
  @query_parameters = {}
60
59
  @cluster_version
61
60
  end
62
61
 
63
- def namespace
64
- @namespace || self.class.kube_context_namespace(kube_context_config) || "default"
65
- end
66
-
67
62
  # Чтобы не перегружать методы явной передачей namespace.
68
63
  # Данный метод может пригодиться только в ситуации, когда надо указать другой namespace,
69
64
  # в большинстве случаев используется namespace из конструктора.
@@ -250,11 +245,13 @@ module Dapp
250
245
 
251
246
  def with_connection(excon_parameters: {}, &blk)
252
247
  connection = begin
253
- Excon.new(kube_cluster_config['cluster']['server'], **kube_server_options(excon_parameters)).tap(&:get)
248
+ context_config = config.context_config(context)
249
+ cluster_config = config.cluster_config(context_config['cluster'])
250
+ Excon.new(cluster_config['server'], **kube_server_options(excon_parameters)).tap(&:get)
254
251
  rescue Excon::Error::Socket => err
255
252
  raise Error::ConnectionRefused,
256
253
  code: :server_connection_refused,
257
- data: { kube_cluster_config: kube_cluster_config, kube_user_config: kube_user_config, error: err.message }
254
+ data: { url: cluster_config['server'], error: err.message }
258
255
  end
259
256
 
260
257
  return yield connection
@@ -262,22 +259,26 @@ module Dapp
262
259
 
263
260
  def kube_server_options(excon_parameters = {})
264
261
  {}.tap do |opts|
265
- client_cert = kube_config.fetch('users', [{}]).first.fetch('user', {}).fetch('client-certificate', nil)
262
+ context_config = config.context_config(context)
263
+ user_config = config.user_config(context_config['user'])
264
+ cluster_config = config.cluster_config(context_config['cluster'])
265
+
266
+ client_cert = user_config['client-certificate']
266
267
  opts[:client_cert] = client_cert if client_cert
267
268
 
268
- client_cert_data = kube_config.fetch('users', [{}]).first.fetch('user', {}).fetch('client-certificate-data', nil)
269
+ client_cert_data= user_config['client-certificate-data']
269
270
  opts[:client_cert_data] = Base64.decode64(client_cert_data) if client_cert_data
270
271
 
271
- client_key = kube_config.fetch('users', [{}]).first.fetch('user', {}).fetch('client-key', nil)
272
+ client_key = user_config['client-key']
272
273
  opts[:client_key] = client_key if client_key
273
274
 
274
- client_key_data = kube_config.fetch('users', [{}]).first.fetch('user', {}).fetch('client-key-data', nil)
275
+ client_key_data = user_config['client-key-data']
275
276
  opts[:client_key_data] = Base64.decode64(client_key_data) if client_key_data
276
277
 
277
278
  ssl_cert_store = OpenSSL::X509::Store.new
278
- if ssl_ca_file = kube_config.fetch('clusters', [{}]).first.fetch('cluster', {}).fetch('certificate-authority', nil)
279
+ if ssl_ca_file = cluster_config['certificate-authority']
279
280
  ssl_cert_store.add_file ssl_ca_file
280
- elsif ssl_ca_data = kube_config.fetch('clusters', [{}]).first.fetch('cluster', {}).fetch('certificate-authority-data', nil)
281
+ elsif ssl_ca_data = cluster_config['certificate-authority-data']
281
282
  ssl_cert_store.add_cert OpenSSL::X509::Certificate.new(Base64.decode64(ssl_ca_data))
282
283
  end
283
284
  opts[:ssl_cert_store] = ssl_cert_store
@@ -289,76 +290,6 @@ module Dapp
289
290
  opts.merge!(excon_parameters)
290
291
  end
291
292
  end
292
-
293
- def kube_user_config
294
- @kube_user_config ||= begin
295
- kube_user_config = self.class.kube_user_config(kube_config, kube_context_config['context']['user'])
296
- raise Error::BadConfig, code: :user_config_not_found, data: {config_path: self.class.kube_config_path, context: kube_context_config, user: kube_context_config['context']['user']} if kube_user_config.nil?
297
- kube_user_config
298
- end
299
- end
300
-
301
- def kube_cluster_config
302
- @kube_cluster_config ||= begin
303
- kube_cluster_config = self.class.kube_cluster_config(kube_config, kube_context_config['context']['cluster'])
304
- raise Error::BadConfig, code: :cluster_config_not_found, data: {config_path: self.class.kube_config_path, context: kube_context_config, cluster: kube_context_config['context']['cluster']} if kube_cluster_config.nil?
305
- kube_cluster_config
306
- end
307
- end
308
-
309
- def kube_context_config
310
- @kube_context_config ||= begin
311
- context_name = self.class.kube_context_name(kube_config)
312
- kube_context_config = self.class.kube_context_config(kube_config, context_name)
313
- raise Error::BadConfig, code: :config_context_not_found, data: {config_path: self.class.kube_config_path, config: kube_config, context_name: context_name} if kube_context_config.nil?
314
- kube_context_config
315
- end
316
- end
317
-
318
- def kube_config
319
- @kube_config ||= begin
320
- kube_config = self.class.kube_config(self.class.kube_config_path)
321
- raise Error::BadConfig, code: :config_not_found, data: { config_path: self.class.kube_config_path } if kube_config.nil?
322
- kube_config
323
- end
324
- end
325
-
326
- class << self
327
- def kube_config_path
328
- kube_config_path = ENV['KUBECONFIG']
329
- kube_config_path = File.join(ENV['HOME'], '.kube/config') unless kube_config_path
330
- kube_config_path
331
- end
332
-
333
- def kube_config(kube_config_path)
334
- yaml_load_file(kube_config_path) if File.exist?(kube_config_path)
335
- end
336
-
337
- def kube_context_name(kube_config)
338
- kube_config['current-context'] || begin
339
- if (context = kube_config.fetch('contexts', []).first)
340
- warn "[WARN] .kube/config current-context is not set, using context '#{context['name']}'"
341
- context['name']
342
- end
343
- end
344
- end
345
-
346
- def kube_context_config(kube_config, kube_context_name)
347
- kube_config.fetch('contexts', []).find {|context| context['name'] == kube_context_name}
348
- end
349
-
350
- def kube_user_config(kube_config, user_name)
351
- kube_config.fetch('users', []).find {|user| user['name'] == user_name}
352
- end
353
-
354
- def kube_cluster_config(kube_config, cluster_name)
355
- kube_config.fetch('clusters', []).find {|cluster| cluster['name'] == cluster_name}
356
- end
357
-
358
- def kube_context_namespace(kube_context_config)
359
- kube_context_config['context']['namespace']
360
- end
361
- end
362
293
  end # Client
363
294
  end # Kubernetes
364
295
  end # Kube
@@ -0,0 +1,129 @@
1
+ module Dapp
2
+ module Kube
3
+ module Kubernetes
4
+ class Config
5
+ extend Helper::YAML
6
+ extend Dapp::Shellout::Base
7
+
8
+ class << self
9
+ def new_auto_if_available
10
+ if ENV['KUBECONFIG']
11
+ Kubernetes::Config.new_from_kubeconfig(ENV['KUBECONFIG'])
12
+ elsif Kubernetes::Config.kubectl_available?
13
+ Kubernetes::Config.new_from_kubectl
14
+ else
15
+ default_path = File.join(ENV['HOME'], '.kube/config')
16
+ if File.exists? default_path
17
+ Kubernetes::Config.new_from_kubeconfig(default_path)
18
+ end
19
+ end
20
+ end
21
+
22
+ def new_auto
23
+ new_auto_if_available.tap do |cfg|
24
+ raise(Kubernetes::Error::Default,
25
+ code: :config_not_found,
26
+ data: { },
27
+ ) if cfg.nil?
28
+ end
29
+ end
30
+
31
+ def new_from_kubeconfig(path)
32
+ unless File.exists?(path)
33
+ raise(Kubernetes::Error::Default,
34
+ code: :config_file_not_found,
35
+ data: { config_path: path }
36
+ )
37
+ end
38
+ self.new yaml_load_file(path), path
39
+ end
40
+
41
+ def kubectl_available?
42
+ shellout("kubectl").exitstatus.zero?
43
+ end
44
+
45
+ def new_from_kubectl
46
+ cmd_res = shellout("kubectl config view")
47
+
48
+ shellout_cmd_should_succeed! cmd_res
49
+
50
+ self.new YAML.load(cmd_res.stdout), "kubectl config view"
51
+ end
52
+ end # << self
53
+
54
+ attr_reader :config_hash, :config_path
55
+
56
+ def initialize(config_hash, config_path)
57
+ @config_hash = config_hash
58
+ @config_path = config_path
59
+ end
60
+
61
+ def context_names
62
+ config_hash.fetch('contexts', []).map {|context| context['name']}
63
+ end
64
+
65
+ def current_context_name
66
+ @current_context_name ||= begin
67
+ config_hash['current-context'] || begin
68
+ if (context = config_hash.fetch('contexts', []).first)
69
+ warn "[WARN] .kube/config current-context is not set, using first context '#{context['name']}'"
70
+ context['name']
71
+ end
72
+ end
73
+ end
74
+ end
75
+
76
+ def context_config(context_name)
77
+ res = config_hash.fetch('contexts', [])
78
+ .find {|context| context['name'] == context_name}
79
+
80
+ raise(Kubernetes::Error::Default,
81
+ code: :context_config_not_found,
82
+ data: {config_path: config_path,
83
+ config: config_hash,
84
+ context_name: context_name}
85
+ ) if res.nil?
86
+
87
+ res['context']
88
+ end
89
+
90
+ def user_config(user_name)
91
+ res = config_hash.fetch('users', [])
92
+ .find {|user| user['name'] == user_name}
93
+
94
+ raise(Kubernetes::Error::Default,
95
+ code: :user_config_not_found,
96
+ data: {config_path: config_path,
97
+ user: user_name}
98
+ ) if res.nil?
99
+
100
+ res['user']
101
+ end
102
+
103
+ def cluster_config(cluster_name)
104
+ res = config_hash.fetch('clusters', [])
105
+ .find {|cluster| cluster['name'] == cluster_name}
106
+
107
+ raise(Kubernetes::Error::Default,
108
+ code: :cluster_config_not_found,
109
+ data: {config_path: config_path,
110
+ cluster: cluster_name}
111
+ ) if res.nil?
112
+
113
+ res['cluster']
114
+ end
115
+
116
+ def cluster_name(context_name)
117
+ cfg = context_config(context_name)
118
+ cfg['cluster'] if cfg
119
+ end
120
+
121
+ def namespace(context_name)
122
+ cfg = context_config(context_name)
123
+ cfg['namespace'] if cfg
124
+ end
125
+
126
+ end # Kubeconfig
127
+ end # Kubernetes
128
+ end # Kube
129
+ end # Dapp