dapp 0.18.3 → 0.18.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 767fd53a4c1d7206a3f9e7f94076eea3c21fb99f9221b8d05648f4798736a699
4
- data.tar.gz: be17e4e371969d98f96a7d7c1b79768b35bd277e262c0fa0295ce17d0db890f8
3
+ metadata.gz: 6c75ea637d359d2090871e5069ecab913d92ac7c1daaf2d683e0c70bc5fd636a
4
+ data.tar.gz: 6a0881dca7824efef341e27cd47e74e49874df9b6e806fa91d36ac78f234820e
5
5
  SHA512:
6
- metadata.gz: 6855db575b0ae8392f2c718fc226aa2d7c849623ecce99ca0331b9f7fd1e353ddf894e53345acd3a7e0da48469ea6db2b7168fe6d2c782bcefe35df73b844bcf
7
- data.tar.gz: f71e683cb58fc85bfdb96997dfee161dc77e021e8a554ad8bc54f12a59c1a93ce404a747eea85382abd536ef104a679a2af9e6e5ebc3f92013af87c512aff0bd
6
+ metadata.gz: 2046f3d7320ac03db82faef2251407a38ae63173b4ff90eff205a2ed04e5cccbfd562ee7c5a52bc9c0b62d01d080155f16b8d0056342ec54133dc842f77418fc
7
+ data.tar.gz: ed4bff41fc5a25d3e34cb3059dad42c3268dbed19fa90efa7f804798b21c5a0ef64d5906df9bd33826669ce5b9438d428d02f3eecbe5a454fb636a69939a8f52
@@ -143,10 +143,6 @@ module Dapp
143
143
  "#{name}-#{kube_namespace}".slugify
144
144
  end
145
145
 
146
- def kube_namespace
147
- kubernetes.namespace
148
- end
149
-
150
146
  { encode: :generate, decode: :extract }.each do |type, secret_method|
151
147
  define_method "kube_helm_#{type}_json" do |secret, json|
152
148
  change_json_value = proc do |value|
@@ -225,12 +221,34 @@ module Dapp
225
221
  @secret_key_not_found_in ||= []
226
222
  end
227
223
 
228
- def kubernetes
229
- @kubernetes ||= begin
230
- namespace = options[:namespace].nil? ? nil : options[:namespace].tr('_', '-')
231
- Kubernetes::Client.new(namespace: namespace)
224
+ def namespace_option
225
+ options[:namespace].nil? ? nil : options[:namespace].tr('_', '-')
226
+ end
227
+
228
+ def kube_namespace
229
+ namespace_option || begin
230
+ namespace = "default"
231
+
232
+ Kubernetes::Client.tap do |kube|
233
+ kube_config = kube.kube_config(kube.kube_config_path)
234
+ if kube_config
235
+ kube_context_name = kube.kube_context_name(kube_config)
236
+ kube_context_config = kube.kube_context_config(kube_config, kube_context_name)
237
+
238
+ if kube_context_config
239
+ context_namespace = kube.kube_context_namespace(kube_context_config)
240
+ namespace = context_namespace if context_namespace
241
+ end
242
+ end
243
+ end
244
+
245
+ namespace
232
246
  end
233
247
  end
248
+
249
+ def kubernetes
250
+ @kubernetes ||= Kubernetes::Client.new(namespace: kube_namespace)
251
+ end
234
252
  end
235
253
  end
236
254
  end
@@ -3,6 +3,7 @@ module Dapp
3
3
  module Kubernetes
4
4
  class Client
5
5
  include Helper::YAML
6
+ extend Helper::YAML
6
7
 
7
8
  ::Dapp::Dapp::Shellout::Base.default_env_keys << 'KUBECONFIG'
8
9
 
@@ -12,7 +13,7 @@ module Dapp
12
13
  end
13
14
 
14
15
  def namespace
15
- @namespace || kube_context_config['context']['namespace'] || 'default'
16
+ @namespace || self.class.kube_context_namespace(kube_context_config) || "default"
16
17
  end
17
18
 
18
19
  # Чтобы не перегружать методы явной передачей namespace.
@@ -222,48 +223,72 @@ module Dapp
222
223
 
223
224
  def kube_user_config
224
225
  @kube_user_config ||= begin
225
- kube_config.fetch('users', []).find {|user| user['name'] == kube_context_config['context']['user']} || begin
226
- raise Error::BadConfig, code: :kube_user_not_found, data: {context: kube_context_config}
227
- end
226
+ kube_user_config = self.class.kube_user_config(kube_config, kube_context_config['context']['user'])
227
+ raise Error::BadConfig, code: :kube_user_not_found, data: {context: kube_context_config} if kube_user_config.nil?
228
+ kube_user_config
228
229
  end
229
230
  end
230
231
 
231
232
  def kube_cluster_config
232
233
  @kube_cluster_config ||= begin
233
- kube_config.fetch('clusters', []).find {|cluster| cluster['name'] == kube_context_config['context']['cluster']} || begin
234
- raise Error::BadConfig, code: :kube_cluster_not_found, data: {context: kube_context_config}
235
- end
234
+ kube_cluster_config = self.class.kube_cluster_config(kube_config, kube_context_config['context']['cluster'])
235
+ raise Error::BadConfig, code: :kube_cluster_not_found, data: {context: kube_context_config} if kube_cluster_config.nil?
236
+ kube_cluster_config
236
237
  end
237
238
  end
238
239
 
239
240
  def kube_context_config
240
241
  @kube_context_config ||= begin
241
- kube_config.fetch('contexts', []).find {|context| context['name'] == kube_context_name} || begin
242
- raise Error::BadConfig, code: :kube_context_not_found, data: {context_name: kube_context_name}
243
- end
242
+ context_name = self.class.kube_context_name(kube_config)
243
+ kube_context_config = self.class.kube_context_config(kube_config, context_name)
244
+ raise Error::BadConfig, code: :kube_context_not_found, data: {context_name: context_name} if kube_context_config.nil?
245
+ kube_context_config
244
246
  end
245
247
  end
246
248
 
247
- def kube_context_name
248
- @kube_context_name ||= kube_config['current-context'] || begin
249
- if context = kube_config.fetch('contexts', []).first
250
- warn "[WARN] .kube/config current-context is not set, using context '#{context['name']}'"
251
- context['name']
252
- end
249
+ def kube_config
250
+ @kube_config ||= begin
251
+ kube_config = self.class.kube_config(self.class.kube_config_path)
252
+ raise Error::Base, code: :kube_config_not_found, data: { path: self.class.kube_config_path } if kube_config.nil?
253
+ kube_config
253
254
  end
254
255
  end
255
256
 
256
- def kube_config
257
- @kube_config ||= begin
257
+ class << self
258
+ def kube_config_path
258
259
  kube_config_path = ENV['KUBECONFIG']
259
260
  kube_config_path = File.join(ENV['HOME'], '.kube/config') unless kube_config_path
261
+ kube_config_path
262
+ end
260
263
 
261
- if File.exist?(kube_config_path)
262
- yaml_load_file(kube_config_path)
263
- else
264
- raise Error::Base, code: :kube_config_not_found, data: { path: kube_config_path }
264
+ def kube_config(kube_config_path)
265
+ yaml_load_file(kube_config_path) if File.exist?(kube_config_path)
266
+ end
267
+
268
+ def kube_context_name(kube_config)
269
+ kube_config['current-context'] || begin
270
+ if context = kube_config.fetch('contexts', []).first
271
+ warn "[WARN] .kube/config current-context is not set, using context '#{context['name']}'"
272
+ context['name']
273
+ end
265
274
  end
266
275
  end
276
+
277
+ def kube_context_config(kube_config, kube_context_name)
278
+ kube_config.fetch('contexts', []).find {|context| context['name'] == kube_context_name}
279
+ end
280
+
281
+ def kube_user_config(kube_config, user_name)
282
+ kube_config.fetch('users', []).find {|user| user['name'] == user_name}
283
+ end
284
+
285
+ def kube_cluster_config(kube_config, cluster_name)
286
+ kube_config.fetch('clusters', []).find {|cluster| cluster['name'] == cluster_name}
287
+ end
288
+
289
+ def kube_context_namespace(kube_context_config)
290
+ kube_context_config['context']['namespace']
291
+ end
267
292
  end
268
293
  end # Client
269
294
  end # Kubernetes
data/lib/dapp/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  module Dapp
2
- VERSION = '0.18.3'.freeze
2
+ VERSION = '0.18.4'.freeze
3
3
  BUILD_CACHE_VERSION = 20
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dapp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.18.3
4
+ version: 0.18.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dmitry Stolyarov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-11-08 00:00:00.000000000 Z
11
+ date: 2017-11-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mixlib-shellout