kubeclient 0.1.17 → 0.2.0

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: d22e1e45425956eb53bd9177f30d99985689a53f
4
- data.tar.gz: 2027dcdc5c43ba8920792de49c5b35ead7e98e64
3
+ metadata.gz: 45dfb8861491dea98be3173a783b1438c6e4483b
4
+ data.tar.gz: fe9da34142a77fe2ac714e99e9498330476b1b00
5
5
  SHA512:
6
- metadata.gz: e71f71efb21a87e7184ddc2415f950d82d931bfc2b02bac01af66c87b1a97afd8bad320918fcc602e80c710ebae67d411542686c1b6bb7e67cf8b77d126f52ac
7
- data.tar.gz: 4f0a8c4b33a3871ec3a8c0f9e4f8de1628b9bf6dc9ce0fc12c23865037ec74e8cc47f7f6feb110370bbcc4c2c0eb1409e04f78fdfeda01a8482d381051d5f449
6
+ metadata.gz: 2bb7e57ecd253bcd201825abd3d2dbb3714f5b7109046ae04e0eb1fe6248cb7d0d96607bbf3533cf94ddc67d7c31ef6fb8dad2d30a5b9377ed9720d3c5339674
7
+ data.tar.gz: 0be89794e620ecd80e112ef8929c0eee1314277db6f8e30a9c053a1bf9db9b587e0f1754e57466b416285fabdb8319d757db6a481d36e06d5e4d9239727ea06c
data/README.md CHANGED
@@ -45,23 +45,25 @@ uri = URI::HTTP.build(host: "somehostname", port: 8080)
45
45
  client = Kubeclient::Client.new uri
46
46
  ```
47
47
 
48
-
49
48
  It is also possible to use https and configure ssl with:
50
49
 
51
50
  ```ruby
52
- client = Kubeclient::Client.new 'https://localhost:8443/api/' , "v1beta3"
53
- client.ssl_options(
51
+ ssl_options = {
54
52
  client_cert: OpenSSL::X509::Certificate.new(File.read('/path/to/client.crt')),
55
53
  client_key: OpenSSL::PKey::RSA.new(File.read('/path/to/client.key')),
56
54
  ca_file: '/path/to/ca.crt',
57
55
  verify_ssl: OpenSSL::SSL::VERIFY_PEER
58
- )
56
+ }
57
+ client = Kubeclient::Client.new 'https://localhost:8443/api/' , "v1beta3",
58
+ ssl_options: ssl_options
59
59
  ```
60
60
 
61
61
  For testing and development purpose you can disable the ssl check with:
62
62
 
63
63
  ```ruby
64
- client.ssl_options(verify_ssl: OpenSSL::SSL::VERIFY_NONE)
64
+ ssl_options = { verify_ssl: OpenSSL::SSL::VERIFY_NONE }
65
+ client = Kubeclient::Client.new 'https://localhost:8443/api/' , 'v1beta3',
66
+ ssl_options: ssl_options
65
67
  ```
66
68
 
67
69
  If you are using basic authentication or bearer tokens as described
@@ -69,14 +71,32 @@ If you are using basic authentication or bearer tokens as described
69
71
  of the following:
70
72
 
71
73
  ```ruby
72
- client.basic_auth('username', 'password')
74
+ auth_options = {
75
+ user: 'username',
76
+ password: 'password'
77
+ }
78
+ client = Kubeclient::Client.new 'https://localhost:8443/api/' , 'v1beta3',
79
+ auth_options: auth_options
80
+ ```
81
+
82
+ or
83
+
84
+ ```ruby
85
+ auth_options = {
86
+ bearer_token: 'MDExMWJkMjItOWY1Ny00OGM5LWJlNDEtMjBiMzgxODkxYzYz'
87
+ }
88
+ client = Kubeclient::Client.new 'https://localhost:8443/api/' , 'v1beta3',
89
+ auth_options: auth_options
73
90
  ```
74
91
 
75
- <br>
76
- or <br>
92
+ or
77
93
 
78
94
  ```ruby
79
- client.bearer_token('MDExMWJkMjItOWY1Ny00OGM5LWJlNDEtMjBiMzgxODkxYzYz')
95
+ auth_options = {
96
+ bearer_token_file: '/path/to/token_file'
97
+ }
98
+ client = Kubeclient::Client.new 'https://localhost:8443/api/' , 'v1beta3',
99
+ auth_options: auth_options
80
100
  ```
81
101
 
82
102
  If you are running your app using kubeclient inside a Kubernetes cluster, then you can have a bearer token file
@@ -84,7 +104,16 @@ mounted inside your pod by using a
84
104
  [Service Account](https://github.com/GoogleCloudPlatform/kubernetes/blob/master/docs/design/service_accounts.md). This
85
105
  will mount a bearer token [secret](https://github.com/GoogleCloudPlatform/kubernetes/blob/master/docs/design/secrets.md)
86
106
  a/ `/var/run/secrets/kubernetes.io/serviceaccount/token` (see [here](https://github.com/GoogleCloudPlatform/kubernetes/pull/7101)
87
- for more details).
107
+ for more details). For example:
108
+
109
+ ```ruby
110
+ auth_options = {
111
+ bearer_token_file: '/var/run/secrets/kubernetes.io/serviceaccount/token'
112
+
113
+ }
114
+ client = Kubeclient::Client.new 'https://localhost:8443/api/' , 'v1beta3',
115
+ auth_options: auth_options
116
+ ```
88
117
 
89
118
  ## Examples:
90
119
 
data/lib/kubeclient.rb CHANGED
@@ -29,11 +29,35 @@ module Kubeclient
29
29
  [Kubeclient.const_set(et, clazz), et]
30
30
  end
31
31
 
32
- def initialize(uri, version = 'v1beta3')
32
+ def initialize(uri,
33
+ version = 'v1beta3',
34
+ ssl_options: {
35
+ client_cert: nil,
36
+ client_key: nil,
37
+ ca_file: nil,
38
+ verify_ssl: OpenSSL::SSL::VERIFY_PEER
39
+ },
40
+ auth_options: {}
41
+ )
42
+
43
+ fail ArgumentError, 'Missing uri' if uri.nil?
44
+
45
+ validate_auth_options(auth_options)
46
+
33
47
  handle_uri(uri, '/api')
34
48
  @api_version = version
35
49
  @headers = {}
36
- ssl_options
50
+ @ssl_options = ssl_options
51
+
52
+ if auth_options[:user]
53
+ @basic_auth_user = auth_options[:user]
54
+ @basic_auth_password = auth_options[:password]
55
+ elsif auth_options[:bearer_token]
56
+ bearer_token(auth_options[:bearer_token])
57
+ elsif auth_options[:bearer_token_file]
58
+ validate_bearer_token_file(auth_options[:bearer_token_file])
59
+ bearer_token(File.read(auth_options[:bearer_token_file]))
60
+ end
37
61
  end
38
62
 
39
63
  def all_entities
@@ -41,5 +65,28 @@ module Kubeclient
41
65
  end
42
66
 
43
67
  define_entity_methods(ENTITY_TYPES)
68
+
69
+ private
70
+
71
+ def validate_auth_options(opts)
72
+ exclusive_keys = [:bearer_token, :bearer_token_file, :user]
73
+
74
+ return if exclusive_keys.none? { |s| opts.key?(s) }
75
+
76
+ msg = 'Invalid auth options: specify only one of user/password,' \
77
+ ' bearer_token or bearer_token_file'
78
+ fail ArgumentError, msg unless exclusive_keys.one? { |s| opts.key?(s) }
79
+
80
+ msg = 'Basic auth requires both user & password'
81
+ fail ArgumentError, msg if opts.key?(:user) && !opts.key?(:password)
82
+ end
83
+
84
+ def validate_bearer_token_file(bearer_token_file)
85
+ msg = "Token file #{bearer_token_file} does not exist"
86
+ fail ArgumentError, msg unless File.file?(bearer_token_file)
87
+
88
+ msg = "Cannot read token file #{bearer_token_file}"
89
+ fail ArgumentError, msg unless File.readable?(bearer_token_file)
90
+ end
44
91
  end
45
92
  end
@@ -173,7 +173,7 @@ module Kubeclient
173
173
  end
174
174
 
175
175
  def update_entity(entity_type, entity_config)
176
- name = entity_config.name
176
+ name = entity_config.metadata.name
177
177
  # to_hash should be called because of issue #9 in recursive open
178
178
  # struct
179
179
  hash = entity_config.to_hash
@@ -214,25 +214,12 @@ module Kubeclient
214
214
  JSON.parse(response)
215
215
  end
216
216
 
217
- def ssl_options(client_cert: nil, client_key: nil, ca_file: nil,
218
- verify_ssl: OpenSSL::SSL::VERIFY_PEER)
219
- @ssl_options = {
220
- ca_file: ca_file,
221
- verify_ssl: verify_ssl,
222
- client_cert: client_cert,
223
- client_key: client_key
224
- }
225
- end
217
+ private
226
218
 
227
219
  def bearer_token(bearer_token)
228
220
  @headers ||= {}
229
221
  @headers[:Authorization] = "Bearer #{bearer_token}"
230
222
  end
231
-
232
- def basic_auth(user, password)
233
- @basic_auth_user = user
234
- @basic_auth_password = password
235
- end
236
223
  end
237
224
  end
238
225
  end
@@ -1,4 +1,4 @@
1
1
  # Kubernetes REST-API Client
2
2
  module Kubeclient
3
- VERSION = '0.1.17'
3
+ VERSION = '0.2.0'
4
4
  end
@@ -40,7 +40,7 @@ module Kubeclient
40
40
  end
41
41
 
42
42
  @options[:headers].each do |header, value|
43
- request[header] = value
43
+ request[header.to_s] = value
44
44
  end
45
45
  request
46
46
  end
@@ -0,0 +1,22 @@
1
+ {
2
+ "status" : {},
3
+ "kind" : "Service",
4
+ "apiVersion" : "v1beta3",
5
+ "spec" : {
6
+ "ports" : [
7
+ {
8
+ "targetPort" : 80,
9
+ "nodePort" : 0,
10
+ "port" : 80,
11
+ "protocol" : "TCP"
12
+ }
13
+ ],
14
+ "portalIP" : "1.2.3.4"
15
+ },
16
+ "metadata" : {
17
+ "name" : "my_service",
18
+ "creationTimestamp" : null,
19
+ "namespace" : "default",
20
+ "resourceVersion" : "2"
21
+ }
22
+ }
@@ -1,7 +1,6 @@
1
1
  {
2
2
  "versions": [
3
- "v1beta1",
4
- "v1beta2",
5
- "v1beta3"
3
+ "v1beta3",
4
+ "v1"
6
5
  ]
7
- }
6
+ }
@@ -94,7 +94,7 @@ class KubeClientTest < MiniTest::Test
94
94
 
95
95
  args = ['http://localhost:8080/api/']
96
96
 
97
- [nil, 'v1beta1', 'v1beta2', 'v1beta3'].each do |version|
97
+ [nil, 'v1beta3', 'v1'].each do |version|
98
98
  client = Kubeclient::Client.new(*(version ? args + [version] : args))
99
99
  assert client.api_valid?
100
100
  end
@@ -238,8 +238,10 @@ class KubeClientTest < MiniTest::Test
238
238
  .to_return(body: open_test_json_file('pod_list_b3.json'),
239
239
  status: 200)
240
240
 
241
- client = Kubeclient::Client.new 'http://localhost:8080/api/'
242
- client.bearer_token('valid_token')
241
+ client = Kubeclient::Client.new 'http://localhost:8080/api/',
242
+ auth_options: {
243
+ bearer_token: 'valid_token'
244
+ }
243
245
 
244
246
  pods = client.get_pods(label_selector: 'name=redis-master')
245
247
 
@@ -253,8 +255,10 @@ class KubeClientTest < MiniTest::Test
253
255
  .to_return(body: open_test_json_file('pod_list_b3.json'),
254
256
  status: 200)
255
257
 
256
- client = Kubeclient::Client.new 'http://localhost:8080/api/'
257
- client.bearer_token('valid_token')
258
+ client = Kubeclient::Client.new 'http://localhost:8080/api/',
259
+ auth_options: {
260
+ bearer_token: 'valid_token'
261
+ }
258
262
 
259
263
  pods = client.get_pods
260
264
 
@@ -270,8 +274,10 @@ class KubeClientTest < MiniTest::Test
270
274
  .with(headers: { Authorization: 'Bearer invalid_token' })
271
275
  .to_raise(KubeException.new(403, error_message))
272
276
 
273
- client = Kubeclient::Client.new 'http://localhost:8080/api/'
274
- client.bearer_token('invalid_token')
277
+ client = Kubeclient::Client.new 'http://localhost:8080/api/',
278
+ auth_options: {
279
+ bearer_token: 'invalid_token'
280
+ }
275
281
 
276
282
  exception = assert_raises(KubeException) { client.get_pods }
277
283
  assert_equal(403, exception.error_code)
@@ -283,8 +289,11 @@ class KubeClientTest < MiniTest::Test
283
289
  .to_return(body: open_test_json_file('pod_list_b3.json'),
284
290
  status: 200)
285
291
 
286
- client = Kubeclient::Client.new 'http://localhost:8080/api/'
287
- client.basic_auth('username', 'password')
292
+ client = Kubeclient::Client.new 'http://localhost:8080/api/',
293
+ auth_options: {
294
+ user: 'username',
295
+ password: 'password'
296
+ }
288
297
 
289
298
  pods = client.get_pods
290
299
 
@@ -301,8 +310,11 @@ class KubeClientTest < MiniTest::Test
301
310
  stub_request(:get, 'http://username:password@localhost:8080/api/v1beta3/pods')
302
311
  .to_raise(KubeException.new(401, error_message))
303
312
 
304
- client = Kubeclient::Client.new 'http://localhost:8080/api/'
305
- client.basic_auth('username', 'password')
313
+ client = Kubeclient::Client.new 'http://localhost:8080/api/',
314
+ auth_options: {
315
+ user: 'username',
316
+ password: 'password'
317
+ }
306
318
 
307
319
  exception = assert_raises(KubeException) { client.get_pods }
308
320
  assert_equal(401, exception.error_code)
@@ -312,6 +324,72 @@ class KubeClientTest < MiniTest::Test
312
324
  times: 1)
313
325
  end
314
326
 
327
+ def test_init_user_no_password
328
+ expected_msg = 'Basic auth requires both user & password'
329
+ exception = assert_raises(ArgumentError) do
330
+ Kubeclient::Client.new 'http://localhost:8080',
331
+ auth_options: {
332
+ user: 'username'
333
+ }
334
+ end
335
+ assert_equal expected_msg, exception.message
336
+ end
337
+
338
+ def test_init_user_and_bearer_token
339
+ expected_msg = 'Invalid auth options: specify only one of user/password,' \
340
+ ' bearer_token or bearer_token_file'
341
+ exception = assert_raises(ArgumentError) do
342
+ Kubeclient::Client.new 'http://localhost:8080',
343
+ auth_options: {
344
+ user: 'username',
345
+ bearer_token: 'token'
346
+ }
347
+ end
348
+ assert_equal expected_msg, exception.message
349
+ end
350
+
351
+ def test_bearer_token_and_bearer_token_file
352
+ expected_msg = 'Invalid auth options: specify only one of user/password,' \
353
+ ' bearer_token or bearer_token_file'
354
+ exception = assert_raises(ArgumentError) do
355
+ Kubeclient::Client.new 'http://localhost:8080',
356
+ auth_options: {
357
+ bearer_token: 'token',
358
+ bearer_token_file: 'token-file'
359
+ }
360
+ end
361
+ assert_equal expected_msg, exception.message
362
+ end
363
+
364
+ def test_bearer_token_file_not_exist
365
+ expected_msg = 'Token file token-file does not exist'
366
+ exception = assert_raises(ArgumentError) do
367
+ Kubeclient::Client.new 'http://localhost:8080',
368
+ auth_options: {
369
+ bearer_token_file: 'token-file'
370
+ }
371
+ end
372
+ assert_equal expected_msg, exception.message
373
+ end
374
+
375
+ def test_api_bearer_token_file_success
376
+ stub_request(:get, 'http://localhost:8080/api/v1beta3/pods')
377
+ .with(headers: { Authorization: 'Bearer valid_token' })
378
+ .to_return(body: open_test_json_file('pod_list_b3.json'),
379
+ status: 200)
380
+
381
+ file = File.join(File.dirname(__FILE__), 'valid_token_file')
382
+ client = Kubeclient::Client.new 'http://localhost:8080/api/',
383
+ auth_options: {
384
+ bearer_token_file: file
385
+ }
386
+
387
+ pods = client.get_pods
388
+
389
+ assert_equal('Pod', pods.kind)
390
+ assert_equal(1, pods.size)
391
+ end
392
+
315
393
  private
316
394
 
317
395
  # dup method creates a shallow copy which is not good in this case
data/test/test_service.rb CHANGED
@@ -109,4 +109,28 @@ class TestService < MiniTest::Test
109
109
  'http://localhost:8080/api/v1beta3/namespaces/development/services/redis-slave',
110
110
  times: 1)
111
111
  end
112
+
113
+ def test_update_service
114
+ entity = 'service'
115
+ object = Kubeclient::Service.new
116
+ name = 'my_service'
117
+ object.metadata = {
118
+ 'name' => name
119
+ }
120
+
121
+ stub_request(:put, %r{/services/#{name}})\
122
+ .to_return(
123
+ body: open_test_json_file('service_update_b3.json'),
124
+ status: 200
125
+ )
126
+
127
+ client = Kubeclient::Client.new 'http://localhost:8080/api/', 'v1beta3'
128
+ client.update_entity entity, object
129
+
130
+ assert_requested(
131
+ :put,
132
+ "http://localhost:8080/api/v1beta3/services/#{name}",
133
+ times: 1
134
+ )
135
+ end
112
136
  end
@@ -0,0 +1 @@
1
+ valid_token
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: 0.1.17
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alissa Bonas
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-07 00:00:00.000000000 Z
11
+ date: 2015-07-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -176,6 +176,7 @@ files:
176
176
  - test/json/service_b3.json
177
177
  - test/json/service_illegal_json_404.json
178
178
  - test/json/service_list_b3.json
179
+ - test/json/service_update_b3.json
179
180
  - test/json/versions_list.json
180
181
  - test/json/watch_stream_b3.json
181
182
  - test/test_helper.rb
@@ -186,6 +187,7 @@ files:
186
187
  - test/test_replication_controller.rb
187
188
  - test/test_service.rb
188
189
  - test/test_watch.rb
190
+ - test/valid_token_file
189
191
  homepage: https://github.com/abonas/kubeclient
190
192
  licenses:
191
193
  - MIT
@@ -229,6 +231,7 @@ test_files:
229
231
  - test/json/service_b3.json
230
232
  - test/json/service_illegal_json_404.json
231
233
  - test/json/service_list_b3.json
234
+ - test/json/service_update_b3.json
232
235
  - test/json/versions_list.json
233
236
  - test/json/watch_stream_b3.json
234
237
  - test/test_helper.rb
@@ -239,3 +242,4 @@ test_files:
239
242
  - test/test_replication_controller.rb
240
243
  - test/test_service.rb
241
244
  - test/test_watch.rb
245
+ - test/valid_token_file