k8s-client 0.5.0 → 0.6.0

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: 2f06cad5e9cbad737bd7a94f169f56c71f6a323d06f3cfc68dd01ba1733abc95
4
- data.tar.gz: 694b1835b27534c3c86841a5d58763b6494331f7078e89115d8c6f6bc50f6260
3
+ metadata.gz: 6021ff799426838c2f3d874b13b2cd2d53ac8298ccfbb983e3ca53943dcfb3be
4
+ data.tar.gz: 516330c9bcb21af9221eef99f3304187cde161db1d9f7cc8ec1d9c1a1ac4360f
5
5
  SHA512:
6
- metadata.gz: d7f1cb6a943da7f7d9e1ecbf396d50f1c7bd481425ed68232b8a99340c20ec5fd7304d89a56c312c1eabf078d7989a37bd22353c7c9a88806ee8c13029d586c5
7
- data.tar.gz: aa6b067859e0838f7b1240d282cf6bdb5d2dc1187ead8247409a45c29b0bc9fa2c827fe844323ab7e664a3910716b6646d65e862c4d3c596bd7f95ab10a548ff
6
+ metadata.gz: ed9e425df7d635def2d912a4083ac64dd189dc2c644983e162066a2f9b3026b4192a79bcfe64df6f38a35bffc5cc970cb6966ab1e12a4b7a3aadff2aa5a6aa9a
7
+ data.tar.gz: ac16a5c433205dc8a9db24d73413d4053c73cc4da9eac09a180e472d96f87c3ce300e32c70079e9b745f6fed18e507609b352b6e3e10e5d9e09235103668fb8e
data/README.md CHANGED
@@ -204,6 +204,14 @@ client.api('apps/v1').resource('deployments', namespace: 'default').merge_patch(
204
204
  })
205
205
  ```
206
206
 
207
+ ### Watching resources
208
+
209
+ ```ruby
210
+ client.api('v1').resource('pods', namespace: 'default').watch(labelSelector: {'role' => 'test'}) do |watch_event|
211
+ puts "type=#{watch_event.type} pod=#{watch_event.resource.metadata.name}"
212
+ end
213
+ ```
214
+
207
215
  ## Contributing
208
216
 
209
217
  Bug reports and pull requests are welcome on GitHub at https://github.com/kontena/k8s-client.
@@ -23,3 +23,4 @@ require 'k8s/api/metav1/api_resource'
23
23
  require 'k8s/api/metav1/list'
24
24
  require 'k8s/api/metav1/object'
25
25
  require 'k8s/api/metav1/status'
26
+ require 'k8s/api/metav1/watch_event'
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../../resource'
4
+
5
+ module K8s
6
+ module API
7
+ module MetaV1
8
+ # @see https://godoc.org/k8s.io/apimachinery/pkg/apis/meta/v1#WatchEvent
9
+ class WatchEvent < Resource
10
+ attribute :type, Types::Strict::String
11
+ attribute :object, Types::Strict::Hash
12
+
13
+ # @return [K8s::Resource]
14
+ def resource
15
+ @resource ||= K8s::Resource.new(object)
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
data/lib/k8s/client.rb CHANGED
@@ -45,6 +45,8 @@ module K8s
45
45
  new(Transport.in_cluster_config)
46
46
  end
47
47
 
48
+ attr_reader :transport
49
+
48
50
  # @param transport [K8s::Transport]
49
51
  # @param namespace [String] default namespace for all operations
50
52
  def initialize(transport, namespace: nil)
@@ -3,6 +3,6 @@
3
3
  module K8s
4
4
  class Client
5
5
  # Updated on releases using semver.
6
- VERSION = "0.5.0"
6
+ VERSION = "0.6.0"
7
7
  end
8
8
  end
@@ -189,9 +189,19 @@ module K8s
189
189
 
190
190
  # @param labelSelector [nil, String, Hash{String => String}]
191
191
  # @param fieldSelector [nil, String, Hash{String => String}]
192
+ # @param namespace [nil, String]
192
193
  # @return [Array<resource_class>]
193
194
  def list(labelSelector: nil, fieldSelector: nil, namespace: @namespace)
194
- list = @transport.request(
195
+ list = meta_list(labelSelector: labelSelector, fieldSelector: fieldSelector, namespace: namespace)
196
+ process_list(list)
197
+ end
198
+
199
+ # @param labelSelector [nil, String, Hash{String => String}]
200
+ # @param fieldSelector [nil, String, Hash{String => String}]
201
+ # @param namespace [nil, String]
202
+ # @return [K8s::API::MetaV1::List]
203
+ def meta_list(labelSelector: nil, fieldSelector: nil, namespace: @namespace)
204
+ @transport.request(
195
205
  method: 'GET',
196
206
  path: path(namespace: namespace),
197
207
  response_class: K8s::API::MetaV1::List,
@@ -200,7 +210,39 @@ module K8s
200
210
  'fieldSelector' => selector_query(fieldSelector)
201
211
  )
202
212
  )
203
- process_list(list)
213
+ end
214
+
215
+ # @param labelSelector [nil, String, Hash{String => String}]
216
+ # @param fieldSelector [nil, String, Hash{String => String}]
217
+ # @param resourceVersion [nil, String]
218
+ # @param timeout [nil, Integer]
219
+ # @yield [K8S::API::MetaV1::WatchEvent]
220
+ # @raise [Excon::Error]
221
+ def watch(labelSelector: nil, fieldSelector: nil, resourceVersion: nil, timeout: nil, namespace: @namespace)
222
+ method = 'GET'
223
+ path = path(namespace: namespace)
224
+ @transport.request(
225
+ method: method,
226
+ path: path,
227
+ response_class: K8s::API::MetaV1::List,
228
+ query: make_query(
229
+ 'labelSelector' => selector_query(labelSelector),
230
+ 'fieldSelector' => selector_query(fieldSelector),
231
+ 'resourceVersion' => resourceVersion,
232
+ 'watch' => '1'
233
+ ),
234
+ response_block: lambda do |chunk, _, _|
235
+ data = JSON.parse(chunk)
236
+ event = K8s::API::MetaV1::WatchEvent.new(data)
237
+ resourceVersion = event.resource&.metadata&.resourceVersion
238
+ yield event
239
+ end,
240
+ read_timeout: timeout
241
+ )
242
+ rescue Excon::Error::Timeout
243
+ retry if timeout.nil?
244
+
245
+ raise K8s::Error::Timeout.new(method, path, 408, 'timeout')
204
246
  end
205
247
 
206
248
  # @return [Bool]
data/lib/k8s/transport.rb CHANGED
@@ -190,6 +190,8 @@ module K8s
190
190
  end
191
191
 
192
192
  if response.status.between? 200, 299
193
+ return response_data if content_type == 'text/plain'
194
+
193
195
  unless response_data.is_a? Hash
194
196
  raise K8s::Error::API.new(method, path, response.status, "Invalid JSON response: #{response_data.inspect}")
195
197
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: k8s-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kontena, Inc.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-10-27 00:00:00.000000000 Z
11
+ date: 2018-11-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: excon
@@ -192,6 +192,7 @@ files:
192
192
  - lib/k8s/api/metav1/list.rb
193
193
  - lib/k8s/api/metav1/object.rb
194
194
  - lib/k8s/api/metav1/status.rb
195
+ - lib/k8s/api/metav1/watch_event.rb
195
196
  - lib/k8s/api/version.rb
196
197
  - lib/k8s/api_client.rb
197
198
  - lib/k8s/client.rb