kubeclient 2.4.0 → 2.5.1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of kubeclient might be problematic. Click here for more details.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8fce3b9093ea679f3884d8af4728dfc3005a0c36
4
- data.tar.gz: 561a92297a3da43106d56ebf7bef5d0e572f0904
3
+ metadata.gz: a5552be66b20798c35381487804e97569949799f
4
+ data.tar.gz: 83a18e7c1c6239b6b1bb1e201134200caac6d4ee
5
5
  SHA512:
6
- metadata.gz: e250ffc5c7246e29340212a65132496e4aff7aacd88cac3906348bc1b62988104af59ac6c50adc83f036132cbd989d958cdf728afc4679f9aa1b60b4b1e4b5ff
7
- data.tar.gz: 4d1f175cf90cbf673f16e55601bb7b86801f5a7fbd6e7ec1507517761e83733b0a45a8c43848caf608f7389e29350ad5b6f3389ec12957f1de613ed45d9d5601
6
+ metadata.gz: f71802cdea55b41565cf7e044b740ceeb280a763cb8ef5230a5fe470944acbb798b3a26cd6f684c90468a666a763c0eff1c6c59a21c487e6c56d0e2fa4151a45
7
+ data.tar.gz: ff7daf8d166dc97c26b89a5e08576b18456d0eddff34b9413e9b6dfd1680e527deb6af8e284fda1c6830dbb3cb8d77871dd1ad477803b5ce1ffb0bdaea498c70
data/README.md CHANGED
@@ -276,6 +276,14 @@ service = client.get_service "guestbook", 'development'
276
276
  Note - Kubernetes doesn't work with the uid, but rather with the 'name' property.
277
277
  Querying with uid causes 404.
278
278
 
279
+ #### Getting raw responses
280
+ By passing `as: :raw`, the response from the client is given as a string, which is the raw JSON body from openshift:
281
+
282
+ ```ruby
283
+ pods = client.get_pods as: :raw
284
+ node = client.get_node "127.0.0.1", as: :raw
285
+ ```
286
+
279
287
  #### Delete an entity (by name)
280
288
 
281
289
  For example: `delete_pod "pod name"` , `delete_replication_controller "rc name"`, `delete_node "node name"`, `delete_secret "secret name"`
@@ -189,8 +189,9 @@ module Kubeclient
189
189
  end
190
190
 
191
191
  # get a single entity of a specific type by name
192
- define_singleton_method("get_#{entity.method_names[0]}") do |name, namespace = nil|
193
- get_entity(klass, entity.resource_name, name, namespace)
192
+ define_singleton_method("get_#{entity.method_names[0]}") \
193
+ do |name, namespace = nil, opts = {}|
194
+ get_entity(klass, entity.resource_name, name, namespace, opts)
194
195
  end
195
196
 
196
197
  define_singleton_method("delete_#{entity.method_names[0]}") do |name, namespace = nil|
@@ -238,12 +239,12 @@ module Kubeclient
238
239
  end
239
240
  end
240
241
 
241
- # Accepts the following string options:
242
- # :namespace - the namespace of the entity.
243
- # :name - the name of the entity to watch.
244
- # :label_selector - a selector to restrict the list of returned objects by their labels.
245
- # :field_selector - a selector to restrict the list of returned objects by their fields.
246
- # :resource_version - shows changes that occur after that particular version of a resource.
242
+ # Accepts the following options:
243
+ # :namespace (string) - the namespace of the entity.
244
+ # :name (string) - the name of the entity to watch.
245
+ # :label_selector (string) - a selector to restrict the list of returned objects by labels.
246
+ # :field_selector (string) - a selector to restrict the list of returned objects by fields.
247
+ # :resource_version (string) - shows changes that occur after passed version of a resource.
247
248
  def watch_entities(resource_name, options = {})
248
249
  ns = build_namespace_prefix(options[:namespace])
249
250
 
@@ -258,10 +259,14 @@ module Kubeclient
258
259
  Kubeclient::Common::WatchStream.new(uri, http_options(uri))
259
260
  end
260
261
 
261
- # Accepts the following string options:
262
- # :namespace - the namespace of the entity.
263
- # :label_selector - a selector to restrict the list of returned objects by their labels.
264
- # :field_selector - a selector to restrict the list of returned objects by their fields.
262
+ # Accepts the following options:
263
+ # :namespace (string) - the namespace of the entity.
264
+ # :label_selector (string) - a selector to restrict the list of returned objects by labels.
265
+ # :field_selector (string) - a selector to restrict the list of returned objects by fields.
266
+ # :as (symbol) - if :raw, return the raw response body (as a string)
267
+ #
268
+ # Default response type will return a collection RecursiveOpenStruct
269
+ # (:ros) objects, unless `:as` is passed with `:raw`.
265
270
  def get_entities(entity_type, klass, resource_name, options = {})
266
271
  params = {}
267
272
  SEARCH_ARGUMENTS.each { |k, v| params[k] = options[v] if options[v] }
@@ -271,6 +276,7 @@ module Kubeclient
271
276
  rest_client[ns_prefix + resource_name]
272
277
  .get({ 'params' => params }.merge(@headers))
273
278
  end
279
+ return response.body if options[:as] == :raw
274
280
 
275
281
  result = JSON.parse(response)
276
282
 
@@ -286,12 +292,19 @@ module Kubeclient
286
292
  Kubeclient::Common::EntityList.new(entity_type, resource_version, collection)
287
293
  end
288
294
 
289
- def get_entity(klass, resource_name, name, namespace = nil)
295
+ # Accepts the following options:
296
+ # :as (symbol) - if :raw, return the raw response body (as a string)
297
+ #
298
+ # Default response type will return an entity as a RecursiveOpenStruct
299
+ # (:ros) object, unless `:as` is passed with `:raw`.
300
+ def get_entity(klass, resource_name, name, namespace = nil, options = {})
290
301
  ns_prefix = build_namespace_prefix(namespace)
291
302
  response = handle_exception do
292
303
  rest_client[ns_prefix + resource_name + "/#{name}"]
293
304
  .get(@headers)
294
305
  end
306
+ return response.body if options[:as] == :raw
307
+
295
308
  result = JSON.parse(response)
296
309
  new_entity(result, klass)
297
310
  end
@@ -354,14 +367,14 @@ module Kubeclient
354
367
  klass.new(hash)
355
368
  end
356
369
 
357
- def all_entities
370
+ def all_entities(options = {})
358
371
  discover unless @discovered
359
372
  @entities.values.each_with_object({}) do |entity, result_hash|
360
373
  # method call for get each entities
361
374
  # build hash of entity name to array of the entities
362
375
  method_name = "get_#{entity.method_names[1]}"
363
376
  begin
364
- result_hash[entity.method_names[0]] = send(method_name)
377
+ result_hash[entity.method_names[0]] = send(method_name, options)
365
378
  rescue KubeException
366
379
  next # do not fail due to resources not supporting get
367
380
  end
@@ -1,4 +1,4 @@
1
1
  # Kubernetes REST-API Client
2
2
  module Kubeclient
3
- VERSION = '2.4.0'
3
+ VERSION = '2.5.1'
4
4
  end
@@ -183,6 +183,22 @@ class KubeClientTest < MiniTest::Test
183
183
  assert_equal(404, exception.error_code)
184
184
  end
185
185
 
186
+ def test_nonjson_exception_raw
187
+ stub_request(:get, %r{/api/v1$})
188
+ .to_return(body: open_test_file('core_api_resource_list.json'), status: 200)
189
+ stub_request(:get, %r{/servic})
190
+ .to_return(body: open_test_file('service_illegal_json_404.json'), status: 404)
191
+
192
+ client = Kubeclient::Client.new('http://localhost:8080/api/', 'v1')
193
+
194
+ exception = assert_raises(KubeException) do
195
+ client.get_services(as: :raw)
196
+ end
197
+
198
+ assert(exception.message.include?('Not Found'))
199
+ assert_equal(404, exception.error_code)
200
+ end
201
+
186
202
  def test_entity_list
187
203
  stub_request(:get, %r{/api/v1$})
188
204
  .to_return(body: open_test_file('core_api_resource_list.json'),
@@ -206,6 +222,34 @@ class KubeClientTest < MiniTest::Test
206
222
  times: 1)
207
223
  end
208
224
 
225
+ def test_entity_list_raw
226
+ stub_request(:get, %r{/api/v1$})
227
+ .to_return(body: open_test_file('core_api_resource_list.json'), status: 200)
228
+ stub_request(:get, %r{/services})
229
+ .to_return(body: open_test_file('entity_list.json'), status: 200)
230
+
231
+ client = Kubeclient::Client.new('http://localhost:8080/api/', 'v1')
232
+ response = client.get_services(as: :raw)
233
+
234
+ refute_empty(response)
235
+ assert_equal(open_test_file('entity_list.json').read, response)
236
+
237
+ assert_requested(:get, 'http://localhost:8080/api/v1/services', times: 1)
238
+ end
239
+
240
+ def test_entity_list_raw_failure
241
+ stub_request(:get, %r{/api/v1$})
242
+ .to_return(body: open_test_file('core_api_resource_list.json'), status: 200)
243
+ stub_request(:get, %r{/services})
244
+ .to_return(body: open_test_file('entity_list.json'), status: 500)
245
+
246
+ client = Kubeclient::Client.new('http://localhost:8080/api/', 'v1')
247
+
248
+ exception = assert_raises(KubeException) { client.get_services(as: :raw) }
249
+ assert_equal('500 Internal Server Error', exception.message)
250
+ assert_equal(500, exception.error_code)
251
+ end
252
+
209
253
  def test_entities_with_label_selector
210
254
  selector = 'component=apiserver'
211
255
 
@@ -354,6 +398,74 @@ class KubeClientTest < MiniTest::Test
354
398
  assert_instance_of(Kubeclient::ServiceAccount, result['service_account'][0])
355
399
  end
356
400
 
401
+ def test_get_all_raw
402
+ stub_request(:get, %r{/api/v1$})
403
+ .to_return(body: open_test_file('core_api_resource_list.json'), status: 200)
404
+
405
+ stub_request(:get, %r{/bindings})
406
+ .to_return(body: open_test_file('bindings_list.json'), status: 404)
407
+
408
+ stub_request(:get, %r{/configmaps})
409
+ .to_return(body: open_test_file('config_map_list.json'), status: 200)
410
+
411
+ stub_request(:get, %r{/podtemplates})
412
+ .to_return(body: open_test_file('pod_template_list.json'), status: 200)
413
+
414
+ stub_request(:get, %r{/services})
415
+ .to_return(body: open_test_file('service_list.json'), status: 200)
416
+
417
+ stub_request(:get, %r{/pods})
418
+ .to_return(body: open_test_file('pod_list.json'), status: 200)
419
+
420
+ stub_request(:get, %r{/nodes})
421
+ .to_return(body: open_test_file('node_list.json'), status: 200)
422
+
423
+ stub_request(:get, %r{/replicationcontrollers})
424
+ .to_return(body: open_test_file('replication_controller_list.json'), status: 200)
425
+
426
+ stub_request(:get, %r{/events})
427
+ .to_return(body: open_test_file('event_list.json'), status: 200)
428
+
429
+ stub_request(:get, %r{/endpoints})
430
+ .to_return(body: open_test_file('endpoint_list.json'), status: 200)
431
+
432
+ stub_request(:get, %r{/namespaces})
433
+ .to_return(body: open_test_file('namespace_list.json'), status: 200)
434
+
435
+ stub_request(:get, %r{/secrets})
436
+ .to_return(body: open_test_file('secret_list.json'), status: 200)
437
+
438
+ stub_request(:get, %r{/resourcequotas})
439
+ .to_return(body: open_test_file('resource_quota_list.json'), status: 200)
440
+
441
+ stub_request(:get, %r{/limitranges})
442
+ .to_return(body: open_test_file('limit_range_list.json'), status: 200)
443
+
444
+ stub_request(:get, %r{/persistentvolumes})
445
+ .to_return(body: open_test_file('persistent_volume_list.json'), status: 200)
446
+
447
+ stub_request(:get, %r{/persistentvolumeclaims})
448
+ .to_return(body: open_test_file('persistent_volume_claim_list.json'), status: 200)
449
+
450
+ stub_request(:get, %r{/componentstatuses})
451
+ .to_return(body: open_test_file('component_status_list.json'), status: 200)
452
+
453
+ stub_request(:get, %r{/serviceaccounts})
454
+ .to_return(body: open_test_file('service_account_list.json'), status: 200)
455
+
456
+ client = Kubeclient::Client.new('http://localhost:8080/api/', 'v1')
457
+ result = client.all_entities(as: :raw)
458
+ assert_equal(16, result.keys.size)
459
+
460
+ %w(
461
+ component_status config_map endpoint event limit_range namespace node
462
+ persistent_volume persistent_volume_claim pod replication_controller
463
+ resource_quota secret service service_account
464
+ ).each do |entity|
465
+ assert_equal(open_test_file("#{entity}_list.json").read, result[entity])
466
+ end
467
+ end
468
+
357
469
  def test_api_bearer_token_with_params_success
358
470
  stub_request(:get, 'http://localhost:8080/api/v1/pods?labelSelector=name=redis-master')
359
471
  .with(headers: { Authorization: 'Bearer valid_token' })
@@ -415,6 +527,27 @@ class KubeClientTest < MiniTest::Test
415
527
  assert_equal(response, exception.response)
416
528
  end
417
529
 
530
+ def test_api_bearer_token_failure_raw
531
+ error_message =
532
+ '"/api/v1" is forbidden because ' \
533
+ 'system:anonymous cannot list on pods in'
534
+ response = OpenStruct.new(code: 401, message: error_message)
535
+
536
+ stub_request(:get, 'http://localhost:8080/api/v1')
537
+ .with(headers: { Authorization: 'Bearer invalid_token' })
538
+ .to_raise(KubeException.new(403, error_message, response))
539
+
540
+ client = Kubeclient::Client.new(
541
+ 'http://localhost:8080/api/',
542
+ auth_options: { bearer_token: 'invalid_token' }
543
+ )
544
+
545
+ exception = assert_raises(KubeException) { client.get_pods(as: :raw) }
546
+ assert_equal(403, exception.error_code)
547
+ assert_equal(error_message, exception.message)
548
+ assert_equal(response, exception.response)
549
+ end
550
+
418
551
  def test_api_basic_auth_success
419
552
  stub_request(:get, 'http://username:password@localhost:8080/api/v1')
420
553
  .to_return(body: open_test_file('core_api_resource_list.json'),
@@ -483,6 +616,28 @@ class KubeClientTest < MiniTest::Test
483
616
  times: 1)
484
617
  end
485
618
 
619
+ def test_api_basic_auth_failure_raw
620
+ error_message = 'HTTP status code 401, 401 Unauthorized'
621
+ response = OpenStruct.new(code: 401, message: '401 Unauthorized')
622
+
623
+ stub_request(:get, 'http://username:password@localhost:8080/api/v1')
624
+ .to_raise(KubeException.new(401, error_message, response))
625
+
626
+ client = Kubeclient::Client.new(
627
+ 'http://localhost:8080/api/',
628
+ auth_options: { username: 'username', password: 'password' }
629
+ )
630
+
631
+ exception = assert_raises(KubeException) { client.get_pods(as: :raw) }
632
+ assert_equal(401, exception.error_code)
633
+ assert_equal(error_message, exception.message)
634
+ assert_equal(response, exception.response)
635
+
636
+ assert_requested(:get,
637
+ 'http://username:password@localhost:8080/api/v1',
638
+ times: 1)
639
+ end
640
+
486
641
  def test_init_username_no_password
487
642
  expected_msg = 'Basic auth requires both username & password'
488
643
  exception = assert_raises(ArgumentError) do
@@ -28,4 +28,43 @@ class TestNode < MiniTest::Test
28
28
  'http://localhost:8080/api/v1/nodes/127.0.0.1',
29
29
  times: 1)
30
30
  end
31
+
32
+ def test_get_from_json_v1_raw
33
+ stub_request(:get, %r{/nodes})
34
+ .to_return(body: open_test_file('node.json'), status: 200)
35
+ stub_request(:get, %r{/api/v1$})
36
+ .to_return(body: open_test_file('core_api_resource_list.json'), status: 200)
37
+
38
+ client = Kubeclient::Client.new('http://localhost:8080/api/', 'v1')
39
+ response = client.get_node('127.0.0.1', nil, as: :raw)
40
+
41
+ assert_equal(open_test_file('node.json').read, response)
42
+
43
+ assert_requested(
44
+ :get,
45
+ 'http://localhost:8080/api/v1',
46
+ times: 1
47
+ )
48
+ assert_requested(
49
+ :get,
50
+ 'http://localhost:8080/api/v1/nodes/127.0.0.1',
51
+ times: 1
52
+ )
53
+ end
54
+
55
+ def test_get_from_json_v1_raw_error
56
+ stub_request(:get, %r{/nodes})
57
+ .to_return(body: open_test_file('node.json'), status: 200)
58
+ stub_request(:get, %r{/api/v1$})
59
+ .to_return(body: open_test_file('core_api_resource_list.json'), status: 500)
60
+
61
+ client = Kubeclient::Client.new('http://localhost:8080/api/', 'v1')
62
+
63
+ exception = assert_raises(KubeException) do
64
+ client.get_node('127.0.0.1', nil, as: :raw)
65
+ end
66
+
67
+ assert_instance_of(KubeException, exception)
68
+ assert_equal('500 Internal Server Error', exception.message)
69
+ end
31
70
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kubeclient
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.4.0
4
+ version: 2.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alissa Bonas
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-05-10 00:00:00.000000000 Z
11
+ date: 2017-10-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler