kubeclient 0.1.14 → 0.1.15

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: 4683f1e62a19fc625503044618924b1af4ff86e3
4
- data.tar.gz: c00b7d81ef2ddf4e0989a41ac672d6d60b9ce7c4
3
+ metadata.gz: 57b03781d0875698e18791018710eb4c84dce1bf
4
+ data.tar.gz: bff15e091daf39f5663b3cac73a28f92a3ed0cc2
5
5
  SHA512:
6
- metadata.gz: 0f59df446d480eae9100131ed724d33b1b575668a93418c5cb9846e4f41c4cabea36517231ab127b6cdb14540d8b1389b0f4eedc405bfc2fe21a75d16b63ae28
7
- data.tar.gz: 3ab1f5980bd52ef68eb2b6e47d8a5d28e10f02a005e89f4f83f6df93933050462e766f872971dc0419f05f4b30c2e465f6a2573750e8fdc19a63e35aa70f64a8
6
+ metadata.gz: f7f8e2ae7884ea7afbca1725905c764b5858d25dce119115b610e4707ec82312fc4e9f710466754fac05edf6da9a2e771422d254162ce7764dce5375e1ce35c2
7
+ data.tar.gz: 3041056407fdc8d777bd7e5c32c55298d1c54919c3a10d5daa4e17d6aeb04069f9b12ba74680558768aac2a783bb4d110c77e4aa7cd35bddc4fbbb7c97ffd2ee
data/README.md CHANGED
@@ -27,96 +27,154 @@ Or install it yourself as:
27
27
 
28
28
  ## Usage
29
29
 
30
- Initialize the client: <br>
31
- `client = Kubeclient::Client.new 'http://localhost:8080/api/' , "v1beta3"`
30
+ Initialize the client:
31
+ ```ruby
32
+ client = Kubeclient::Client.new 'http://localhost:8080/api/' , "v1beta3"
33
+ ```
32
34
 
33
35
  Or without specifying version (it will be set by default to "v1beta3"
34
36
 
35
- `client = Kubeclient::Client.new 'http://localhost:8080/api/' `
37
+ ```ruby
38
+ client = Kubeclient::Client.new 'http://localhost:8080/api/'
39
+ ```
36
40
 
37
41
  Another option is to initialize the client with URI object:
38
42
 
39
- `uri = URI::HTTP.build(host: "somehostname", port: 8080)`
40
- `client = Kubeclient::Client.new uri`
43
+ ```ruby
44
+ uri = URI::HTTP.build(host: "somehostname", port: 8080)
45
+ client = Kubeclient::Client.new uri
46
+ ```
41
47
 
42
48
 
43
49
  It is also possible to use https and configure ssl with:
44
50
 
45
- `client = Kubeclient::Client.new 'https://localhost:8443/api/' , "v1beta3"`
46
- `client.ssl_options(` <br>
47
- ` client_cert: OpenSSL::X509::Certificate.new(File.read('/path/to/client.crt')),` <br>
48
- ` client_key: OpenSSL::PKey::RSA.new(File.read('/path/to/client.key')),` <br>
49
- ` ca_file: '/path/to/ca.crt', ` <br>
50
- ` verify_ssl: OpenSSL::SSL::VERIFY_PEER` <br>
51
- `)` <br>
51
+ ```ruby
52
+ client = Kubeclient::Client.new 'https://localhost:8443/api/' , "v1beta3"
53
+ client.ssl_options(
54
+ client_cert: OpenSSL::X509::Certificate.new(File.read('/path/to/client.crt')),
55
+ client_key: OpenSSL::PKey::RSA.new(File.read('/path/to/client.key')),
56
+ ca_file: '/path/to/ca.crt',
57
+ verify_ssl: OpenSSL::SSL::VERIFY_PEER
58
+ )
59
+ ```
52
60
 
53
61
  For testing and development purpose you can disable the ssl check with:
54
62
 
55
- `client.ssl_options(verify_ssl: OpenSSL::SSL::VERIFY_NONE)`
63
+ ```ruby
64
+ client.ssl_options(verify_ssl: OpenSSL::SSL::VERIFY_NONE)
65
+ ```
66
+
67
+ If you are using bearer tokens for authentication as described
68
+ [here](https://github.com/GoogleCloudPlatform/kubernetes/blob/master/docs/authentication.md) then you can specify the
69
+ bearer token to use for authentication:
70
+
71
+ ```ruby
72
+ client.bearer_token('MDExMWJkMjItOWY1Ny00OGM5LWJlNDEtMjBiMzgxODkxYzYz')
73
+ ```
56
74
 
75
+ If you are running your app using kubeclient inside a Kubernetes cluster, then you can have a bearer token file
76
+ mounted inside your pod by using a
77
+ [Service Account](https://github.com/GoogleCloudPlatform/kubernetes/blob/master/docs/design/service_accounts.md). This
78
+ will mount a bearer token [secret](https://github.com/GoogleCloudPlatform/kubernetes/blob/master/docs/design/secrets.md)
79
+ a/ `/var/run/secrets/kubernetes.io/serviceaccount/token` (see [here](https://github.com/GoogleCloudPlatform/kubernetes/pull/7101)
80
+ for more details).
57
81
 
58
- Examples:
82
+ ## Examples:
59
83
 
60
- 1. Get all pods (and respectively: get_services, get_nodes, get_replication_controllers)
61
- <br>
62
- `pods = client.get_pods`
63
- <br>
64
- You can get entities which have specific labels by specifying a parameter named `label_selector` (named `labelSelector` in Kubernetes server): <br>
65
- `pods = client.get_pods(label_selector: 'name=redis-master')` <br>
66
- You can specify multiple labels (that option will return entities which have both labels: <br>
67
- `pods = client.get_pods(label_selector: 'name=redis-master,app=redis')`
84
+ #### Get all pods
85
+ And respectively: `get_services`, `get_nodes`, `get_replication_controllers`
86
+
87
+ ```ruby
88
+ pods = client.get_pods
89
+ ```
90
+
91
+ You can get entities which have specific labels by specifying a parameter named `label_selector` (named `labelSelector` in Kubernetes server):
92
+ ```ruby
93
+ pods = client.get_pods(label_selector: 'name=redis-master')
94
+ ```
95
+ You can specify multiple labels (that option will return entities which have both labels:
96
+ ```ruby
97
+ pods = client.get_pods(label_selector: 'name=redis-master,app=redis')
98
+ ```
99
+
100
+ #### Get a specific node
101
+ And respectively: `get_service "service name"` , `get_pod "pod name"` , `get_replication_controller "rc name"`
68
102
 
69
- 2. Get a specific node (and respectively: get_service "service name" , get_pod "pod name" , get_replication_controller "rc name" )
70
- <br>
71
103
  The GET request should include the namespace name, except for nodes and namespaces entities.
72
- <br>
73
- `node = client.get_node "127.0.0.1"`
74
- <br>
75
- `service = client.get_service "guestbook", 'development'`
76
- <br>
104
+
105
+ ```ruby
106
+ node = client.get_node "127.0.0.1"
107
+ ```
108
+
109
+ ```ruby
110
+ service = client.get_service "guestbook", 'development'
111
+ ```
112
+
77
113
  Note - Kubernetes doesn't work with the uid, but rather with the 'name' property.
78
114
  Querying with uid causes 404.
79
115
 
80
- 3. Delete a service (and respectively delete_pod "pod id" , delete_replication_controller "rc id", delete node "node id") <br>
116
+ #### Delete a service
117
+
118
+ And respectively `delete_pod "pod id"` , `delete_replication_controller "rc id"`, `delete node "node id"`
119
+
81
120
  Input parameter - id (string) specifying service id, pod id, replication controller id.
82
- <br>
83
- `client.delete_service "redis-service"`
84
- <br>
121
+ ```ruby
122
+ client.delete_service "redis-service"
123
+ ```
124
+
125
+ #### Create a service
126
+ And respectively: `create_pod pod_object`, `create_replication_controller rc_obj`
127
+
128
+ Input parameter - object of type `Service`, `Pod`, `ReplicationController`.
85
129
 
86
- 4. Create a service (and respectively: create_pod pod_object, create_replication_controller rc_obj) <br>
87
- Input parameter - object of type Service, Pod, ReplicationController. <br>
88
130
  The below example is for v1beta3
89
- <br>
90
- `service = Service.new` <br>
91
- `service.metadata.name = "redis-master"`<br>
92
- `service.spec.port = 6379`<br>
93
- `service.spec.containerPort = "redis-server"`<br>
94
- `service.spec.selector = {}`<br>
95
- `service.spec.selector.name = "redis"`<br>
96
- `service.spec.selector.role = "master"`<br>
97
- `client.create_service service`<br>
98
- <br>
99
-
100
- 5. Update entity (update pod, service, replication controller) <br>
101
- Input parameter - object of type Service, Pod, ReplicationController <br>
102
- The below example is for v1beta3 <br>
103
- `client.update_service service1`
104
- <br>
105
-
106
- 6. all_entities - Returns a hash with 7 keys (node, service, pod, replication_controller, namespace, endpoint and event). Each key points to an EntityList of same type. This method
107
- is a convenience method instead of calling each entity's get method separately. <br>
108
- `client.all_entities`
109
-
110
- 7. Receive entity updates <br>
131
+
132
+ ```ruby
133
+ service = Service.new
134
+ service.metadata.name = "redis-master"
135
+ service.spec.port = 6379
136
+ service.spec.containerPort = "redis-server"
137
+ service.spec.selector = {}
138
+ service.spec.selector.name = "redis"
139
+ service.spec.selector.role = "master"
140
+ client.create_service service`
141
+ ```
142
+
143
+ #### Update entity
144
+ And respectively `update_pod`, `update_service`, `update_replication_controller`
145
+
146
+ Input parameter - object of type `Service`, `Pod`, `ReplicationController`
147
+
148
+ The below example is for v1beta3
149
+
150
+ ```ruby
151
+ client.update_service service1
152
+ ```
153
+
154
+ #### all_entities
155
+ Returns a hash with 7 keys (node, service, pod, replication_controller, namespace, endpoint and event). Each key points to an EntityList of same type.
156
+
157
+ This method is a convenience method instead of calling each entity's get method separately.
158
+
159
+ ```ruby
160
+ client.all_entities
161
+ ```
162
+
163
+ #### Receive entity updates
111
164
  It is possible to receive live update notices watching the relevant entities:
112
- <br>
113
- `watcher = client.watch_pods` <br>
114
- `watcher.each do |notice|` <br>
115
- ` # process notice data` <br>
116
- `end` <br>
165
+
166
+ ```ruby
167
+ watcher = client.watch_pods
168
+ watcher.each do |notice|
169
+ # process notice data
170
+ end
171
+ ```
172
+
117
173
  It is possible to interrupt the watcher from another thread with:
118
- <br>
119
- `watcher.finish` <br>
174
+
175
+ ```ruby
176
+ watcher.finish
177
+ ```
120
178
 
121
179
  ## Contributing
122
180
 
@@ -133,5 +191,7 @@ It is possible to interrupt the watcher from another thread with:
133
191
  This client is tested with Minitest.
134
192
  Please run all tests before submitting a Pull Request, and add new tests for new functionality.
135
193
 
136
- Running tests: <br>
137
- `rake test`
194
+ Running tests:
195
+ ```ruby
196
+ rake test
197
+ ```
@@ -70,7 +70,8 @@ module Kubeclient
70
70
  ssl_ca_file: @ssl_options[:ca_file],
71
71
  verify_ssl: @ssl_options[:verify_ssl],
72
72
  ssl_client_cert: @ssl_options[:client_cert],
73
- ssl_client_key: @ssl_options[:client_key]
73
+ ssl_client_key: @ssl_options[:client_key],
74
+ bearer_token: @bearer_token
74
75
  }
75
76
  RestClient::Resource.new(@api_endpoint.merge(path).to_s, options)
76
77
  end
@@ -97,8 +98,9 @@ module Kubeclient
97
98
  # ruby Net::HTTP uses verify_mode instead of verify_ssl
98
99
  # http://ruby-doc.org/stdlib-1.9.3/libdoc/net/http/rdoc/Net/HTTP.html
99
100
  verify_mode: @ssl_options[:verify_ssl],
100
- client_cert: @ssl_options[:client_cert],
101
- client_key: @ssl_options[:client_key]
101
+ cert: @ssl_options[:client_cert],
102
+ key: @ssl_options[:client_key],
103
+ bearer_token: @bearer_token
102
104
  }
103
105
 
104
106
  WatchStream.new(uri, options)
@@ -215,6 +217,13 @@ module Kubeclient
215
217
  client_key: client_key
216
218
  }
217
219
  end
220
+
221
+ def bearer_token(bearer_token)
222
+ @bearer_token = bearer_token
223
+ RestClient.add_before_execution_proc do |req|
224
+ req['authorization'] = "Bearer #{@bearer_token}"
225
+ end
226
+ end
218
227
  end
219
228
  end
220
229
  end
@@ -1,4 +1,4 @@
1
1
  # Kubernetes REST-API Client
2
2
  module Kubeclient
3
- VERSION = '0.1.14'
3
+ VERSION = '0.1.15'
4
4
  end
@@ -17,6 +17,10 @@ module Kubeclient
17
17
  buffer = ''
18
18
  request = Net::HTTP::Get.new(@uri)
19
19
 
20
+ if @options[:bearer_token]
21
+ request['authorization'] = "Bearer #{@options[:bearer_token]}"
22
+ end
23
+
20
24
  @http.request(request) do |response|
21
25
  unless response.is_a? Net::HTTPSuccess
22
26
  fail KubeException.new(response.code, response.message)
@@ -232,6 +232,37 @@ class KubeClientTest < MiniTest::Test
232
232
  assert_instance_of(Kubeclient::Namespace, result['namespace'][0])
233
233
  end
234
234
 
235
+ def test_api_bearer_token_success
236
+ stub_request(:get, 'http://localhost:8080/api/v1beta3/pods')
237
+ .with(headers: { Authorization: 'Bearer valid_token' })
238
+ .to_return(body: open_test_json_file('pod_list_b3.json'),
239
+ status: 200)
240
+
241
+ client = Kubeclient::Client.new 'http://localhost:8080/api/'
242
+ client.bearer_token('valid_token')
243
+
244
+ pods = client.get_pods
245
+
246
+ assert_equal('Pod', pods.kind)
247
+ assert_equal(1, pods.size)
248
+ end
249
+
250
+ def test_api_bearer_token_failure
251
+ error_message = '"/api/v1beta3/pods" is forbidden because ' \
252
+ 'system:anonymous cannot list on pods in'
253
+
254
+ stub_request(:get, 'http://localhost:8080/api/v1beta3/pods')
255
+ .with(headers: { Authorization: 'Bearer invalid_token' })
256
+ .to_raise(KubeException.new(403, error_message))
257
+
258
+ client = Kubeclient::Client.new 'http://localhost:8080/api/'
259
+ client.bearer_token('invalid_token')
260
+
261
+ exception = assert_raises(KubeException) { client.get_pods }
262
+ assert_equal(403, exception.error_code)
263
+ assert_equal(error_message, exception.message)
264
+ end
265
+
235
266
  private
236
267
 
237
268
  # dup method creates a shallow copy which is not good in this case
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.14
4
+ version: 0.1.15
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-05-11 00:00:00.000000000 Z
11
+ date: 2015-05-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler